Skip to content

Latest commit

 

History

History
695 lines (626 loc) · 21 KB

rsaperf.c

File metadata and controls

695 lines (626 loc) · 21 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
#include "seccomon.h"
#include "cert.h"
#include "secutil.h"
#include "nspr.h"
#include "nss.h"
#include "blapi.h"
#include "plgetopt.h"
Nov 8, 2001
Nov 8, 2001
12
#include "lowkeyi.h"
Mar 31, 2005
Mar 31, 2005
13
#include "pk11pub.h"
Apr 21, 2016
Apr 21, 2016
15
16
17
18
19
20
21
22
23
24
25
#define DEFAULT_ITERS 10
#define DEFAULT_DURATION 10
#define DEFAULT_KEY_BITS 1024
#define MIN_KEY_BITS 512
#define MAX_KEY_BITS 65536
#define BUFFER_BYTES MAX_KEY_BITS / 8
#define DEFAULT_THREADS 1
#define DEFAULT_EXPONENT 0x10001
extern NSSLOWKEYPrivateKey *getDefaultRSAPrivateKey(void);
extern NSSLOWKEYPublicKey *getDefaultRSAPublicKey(void);
Mar 31, 2005
Mar 31, 2005
27
28
secuPWData pwData = { PW_NONE, NULL };
29
30
31
typedef struct TimingContextStr TimingContext;
struct TimingContextStr {
Mar 29, 2005
Mar 29, 2005
32
33
34
PRTime start;
PRTime end;
PRTime interval;
Apr 21, 2016
Apr 21, 2016
36
37
38
39
40
long days;
int hours;
int minutes;
int seconds;
int millisecs;
Apr 21, 2016
Apr 21, 2016
43
44
45
TimingContext *
CreateTimingContext(void)
{
Mar 29, 2005
Mar 29, 2005
46
return PORT_Alloc(sizeof(TimingContext));
Apr 21, 2016
Apr 21, 2016
49
50
51
void
DestroyTimingContext(TimingContext *ctx)
{
Mar 29, 2005
Mar 29, 2005
52
PORT_Free(ctx);
Apr 21, 2016
Apr 21, 2016
55
56
57
void
TimingBegin(TimingContext *ctx, PRTime begin)
{
Mar 29, 2005
Mar 29, 2005
58
ctx->start = begin;
Apr 21, 2016
Apr 21, 2016
61
62
63
static void
timingUpdate(TimingContext *ctx)
{
Mar 29, 2005
Mar 29, 2005
64
PRInt64 tmp, remaining;
Apr 21, 2016
Apr 21, 2016
65
PRInt64 L1000, L60, L24;
Apr 21, 2016
Apr 21, 2016
67
68
69
LL_I2L(L1000, 1000);
LL_I2L(L60, 60);
LL_I2L(L24, 24);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
LL_DIV(remaining, ctx->interval, L1000);
LL_MOD(tmp, remaining, L1000);
LL_L2I(ctx->millisecs, tmp);
LL_DIV(remaining, remaining, L1000);
LL_MOD(tmp, remaining, L60);
LL_L2I(ctx->seconds, tmp);
LL_DIV(remaining, remaining, L60);
LL_MOD(tmp, remaining, L60);
LL_L2I(ctx->minutes, tmp);
LL_DIV(remaining, remaining, L60);
LL_MOD(tmp, remaining, L24);
LL_L2I(ctx->hours, tmp);
LL_DIV(remaining, remaining, L24);
LL_L2I(ctx->days, remaining);
}
Apr 21, 2016
Apr 21, 2016
87
88
89
void
TimingEnd(TimingContext *ctx, PRTime end)
{
Mar 29, 2005
Mar 29, 2005
90
ctx->end = end;
91
92
93
94
95
LL_SUB(ctx->interval, ctx->end, ctx->start);
PORT_Assert(LL_GE_ZERO(ctx->interval));
timingUpdate(ctx);
}
Apr 21, 2016
Apr 21, 2016
96
97
98
void
TimingDivide(TimingContext *ctx, int divisor)
{
Mar 29, 2005
Mar 29, 2005
99
PRInt64 tmp;
100
101
102
103
104
105
106
LL_I2L(tmp, divisor);
LL_DIV(ctx->interval, ctx->interval, tmp);
timingUpdate(ctx);
}
Apr 21, 2016
Apr 21, 2016
107
108
109
char *
TimingGenerateString(TimingContext *ctx)
{
110
111
112
char *buf = NULL;
if (ctx->days != 0) {
Apr 21, 2016
Apr 21, 2016
113
buf = PR_sprintf_append(buf, "%d days", ctx->days);
114
115
}
if (ctx->hours != 0) {
Apr 21, 2016
Apr 21, 2016
116
117
118
if (buf != NULL)
buf = PR_sprintf_append(buf, ", ");
buf = PR_sprintf_append(buf, "%d hours", ctx->hours);
119
120
}
if (ctx->minutes != 0) {
Apr 21, 2016
Apr 21, 2016
121
122
123
if (buf != NULL)
buf = PR_sprintf_append(buf, ", ");
buf = PR_sprintf_append(buf, "%d minutes", ctx->minutes);
Apr 21, 2016
Apr 21, 2016
125
126
if (buf != NULL)
buf = PR_sprintf_append(buf, ", and ");
127
if (!buf && ctx->seconds == 0) {
Apr 21, 2016
Apr 21, 2016
128
129
130
131
132
133
int interval;
LL_L2I(interval, ctx->interval);
if (ctx->millisecs < 100)
buf = PR_sprintf_append(buf, "%d microseconds", interval);
else
buf = PR_sprintf_append(buf, "%d milliseconds", ctx->millisecs);
134
} else if (ctx->millisecs == 0) {
Apr 21, 2016
Apr 21, 2016
135
buf = PR_sprintf_append(buf, "%d seconds", ctx->seconds);
Apr 21, 2016
Apr 21, 2016
137
138
buf = PR_sprintf_append(buf, "%d.%03d seconds",
ctx->seconds, ctx->millisecs);
139
140
141
142
143
144
145
}
return buf;
}
void
Usage(char *progName)
{
Mar 31, 2005
Mar 31, 2005
146
fprintf(stderr, "Usage: %s [-s | -e] [-i iterations | -p period] "
Apr 21, 2016
Apr 21, 2016
147
148
149
150
151
"[-t threads]\n[-n none [-k keylength] [ [-g] -x exponent] |\n"
" -n token:nickname [-d certdir] [-w password] |\n"
" -h token [-d certdir] [-w password] [-g] [-k keylength] "
"[-x exponent] [-f pwfile]\n",
progName);
152
fprintf(stderr, "%-20s Cert database directory (default is ~/.netscape)\n",
Apr 21, 2016
Apr 21, 2016
153
"-d certdir");
154
fprintf(stderr, "%-20s How many operations to perform\n", "-i iterations");
Mar 24, 2005
Mar 24, 2005
155
fprintf(stderr, "%-20s How many seconds to run\n", "-p period");
156
fprintf(stderr, "%-20s Perform signing (private key) operations\n", "-s");
Apr 21, 2016
Apr 21, 2016
157
fprintf(stderr, "%-20s Perform encryption (public key) operations\n", "-e");
Mar 31, 2005
Mar 31, 2005
158
fprintf(stderr, "%-20s Nickname of certificate or key, prefixed "
Apr 21, 2016
Apr 21, 2016
159
160
"by optional token name\n",
"-n nickname");
Mar 31, 2005
Mar 31, 2005
161
162
163
164
165
166
167
fprintf(stderr, "%-20s PKCS#11 token to perform operation with.\n",
"-h token");
fprintf(stderr, "%-20s key size in bits, from %d to %d\n", "-k keylength",
MIN_KEY_BITS, MAX_KEY_BITS);
fprintf(stderr, "%-20s token password\n", "-w password");
fprintf(stderr, "%-20s temporary key generation. Not for token keys.\n",
"-g");
Apr 1, 2005
Apr 1, 2005
168
169
170
fprintf(stderr, "%-20s set public exponent for keygen\n", "-x");
fprintf(stderr, "%-20s Number of execution threads (default 1)\n",
"-t threads");
171
172
173
174
exit(-1);
}
static void
Apr 21, 2016
Apr 21, 2016
175
dumpBytes(unsigned char *b, int l)
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{
int i;
if (l <= 0)
return;
for (i = 0; i < l; ++i) {
if (i % 16 == 0)
printf("\t");
printf(" %02x", b[i]);
if (i % 16 == 15)
printf("\n");
}
if ((i % 16) != 0)
printf("\n");
}
static void
Apr 21, 2016
Apr 21, 2016
192
dumpItem(SECItem *item, const char *description)
193
194
{
if (item->len & 1 && item->data[0] == 0) {
Apr 21, 2016
Apr 21, 2016
195
196
printf("%s: (%d bytes)\n", description, item->len - 1);
dumpBytes(item->data + 1, item->len - 1);
Apr 21, 2016
Apr 21, 2016
198
199
printf("%s: (%d bytes)\n", description, item->len);
dumpBytes(item->data, item->len);
200
201
202
203
}
}
void
Apr 21, 2016
Apr 21, 2016
204
printPrivKey(NSSLOWKEYPrivateKey *privKey)
Oct 24, 2007
Oct 24, 2007
206
207
RSAPrivateKey *rsa = &privKey->u.rsa;
Apr 21, 2016
Apr 21, 2016
208
209
210
211
212
213
214
215
dumpItem(&rsa->modulus, "n");
dumpItem(&rsa->publicExponent, "e");
dumpItem(&rsa->privateExponent, "d");
dumpItem(&rsa->prime1, "P");
dumpItem(&rsa->prime2, "Q");
dumpItem(&rsa->exponent1, "d % (P-1)");
dumpItem(&rsa->exponent2, "d % (Q-1)");
dumpItem(&rsa->coefficient, "(Q ** -1) % P");
216
217
218
puts("");
}
Apr 21, 2016
Apr 21, 2016
219
220
221
typedef SECStatus (*RSAOp)(void *key,
unsigned char *output,
unsigned char *input);
Mar 31, 2005
Mar 31, 2005
223
typedef struct {
Apr 21, 2016
Apr 21, 2016
224
225
SECKEYPublicKey *pubKey;
SECKEYPrivateKey *privKey;
Mar 31, 2005
Mar 31, 2005
226
227
} PK11Keys;
Apr 21, 2016
Apr 21, 2016
228
229
230
231
SECStatus
PK11_PublicKeyOp(SECKEYPublicKey *key,
unsigned char *output,
unsigned char *input)
Mar 31, 2005
Mar 31, 2005
232
233
234
235
236
{
return PK11_PubEncryptRaw(key, output, input, key->u.rsa.modulus.len,
NULL);
}
Apr 21, 2016
Apr 21, 2016
237
238
239
240
SECStatus
PK11_PrivateKeyOp(PK11Keys *keys,
unsigned char *output,
unsigned char *input)
Mar 31, 2005
Mar 31, 2005
241
242
243
244
245
246
247
{
unsigned outLen = 0;
return PK11_PrivDecryptRaw(keys->privKey,
output, &outLen,
keys->pubKey->u.rsa.modulus.len, input,
keys->pubKey->u.rsa.modulus.len);
}
Mar 29, 2005
Mar 29, 2005
248
249
250
typedef struct ThreadRunDataStr ThreadRunData;
struct ThreadRunDataStr {
Apr 21, 2016
Apr 21, 2016
251
252
const PRBool *doIters;
const void *rsaKey;
Mar 29, 2005
Mar 29, 2005
253
const unsigned char *buf;
Apr 21, 2016
Apr 21, 2016
254
255
256
257
258
259
RSAOp fn;
int seconds;
long iters;
long iterRes;
PRErrorCode errNum;
SECStatus status;
Mar 29, 2005
Mar 29, 2005
260
261
};
Apr 21, 2016
Apr 21, 2016
262
263
void
ThreadExecFunction(void *data)
Mar 29, 2005
Mar 29, 2005
264
{
Apr 21, 2016
Apr 21, 2016
265
ThreadRunData *tdata = (ThreadRunData *)data;
Mar 31, 2005
Mar 31, 2005
266
unsigned char buf2[BUFFER_BYTES];
Mar 29, 2005
Mar 29, 2005
267
268
269
270
tdata->status = SECSuccess;
if (*tdata->doIters) {
long i = tdata->iters;
Mar 31, 2005
Mar 31, 2005
271
tdata->iterRes = 0;
Mar 29, 2005
Mar 29, 2005
272
while (i--) {
Apr 21, 2016
Apr 21, 2016
273
274
SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
(unsigned char *)tdata->buf);
Mar 29, 2005
Mar 29, 2005
275
276
277
if (rv != SECSuccess) {
tdata->errNum = PORT_GetError();
tdata->status = rv;
Mar 31, 2005
Mar 31, 2005
278
break;
Mar 29, 2005
Mar 29, 2005
279
}
Mar 31, 2005
Mar 31, 2005
280
tdata->iterRes++;
Mar 29, 2005
Mar 29, 2005
281
282
283
284
285
286
}
} else {
PRIntervalTime total = PR_SecondsToInterval(tdata->seconds);
PRIntervalTime start = PR_IntervalNow();
tdata->iterRes = 0;
while (PR_IntervalNow() - start < total) {
Apr 21, 2016
Apr 21, 2016
287
288
SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
(unsigned char *)tdata->buf);
Mar 29, 2005
Mar 29, 2005
289
290
291
if (rv != SECSuccess) {
tdata->errNum = PORT_GetError();
tdata->status = rv;
Mar 31, 2005
Mar 31, 2005
292
break;
Mar 29, 2005
Mar 29, 2005
293
294
295
296
297
}
tdata->iterRes++;
}
}
}
Apr 21, 2016
Apr 21, 2016
299
#define INT_ARG(arg, def) atol(arg) > 0 ? atol(arg) : def
Mar 31, 2005
Mar 31, 2005
300
301
302
303
int
main(int argc, char **argv)
{
Apr 21, 2016
Apr 21, 2016
304
305
306
307
308
309
310
311
312
313
314
315
TimingContext *timeCtx = NULL;
SECKEYPublicKey *pubHighKey = NULL;
SECKEYPrivateKey *privHighKey = NULL;
NSSLOWKEYPrivateKey *privKey = NULL;
NSSLOWKEYPublicKey *pubKey = NULL;
CERTCertificate *cert = NULL;
char *progName = NULL;
char *secDir = NULL;
char *nickname = NULL;
char *slotname = NULL;
long keybits = 0;
RSAOp fn;
Feb 14, 2018
Feb 14, 2018
316
void *rsaKeyPtr = NULL;
Apr 21, 2016
Apr 21, 2016
317
318
319
320
321
322
323
324
325
326
327
328
329
PLOptState *optstate;
PLOptStatus optstatus;
long iters = DEFAULT_ITERS;
int i;
PRBool doPriv = PR_FALSE;
PRBool doPub = PR_FALSE;
int rv;
unsigned char buf[BUFFER_BYTES];
unsigned char buf2[BUFFER_BYTES];
int seconds = DEFAULT_DURATION;
PRBool doIters = PR_FALSE;
PRBool doTime = PR_FALSE;
PRBool useTokenKey = PR_FALSE; /* use PKCS#11 token
Mar 31, 2005
Mar 31, 2005
330
object key */
Apr 21, 2016
Apr 21, 2016
331
PRBool useSessionKey = PR_FALSE; /* use PKCS#11 session
Mar 31, 2005
Mar 31, 2005
332
object key */
Apr 21, 2016
Apr 21, 2016
333
334
PRBool useBLKey = PR_FALSE; /* use freebl */
PK11SlotInfo *slot = NULL; /* slot for session
Mar 31, 2005
Mar 31, 2005
335
object key operations */
Apr 21, 2016
Apr 21, 2016
336
337
PRBool doKeyGen = PR_FALSE;
int publicExponent = DEFAULT_EXPONENT;
Mar 31, 2005
Mar 31, 2005
338
339
340
341
PK11Keys keys;
int peCount = 0;
CK_BYTE pubEx[4];
SECItem pe;
Apr 21, 2016
Apr 21, 2016
342
343
344
345
346
RSAPublicKey pubKeyStr;
int threadNum = DEFAULT_THREADS;
ThreadRunData **runDataArr = NULL;
PRThread **threadsArr = NULL;
int calcThreads = 0;
347
348
349
progName = strrchr(argv[0], '/');
if (!progName)
Apr 21, 2016
Apr 21, 2016
350
351
progName = strrchr(argv[0], '\\');
progName = progName ? progName + 1 : argv[0];
Aug 8, 2008
Aug 8, 2008
353
optstate = PL_CreateOptState(argc, argv, "d:ef:gh:i:k:n:p:st:w:x:");
354
while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
Apr 21, 2016
Apr 21, 2016
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
switch (optstate->option) {
case '?':
Usage(progName);
break;
case 'd':
secDir = PORT_Strdup(optstate->value);
break;
case 'i':
iters = INT_ARG(optstate->value, DEFAULT_ITERS);
doIters = PR_TRUE;
break;
case 's':
doPriv = PR_TRUE;
break;
case 'e':
doPub = PR_TRUE;
break;
case 'g':
doKeyGen = PR_TRUE;
break;
case 'n':
nickname = PORT_Strdup(optstate->value);
/* for compatibility, nickname of "none" means go to freebl */
if (nickname && strcmp(nickname, "none")) {
useTokenKey = PR_TRUE;
} else {
useBLKey = PR_TRUE;
}
break;
case 'p':
seconds = INT_ARG(optstate->value, DEFAULT_DURATION);
doTime = PR_TRUE;
break;
case 'h':
slotname = PORT_Strdup(optstate->value);
useSessionKey = PR_TRUE;
break;
case 'k':
keybits = INT_ARG(optstate->value, DEFAULT_KEY_BITS);
break;
case 'w':
pwData.data = PORT_Strdup(optstate->value);
;
pwData.source = PW_PLAINTEXT;
break;
case 'f':
pwData.data = PORT_Strdup(optstate->value);
pwData.source = PW_FROMFILE;
break;
case 'x':
/* -x public exponent (for RSA keygen) */
publicExponent = INT_ARG(optstate->value, DEFAULT_EXPONENT);
break;
case 't':
threadNum = INT_ARG(optstate->value, DEFAULT_THREADS);
break;
}
412
413
}
if (optstatus == PL_OPT_BAD)
Apr 21, 2016
Apr 21, 2016
414
Usage(progName);
Mar 31, 2005
Mar 31, 2005
416
417
418
if ((doPriv && doPub) || (doIters && doTime) ||
((useTokenKey + useSessionKey + useBLKey) != PR_TRUE) ||
(useTokenKey && keybits) || (useTokenKey && doKeyGen) ||
Apr 21, 2016
Apr 21, 2016
419
(keybits && (keybits < MIN_KEY_BITS || keybits > MAX_KEY_BITS))) {
Mar 31, 2005
Mar 31, 2005
420
421
Usage(progName);
}
Apr 21, 2016
Apr 21, 2016
423
424
if (doIters && doTime)
Usage(progName);
Mar 24, 2005
Mar 24, 2005
425
426
427
428
429
if (!doTime) {
doIters = PR_TRUE;
}
Apr 21, 2016
Apr 21, 2016
430
PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
Mar 31, 2005
Mar 31, 2005
432
PK11_SetPasswordFunc(SECU_GetModulePassword);
433
secDir = SECU_ConfigDirectory(secDir);
Mar 31, 2005
Mar 31, 2005
434
435
if (useTokenKey || useSessionKey) {
Apr 21, 2016
Apr 21, 2016
436
437
438
439
440
rv = NSS_Init(secDir);
if (rv != SECSuccess) {
fprintf(stderr, "NSS_Init failed.\n");
exit(1);
}
Dec 13, 2000
Dec 13, 2000
441
} else {
Apr 21, 2016
Apr 21, 2016
442
443
444
445
446
rv = NSS_NoDB_Init(NULL);
if (rv != SECSuccess) {
fprintf(stderr, "NSS_NoDB_Init failed.\n");
exit(1);
}
Mar 31, 2005
Mar 31, 2005
449
450
if (useTokenKey) {
CK_OBJECT_HANDLE kh = CK_INVALID_HANDLE;
Oct 20, 2014
Oct 20, 2014
451
Jun 30, 2005
Jun 30, 2005
452
cert = PK11_FindCertFromNickname(nickname, &pwData);
Mar 31, 2005
Mar 31, 2005
453
454
455
456
457
458
459
460
461
462
463
464
465
466
if (cert == NULL) {
fprintf(stderr,
"Can't find certificate by name \"%s\"\n", nickname);
exit(1);
}
pubHighKey = CERT_ExtractPublicKey(cert);
if (pubHighKey == NULL) {
fprintf(stderr, "Can't extract public key from certificate");
exit(1);
}
if (doPub) {
/* do public key ops */
fn = (RSAOp)PK11_PublicKeyOp;
Feb 14, 2018
Feb 14, 2018
467
rsaKeyPtr = (void *)pubHighKey;
Apr 21, 2016
Apr 21, 2016
468
Mar 31, 2005
Mar 31, 2005
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
kh = PK11_ImportPublicKey(cert->slot, pubHighKey, PR_FALSE);
if (CK_INVALID_HANDLE == kh) {
fprintf(stderr,
"Unable to import public key to certificate slot.");
exit(1);
}
pubHighKey->pkcs11Slot = PK11_ReferenceSlot(cert->slot);
pubHighKey->pkcs11ID = kh;
printf("Using PKCS#11 for RSA encryption with token %s.\n",
PK11_GetTokenName(cert->slot));
} else {
/* do private key ops */
privHighKey = PK11_FindKeyByAnyCert(cert, &pwData);
if (privHighKey == NULL) {
fprintf(stderr,
"Can't find private key by name \"%s\"\n", nickname);
exit(1);
}
Apr 21, 2016
Apr 21, 2016
487
Mar 31, 2005
Mar 31, 2005
488
489
490
491
SECKEY_CacheStaticFlags(privHighKey);
fn = (RSAOp)PK11_PrivateKeyOp;
keys.privKey = privHighKey;
keys.pubKey = pubHighKey;
Feb 14, 2018
Feb 14, 2018
492
rsaKeyPtr = (void *)&keys;
Mar 31, 2005
Mar 31, 2005
493
494
printf("Using PKCS#11 for RSA decryption with token %s.\n",
PK11_GetTokenName(privHighKey->pkcs11Slot));
Apr 21, 2016
Apr 21, 2016
495
}
Mar 31, 2005
Mar 31, 2005
496
497
} else
Apr 21, 2016
Apr 21, 2016
498
if (useSessionKey) {
Mar 31, 2005
Mar 31, 2005
499
/* use PKCS#11 session key objects */
Apr 21, 2016
Apr 21, 2016
500
501
PK11RSAGenParams rsaparams;
void *params;
Mar 31, 2005
Mar 31, 2005
502
503
504
505
506
507
slot = PK11_FindSlotByName(slotname); /* locate target slot */
if (!slot) {
fprintf(stderr, "Can't find slot \"%s\"\n", slotname);
exit(1);
}
Oct 20, 2014
Oct 20, 2014
509
/* do a temporary keygen in selected slot */
Mar 31, 2005
Mar 31, 2005
510
511
512
if (!keybits) {
keybits = DEFAULT_KEY_BITS;
}
Apr 13, 2005
Apr 13, 2005
514
printf("Using PKCS#11 with %ld bits session key in token %s.\n",
Mar 31, 2005
Mar 31, 2005
515
keybits, PK11_GetTokenName(slot));
Mar 31, 2005
Mar 31, 2005
517
518
519
520
rsaparams.keySizeInBits = keybits;
rsaparams.pe = publicExponent;
params = &rsaparams;
Apr 21, 2016
Apr 21, 2016
521
fprintf(stderr, "\nGenerating RSA key. This may take a few moments.\n");
Mar 31, 2005
Mar 31, 2005
522
523
524
privHighKey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN,
params, &pubHighKey, PR_FALSE,
Apr 21, 2016
Apr 21, 2016
525
PR_FALSE, (void *)&pwData);
Mar 31, 2005
Mar 31, 2005
526
527
528
if (!privHighKey) {
fprintf(stderr,
"Key generation failed in token \"%s\"\n",
May 24, 2005
May 24, 2005
529
PK11_GetTokenName(slot));
Mar 31, 2005
Mar 31, 2005
530
531
532
533
exit(1);
}
SECKEY_CacheStaticFlags(privHighKey);
Apr 21, 2016
Apr 21, 2016
534
535
fprintf(stderr, "Keygen completed.\n");
Mar 31, 2005
Mar 31, 2005
536
537
538
539
if (doPub) {
/* do public key operations */
fn = (RSAOp)PK11_PublicKeyOp;
Feb 14, 2018
Feb 14, 2018
540
rsaKeyPtr = (void *)pubHighKey;
Mar 31, 2005
Mar 31, 2005
541
542
543
544
545
} else {
/* do private key operations */
fn = (RSAOp)PK11_PrivateKeyOp;
keys.privKey = privHighKey;
keys.pubKey = pubHighKey;
Feb 14, 2018
Feb 14, 2018
546
rsaKeyPtr = (void *)&keys;
Apr 21, 2016
Apr 21, 2016
547
}
Mar 31, 2005
Mar 31, 2005
548
} else
Apr 21, 2016
Apr 21, 2016
549
Mar 31, 2005
Mar 31, 2005
550
551
552
553
554
555
{
/* use freebl directly */
if (!keybits) {
keybits = DEFAULT_KEY_BITS;
}
if (!doKeyGen) {
Oct 24, 2007
Oct 24, 2007
556
if (keybits != DEFAULT_KEY_BITS) {
Mar 31, 2005
Mar 31, 2005
557
558
559
doKeyGen = PR_TRUE;
}
}
Apr 13, 2005
Apr 13, 2005
560
printf("Using freebl with %ld bits key.\n", keybits);
Mar 31, 2005
Mar 31, 2005
561
if (doKeyGen) {
Apr 21, 2016
Apr 21, 2016
562
563
564
fprintf(stderr, "\nGenerating RSA key. "
"This may take a few moments.\n");
for (i = 0; i < 4; i++) {
Mar 31, 2005
Mar 31, 2005
565
if (peCount || (publicExponent & ((unsigned long)0xff000000L >>
Apr 21, 2016
Apr 21, 2016
566
567
568
569
(i * 8)))) {
pubEx[peCount] = (CK_BYTE)((publicExponent >>
(3 - i) * 8) &
0xff);
Mar 31, 2005
Mar 31, 2005
570
571
572
573
574
575
576
peCount++;
}
}
pe.len = peCount;
pe.data = &pubEx[0];
pe.type = siBuffer;
Feb 14, 2018
Feb 14, 2018
577
rsaKeyPtr = RSA_NewKey(keybits, &pe);
Apr 21, 2016
Apr 21, 2016
578
fprintf(stderr, "Keygen completed.\n");
Mar 31, 2005
Mar 31, 2005
579
580
} else {
/* use a hardcoded key */
Apr 13, 2005
Apr 13, 2005
581
printf("Using hardcoded %ld bits key.\n", keybits);
Mar 31, 2005
Mar 31, 2005
582
if (doPub) {
Oct 24, 2007
Oct 24, 2007
583
pubKey = getDefaultRSAPublicKey();
Mar 31, 2005
Mar 31, 2005
584
} else {
Oct 24, 2007
Oct 24, 2007
585
privKey = getDefaultRSAPrivateKey();
Mar 31, 2005
Mar 31, 2005
586
587
588
589
590
591
}
}
if (doPub) {
/* do public key operations */
fn = (RSAOp)RSA_PublicKeyOp;
Feb 14, 2018
Feb 14, 2018
592
if (rsaKeyPtr) {
Mar 31, 2005
Mar 31, 2005
593
594
/* convert the RSAPrivateKey to RSAPublicKey */
pubKeyStr.arena = NULL;
Feb 14, 2018
Feb 14, 2018
595
pubKeyStr.modulus = ((RSAPrivateKey *)rsaKeyPtr)->modulus;
Mar 31, 2005
Mar 31, 2005
596
pubKeyStr.publicExponent =
Feb 14, 2018
Feb 14, 2018
597
598
((RSAPrivateKey *)rsaKeyPtr)->publicExponent;
rsaKeyPtr = &pubKeyStr;
Mar 31, 2005
Mar 31, 2005
599
600
} else {
/* convert NSSLOWKeyPublicKey to RSAPublicKey */
Feb 14, 2018
Feb 14, 2018
601
rsaKeyPtr = (void *)(&pubKey->u.rsa);
Mar 31, 2005
Mar 31, 2005
602
}
Feb 14, 2018
Feb 14, 2018
603
PORT_Assert(rsaKeyPtr);
Mar 31, 2005
Mar 31, 2005
604
605
606
607
608
} else {
/* do private key operations */
fn = (RSAOp)RSA_PrivateKeyOp;
if (privKey) {
/* convert NSSLOWKeyPrivateKey to RSAPrivateKey */
Feb 14, 2018
Feb 14, 2018
609
rsaKeyPtr = (void *)(&privKey->u.rsa);
Mar 31, 2005
Mar 31, 2005
610
}
Feb 14, 2018
Feb 14, 2018
611
PORT_Assert(rsaKeyPtr);
Mar 31, 2005
Mar 31, 2005
612
}
Jan 7, 2001
Jan 7, 2001
613
}
614
615
memset(buf, 1, sizeof buf);
Feb 14, 2018
Feb 14, 2018
616
rv = fn(rsaKeyPtr, buf2, buf);
617
if (rv != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
618
619
620
621
622
623
624
625
626
627
628
629
PRErrorCode errNum;
const char *errStr = NULL;
errNum = PORT_GetError();
if (errNum)
errStr = SECU_Strerror(errNum);
else
errNum = rv;
if (!errStr)
errStr = "(null)";
fprintf(stderr, "Error in RSA operation: %d : %s\n", errNum, errStr);
exit(1);
Apr 21, 2016
Apr 21, 2016
632
633
threadsArr = (PRThread **)PORT_Alloc(threadNum * sizeof(PRThread *));
runDataArr = (ThreadRunData **)PORT_Alloc(threadNum * sizeof(ThreadRunData *));
Mar 31, 2005
Mar 31, 2005
634
635
timeCtx = CreateTimingContext();
TimingBegin(timeCtx, PR_Now());
Apr 21, 2016
Apr 21, 2016
636
637
for (i = 0; i < threadNum; i++) {
runDataArr[i] = (ThreadRunData *)PORT_Alloc(sizeof(ThreadRunData));
Mar 29, 2005
Mar 29, 2005
638
639
640
runDataArr[i]->fn = fn;
runDataArr[i]->buf = buf;
runDataArr[i]->doIters = &doIters;
Feb 14, 2018
Feb 14, 2018
641
runDataArr[i]->rsaKey = rsaKeyPtr;
Mar 29, 2005
Mar 29, 2005
642
643
runDataArr[i]->seconds = seconds;
runDataArr[i]->iters = iters;
Apr 21, 2016
Apr 21, 2016
644
threadsArr[i] =
Mar 29, 2005
Mar 29, 2005
645
PR_CreateThread(PR_USER_THREAD,
Apr 21, 2016
Apr 21, 2016
646
647
648
649
650
651
ThreadExecFunction,
(void *)runDataArr[i],
PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD,
0);
Mar 29, 2005
Mar 29, 2005
652
653
654
}
iters = 0;
calcThreads = 0;
Apr 21, 2016
Apr 21, 2016
655
for (i = 0; i < threadNum; i++, calcThreads++) {
Mar 29, 2005
Mar 29, 2005
656
657
PR_JoinThread(threadsArr[i]);
if (runDataArr[i]->status != SECSuccess) {
Apr 21, 2016
Apr 21, 2016
658
const char *errStr = SECU_Strerror(runDataArr[i]->errNum);
Mar 31, 2005
Mar 31, 2005
659
fprintf(stderr, "Thread %d: Error in RSA operation: %d : %s\n",
Mar 29, 2005
Mar 29, 2005
660
661
662
663
i, runDataArr[i]->errNum, errStr);
calcThreads -= 1;
} else {
iters += runDataArr[i]->iterRes;
Mar 24, 2005
Mar 24, 2005
664
}
Apr 21, 2016
Apr 21, 2016
665
PORT_Free((void *)runDataArr[i]);
Mar 29, 2005
Mar 29, 2005
667
668
669
PORT_Free(runDataArr);
PORT_Free(threadsArr);
Mar 31, 2005
Mar 31, 2005
670
TimingEnd(timeCtx, PR_Now());
Apr 21, 2016
Apr 21, 2016
671
672
printf("%ld iterations in %s\n",
Apr 21, 2016
Apr 21, 2016
673
iters, TimingGenerateString(timeCtx));
Sep 8, 2017
Sep 8, 2017
674
printf("%.2f operations/s .\n", ((double)(iters) * (double)1000000.0) / (double)timeCtx->interval);
675
676
677
TimingDivide(timeCtx, iters);
printf("one operation every %s\n", TimingGenerateString(timeCtx));
Mar 31, 2005
Mar 31, 2005
678
679
680
681
682
if (pubHighKey) {
SECKEY_DestroyPublicKey(pubHighKey);
}
if (privHighKey) {
Apr 21, 2016
Apr 21, 2016
683
SECKEY_DestroyPrivateKey(privHighKey);
Mar 31, 2005
Mar 31, 2005
684
685
686
687
688
689
}
if (cert) {
CERT_DestroyCertificate(cert);
}
Jan 7, 2003
Jan 7, 2003
690
691
692
693
if (NSS_Shutdown() != SECSuccess) {
exit(1);
}