-
Notifications
You must be signed in to change notification settings - Fork 1
/
signing_test.c
254 lines (212 loc) · 11.9 KB
/
signing_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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "test_common.h"
#include "iota/conversion.h"
#include "iota/bundle.c"
#include "iota/iota_types.h"
#include "test_vectors.h"
// include the c-file to be able to test static functions
#include "iota/signing.c"
#define MAX_SIGNATURE_LENGTH ((MAX_SECURITY_LEVEL)*27 * (NUM_HASH_TRYTES))
static const uint32_t IDX = 0;
static const tryte_t NORMALIZED_HASH[NUM_HASH_TRYTES] = {};
static const char *FRAGMENTS[] = {
"GTYOKCTTBPHTOTHZPNCNVFSFXGPSPUYFPUMSJKGJIBLLRBLKBEQEVXQT9QZWIKYDTYHZU9YOGX"
"IMIGHJWZICUMVD9NDTLK9UWTWYIMAYNN9V9VPVUQPXZMFUTQZCFUNMONHDQYYFKUMIPJTGCZNC"
"MHKDLHNEYFVZGAUO9LSSZNXIYPTIXZQFZSYONIVMLWRZRQHPJTYALCBHPKN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XBVVOTTREJKEOYZKSDDEHZEJ"
"FLKTKLKDRXAKWQFDM9OBISSPCMU9JYHWWEFXICKGHPSMBFLOCKUKIRWHSBAJXVUPLCMEZCHXQ9"
"DPHJN99WJOOOMRWM9CFMXKVAXR9RFPANVWGRAUKZRZPGEEXYMCJFWTUHFTUPEIDFHXLAFG9NMN"
"CWQDPIVXOXB9WHGEVOTYXANITICWGTFTEWLVXFSIQQUSQEKUJTUOLBIWOIFNQOIHUVTCEAUVMM"
"YVYTLBFXSBIISGCAWSQJPEUZBMTEBXMUBVNCEFFMB"};
static void generate_signature(SIGNING_CTX *ctx,
const unsigned char *seed_bytes,
uint32_t address_idx, uint8_t security,
char *signature)
{
unsigned char signature_fragment[SIGNATURE_FRAGMENT_SIZE * NUM_HASH_BYTES];
// initialize a minimal bundle info only with the address index
const BUNDLE_INFO bundle_info = {.indices = {address_idx}};
signing_initialize(ctx, &bundle_info, NORMALIZED_HASH);
signing_start(ctx, 0, seed_bytes, security);
for (unsigned int i = 0; i < num_fragments(security); i++) {
const unsigned int fragment_index =
signing_next_fragment(ctx, signature_fragment);
assert_int_equal(fragment_index, i);
bytes_to_chars(signature_fragment,
signature + i * SIGNATURE_FRAGMENT_SIZE * 81,
SIGNATURE_FRAGMENT_SIZE * NUM_HASH_BYTES);
}
}
static void assert_signature_equals(uint8_t security, const char *signature)
{
for (unsigned int i = 0; i < security; i++) {
// create zero terminated fragment
const size_t fragment_length = strlen(FRAGMENTS[i]);
char fragment[fragment_length + 1];
memcpy(fragment, signature + i * 27 * NUM_HASH_TRYTES, fragment_length);
fragment[fragment_length] = '\0';
assert_string_equal(fragment, FRAGMENTS[i]);
}
}
static void test_signing_start_invalid_index(void **state)
{
UNUSED(state);
unsigned char seed_bytes[NUM_HASH_BYTES] = {};
const uint8_t security = 2;
SIGNING_CTX ctx;
const BUNDLE_INFO bundle_info = {.indices = {IDX}};
signing_initialize(&ctx, &bundle_info, NORMALIZED_HASH);
expect_assert_failure(signing_start(&ctx, 1, seed_bytes, security));
}
static void test_signing_start_invalid_security(void **state)
{
UNUSED(state);
unsigned char seed_bytes[NUM_HASH_BYTES] = {};
SIGNING_CTX ctx;
const BUNDLE_INFO bundle_info = {.indices = {IDX}};
signing_initialize(&ctx, &bundle_info, NORMALIZED_HASH);
expect_assert_failure(
signing_start(&ctx, 0, seed_bytes, MIN_SECURITY_LEVEL - 1));
expect_assert_failure(
signing_start(&ctx, 0, seed_bytes, MAX_SECURITY_LEVEL + 1));
}
static void test_signature_level_one(void **state)
{
UNUSED(state);
SIGNING_CTX ctx;
const uint8_t security = 1;
char signature[MAX_SIGNATURE_LENGTH + 1] = {};
unsigned char seed_bytes[NUM_HASH_BYTES];
chars_to_bytes(PETER_VECTOR.seed, seed_bytes, NUM_HASH_TRYTES);
generate_signature(&ctx, seed_bytes, IDX, security, signature);
assert_signature_equals(security, signature);
}
static void test_signature_level_two(void **state)
{
UNUSED(state);
SIGNING_CTX ctx;
const uint8_t security = 2;
char signature[MAX_SIGNATURE_LENGTH + 1] = {};
unsigned char seed_bytes[NUM_HASH_BYTES];
chars_to_bytes(PETER_VECTOR.seed, seed_bytes, NUM_HASH_TRYTES);
generate_signature(&ctx, seed_bytes, IDX, security, signature);
assert_signature_equals(security, signature);
}
static void test_signature_level_three(void **state)
{
UNUSED(state);
SIGNING_CTX ctx;
const uint8_t security = 3;
char signature[MAX_SIGNATURE_LENGTH + 1] = {};
unsigned char seed_bytes[NUM_HASH_BYTES];
chars_to_bytes(PETER_VECTOR.seed, seed_bytes, NUM_HASH_TRYTES);
generate_signature(&ctx, seed_bytes, IDX, security, signature);
assert_signature_equals(security, signature);
}
static void test_signing_too_many_fragments(void **state)
{
UNUSED(state);
SIGNING_CTX ctx;
const uint8_t security = 1;
char signature[MAX_SIGNATURE_LENGTH + 1] = {};
unsigned char seed_bytes[NUM_HASH_BYTES];
chars_to_bytes(PETER_VECTOR.seed, seed_bytes, NUM_HASH_TRYTES);
generate_signature(&ctx, seed_bytes, IDX, security, signature);
unsigned char signature_fragment[SIGNATURE_FRAGMENT_SIZE * NUM_HASH_BYTES];
expect_assert_failure(signing_next_fragment(&ctx, signature_fragment));
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_signing_start_invalid_index),
cmocka_unit_test(test_signing_start_invalid_security),
cmocka_unit_test(test_signature_level_one),
cmocka_unit_test(test_signature_level_two),
cmocka_unit_test(test_signature_level_three),
cmocka_unit_test(test_signing_too_many_fragments)};
return cmocka_run_group_tests(tests, NULL, NULL);
}