forked from google/glome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.c
108 lines (94 loc) · 3.99 KB
/
crypto_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "crypto.h"
#include <glib.h>
#include <glome.h>
#include <stdio.h>
#include <string.h>
#include "base64.h"
#include "login.h"
static void test_derive() {
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
uint8_t public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
uint8_t expected_public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
decode_hex(
private_key, sizeof private_key,
"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
decode_hex(
expected_public_key, sizeof expected_public_key,
"8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
g_assert_true(derive_or_generate_key(private_key, public_key) == 0);
g_assert_cmpmem(expected_public_key, sizeof expected_public_key, public_key,
sizeof public_key);
}
static void test_generate() {
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
uint8_t public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
uint8_t empty_public_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
uint8_t empty_private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
g_assert_true(derive_or_generate_key(private_key, public_key) == 0);
g_assert_true(memcmp(empty_public_key, public_key, sizeof empty_public_key));
g_assert_true(
memcmp(empty_private_key, private_key, sizeof empty_private_key));
}
static void test_authcode() {
const char* host_id = "serial-number:1234567890=ABCDFGH/#?";
const char* action = "reboot";
uint8_t service_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
decode_hex(
private_key, sizeof private_key,
"fee1deadfee1deadfee1deadfee1deadfee1deadfee1deadfee1deadfee1dead");
decode_hex(
service_key, sizeof service_key,
"d1b6941bba120bcd131f335da15778d9c68dadd398ae61cf8e7d94484ee65647");
uint8_t authcode[GLOME_MAX_TAG_LENGTH];
uint8_t expected_authcode[GLOME_MAX_TAG_LENGTH];
decode_hex(
expected_authcode, sizeof expected_authcode,
"a7c33f0542a3ef35c154cd8995084d605c6ce09f83cf1440a6cf3765a343aae6");
g_assert_true(
get_authcode(host_id, action, service_key, private_key, authcode) == 0);
g_assert_cmpmem(expected_authcode, sizeof expected_authcode, authcode,
sizeof authcode);
}
static void test_msg_tag() {
const char* host_id = "serial-number:1234567890=ABCDFGH/#?";
const char* action = "reboot";
uint8_t service_key[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
uint8_t private_key[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
decode_hex(
private_key, sizeof private_key,
"fee1deadfee1deadfee1deadfee1deadfee1deadfee1deadfee1deadfee1dead");
decode_hex(
service_key, sizeof service_key,
"d1b6941bba120bcd131f335da15778d9c68dadd398ae61cf8e7d94484ee65647");
uint8_t msg_tag[GLOME_MAX_TAG_LENGTH];
uint8_t expected_msg_tag[GLOME_MAX_TAG_LENGTH];
decode_hex(
expected_msg_tag, sizeof expected_msg_tag,
"dff5aae753a8bdce06038a20adcdb26c7be19cb6bd05a7850fae542f4af29720");
g_assert_true(
get_msg_tag(host_id, action, service_key, private_key, msg_tag) == 0);
g_assert_cmpmem(expected_msg_tag, sizeof expected_msg_tag, msg_tag,
sizeof msg_tag);
}
int main(int argc, char** argv) {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/test-derive", test_derive);
g_test_add_func("/test-generate", test_generate);
g_test_add_func("/test-authcode", test_authcode);
g_test_add_func("/test-msg-tag", test_msg_tag);
return g_test_run();
}