Skip to content

Latest commit

 

History

History
640 lines (584 loc) · 21.2 KB

rsapoptst.c

File metadata and controls

640 lines (584 loc) · 21.2 KB
 
Mar 20, 2012
Mar 20, 2012
1
2
3
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include "plgetopt.h"
#include "nss.h"
#include "secutil.h"
#include "pk11table.h"
#include "secmodt.h"
#include "pk11pub.h"
struct test_args {
char *arg;
Apr 21, 2016
Apr 21, 2016
16
int mask_value;
17
18
19
20
char *description;
};
static const struct test_args test_array[] = {
Apr 21, 2016
Apr 21, 2016
21
22
23
24
25
{ "all", 0x1f, "run all the tests" },
{ "e_n_p", 0x01, "public exponent, modulus, prime1" },
{ "d_n_q", 0x02, "private exponent, modulus, prime2" },
{ "d_p_q", 0x04, "private exponent, prime1, prime2" },
{ "e_d_q", 0x08, "public exponent, private exponent, prime2" },
Oct 30, 2017
Oct 30, 2017
26
{ "e_d_n", 0x10, "public exponent, private exponent, modulus" }
Apr 21, 2016
Apr 21, 2016
28
29
static const int test_array_size =
(sizeof(test_array) / sizeof(struct test_args));
Apr 21, 2016
Apr 21, 2016
31
32
static void
Usage(char *progName)
33
34
35
36
37
{
int i;
#define PRINTUSAGE(subject, option, predicate) \
fprintf(stderr, "%10s %s\t%s\n", subject, option, predicate);
fprintf(stderr, "%s [-k keysize] [-e exp] [-r rounds] [-t tests]\n "
Apr 21, 2016
Apr 21, 2016
38
39
"Test creating RSA private keys from Partial components\n",
progName);
40
41
42
43
PRINTUSAGE("", "-k", "key size (in bit)");
PRINTUSAGE("", "-e", "rsa public exponent");
PRINTUSAGE("", "-r", "number times to repeat the test");
PRINTUSAGE("", "-t", "run the specified tests");
Apr 21, 2016
Apr 21, 2016
44
45
for (i = 0; i < test_array_size; i++) {
PRINTUSAGE("", test_array[i].arg, test_array[i].description);
Apr 21, 2016
Apr 21, 2016
47
fprintf(stderr, "\n");
48
49
50
51
52
53
54
55
}
/*
* Test the RSA populate command to see that it can really build
* keys from it's components.
*/
const static CK_ATTRIBUTE rsaTemplate[] = {
Apr 21, 2016
Apr 21, 2016
56
57
58
59
60
{ CKA_CLASS, NULL, 0 },
{ CKA_KEY_TYPE, NULL, 0 },
{ CKA_TOKEN, NULL, 0 },
{ CKA_SENSITIVE, NULL, 0 },
{ CKA_PRIVATE, NULL, 0 },
Oct 30, 2017
Oct 30, 2017
61
{ CKA_ID, NULL, 0 },
Apr 21, 2016
Apr 21, 2016
62
63
64
65
66
67
68
69
{ CKA_MODULUS, NULL, 0 },
{ CKA_PUBLIC_EXPONENT, NULL, 0 },
{ CKA_PRIVATE_EXPONENT, NULL, 0 },
{ CKA_PRIME_1, NULL, 0 },
{ CKA_PRIME_2, NULL, 0 },
{ CKA_EXPONENT_1, NULL, 0 },
{ CKA_EXPONENT_2, NULL, 0 },
{ CKA_COEFFICIENT, NULL, 0 },
70
71
72
};
#define RSA_SIZE (sizeof(rsaTemplate))
Apr 21, 2016
Apr 21, 2016
73
#define RSA_ATTRIBUTES (sizeof(rsaTemplate) / sizeof(CK_ATTRIBUTE))
74
75
76
77
78
static void
resetTemplate(CK_ATTRIBUTE *attribute, int start, int end)
{
int i;
Apr 21, 2016
Apr 21, 2016
79
80
81
82
83
84
for (i = start; i < end; i++) {
if (attribute[i].pValue) {
PORT_Free(attribute[i].pValue);
}
attribute[i].pValue = NULL;
attribute[i].ulValueLen = 0;
85
86
87
88
}
}
static SECStatus
Apr 21, 2016
Apr 21, 2016
89
90
copyAttribute(PK11ObjectType objType, void *object, CK_ATTRIBUTE *template,
int offset, CK_ATTRIBUTE_TYPE attrType)
Apr 21, 2016
Apr 21, 2016
92
SECItem attributeItem = { 0, 0, 0 };
93
94
95
96
SECStatus rv;
rv = PK11_ReadRawAttribute(objType, object, attrType, &attributeItem);
if (rv != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
97
return rv;
98
99
100
101
102
103
104
105
106
}
template[offset].type = attrType;
template[offset].pValue = attributeItem.data;
template[offset].ulValueLen = attributeItem.len;
return SECSuccess;
}
static SECStatus
readKey(PK11ObjectType objType, void *object, CK_ATTRIBUTE *template,
Apr 21, 2016
Apr 21, 2016
107
int start, int end)
108
109
110
111
{
int i;
SECStatus rv;
Apr 21, 2016
Apr 21, 2016
112
113
114
115
116
for (i = start; i < end; i++) {
rv = copyAttribute(objType, object, template, i, template[i].type);
if (rv != SECSuccess) {
goto fail;
}
117
118
119
120
121
122
123
124
125
126
}
return SECSuccess;
fail:
resetTemplate(template, start, i);
return rv;
}
#define ATTR_STRING(x) getNameFromAttribute(x)
Oct 30, 2017
Oct 30, 2017
127
128
129
130
131
132
133
134
135
136
137
138
static void
dumphex(FILE *file, const unsigned char *cpval, int start, int end)
{
int i;
for (i = start; i < end; i++) {
if ((i % 16) == 0)
fprintf(file, "\n ");
fprintf(file, " %02x", cpval[i]);
}
return;
}
Oct 30, 2017
Oct 30, 2017
140
dumpTemplate(FILE *file, const CK_ATTRIBUTE *template, int start, int end)
Oct 30, 2017
Oct 30, 2017
142
143
int i;
for (i = start; i < end; i++) {
Apr 21, 2016
Apr 21, 2016
144
145
unsigned char cval;
CK_ULONG ulval;
Oct 30, 2017
Oct 30, 2017
146
const unsigned char *cpval;
Apr 21, 2016
Apr 21, 2016
147
Oct 30, 2017
Oct 30, 2017
148
fprintf(file, "%s:", ATTR_STRING(template[i].type));
Apr 21, 2016
Apr 21, 2016
149
150
151
152
153
switch (template[i].ulValueLen) {
case 1:
cval = *(unsigned char *)template[i].pValue;
switch (cval) {
case 0:
Oct 30, 2017
Oct 30, 2017
154
fprintf(file, " false");
Apr 21, 2016
Apr 21, 2016
155
156
break;
case 1:
Oct 30, 2017
Oct 30, 2017
157
fprintf(file, " true");
Apr 21, 2016
Apr 21, 2016
158
159
break;
default:
Oct 30, 2017
Oct 30, 2017
160
fprintf(file, " %d (=0x%02x,'%c')", cval, cval, cval);
Apr 21, 2016
Apr 21, 2016
161
162
163
164
165
break;
}
break;
case sizeof(CK_ULONG):
ulval = *(CK_ULONG *)template[i].pValue;
Oct 30, 2017
Oct 30, 2017
166
fprintf(file, " %ld (=0x%04lx)", ulval, ulval);
Apr 21, 2016
Apr 21, 2016
167
168
break;
default:
Oct 30, 2017
Oct 30, 2017
169
170
cpval = (const unsigned char *)template[i].pValue;
dumphex(file, cpval, 0, template[i].ulValueLen);
Apr 21, 2016
Apr 21, 2016
171
172
break;
}
Oct 30, 2017
Oct 30, 2017
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
fprintf(file, "\n");
}
}
void
dumpItem(FILE *file, const SECItem *item)
{
const unsigned char *cpval;
if (item == NULL) {
fprintf(file, " pNULL ");
return;
}
if (item->data == NULL) {
fprintf(file, " NULL ");
return;
Oct 30, 2017
Oct 30, 2017
190
191
192
193
194
195
196
197
if (item->len == 0) {
fprintf(file, " Empty ");
return;
}
cpval = item->data;
dumphex(file, cpval, 0, item->len);
fprintf(file, " ");
return;
198
199
200
}
PRBool
Apr 21, 2016
Apr 21, 2016
201
202
rsaKeysAreEqual(PK11ObjectType srcType, void *src,
PK11ObjectType destType, void *dest)
203
204
205
206
207
208
209
210
211
212
213
214
215
{
CK_ATTRIBUTE srcTemplate[RSA_ATTRIBUTES];
CK_ATTRIBUTE destTemplate[RSA_ATTRIBUTES];
PRBool areEqual = PR_TRUE;
SECStatus rv;
int i;
memcpy(srcTemplate, rsaTemplate, RSA_SIZE);
memcpy(destTemplate, rsaTemplate, RSA_SIZE);
rv = readKey(srcType, src, srcTemplate, 0, RSA_ATTRIBUTES);
if (rv != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
216
217
printf("Could read source key\n");
return PR_FALSE;
218
219
220
}
readKey(destType, dest, destTemplate, 0, RSA_ATTRIBUTES);
if (rv != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
221
222
printf("Could read dest key\n");
return PR_FALSE;
Apr 21, 2016
Apr 21, 2016
225
for (i = 0; i < RSA_ATTRIBUTES; i++) {
Oct 30, 2017
Oct 30, 2017
226
227
228
if (srcTemplate[i].type == CKA_ID) {
continue; /* we purposefully make the CKA_ID different */
}
Apr 21, 2016
Apr 21, 2016
229
230
231
232
233
234
235
236
237
238
if (srcTemplate[i].ulValueLen != destTemplate[i].ulValueLen) {
printf("key->%s not equal src_len = %ld, dest_len=%ld\n",
ATTR_STRING(srcTemplate[i].type),
srcTemplate[i].ulValueLen, destTemplate[i].ulValueLen);
areEqual = 0;
} else if (memcmp(srcTemplate[i].pValue, destTemplate[i].pValue,
destTemplate[i].ulValueLen) != 0) {
printf("key->%s not equal.\n", ATTR_STRING(srcTemplate[i].type));
areEqual = 0;
}
239
240
}
if (!areEqual) {
Apr 21, 2016
Apr 21, 2016
241
fprintf(stderr, "original key:\n");
Oct 30, 2017
Oct 30, 2017
242
dumpTemplate(stderr, srcTemplate, 0, RSA_ATTRIBUTES);
Apr 21, 2016
Apr 21, 2016
243
fprintf(stderr, "created key:\n");
Oct 30, 2017
Oct 30, 2017
244
dumpTemplate(stderr, destTemplate, 0, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
246
247
resetTemplate(srcTemplate, 0, RSA_ATTRIBUTES);
resetTemplate(destTemplate, 0, RSA_ATTRIBUTES);
248
249
250
251
return areEqual;
}
static int exp_exp_prime_fail_count = 0;
Apr 21, 2016
Apr 21, 2016
252
Oct 30, 2017
Oct 30, 2017
253
254
#define LEAK_ID 0xf
Apr 21, 2016
Apr 21, 2016
255
256
static int
doRSAPopulateTest(unsigned int keySize, unsigned long exponent,
Oct 30, 2017
Oct 30, 2017
257
int mask, int round, void *pwarg)
258
259
260
261
262
263
264
265
266
267
268
{
SECKEYPrivateKey *rsaPrivKey;
SECKEYPublicKey *rsaPubKey;
PK11GenericObject *tstPrivKey;
CK_ATTRIBUTE tstTemplate[RSA_ATTRIBUTES];
int tstHeaderCount;
PK11SlotInfo *slot = NULL;
PK11RSAGenParams rsaParams;
CK_OBJECT_CLASS obj_class = CKO_PRIVATE_KEY;
CK_KEY_TYPE key_type = CKK_RSA;
CK_BBOOL ck_false = CK_FALSE;
Oct 30, 2017
Oct 30, 2017
269
CK_BYTE cka_id[2] = { 0, 0 };
270
int failed = 0;
Oct 30, 2017
Oct 30, 2017
271
272
int leak_found; /* did we find the expected leak */
int expect_leak = 0; /* are we expecting a leak? */
273
274
275
276
277
278
rsaParams.pe = exponent;
rsaParams.keySizeInBits = keySize;
slot = PK11_GetInternalSlot();
if (slot == NULL) {
Apr 21, 2016
Apr 21, 2016
279
280
fprintf(stderr, "Couldn't get the internal slot for the test \n");
return -1;
Apr 21, 2016
Apr 21, 2016
283
284
285
rsaPrivKey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN,
&rsaParams, &rsaPubKey, PR_FALSE,
PR_FALSE, pwarg);
286
if (rsaPrivKey == NULL) {
Apr 21, 2016
Apr 21, 2016
287
288
289
fprintf(stderr, "RSA Key Gen failed");
PK11_FreeSlot(slot);
return -1;
290
291
292
293
294
295
296
297
298
299
300
301
302
303
}
memcpy(tstTemplate, rsaTemplate, RSA_SIZE);
tstTemplate[0].pValue = &obj_class;
tstTemplate[0].ulValueLen = sizeof(obj_class);
tstTemplate[1].pValue = &key_type;
tstTemplate[1].ulValueLen = sizeof(key_type);
tstTemplate[2].pValue = &ck_false;
tstTemplate[2].ulValueLen = sizeof(ck_false);
tstTemplate[3].pValue = &ck_false;
tstTemplate[3].ulValueLen = sizeof(ck_false);
tstTemplate[4].pValue = &ck_false;
tstTemplate[4].ulValueLen = sizeof(ck_false);
Oct 30, 2017
Oct 30, 2017
304
305
306
307
tstTemplate[5].pValue = &cka_id[0];
tstTemplate[5].ulValueLen = sizeof(cka_id);
tstHeaderCount = 6;
cka_id[0] = round;
308
309
if (mask & 1) {
Apr 21, 2016
Apr 21, 2016
310
311
printf("%s\n", test_array[1].description);
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
312
cka_id[1] = 0;
Apr 21, 2016
Apr 21, 2016
313
314
315
316
317
318
319
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount, CKA_PUBLIC_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 1, CKA_MODULUS);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 2, CKA_PRIME_1);
Oct 30, 2017
Oct 30, 2017
320
321
322
323
tstPrivKey = PK11_CreateManagedGenericObject(slot, tstTemplate,
tstHeaderCount +
3,
PR_FALSE);
Apr 21, 2016
Apr 21, 2016
324
325
326
327
328
329
330
331
332
333
if (tstPrivKey == NULL) {
fprintf(stderr, "RSA Populate failed: pubExp mod p\n");
failed = 1;
} else if (!rsaKeysAreEqual(PK11_TypePrivKey, rsaPrivKey,
PK11_TypeGeneric, tstPrivKey)) {
fprintf(stderr, "RSA Populate key mismatch: pubExp mod p\n");
failed = 1;
}
if (tstPrivKey)
PK11_DestroyGenericObject(tstPrivKey);
334
335
}
if (mask & 2) {
Apr 21, 2016
Apr 21, 2016
336
337
338
printf("%s\n", test_array[2].description);
/* test the basic2 case, public exponent, modulus, prime2 */
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
339
cka_id[1] = 1;
Apr 21, 2016
Apr 21, 2016
340
341
342
343
344
345
346
347
348
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount, CKA_PUBLIC_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 1, CKA_MODULUS);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 2, CKA_PRIME_2);
/* test with q in the prime1 position */
tstTemplate[tstHeaderCount + 2].type = CKA_PRIME_1;
Oct 30, 2017
Oct 30, 2017
349
350
351
352
tstPrivKey = PK11_CreateManagedGenericObject(slot, tstTemplate,
tstHeaderCount +
3,
PR_FALSE);
Apr 21, 2016
Apr 21, 2016
353
354
355
356
357
358
359
360
361
362
if (tstPrivKey == NULL) {
fprintf(stderr, "RSA Populate failed: pubExp mod q\n");
failed = 1;
} else if (!rsaKeysAreEqual(PK11_TypePrivKey, rsaPrivKey,
PK11_TypeGeneric, tstPrivKey)) {
fprintf(stderr, "RSA Populate key mismatch: pubExp mod q\n");
failed = 1;
}
if (tstPrivKey)
PK11_DestroyGenericObject(tstPrivKey);
363
364
}
if (mask & 4) {
Apr 21, 2016
Apr 21, 2016
365
366
367
printf("%s\n", test_array[3].description);
/* test the medium case, private exponent, prime1, prime2 */
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
368
cka_id[1] = 2;
Apr 21, 2016
Apr 21, 2016
369
370
371
372
373
374
375
376
377
378
379
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount, CKA_PRIVATE_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 1, CKA_PRIME_1);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 2, CKA_PRIME_2);
/* test with p & q swapped. Underlying code should swap these back */
tstTemplate[tstHeaderCount + 2].type = CKA_PRIME_1;
tstTemplate[tstHeaderCount + 1].type = CKA_PRIME_2;
Oct 30, 2017
Oct 30, 2017
380
381
382
383
tstPrivKey = PK11_CreateManagedGenericObject(slot, tstTemplate,
tstHeaderCount +
3,
PR_FALSE);
Apr 21, 2016
Apr 21, 2016
384
385
386
387
388
389
390
391
392
393
if (tstPrivKey == NULL) {
fprintf(stderr, "RSA Populate failed: privExp p q\n");
failed = 1;
} else if (!rsaKeysAreEqual(PK11_TypePrivKey, rsaPrivKey,
PK11_TypeGeneric, tstPrivKey)) {
fprintf(stderr, "RSA Populate key mismatch: privExp p q\n");
failed = 1;
}
if (tstPrivKey)
PK11_DestroyGenericObject(tstPrivKey);
394
395
}
if (mask & 8) {
Apr 21, 2016
Apr 21, 2016
396
397
398
printf("%s\n", test_array[4].description);
/* test the advanced case, public exponent, private exponent, prime2 */
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
399
cka_id[1] = 3;
Apr 21, 2016
Apr 21, 2016
400
401
402
403
404
405
406
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount, CKA_PRIVATE_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 1, CKA_PUBLIC_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 2, CKA_PRIME_2);
Oct 30, 2017
Oct 30, 2017
407
408
409
410
tstPrivKey = PK11_CreateManagedGenericObject(slot, tstTemplate,
tstHeaderCount +
3,
PR_FALSE);
Apr 21, 2016
Apr 21, 2016
411
412
413
414
415
416
417
418
419
420
421
422
423
if (tstPrivKey == NULL) {
fprintf(stderr, "RSA Populate failed: pubExp privExp q\n");
fprintf(stderr, " this is expected periodically. It means we\n");
fprintf(stderr, " had more than one key that meets the "
"specification\n");
exp_exp_prime_fail_count++;
} else if (!rsaKeysAreEqual(PK11_TypePrivKey, rsaPrivKey,
PK11_TypeGeneric, tstPrivKey)) {
fprintf(stderr, "RSA Populate key mismatch: pubExp privExp q\n");
failed = 1;
}
if (tstPrivKey)
PK11_DestroyGenericObject(tstPrivKey);
Oct 30, 2017
Oct 30, 2017
425
if (mask & 0x10) {
Apr 21, 2016
Apr 21, 2016
426
427
428
429
printf("%s\n", test_array[5].description);
/* test the advanced case2, public exponent, private exponent, modulus
*/
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
Oct 30, 2017
Oct 30, 2017
430
cka_id[1] = LEAK_ID;
Apr 21, 2016
Apr 21, 2016
431
432
433
434
435
436
437
438
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount, CKA_PRIVATE_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 1, CKA_PUBLIC_EXPONENT);
copyAttribute(PK11_TypePrivKey, rsaPrivKey, tstTemplate,
tstHeaderCount + 2, CKA_MODULUS);
Oct 30, 2017
Oct 30, 2017
439
/* purposefully use the old version. This will create a leak */
Apr 21, 2016
Apr 21, 2016
440
441
442
443
444
445
446
447
448
449
450
451
tstPrivKey = PK11_CreateGenericObject(slot, tstTemplate,
tstHeaderCount +
3,
PR_FALSE);
if (tstPrivKey == NULL) {
fprintf(stderr, "RSA Populate failed: pubExp privExp mod\n");
failed = 1;
} else if (!rsaKeysAreEqual(PK11_TypePrivKey, rsaPrivKey,
PK11_TypeGeneric, tstPrivKey)) {
fprintf(stderr, "RSA Populate key mismatch: pubExp privExp mod\n");
failed = 1;
}
Oct 30, 2017
Oct 30, 2017
452
expect_leak = 1;
Apr 21, 2016
Apr 21, 2016
453
454
if (tstPrivKey)
PK11_DestroyGenericObject(tstPrivKey);
Oct 30, 2017
Oct 30, 2017
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
resetTemplate(tstTemplate, tstHeaderCount, RSA_ATTRIBUTES);
SECKEY_DestroyPrivateKey(rsaPrivKey);
SECKEY_DestroyPublicKey(rsaPubKey);
/* make sure we didn't leak */
leak_found = 0;
tstPrivKey = PK11_FindGenericObjects(slot, CKO_PRIVATE_KEY);
if (tstPrivKey) {
SECStatus rv;
PK11GenericObject *thisKey;
int i;
fprintf(stderr, "Leaking keys...\n");
for (i = 0, thisKey = tstPrivKey; thisKey; i++,
thisKey = PK11_GetNextGenericObject(thisKey)) {
SECItem id = { 0, NULL, 0 };
rv = PK11_ReadRawAttribute(PK11_TypeGeneric, thisKey,
CKA_ID, &id);
if (rv != SECSuccess) {
fprintf(stderr, "Key %d: couldn't read CKA_ID: %s\n",
i, PORT_ErrorToString(PORT_GetError()));
continue;
}
fprintf(stderr, "id = { ");
dumpItem(stderr, &id);
fprintf(stderr, "};");
if (id.data[1] == LEAK_ID) {
fprintf(stderr, " ---> leak expected\n");
if (id.data[0] == round)
leak_found = 1;
} else {
if (id.len != sizeof(cka_id)) {
fprintf(stderr,
" ---> ERROR unexpected leak in generated key\n");
} else {
fprintf(stderr,
" ---> ERROR unexpected leak in constructed key\n");
}
failed = 1;
}
SECITEM_FreeItem(&id, PR_FALSE);
}
PK11_DestroyGenericObjects(tstPrivKey);
}
if (expect_leak && !leak_found) {
fprintf(stderr, "ERROR expected leak not found\n");
failed = 1;
}
505
506
507
508
509
510
511
512
513
514
515
516
517
518
PK11_FreeSlot(slot);
return failed ? -1 : 0;
}
/* populate options */
enum {
opt_Exponent = 0,
opt_KeySize,
opt_Repeat,
opt_Tests
};
static secuCommandFlag populate_options[] =
Apr 21, 2016
Apr 21, 2016
519
520
521
522
523
524
{
{ /* opt_Exponent */ 'e', PR_TRUE, 0, PR_FALSE },
{ /* opt_KeySize */ 'k', PR_TRUE, 0, PR_FALSE },
{ /* opt_Repeat */ 'r', PR_TRUE, 0, PR_FALSE },
{ /* opt_Tests */ 't', PR_TRUE, 0, PR_FALSE },
};
525
526
527
528
int
is_delimiter(char c)
{
Apr 21, 2016
Apr 21, 2016
529
530
if ((c == '+') || (c == ',') || (c == '|')) {
return 1;
531
532
533
534
}
return 0;
}
Apr 21, 2016
Apr 21, 2016
535
int
536
537
538
539
540
541
parse_tests(char *test_string)
{
int mask = 0;
int i;
while (*test_string) {
Apr 21, 2016
Apr 21, 2016
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
if (is_delimiter(*test_string)) {
test_string++;
}
for (i = 0; i < test_array_size; i++) {
char *arg = test_array[i].arg;
int len = strlen(arg);
if (strncmp(test_string, arg, len) == 0) {
test_string += len;
mask |= test_array[i].mask_value;
break;
}
}
if (i == test_array_size) {
break;
}
557
558
559
560
}
return mask;
}
Apr 21, 2016
Apr 21, 2016
561
562
int
main(int argc, char **argv)
563
564
565
566
567
568
569
570
571
572
{
unsigned int keySize = 1024;
unsigned long exponent = 65537;
int i, repeat = 1, ret = 0;
SECStatus rv = SECFailure;
secuCommand populateArgs;
char *progName;
int mask = 0xff;
populateArgs.numCommands = 0;
Apr 21, 2016
Apr 21, 2016
573
574
populateArgs.numOptions = sizeof(populate_options) /
sizeof(secuCommandFlag);
575
576
577
578
populateArgs.commands = NULL;
populateArgs.options = populate_options;
progName = strrchr(argv[0], '/');
Apr 21, 2016
Apr 21, 2016
579
580
581
if (!progName)
progName = strrchr(argv[0], '\\');
progName = progName ? progName + 1 : argv[0];
582
583
584
rv = NSS_NoDB_Init(NULL);
if (rv != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
585
586
SECU_PrintPRandOSError(progName);
return -1;
587
588
589
590
591
592
}
rv = SECU_ParseCommandLine(argc, argv, progName, &populateArgs);
if (rv == SECFailure) {
fprintf(stderr, "%s: command line parsing error!\n", progName);
Usage(progName);
Apr 21, 2016
Apr 21, 2016
593
return -1;
594
595
596
597
}
rv = SECFailure;
if (populateArgs.options[opt_KeySize].activated) {
Apr 21, 2016
Apr 21, 2016
598
keySize = PORT_Atoi(populateArgs.options[opt_KeySize].arg);
599
600
}
if (populateArgs.options[opt_Repeat].activated) {
Apr 21, 2016
Apr 21, 2016
601
repeat = PORT_Atoi(populateArgs.options[opt_Repeat].arg);
602
603
}
if (populateArgs.options[opt_Exponent].activated) {
Apr 21, 2016
Apr 21, 2016
604
exponent = PORT_Atoi(populateArgs.options[opt_Exponent].arg);
605
606
}
if (populateArgs.options[opt_Tests].activated) {
Apr 21, 2016
Apr 21, 2016
607
608
609
610
611
612
613
614
615
char *test_string = populateArgs.options[opt_Tests].arg;
mask = PORT_Atoi(test_string);
if (mask == 0) {
mask = parse_tests(test_string);
}
if (mask == 0) {
Usage(progName);
return -1;
}
616
617
618
}
exp_exp_prime_fail_count = 0;
Apr 21, 2016
Apr 21, 2016
619
620
for (i = 0; i < repeat; i++) {
printf("Running RSA Populate test run %d\n", i);
Oct 30, 2017
Oct 30, 2017
621
ret = doRSAPopulateTest(keySize, exponent, mask, i, NULL);
Apr 21, 2016
Apr 21, 2016
622
623
624
625
if (ret != 0) {
i++;
break;
}
626
627
}
if (ret != 0) {
Apr 21, 2016
Apr 21, 2016
628
fprintf(stderr, "RSA Populate test round %d: FAILED\n", i);
629
630
}
if (repeat > 1) {
Apr 21, 2016
Apr 21, 2016
631
632
633
printf(" pub priv prime test: %d failures out of %d runs (%f %%)\n",
exp_exp_prime_fail_count, i,
(((double)exp_exp_prime_fail_count) * 100.0) / (double)i);
Oct 30, 2017
Oct 30, 2017
635
636
637
638
if (NSS_Shutdown() != SECSuccess) {
fprintf(stderr, "Shutdown failed\n");
ret = -1;
}
639
640
return ret;
}