Bug Summary

File:s/cmd/rsaperf/rsaperf.c
Warning:line 102, column 5
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name rsaperf.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/cmd/rsaperf -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/cmd/rsaperf -resource-dir /usr/lib/llvm-18/lib/clang/18 -D HAVE_STRERROR -D LINUX -D linux -D XP_UNIX -D XP_UNIX -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D NSS_DISABLE_SSE3 -D NSS_NO_INIT_SUPPORT -D USE_UTIL_DIRECTLY -D NO_NSPR_10_SUPPORT -D SSL_DISABLE_DEPRECATED_CIPHER_SUITE_NAMES -D NSS_USE_STATIC_LIBS -I ../../nss/lib/softoken -I ../../../dist/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/include -I ../../../dist/public/nss -I ../../../dist/private/nss -I ../../../dist/public/dbm -I ../../../dist/public/seccmd -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c99 -ferror-limit 19 -fgnuc-version=4.2.1 -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-05-18-082241-28900-1 -x c rsaperf.c
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include "seccomon.h"
6#include "cert.h"
7#include "secutil.h"
8#include "nspr.h"
9#include "nss.h"
10#include "blapi.h"
11#include "plgetopt.h"
12#include "lowkeyi.h"
13#include "pk11pub.h"
14
15#define DEFAULT_ITERS10 10
16#define DEFAULT_DURATION10 10
17#define DEFAULT_KEY_BITS1024 1024
18#define MIN_KEY_BITS512 512
19#define MAX_KEY_BITS65536 65536
20#define BUFFER_BYTES65536 / 8 MAX_KEY_BITS65536 / 8
21#define DEFAULT_THREADS1 1
22#define DEFAULT_EXPONENT0x10001 0x10001
23
24extern NSSLOWKEYPrivateKey *getDefaultRSAPrivateKey(int);
25extern NSSLOWKEYPublicKey *getDefaultRSAPublicKey(int);
26
27secuPWData pwData = { PW_NONE, NULL((void*)0) };
28
29typedef struct TimingContextStr TimingContext;
30
31struct TimingContextStr {
32 PRTime start;
33 PRTime end;
34 PRTime interval;
35
36 long days;
37 int hours;
38 int minutes;
39 int seconds;
40 int millisecs;
41};
42
43TimingContext *
44CreateTimingContext(void)
45{
46 return PORT_AllocPORT_Alloc_Util(sizeof(TimingContext));
47}
48
49void
50DestroyTimingContext(TimingContext *ctx)
51{
52 PORT_FreePORT_Free_Util(ctx);
53}
54
55void
56TimingBegin(TimingContext *ctx, PRTime begin)
57{
58 ctx->start = begin;
59}
60
61static void
62timingUpdate(TimingContext *ctx)
63{
64 PRInt64 tmp, remaining;
65 PRInt64 L1000, L60, L24;
66
67 LL_I2L(L1000, 1000)((L1000) = (PRInt64)(1000));
68 LL_I2L(L60, 60)((L60) = (PRInt64)(60));
69 LL_I2L(L24, 24)((L24) = (PRInt64)(24));
70
71 LL_DIV(remaining, ctx->interval, L1000)((remaining) = (ctx->interval) / (L1000));
72 LL_MOD(tmp, remaining, L1000)((tmp) = (remaining) % (L1000));
73 LL_L2I(ctx->millisecs, tmp)((ctx->millisecs) = (PRInt32)(tmp));
74 LL_DIV(remaining, remaining, L1000)((remaining) = (remaining) / (L1000));
75 LL_MOD(tmp, remaining, L60)((tmp) = (remaining) % (L60));
76 LL_L2I(ctx->seconds, tmp)((ctx->seconds) = (PRInt32)(tmp));
77 LL_DIV(remaining, remaining, L60)((remaining) = (remaining) / (L60));
78 LL_MOD(tmp, remaining, L60)((tmp) = (remaining) % (L60));
79 LL_L2I(ctx->minutes, tmp)((ctx->minutes) = (PRInt32)(tmp));
80 LL_DIV(remaining, remaining, L60)((remaining) = (remaining) / (L60));
81 LL_MOD(tmp, remaining, L24)((tmp) = (remaining) % (L24));
82 LL_L2I(ctx->hours, tmp)((ctx->hours) = (PRInt32)(tmp));
83 LL_DIV(remaining, remaining, L24)((remaining) = (remaining) / (L24));
84 LL_L2I(ctx->days, remaining)((ctx->days) = (PRInt32)(remaining));
85}
86
87void
88TimingEnd(TimingContext *ctx, PRTime end)
89{
90 ctx->end = end;
91 LL_SUB(ctx->interval, ctx->end, ctx->start)((ctx->interval) = (ctx->end) - (ctx->start));
92 PORT_Assert(LL_GE_ZERO(ctx->interval))((((ctx->interval) >= 0))?((void)0):PR_Assert("LL_GE_ZERO(ctx->interval)"
,"rsaperf.c",92))
;
93 timingUpdate(ctx);
94}
95
96void
97TimingDivide(TimingContext *ctx, int divisor)
98{
99 PRInt64 tmp;
100
101 LL_I2L(tmp, divisor)((tmp) = (PRInt64)(divisor));
41
The value 0 is assigned to 'tmp'
102 LL_DIV(ctx->interval, ctx->interval, tmp)((ctx->interval) = (ctx->interval) / (tmp));
42
Division by zero
103
104 timingUpdate(ctx);
105}
106
107char *
108TimingGenerateString(TimingContext *ctx)
109{
110 char *buf = NULL((void*)0);
111
112 if (ctx->days != 0) {
113 buf = PR_sprintf_append(buf, "%d days", ctx->days);
114 }
115 if (ctx->hours != 0) {
116 if (buf != NULL((void*)0))
117 buf = PR_sprintf_append(buf, ", ");
118 buf = PR_sprintf_append(buf, "%d hours", ctx->hours);
119 }
120 if (ctx->minutes != 0) {
121 if (buf != NULL((void*)0))
122 buf = PR_sprintf_append(buf, ", ");
123 buf = PR_sprintf_append(buf, "%d minutes", ctx->minutes);
124 }
125 if (buf != NULL((void*)0))
126 buf = PR_sprintf_append(buf, ", and ");
127 if (!buf && ctx->seconds == 0) {
128 int interval;
129 LL_L2I(interval, ctx->interval)((interval) = (PRInt32)(ctx->interval));
130 if (ctx->millisecs < 100)
131 buf = PR_sprintf_append(buf, "%d microseconds", interval);
132 else
133 buf = PR_sprintf_append(buf, "%d milliseconds", ctx->millisecs);
134 } else if (ctx->millisecs == 0) {
135 buf = PR_sprintf_append(buf, "%d seconds", ctx->seconds);
136 } else {
137 buf = PR_sprintf_append(buf, "%d.%03d seconds",
138 ctx->seconds, ctx->millisecs);
139 }
140 return buf;
141}
142
143void
144Usage(char *progName)
145{
146 fprintf(stderrstderr, "Usage: %s [-s | -e] [-i iterations | -p period] "
147 "[-t threads]\n[-n none [-k keylength] [ [-g] -x exponent] |\n"
148 " -n token:nickname [-d certdir] [-w password] |\n"
149 " -h token [-d certdir] [-w password] [-g] [-k keylength] "
150 "[-x exponent] [-f pwfile]\n",
151 progName);
152 fprintf(stderrstderr, "%-20s Cert database directory (default is ~/.netscape)\n",
153 "-d certdir");
154 fprintf(stderrstderr, "%-20s How many operations to perform\n", "-i iterations");
155 fprintf(stderrstderr, "%-20s How many seconds to run\n", "-p period");
156 fprintf(stderrstderr, "%-20s Perform signing (private key) operations\n", "-s");
157 fprintf(stderrstderr, "%-20s Perform encryption (public key) operations\n", "-e");
158 fprintf(stderrstderr, "%-20s Nickname of certificate or key, prefixed "
159 "by optional token name\n",
160 "-n nickname");
161 fprintf(stderrstderr, "%-20s PKCS#11 token to perform operation with.\n",
162 "-h token");
163 fprintf(stderrstderr, "%-20s key size in bits, from %d to %d\n", "-k keylength",
164 MIN_KEY_BITS512, MAX_KEY_BITS65536);
165 fprintf(stderrstderr, "%-20s token password\n", "-w password");
166 fprintf(stderrstderr, "%-20s temporary key generation. Not for token keys.\n",
167 "-g");
168 fprintf(stderrstderr, "%-20s set public exponent for keygen\n", "-x");
169 fprintf(stderrstderr, "%-20s Number of execution threads (default 1)\n",
170 "-t threads");
171 exit(-1);
172}
173
174static void
175dumpBytes(unsigned char *b, int l)
176{
177 int i;
178 if (l <= 0)
179 return;
180 for (i = 0; i < l; ++i) {
181 if (i % 16 == 0)
182 printf("\t");
183 printf(" %02x", b[i]);
184 if (i % 16 == 15)
185 printf("\n");
186 }
187 if ((i % 16) != 0)
188 printf("\n");
189}
190
191static void
192dumpItem(SECItem *item, const char *description)
193{
194 if (item->len & 1 && item->data[0] == 0) {
195 printf("%s: (%d bytes)\n", description, item->len - 1);
196 dumpBytes(item->data + 1, item->len - 1);
197 } else {
198 printf("%s: (%d bytes)\n", description, item->len);
199 dumpBytes(item->data, item->len);
200 }
201}
202
203void
204printPrivKey(NSSLOWKEYPrivateKey *privKey)
205{
206 RSAPrivateKey *rsa = &privKey->u.rsa;
207
208 dumpItem(&rsa->modulus, "n");
209 dumpItem(&rsa->publicExponent, "e");
210 dumpItem(&rsa->privateExponent, "d");
211 dumpItem(&rsa->prime1, "P");
212 dumpItem(&rsa->prime2, "Q");
213 dumpItem(&rsa->exponent1, "d % (P-1)");
214 dumpItem(&rsa->exponent2, "d % (Q-1)");
215 dumpItem(&rsa->coefficient, "(Q ** -1) % P");
216 puts("");
217}
218
219typedef SECStatus (*RSAOp)(void *key,
220 unsigned char *output,
221 unsigned char *input);
222
223typedef struct {
224 SECKEYPublicKey *pubKey;
225 SECKEYPrivateKey *privKey;
226} PK11Keys;
227
228SECStatus
229PK11_PublicKeyOp(SECKEYPublicKey *key,
230 unsigned char *output,
231 unsigned char *input)
232{
233 return PK11_PubEncryptRaw(key, output, input, key->u.rsa.modulus.len,
234 NULL((void*)0));
235}
236
237SECStatus
238PK11_PrivateKeyOp(PK11Keys *keys,
239 unsigned char *output,
240 unsigned char *input)
241{
242 unsigned outLen = 0;
243 return PK11_PrivDecryptRawPK11_PubDecryptRaw(keys->privKey,
244 output, &outLen,
245 keys->pubKey->u.rsa.modulus.len, input,
246 keys->pubKey->u.rsa.modulus.len);
247}
248typedef struct ThreadRunDataStr ThreadRunData;
249
250struct ThreadRunDataStr {
251 const PRBool *doIters;
252 const void *rsaKey;
253 const unsigned char *buf;
254 RSAOp fn;
255 int seconds;
256 long iters;
257 long iterRes;
258 PRErrorCode errNum;
259 SECStatus status;
260};
261
262void
263ThreadExecFunction(void *data)
264{
265 ThreadRunData *tdata = (ThreadRunData *)data;
266 unsigned char buf2[BUFFER_BYTES65536 / 8];
267
268 tdata->status = SECSuccess;
269 if (*tdata->doIters) {
270 long i = tdata->iters;
271 tdata->iterRes = 0;
272 while (i--) {
273 SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
274 (unsigned char *)tdata->buf);
275 if (rv != SECSuccess) {
276 tdata->errNum = PORT_GetErrorPORT_GetError_Util();
277 tdata->status = rv;
278 break;
279 }
280 tdata->iterRes++;
281 }
282 } else {
283 PRIntervalTime total = PR_SecondsToInterval(tdata->seconds);
284 PRIntervalTime start = PR_IntervalNow();
285 tdata->iterRes = 0;
286 while (PR_IntervalNow() - start < total) {
287 SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
288 (unsigned char *)tdata->buf);
289 if (rv != SECSuccess) {
290 tdata->errNum = PORT_GetErrorPORT_GetError_Util();
291 tdata->status = rv;
292 break;
293 }
294 tdata->iterRes++;
295 }
296 }
297}
298
299#define INT_ARG(arg, def)atol(arg) > 0 ? atol(arg) : def atol(arg) > 0 ? atol(arg) : def
300
301int
302main(int argc, char **argv)
303{
304 TimingContext *timeCtx = NULL((void*)0);
305 SECKEYPublicKey *pubHighKey = NULL((void*)0);
306 SECKEYPrivateKey *privHighKey = NULL((void*)0);
307 NSSLOWKEYPrivateKey *privKey = NULL((void*)0);
308 NSSLOWKEYPublicKey *pubKey = NULL((void*)0);
309 CERTCertificate *cert = NULL((void*)0);
310 char *progName = NULL((void*)0);
311 char *secDir = NULL((void*)0);
312 char *nickname = NULL((void*)0);
313 char *slotname = NULL((void*)0);
314 long keybits = 0;
315 RSAOp fn;
316 void *rsaKeyPtr = NULL((void*)0);
317 PLOptState *optstate;
318 PLOptStatus optstatus;
319 long iters = DEFAULT_ITERS10;
320 int i;
321 PRBool doPriv = PR_FALSE0;
322 PRBool doPub = PR_FALSE0;
323 int rv;
324 unsigned char buf[BUFFER_BYTES65536 / 8];
325 unsigned char buf2[BUFFER_BYTES65536 / 8];
326 int seconds = DEFAULT_DURATION10;
327 PRBool doIters = PR_FALSE0;
328 PRBool doTime = PR_FALSE0;
329 PRBool useTokenKey = PR_FALSE0; /* use PKCS#11 token
330 object key */
331 PRBool useSessionKey = PR_FALSE0; /* use PKCS#11 session
332 object key */
333 PRBool useBLKey = PR_FALSE0; /* use freebl */
334 PK11SlotInfo *slot = NULL((void*)0); /* slot for session
335 object key operations */
336 PRBool doKeyGen = PR_FALSE0;
337 int publicExponent = DEFAULT_EXPONENT0x10001;
338 PK11Keys keys;
339 int peCount = 0;
340 CK_BYTE pubEx[4];
341 SECItem pe;
342 RSAPublicKey pubKeyStr;
343 int threadNum = DEFAULT_THREADS1;
344 ThreadRunData **runDataArr = NULL((void*)0);
345 PRThread **threadsArr = NULL((void*)0);
346 int calcThreads = 0;
347
348 progName = strrchr(argv[0], '/');
349 if (!progName)
1
Assuming 'progName' is non-null
350 progName = strrchr(argv[0], '\\');
351 progName = progName
2.1
'progName' is non-null
? progName + 1 : argv[0];
2
Taking false branch
3
'?' condition is true
352
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) {
4
Assuming the condition is true
5
Loop condition is true. Entering loop body
8
Execution continues on line 354
9
Assuming the condition is true
10
Loop condition is true. Entering loop body
14
Execution continues on line 354
15
Assuming the condition is false
16
Loop condition is false. Execution continues on line 413
355 switch (optstate->option) {
6
Control jumps to 'case 110:' at line 375
11
Control jumps to 'case 116:' at line 408
356 case '?':
357 Usage(progName);
358 break;
359 case 'd':
360 secDir = PORT_StrdupPORT_Strdup_Util(optstate->value);
361 break;
362 case 'i':
363 iters = INT_ARG(optstate->value, DEFAULT_ITERS)atol(optstate->value) > 0 ? atol(optstate->value) : 10;
364 doIters = PR_TRUE1;
365 break;
366 case 's':
367 doPriv = PR_TRUE1;
368 break;
369 case 'e':
370 doPub = PR_TRUE1;
371 break;
372 case 'g':
373 doKeyGen = PR_TRUE1;
374 break;
375 case 'n':
376 nickname = PORT_StrdupPORT_Strdup_Util(optstate->value);
377 /* for compatibility, nickname of "none" means go to freebl */
378 if (nickname && strcmp(nickname, "none")) {
7
Assuming 'nickname' is null
379 useTokenKey = PR_TRUE1;
380 } else {
381 useBLKey = PR_TRUE1;
382 }
383 break;
384 case 'p':
385 seconds = INT_ARG(optstate->value, DEFAULT_DURATION)atol(optstate->value) > 0 ? atol(optstate->value) : 10;
386 doTime = PR_TRUE1;
387 break;
388 case 'h':
389 slotname = PORT_StrdupPORT_Strdup_Util(optstate->value);
390 useSessionKey = PR_TRUE1;
391 break;
392 case 'k':
393 keybits = INT_ARG(optstate->value, DEFAULT_KEY_BITS)atol(optstate->value) > 0 ? atol(optstate->value) : 1024;
394 break;
395 case 'w':
396 pwData.data = PORT_StrdupPORT_Strdup_Util(optstate->value);
397 ;
398 pwData.source = PW_PLAINTEXT;
399 break;
400 case 'f':
401 pwData.data = PORT_StrdupPORT_Strdup_Util(optstate->value);
402 pwData.source = PW_FROMFILE;
403 break;
404 case 'x':
405 /* -x public exponent (for RSA keygen) */
406 publicExponent = INT_ARG(optstate->value, DEFAULT_EXPONENT)atol(optstate->value) > 0 ? atol(optstate->value) : 0x10001;
407 break;
408 case 't':
409 threadNum = INT_ARG(optstate->value, DEFAULT_THREADS)atol(optstate->value) > 0 ? atol(optstate->value) : 1;
12
Assuming the condition is true
13
'?' condition is true
410 break;
411 }
412 }
413 if (optstatus == PL_OPT_BAD)
17
Assuming 'optstatus' is not equal to PL_OPT_BAD
414 Usage(progName);
415
416 if ((doPriv
17.1
'doPriv' is 0
&& doPub) || (doIters
17.2
'doIters' is 0
&& doTime) ||
417 ((useTokenKey + useSessionKey + useBLKey) != PR_TRUE1) ||
418 (useTokenKey
17.3
'useTokenKey' is 0
&& keybits) || (useTokenKey
17.4
'useTokenKey' is 0
&& doKeyGen) ||
419 (keybits
17.5
'keybits' is 0
&& (keybits < MIN_KEY_BITS512 || keybits > MAX_KEY_BITS65536))) {
420 Usage(progName);
421 }
422
423 if (doIters
17.6
'doIters' is 0
&& doTime)
424 Usage(progName);
425
426 if (!doTime
17.7
'doTime' is 0
) {
18
Taking true branch
427 doIters = PR_TRUE1;
428 }
429
430 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
431
432 PK11_SetPasswordFunc(SECU_GetModulePassword);
433 secDir = SECU_ConfigDirectory(secDir);
434
435 if (useTokenKey
18.1
'useTokenKey' is 0
|| useSessionKey
18.2
'useSessionKey' is 0
) {
19
Taking false branch
436 rv = NSS_Init(secDir);
437 if (rv != SECSuccess) {
438 fprintf(stderrstderr, "NSS_Init failed.\n");
439 exit(1);
440 }
441 } else {
442 rv = NSS_NoDB_Init(NULL((void*)0));
443 if (rv != SECSuccess) {
20
Assuming 'rv' is equal to SECSuccess
21
Taking false branch
444 fprintf(stderrstderr, "NSS_NoDB_Init failed.\n");
445 exit(1);
446 }
447 }
448
449 if (useTokenKey
21.1
'useTokenKey' is 0
) {
22
Taking false branch
450 CK_OBJECT_HANDLE kh = CK_INVALID_HANDLE0;
451
452 cert = PK11_FindCertFromNickname(nickname, &pwData);
453 if (cert == NULL((void*)0)) {
454 fprintf(stderrstderr,
455 "Can't find certificate by name \"%s\"\n", nickname);
456 exit(1);
457 }
458 pubHighKey = CERT_ExtractPublicKey(cert);
459 if (pubHighKey == NULL((void*)0)) {
460 fprintf(stderrstderr, "Can't extract public key from certificate");
461 exit(1);
462 }
463
464 if (doPub) {
465 /* do public key ops */
466 fn = (RSAOp)PK11_PublicKeyOp;
467 rsaKeyPtr = (void *)pubHighKey;
468
469 kh = PK11_ImportPublicKey(cert->slot, pubHighKey, PR_FALSE0);
470 if (CK_INVALID_HANDLE0 == kh) {
471 fprintf(stderrstderr,
472 "Unable to import public key to certificate slot.");
473 exit(1);
474 }
475 pubHighKey->pkcs11Slot = PK11_ReferenceSlot(cert->slot);
476 pubHighKey->pkcs11ID = kh;
477 printf("Using PKCS#11 for RSA encryption with token %s.\n",
478 PK11_GetTokenName(cert->slot));
479 } else {
480 /* do private key ops */
481 privHighKey = PK11_FindKeyByAnyCert(cert, &pwData);
482 if (privHighKey == NULL((void*)0)) {
483 fprintf(stderrstderr,
484 "Can't find private key by name \"%s\"\n", nickname);
485 exit(1);
486 }
487
488 SECKEY_CacheStaticFlags(privHighKey);
489 fn = (RSAOp)PK11_PrivateKeyOp;
490 keys.privKey = privHighKey;
491 keys.pubKey = pubHighKey;
492 rsaKeyPtr = (void *)&keys;
493 printf("Using PKCS#11 for RSA decryption with token %s.\n",
494 PK11_GetTokenName(privHighKey->pkcs11Slot));
495 }
496 } else
497
498 if (useSessionKey
22.1
'useSessionKey' is 0
) {
23
Taking false branch
499 /* use PKCS#11 session key objects */
500 PK11RSAGenParams rsaparams;
501 void *params;
502
503 slot = PK11_FindSlotByName(slotname); /* locate target slot */
504 if (!slot) {
505 fprintf(stderrstderr, "Can't find slot \"%s\"\n", slotname);
506 exit(1);
507 }
508
509 /* do a temporary keygen in selected slot */
510 if (!keybits) {
511 keybits = DEFAULT_KEY_BITS1024;
512 }
513
514 printf("Using PKCS#11 with %ld bits session key in token %s.\n",
515 keybits, PK11_GetTokenName(slot));
516
517 rsaparams.keySizeInBits = keybits;
518 rsaparams.pe = publicExponent;
519 params = &rsaparams;
520
521 fprintf(stderrstderr, "\nGenerating RSA key. This may take a few moments.\n");
522
523 privHighKey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN0x00000000UL,
524 params, &pubHighKey, PR_FALSE0,
525 PR_FALSE0, (void *)&pwData);
526 if (!privHighKey) {
527 fprintf(stderrstderr,
528 "Key generation failed in token \"%s\"\n",
529 PK11_GetTokenName(slot));
530 exit(1);
531 }
532
533 SECKEY_CacheStaticFlags(privHighKey);
534
535 fprintf(stderrstderr, "Keygen completed.\n");
536
537 if (doPub) {
538 /* do public key operations */
539 fn = (RSAOp)PK11_PublicKeyOp;
540 rsaKeyPtr = (void *)pubHighKey;
541 } else {
542 /* do private key operations */
543 fn = (RSAOp)PK11_PrivateKeyOp;
544 keys.privKey = privHighKey;
545 keys.pubKey = pubHighKey;
546 rsaKeyPtr = (void *)&keys;
547 }
548 } else
549
550 {
551 /* use freebl directly */
552 if (!keybits
23.1
'keybits' is 0
) {
24
Taking true branch
553 keybits = DEFAULT_KEY_BITS1024;
554 }
555 if (!doKeyGen
24.1
'doKeyGen' is 0
) {
25
Taking true branch
556 if (keybits
25.1
'keybits' is equal to DEFAULT_KEY_BITS
!= DEFAULT_KEY_BITS1024) {
26
Taking false branch
557 doKeyGen = PR_TRUE1;
558 }
559 }
560 printf("Using freebl with %ld bits key.\n", keybits);
561 if (doKeyGen
26.1
'doKeyGen' is 0
) {
27
Taking false branch
562 fprintf(stderrstderr, "\nGenerating RSA key. "
563 "This may take a few moments.\n");
564 for (i = 0; i < 4; i++) {
565 if (peCount || (publicExponent & ((unsigned long)0xff000000L >>
566 (i * 8)))) {
567 pubEx[peCount] = (CK_BYTE)((publicExponent >>
568 (3 - i) * 8) &
569 0xff);
570 peCount++;
571 }
572 }
573 pe.len = peCount;
574 pe.data = &pubEx[0];
575 pe.type = siBuffer;
576
577 rsaKeyPtr = RSA_NewKey(keybits, &pe);
578 fprintf(stderrstderr, "Keygen completed.\n");
579 } else {
580 /* use a hardcoded key */
581 printf("Using hardcoded %ld bits key.\n", keybits);
582 if (doPub
27.1
'doPub' is 0
) {
28
Taking false branch
583 pubKey = getDefaultRSAPublicKey(keybits);
584 } else {
585 privKey = getDefaultRSAPrivateKey(keybits);
586 }
587 }
588
589 if (doPub
28.1
'doPub' is 0
) {
29
Taking false branch
590 /* do public key operations */
591 fn = (RSAOp)RSA_PublicKeyOp;
592 if (rsaKeyPtr) {
593 /* convert the RSAPrivateKey to RSAPublicKey */
594 pubKeyStr.arena = NULL((void*)0);
595 pubKeyStr.modulus = ((RSAPrivateKey *)rsaKeyPtr)->modulus;
596 pubKeyStr.publicExponent =
597 ((RSAPrivateKey *)rsaKeyPtr)->publicExponent;
598 rsaKeyPtr = &pubKeyStr;
599 } else {
600 /* convert NSSLOWKeyPublicKey to RSAPublicKey */
601 rsaKeyPtr = (void *)(&pubKey->u.rsa);
602 }
603 PORT_Assert(rsaKeyPtr)((rsaKeyPtr)?((void)0):PR_Assert("rsaKeyPtr","rsaperf.c",603)
)
;
604 } else {
605 /* do private key operations */
606 fn = (RSAOp)RSA_PrivateKeyOp;
607 if (privKey) {
30
Assuming 'privKey' is non-null
31
Taking true branch
608 /* convert NSSLOWKeyPrivateKey to RSAPrivateKey */
609 rsaKeyPtr = (void *)(&privKey->u.rsa);
610 }
611 PORT_Assert(rsaKeyPtr)((rsaKeyPtr)?((void)0):PR_Assert("rsaKeyPtr","rsaperf.c",611)
)
;
32
'?' condition is true
612 }
613 }
614
615 memset(buf, 1, sizeof buf);
616 rv = fn(rsaKeyPtr, buf2, buf);
617 if (rv != SECSuccess) {
33
Assuming 'rv' is equal to SECSuccess
34
Taking false branch
618 PRErrorCode errNum;
619 const char *errStr = NULL((void*)0);
620
621 errNum = PORT_GetErrorPORT_GetError_Util();
622 if (errNum)
623 errStr = SECU_Strerror(errNum)PR_ErrorToString((errNum), 0);
624 else
625 errNum = rv;
626 if (!errStr)
627 errStr = "(null)";
628 fprintf(stderrstderr, "Error in RSA operation: %d : %s\n", errNum, errStr);
629 exit(1);
630 }
631
632 threadsArr = (PRThread **)PORT_AllocPORT_Alloc_Util(threadNum * sizeof(PRThread *));
633 runDataArr = (ThreadRunData **)PORT_AllocPORT_Alloc_Util(threadNum * sizeof(ThreadRunData *));
634 timeCtx = CreateTimingContext();
635 TimingBegin(timeCtx, PR_Now());
636 for (i = 0; i < threadNum; i++) {
35
Assuming 'i' is >= 'threadNum'
36
Loop condition is false. Execution continues on line 653
637 runDataArr[i] = (ThreadRunData *)PORT_AllocPORT_Alloc_Util(sizeof(ThreadRunData));
638 runDataArr[i]->fn = fn;
639 runDataArr[i]->buf = buf;
640 runDataArr[i]->doIters = &doIters;
641 runDataArr[i]->rsaKey = rsaKeyPtr;
642 runDataArr[i]->seconds = seconds;
643 runDataArr[i]->iters = iters;
644 threadsArr[i] =
645 PR_CreateThread(PR_USER_THREAD,
646 ThreadExecFunction,
647 (void *)runDataArr[i],
648 PR_PRIORITY_NORMAL,
649 PR_GLOBAL_THREAD,
650 PR_JOINABLE_THREAD,
651 0);
652 }
653 iters = 0;
37
The value 0 is assigned to 'iters'
654 calcThreads = 0;
655 for (i = 0; i
37.1
'i' is >= 'threadNum'
< threadNum; i++, calcThreads++) {
38
Loop condition is false. Execution continues on line 667
656 PR_JoinThread(threadsArr[i]);
657 if (runDataArr[i]->status != SECSuccess) {
658 const char *errStr = SECU_Strerror(runDataArr[i]->errNum)PR_ErrorToString((runDataArr[i]->errNum), 0);
659 fprintf(stderrstderr, "Thread %d: Error in RSA operation: %d : %s\n",
660 i, runDataArr[i]->errNum, errStr);
661 calcThreads -= 1;
662 } else {
663 iters += runDataArr[i]->iterRes;
664 }
665 PORT_FreePORT_Free_Util((void *)runDataArr[i]);
666 }
667 PORT_FreePORT_Free_Util(runDataArr);
668 PORT_FreePORT_Free_Util(threadsArr);
669
670 TimingEnd(timeCtx, PR_Now());
671
672 printf("%ld iterations in %s\n",
673 iters, TimingGenerateString(timeCtx));
674 printf("%.2f operations/s .\n", ((double)(iters) * (double)1000000.0) / (double)timeCtx->interval);
675 TimingDivide(timeCtx, iters);
39
Passing the value 0 via 2nd parameter 'divisor'
40
Calling 'TimingDivide'
676 printf("one operation every %s\n", TimingGenerateString(timeCtx));
677
678 if (pubHighKey) {
679 SECKEY_DestroyPublicKey(pubHighKey);
680 }
681
682 if (privHighKey) {
683 SECKEY_DestroyPrivateKey(privHighKey);
684 }
685
686 if (cert) {
687 CERT_DestroyCertificate(cert);
688 }
689
690 if (NSS_Shutdown() != SECSuccess) {
691 exit(1);
692 }
693
694 return 0;
695}