-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtriad_test.c
More file actions
170 lines (147 loc) · 4.69 KB
/
Copy pathtriad_test.c
File metadata and controls
170 lines (147 loc) · 4.69 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2022-2024 Xiaomi Corporation
*
* 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
*
* http://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 <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <triad_ca_api.h>
#define TRIAD_DID_SIZE 8
#define TRIAD_KEY_SIZE 16
#define TRIAD_HMAC_SIZE 32
static const uint8_t test_key[16] = {
0x46, 0x78, 0x5A, 0x43, 0x30, 0x76, 0x55, 0x78,
0x63, 0x56, 0x7A, 0x61, 0x76, 0x4C, 0x74, 0x51
};
static const uint8_t txt[80] = {
0x37, 0x29, 0xE6, 0x08, 0x9D, 0x3A, 0x36, 0x50,
0x2A, 0x57, 0x9B, 0x56, 0x2D, 0xAB, 0xA0, 0x61,
0x6B, 0xDC, 0x30, 0xC4, 0x4C, 0xDA, 0xC3, 0x94,
0x3B, 0xD6, 0xF2, 0xCB, 0x7C, 0x57, 0x99, 0x88,
0x61, 0x5B, 0xCC, 0x10, 0x44, 0xFC, 0x2C, 0x4A,
0xC5, 0x6D, 0xA9, 0xF0, 0x16, 0x6F, 0xCF, 0xA2,
0x9D, 0x04, 0x34, 0xA4, 0x44, 0xF0, 0xC4, 0xC5,
0xF1, 0xE9, 0xDC, 0x40, 0xA2, 0xA5, 0x01, 0xF1,
0xA8, 0x34, 0xAF, 0xA4, 0xE4, 0x59, 0x6B, 0xD3,
0x1A, 0xF3, 0x3C, 0xBD, 0xF8, 0x0F, 0x74, 0x0C
};
static uint8_t hmac[TRIAD_HMAC_SIZE];
static uint8_t key[TRIAD_KEY_SIZE];
static uint8_t did[TRIAD_DID_SIZE];
static void usage(void)
{
printf("usage:\n"
"\tca_triad_test \n"
"\tca_triad_test store\n");
}
static void test_traid_gcm(void)
{
unsigned char iv[12] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C
};
unsigned char aad[] = "Authentication Data";
unsigned char plaintext[] = "This is a test message for AES-GCM encryption!";
unsigned char tag[16];
unsigned char ciphertext[256];
unsigned char decrypted[256];
size_t plaintext_len = strlen((char*)plaintext);
size_t aad_len = strlen((char*)aad);
int res;
res = triad_gcm_encrypt(iv, sizeof(iv), aad, aad_len,
plaintext, plaintext_len,
tag, sizeof(tag), ciphertext);
if (res != 0) {
printf("triad gcm encrypt failed: %d\n", res);
return;
}
res = triad_gcm_decrypt(iv, sizeof(iv), aad, aad_len,
tag, sizeof(tag), ciphertext,
plaintext_len, decrypted);
if (res != 0) {
printf("triad gcm decrypt failed: %d\n", res);
return;
}
if (memcmp(plaintext, decrypted, plaintext_len) == 0) {
printf("triad gcm test success\n");
}
}
int main(int argc, FAR char* argv[])
{
/*
* argv[1] : store (overwrite triad when reading a triple fails)
*/
if (argc != 1 && argc != 2) {
printf("Invalid argument number\n");
usage();
return -1;
}
bool store = false;
if (argc == 2) {
if (strcmp(argv[1], "store") == 0) {
store = true;
} else {
printf("Unrecognized option: %s\n", argv[1]);
usage();
return -1;
}
}
int res = 0;
if (triad_load_did(did, TRIAD_DID_SIZE) == 0) {
printf("load did ok, %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
did[0], did[1], did[2], did[3], did[4], did[5], did[6], did[7]);
} else if (store) {
memset(did, 0x12, TRIAD_DID_SIZE);
printf("store did...\n");
if (triad_store_did(did, TRIAD_DID_SIZE) == 0) {
printf("store did ok\n");
} else {
printf("store did fail\n");
}
} else {
printf("triad load did fail (nostore)\n");
res = -1;
}
if (triad_load_key(key, TRIAD_KEY_SIZE) == 0) {
printf("load key ok\n");
} else if (store) {
memcpy(key, test_key, TRIAD_KEY_SIZE);
printf("store key...\n");
if (triad_store_key(key, TRIAD_KEY_SIZE) == 0) {
printf("store key ok\n");
} else {
printf("store did fail\n");
}
} else {
printf("triad load key fail (nostore)\n");
res = -1;
}
if (triad_get_hmac((uint8_t*)txt, sizeof(txt), hmac, TRIAD_HMAC_SIZE) == 0) {
/*
* hamc should be
* 356daddc139eb72a4df6623f171312c39d3a994eed12b71503b42ee1af4c761f
*/
printf("get hmac ok, ");
for (int i = 0; i < TRIAD_HMAC_SIZE; i++) {
printf("%02x,", hmac[i]);
}
printf("\n");
} else {
printf("triad get hmac fail\n");
res = -1;
}
test_traid_gcm();
printf("end main\n");
return res;
}