keygen.c
6.57 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>
#include <PR/bbtypes.h>
#include <algorithms.h>
#include <poly_math.h>
#include <stdlib.h>
#include <bb_nn.h>
int main(){
BbEccPrivateKey private_key1;
BbEccPublicKey public_key1;
BbEccPrivateKey private_key2;
BbEccPublicKey public_key2;
BbAesKey shared_key1;
BbAesKey shared_key2;
u8 input_string[1024];
u32 rand_input[8];
BbEccSig eccsign;
boolean res;
field_2n a,b,c;
unsigned int seed;
int i, same;
int iter =0;
u32 rand_bit, rand_word, mask;
seed = getpid();
srand(seed);
for(iter = 0; iter < 50; iter++){
/* simple poly math tests */
for(i=0; i< 8; i++){
a.e[i] = rand();
b.e[i] = rand();
}
a.e[0] &= UPR_MASK;
b.e[0] &= UPR_MASK;
poly_mul(&a, &b, &c);
poly_inv(&a, &b);
poly_mul(&a, &b, &c);
same = 0;
for(i=0; i< 7; i++){
if (c.e[i] != 0){
same = 1;
}
}
if(c.e[7] != 0x1){
same = 1;
}
/* c should be one */
if(same == 0)
printf("POLY TEST PASS %d \n", iter);
else
printf("POLY TEST FAIL %d \n", iter);
}
/* public key and shared key tests */
for(iter = 0; iter < 10; iter++){
/* random private key */
for(i=0; i< 8; i++){
private_key1[i] = rand();
}
private_key1[0] &= UPR_MASK;
private_key1[0] = 0x00000179;
private_key1[1] = 0xcda7e2fe;
private_key1[2] = 0x16d77e37;
private_key1[3] = 0x39fbb3de;
private_key1[4] = 0x618d5d57;
private_key1[5] = 0x9064949b;
private_key1[6] = 0x7721c36c;
private_key1[7] = 0x0a66a2a6;
eccGenPublicKey(public_key1, private_key1);
for(i=0; i< 16; i++){
printf("public key = %08x\n", public_key1[i]);
}
/* random private key */
for(i=0; i< 8; i++){
private_key2[i] = rand();
}
private_key2[0] &= UPR_MASK;
private_key2[0] = 0x0000017a;
private_key2[1] = 0xcda7e2fe;
private_key2[2] = 0x16d77e37;
private_key2[3] = 0x39fbb3de;
private_key2[4] = 0x618d5d57;
private_key2[5] = 0x9064949b;
private_key2[6] = 0x7721c36c;
private_key2[7] = 0x0a66a2a6;
eccGenPublicKey(public_key2, private_key2);
for(i=0; i< 16; i++){
printf("public key = %08x\n", public_key2[i]);
}
/* generate the two shared keys */
eccGenAesKey(public_key1, private_key2, shared_key1);
eccGenAesKey(public_key2, private_key1, shared_key2);
same = 0;
for(i=0; i < sizeof(BbAesKey)/sizeof(u32); i++){
printf("shared key 1 = %08x 2 = %08x\n", (unsigned int)shared_key1[i], (unsigned int)shared_key2[i]);
if(shared_key1[i] != shared_key2[i]){
same = 1;
}
}
if(same == 0)
printf("KEY GEN PASS %d \n", iter);
else
printf("KEY GEN FAIL %d \n", iter);
/* negative test, purturb private_key 2 */
rand_word = ((rand() & 0xff) % 7) + 1;
rand_bit = rand() & 0xff;
mask = 0x1 << (rand_bit % 31);
printf("mask = %08x\n", (unsigned int )mask);
printf("rand_word= %08x\n", (unsigned int )rand_word);
private_key2[rand_word] = private_key2[rand_word] ^ mask;
eccGenAesKey(public_key1, private_key2, shared_key1);
same = 0;
for(i=0; i < sizeof(BbAesKey)/sizeof(u32); i++){
printf("shared key 1 = %08x 2 = %08x\n", (unsigned int)shared_key1[i], (unsigned int)shared_key2[i]);
if(shared_key1[i] != shared_key2[i]){
same = 1;
}
}
if(same == 1)
printf("KEY GEN NEGATIVE TEST PASS %d \n", iter);
else
printf("KEY GEN NEGATIVE TEST FAIL %d \n", iter);
}
/* one test for zeros in signature, small input */
for(i = 0; i < 1024; i++){
input_string[i] = 0x0;
}
for(i=0; i< 8; i++){
private_key1[i] = rand();
}
private_key1[0] &= UPR_MASK;
for(i=0; i< 8; i++){
rand_input[i] = rand();
}
rand_input[0] &= UPR_MASK;
bsl_compute_ecc_sig(input_string, 4, private_key1, rand_input, eccsign, 0x30);
for(i =0; i< 16; i++){
eccsign[i] = 0x0;
}
eccGenPublicKey(public_key1, private_key1);
bsl_verify_ecc_sig(input_string, 4, public_key1, eccsign, &res, 0x00);
if(res == BSL_TRUE){
printf("SIGN VERIFY FAIL ZEROS TEST \n");
}
else{
printf("SIGN VERIFY PASS ZEROS TEST \n");
}
/* test the ECDSA part iteratively */
for(iter = 0; iter < 10000; iter++){
for(i = 0; i < 1024; i++){
input_string[i] = rand() & 0xff;
}
for(i=0; i< 8; i++){
private_key1[i] = rand();
}
private_key1[0] &= UPR_MASK;
for(i=0; i< 8; i++){
rand_input[i] = rand();
}
rand_input[0] &= UPR_MASK;
private_key1[0] = 0x00000179;
private_key1[1] = 0xcda7e2fe;
private_key1[2] = 0x16d77e37;
private_key1[3] = 0x39fbb3de;
private_key1[4] = 0x618d5d57;
private_key1[5] = 0x9064949b;
private_key1[6] = 0x7721c36c;
private_key1[7] = 0x0a66a2a6;
rand_input[0] = (0x00000019);
rand_input[1] = (0xcda7e2fe);
rand_input[2] = (0x16d77e37);
rand_input[3] = (0x39fbb3de);
rand_input[4] = (0x618d5d57);
rand_input[5] = (0x9064949b);
rand_input[6] = (0x7721c36c);
rand_input[7] = (0x0a66a2a6);
/*
for(i=0; i< 8; i++){
rand_input[i] = 0x0;
}
rand_input[7] = 0x1;
*/
bsl_compute_ecc_sig(input_string, 1024, private_key1, rand_input, eccsign, 0x30);
for(i=0; i< 16; i++){
printf("sign = %08x\n", eccsign[i]);
}
eccGenPublicKey(public_key1, private_key1);
bsl_verify_ecc_sig(input_string, 1024, public_key1, eccsign, &res, 0x30);
if(res == BSL_TRUE){
printf("SIGN VERIFY PASS %d\n", iter);
}
else{
printf("SIGN VERIFY FAIL %d\n", iter);
}
/* change a bit */
rand_word = rand() & 0x7;
rand_bit = rand() & 0x7f;
mask = 0x1 << (rand_bit % 32);
eccsign[rand_word] = eccsign[rand_word] ^ mask;
eccGenPublicKey(public_key1, private_key1);
bsl_verify_ecc_sig(input_string, 1024, public_key1, eccsign, &res, 0x30);
printf("rand_word = %ld\n", rand_word);
printf("rand_bit = %ld\n", rand_bit);
printf("mask = %08x\n", (unsigned int )mask);
if(res == BSL_FALSE){
printf("SIGN VERIFY NEG TEST PASS %d\n", iter);
}
else{
printf("SIGN VERIFY NEG TEST FAIL %d\n", iter);
}
}
return 0;
}