DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */

#include "seccomon.h"
#include "seccomon.h"
#include "cert.h"
#include "secutil.h"
#include "nspr.h"
#include "nss.h"
#include "blapi.h"
#include "blapi.h"
#include "plgetopt.h"
#include "lowkeyi.h"
#include "pk11pub.h"

#define DEFAULT_ITERS 10
#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 BUFFER_BYTES MAX_KEY_BITS / 8
#define DEFAULT_THREADS 1
#define DEFAULT_EXPONENT 0x10001

extern NSSLOWKEYPrivateKey *getDefaultRSAPrivateKey(int);
extern NSSLOWKEYPublicKey *getDefaultRSAPublicKey(int);
extern NSSLOWKEYPublicKey *getDefaultRSAPublicKey(int);

secuPWData pwData = { PW_NONE, NULL };

typedef struct TimingContextStr TimingContext;


struct TimingContextStr {
    PRTime start;
    PRTime end;
    PRTime interval;


    long days;
    int hours;
    int minutes;
    int seconds;
    int millisecs;
    int millisecs;
};

TimingContext *
CreateTimingContext(void)
{
{
    return PORT_Alloc(sizeof(TimingContext));
}

void
DestroyTimingContext(TimingContext *ctx)
DestroyTimingContext(TimingContext *ctx)
{
    PORT_Free(ctx);
}

void
void
TimingBegin(TimingContext *ctx, PRTime begin)
{
    ctx->start = begin;
}


static void
timingUpdate(TimingContext *ctx)
{
    PRInt64 tmp, remaining;
    PRInt64 L1000, L60, L24;
    PRInt64 L1000, L60, L24;

    LL_I2L(L1000, 1000);
    LL_I2L(L60, 60);
    LL_I2L(L24, 24);

    LL_DIV(remaining, ctx->interval, L1000);
    LL_MOD(tmp, remaining, 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_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_MOD(tmp, remaining, L24);
    LL_L2I(ctx->hours, tmp);
    LL_DIV(remaining, remaining, L24);
    LL_L2I(ctx->days, remaining);
}


void
TimingEnd(TimingContext *ctx, PRTime end)
{
    ctx->end = end;
    LL_SUB(ctx->interval, ctx->end, ctx->start);
    LL_SUB(ctx->interval, ctx->end, ctx->start);
    PORT_Assert(LL_GE_ZERO(ctx->interval));
    timingUpdate(ctx);
}

void
void
TimingDivide(TimingContext *ctx, int divisor)
{
    PRInt64 tmp;

    LL_I2L(tmp, divisor);
    LL_I2L(tmp, divisor);
    LL_DIV(ctx->interval, ctx->interval, tmp);

    timingUpdate(ctx);
}


char *
TimingGenerateString(TimingContext *ctx)
{
    char *buf = NULL;


    if (ctx->days != 0) {
        buf = PR_sprintf_append(buf, "%d days", ctx->days);
    }
    }
    if (ctx->hours != 0) {
    if (ctx->hours != 0) {
        if (buf != NULL)
            buf = PR_sprintf_append(buf, ", ");
        buf = PR_sprintf_append(buf, "%d hours", ctx->hours);
        buf = PR_sprintf_append(buf, "%d hours", ctx->hours);
    }
    if (ctx->minutes != 0) {
        if (buf != NULL)
            buf = PR_sprintf_append(buf, ", ");
        buf = PR_sprintf_append(buf, "%d minutes", ctx->minutes);
    }
    if (buf != NULL)
        buf = PR_sprintf_append(buf, ", and ");
    if (!buf && ctx->seconds == 0) {
        int interval;
        LL_L2I(interval, ctx->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);
    } else if (ctx->millisecs == 0) {
    } else if (ctx->millisecs == 0) {
        buf = PR_sprintf_append(buf, "%d seconds", ctx->seconds);
    } else {
        buf = PR_sprintf_append(buf, "%d.%03d seconds",
                                ctx->seconds, ctx->millisecs);
    }
    }
    return buf;
}

void
Usage(char *progName)
Usage(char *progName)
{
    fprintf(stderr, "Usage: %s [-s | -e] [-i iterations | -p period] "
                    "[-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] "
                    " -h token [-d certdir] [-w password] [-g] [-k keylength] "
                    "[-x exponent] [-f pwfile]\n",
            progName);
    fprintf(stderr, "%-20s Cert database directory (default is ~/.netscape)\n",
            "-d certdir");
    fprintf(stderr, "%-20s How many operations to perform\n", "-i iterations");
    fprintf(stderr, "%-20s How many operations to perform\n", "-i iterations");
    fprintf(stderr, "%-20s How many seconds to run\n", "-p period");
    fprintf(stderr, "%-20s Perform signing (private key) operations\n", "-s");
    fprintf(stderr, "%-20s Perform encryption (public key) operations\n", "-e");
    fprintf(stderr, "%-20s Nickname of certificate or key, prefixed "
                    "by optional token name\n",
                    "by optional token name\n",
            "-n nickname");
    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",
    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");
    fprintf(stderr, "%-20s set public exponent for keygen\n", "-x");
    fprintf(stderr, "%-20s set public exponent for keygen\n", "-x");
    fprintf(stderr, "%-20s Number of execution threads (default 1)\n",
            "-t threads");
    exit(-1);
}


static void
dumpBytes(unsigned char *b, int l)
{
    int i;
    if (l <= 0)
    if (l <= 0)
        return;
    for (i = 0; i < l; ++i) {
        if (i % 16 == 0)
            printf("\t");
        printf(" %02x", b[i]);
        printf(" %02x", b[i]);
        if (i % 16 == 15)
            printf("\n");
    }
    if ((i % 16) != 0)
    if ((i % 16) != 0)
        printf("\n");
}

static void
dumpItem(SECItem *item, const char *description)
dumpItem(SECItem *item, const char *description)
{
    if (item->len & 1 && item->data[0] == 0) {
        printf("%s: (%d bytes)\n", description, item->len - 1);
        dumpBytes(item->data + 1, item->len - 1);
    } else {
    } else {
        printf("%s: (%d bytes)\n", description, item->len);
        dumpBytes(item->data, item->len);
    }
}


void
printPrivKey(NSSLOWKEYPrivateKey *privKey)
{
    RSAPrivateKey *rsa = &privKey->u.rsa;


    dumpItem(&rsa->modulus, "n");
    dumpItem(&rsa->publicExponent, "e");
    dumpItem(&rsa->privateExponent, "d");
    dumpItem(&rsa->prime1, "P");
    dumpItem(&rsa->prime2, "Q");
    dumpItem(&rsa->prime2, "Q");
    dumpItem(&rsa->exponent1, "d % (P-1)");
    dumpItem(&rsa->exponent2, "d % (Q-1)");
    dumpItem(&rsa->exponent2, "d % (Q-1)");
    dumpItem(&rsa->coefficient, "(Q ** -1) % P");
    puts("");
}

typedef SECStatus (*RSAOp)(void *key,
                           unsigned char *output,
                           unsigned char *input);
                           unsigned char *input);

typedef struct {
    SECKEYPublicKey *pubKey;
    SECKEYPrivateKey *privKey;
} PK11Keys;
} PK11Keys;

SECStatus
PK11_PublicKeyOp(SECKEYPublicKey *key,
                 unsigned char *output,
                 unsigned char *input)
                 unsigned char *input)
{
    return PK11_PubEncryptRaw(key, output, input, key->u.rsa.modulus.len,
                              NULL);
}


SECStatus
PK11_PrivateKeyOp(PK11Keys *keys,
                  unsigned char *output,
                  unsigned char *input)
{
{
    unsigned outLen = 0;
    return PK11_PrivDecryptRaw(keys->privKey,
                               output, &outLen,
                               keys->pubKey->u.rsa.modulus.len, input,
                               keys->pubKey->u.rsa.modulus.len);
                               keys->pubKey->u.rsa.modulus.len);
}
typedef struct ThreadRunDataStr ThreadRunData;

struct ThreadRunDataStr {
struct ThreadRunDataStr {
    const PRBool *doIters;
    const void *rsaKey;
    const unsigned char *buf;
    RSAOp fn;
    int seconds;
    int seconds;
    long iters;
    long iterRes;
    PRErrorCode errNum;
    SECStatus status;
};
};

void
ThreadExecFunction(void *data)
{
    ThreadRunData *tdata = (ThreadRunData *)data;
    ThreadRunData *tdata = (ThreadRunData *)data;
    unsigned char buf2[BUFFER_BYTES];

    tdata->status = SECSuccess;
    if (*tdata->doIters) {
        long i = tdata->iters;
        long i = tdata->iters;
        tdata->iterRes = 0;
        while (i--) {
            SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
                                     (unsigned char *)tdata->buf);
            if (rv != SECSuccess) {
            if (rv != SECSuccess) {
                tdata->errNum = PORT_GetError();
                tdata->status = rv;
                break;
            }
            tdata->iterRes++;
            tdata->iterRes++;
        }
    } else {
        PRIntervalTime total = PR_SecondsToInterval(tdata->seconds);
        PRIntervalTime start = PR_IntervalNow();
        tdata->iterRes = 0;
        tdata->iterRes = 0;
        while (PR_IntervalNow() - start < total) {
            SECStatus rv = tdata->fn((void *)tdata->rsaKey, buf2,
                                     (unsigned char *)tdata->buf);
            if (rv != SECSuccess) {
                tdata->errNum = PORT_GetError();
                tdata->errNum = PORT_GetError();
                tdata->status = rv;
                break;
            }
            tdata->iterRes++;
        }
    }
}

#define INT_ARG(arg, def) atol(arg) > 0 ? atol(arg) : def


int
main(int argc, char **argv)
{
    TimingContext *timeCtx = NULL;
    SECKEYPublicKey *pubHighKey = NULL;
    SECKEYPublicKey *pubHighKey = NULL;
    SECKEYPrivateKey *privHighKey = NULL;
    NSSLOWKEYPrivateKey *privKey = NULL;
    NSSLOWKEYPublicKey *pubKey = NULL;
    CERTCertificate *cert = NULL;
    char *progName = NULL;
    char *progName = NULL;
    char *secDir = NULL;
    char *nickname = NULL;
    char *slotname = NULL;
    long keybits = 0;
    RSAOp fn;
    RSAOp fn;
    void *rsaKeyPtr = NULL;
    PLOptState *optstate;
    PLOptStatus optstatus;
    long iters = DEFAULT_ITERS;
    int i;
    int i;
    PRBool doPriv = PR_FALSE;
    PRBool doPub = PR_FALSE;
    int rv;
    unsigned char buf[BUFFER_BYTES];
    unsigned char buf2[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
                                                       object key */
                                                       object key */
    PRBool useSessionKey = PR_FALSE; /* use PKCS#11 session
                                                       object key */
    PRBool useBLKey = PR_FALSE;      /* use freebl */
    PK11SlotInfo *slot = NULL;       /* slot for session
                                                       object key operations */
    PRBool doKeyGen = PR_FALSE;
    int publicExponent = DEFAULT_EXPONENT;
    int publicExponent = DEFAULT_EXPONENT;
    PK11Keys keys;
    int peCount = 0;
    CK_BYTE pubEx[4];
    SECItem pe;
    RSAPublicKey pubKeyStr;
    RSAPublicKey pubKeyStr;
    int threadNum = DEFAULT_THREADS;
    ThreadRunData **runDataArr = NULL;
    PRThread **threadsArr = NULL;
    int calcThreads = 0;


    progName = strrchr(argv[0], '/');
    if (!progName)
        progName = strrchr(argv[0], '\\');
    progName = progName ? progName + 1 : argv[0];
    progName = progName ? progName + 1 : argv[0];

    optstate = PL_CreateOptState(argc, argv, "d:ef:gh:i:k:n:p:st:w:x:");
    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
        switch (optstate->option) {
            case '?':
            case '?':
                Usage(progName);
                break;
            case 'd':
                secDir = PORT_Strdup(optstate->value);
                break;
                break;
            case 'i':
                iters = INT_ARG(optstate->value, DEFAULT_ITERS);
                doIters = PR_TRUE;
                break;
            case 's':
            case 's':
                doPriv = PR_TRUE;
                break;
            case 'e':
                doPub = PR_TRUE;
                break;
                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")) {
                if (nickname && strcmp(nickname, "none")) {
                    useTokenKey = PR_TRUE;
                } else {
                    useBLKey = PR_TRUE;
                }
                break;
                break;
            case 'p':
                seconds = INT_ARG(optstate->value, DEFAULT_DURATION);
                doTime = PR_TRUE;
                break;
            case 'h':
            case 'h':
                slotname = PORT_Strdup(optstate->value);
                useSessionKey = PR_TRUE;
                break;
            case 'k':
                keybits = INT_ARG(optstate->value, DEFAULT_KEY_BITS);
                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':
            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);
                threadNum = INT_ARG(optstate->value, DEFAULT_THREADS);
                break;
        }
    }
    if (optstatus == PL_OPT_BAD)
        Usage(progName);

    if ((doPriv && doPub) || (doIters && doTime) ||
    if ((doPriv && doPub) || (doIters && doTime) ||
        ((useTokenKey + useSessionKey + useBLKey) != PR_TRUE) ||
        (useTokenKey && keybits) || (useTokenKey && doKeyGen) ||
        (keybits && (keybits < MIN_KEY_BITS || keybits > MAX_KEY_BITS))) {
        Usage(progName);
    }
    }

    if (doIters && doTime)
        Usage(progName);

    if (!doTime) {
    if (!doTime) {
        doIters = PR_TRUE;
    }

    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);


    PK11_SetPasswordFunc(SECU_GetModulePassword);
    secDir = SECU_ConfigDirectory(secDir);

    if (useTokenKey || useSessionKey) {
        rv = NSS_Init(secDir);
        rv = NSS_Init(secDir);
        if (rv != SECSuccess) {
            fprintf(stderr, "NSS_Init failed.\n");
            exit(1);
        }
    } else {
        rv = NSS_NoDB_Init(NULL);
        if (rv != SECSuccess) {
        if (rv != SECSuccess) {
            fprintf(stderr, "NSS_NoDB_Init failed.\n");
            exit(1);
        }
    }


    if (useTokenKey) {
        CK_OBJECT_HANDLE kh = CK_INVALID_HANDLE;

        cert = PK11_FindCertFromNickname(nickname, &pwData);
        if (cert == NULL) {
        if (cert == NULL) {
            fprintf(stderr,
                    "Can't find certificate by name \"%s\"\n", nickname);
            exit(1);
        }
        pubHighKey = CERT_ExtractPublicKey(cert);
        pubHighKey = CERT_ExtractPublicKey(cert);
        if (pubHighKey == NULL) {
            fprintf(stderr, "Can't extract public key from certificate");
            exit(1);
        }

        if (doPub) {
            /* do public key ops */
            /* do public key ops */
            fn = (RSAOp)PK11_PublicKeyOp;
            rsaKeyPtr = (void *)pubHighKey;

            kh = PK11_ImportPublicKey(cert->slot, pubHighKey, PR_FALSE);
            if (CK_INVALID_HANDLE == kh) {
            if (CK_INVALID_HANDLE == kh) {
                fprintf(stderr,
                        "Unable to import public key to certificate slot.");
                exit(1);
            }
            pubHighKey->pkcs11Slot = PK11_ReferenceSlot(cert->slot);
            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 */
            /* 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);
                exit(1);
            }

            SECKEY_CacheStaticFlags(privHighKey);
            fn = (RSAOp)PK11_PrivateKeyOp;
            keys.privKey = privHighKey;
            keys.privKey = privHighKey;
            keys.pubKey = pubHighKey;
            rsaKeyPtr = (void *)&keys;
            printf("Using PKCS#11 for RSA decryption with token %s.\n",
                   PK11_GetTokenName(privHighKey->pkcs11Slot));
        }
        }
    } else

        if (useSessionKey) {
        /* use PKCS#11 session key objects */
        PK11RSAGenParams rsaparams;
        PK11RSAGenParams rsaparams;
        void *params;

        slot = PK11_FindSlotByName(slotname); /* locate target slot */
        if (!slot) {
        if (!slot) {
            fprintf(stderr, "Can't find slot \"%s\"\n", slotname);
            exit(1);
        }

        /* do a temporary keygen in selected slot */
        /* do a temporary keygen in selected slot */
        if (!keybits) {
            keybits = DEFAULT_KEY_BITS;
        }

        printf("Using PKCS#11 with %ld bits session key in token %s.\n",
        printf("Using PKCS#11 with %ld bits session key in token %s.\n",
               keybits, PK11_GetTokenName(slot));

        rsaparams.keySizeInBits = keybits;
        rsaparams.pe = publicExponent;
        params = &rsaparams;
        params = &rsaparams;

        fprintf(stderr, "\nGenerating RSA key. This may take a few moments.\n");

        privHighKey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN,
                                           params, &pubHighKey, PR_FALSE,
                                           params, &pubHighKey, PR_FALSE,
                                           PR_FALSE, (void *)&pwData);
        if (!privHighKey) {
            fprintf(stderr,
                    "Key generation failed in token \"%s\"\n",
                    PK11_GetTokenName(slot));
                    PK11_GetTokenName(slot));
            exit(1);
        }

        SECKEY_CacheStaticFlags(privHighKey);


        fprintf(stderr, "Keygen completed.\n");

        if (doPub) {
            /* do public key operations */
            fn = (RSAOp)PK11_PublicKeyOp;
            fn = (RSAOp)PK11_PublicKeyOp;
            rsaKeyPtr = (void *)pubHighKey;
        } else {
            /* do private key operations */
            fn = (RSAOp)PK11_PrivateKeyOp;
            keys.privKey = privHighKey;
            keys.pubKey = pubHighKey;
            rsaKeyPtr = (void *)&keys;
            rsaKeyPtr = (void *)&keys;
        }
    } else

    {
        /* use freebl directly */
        /* use freebl directly */
        if (!keybits) {
            keybits = DEFAULT_KEY_BITS;
        }
        if (!doKeyGen) {
            if (keybits != DEFAULT_KEY_BITS) {
            if (keybits != DEFAULT_KEY_BITS) {
                doKeyGen = PR_TRUE;
            }
        }
        printf("Using freebl with %ld bits key.\n", keybits);
        if (doKeyGen) {
        if (doKeyGen) {
            fprintf(stderr, "\nGenerating RSA key. "
                            "This may take a few moments.\n");
            for (i = 0; i < 4; i++) {
                if (peCount || (publicExponent & ((unsigned long)0xff000000L >>
                                                  (i * 8)))) {
                                                  (i * 8)))) {
                    pubEx[peCount] = (CK_BYTE)((publicExponent >>
                                                (3 - i) * 8) &
                                                (3 - i) * 8) &
                                               0xff);
                    peCount++;
                }
            }
            pe.len = peCount;
            pe.len = peCount;
            pe.data = &pubEx[0];
            pe.type = siBuffer;

            rsaKeyPtr = RSA_NewKey(keybits, &pe);
            fprintf(stderr, "Keygen completed.\n");
            fprintf(stderr, "Keygen completed.\n");
        } else {
            /* use a hardcoded key */
            printf("Using hardcoded %ld bits key.\n", keybits);
            if (doPub) {
                pubKey = getDefaultRSAPublicKey(keybits);
                pubKey = getDefaultRSAPublicKey(keybits);
            } else {
                privKey = getDefaultRSAPrivateKey(keybits);
            }
        }


        if (doPub) {
            /* do public key operations */
            fn = (RSAOp)RSA_PublicKeyOp;
            if (rsaKeyPtr) {
            if (rsaKeyPtr) {
                /* convert the RSAPrivateKey to RSAPublicKey */
                pubKeyStr.arena = NULL;
                pubKeyStr.modulus = ((RSAPrivateKey *)rsaKeyPtr)->modulus;
                pubKeyStr.publicExponent =
                    ((RSAPrivateKey *)rsaKeyPtr)->publicExponent;
                    ((RSAPrivateKey *)rsaKeyPtr)->publicExponent;
                rsaKeyPtr = &pubKeyStr;
            } else {
                /* convert NSSLOWKeyPublicKey to RSAPublicKey */
                rsaKeyPtr = (void *)(&pubKey->u.rsa);
            }
            }
            PORT_Assert(rsaKeyPtr);
        } else {
            /* do private key operations */
            fn = (RSAOp)RSA_PrivateKeyOp;
            if (privKey) {
            if (privKey) {
                /* convert NSSLOWKeyPrivateKey to RSAPrivateKey */
                rsaKeyPtr = (void *)(&privKey->u.rsa);
            }
            PORT_Assert(rsaKeyPtr);
            PORT_Assert(rsaKeyPtr);
        }
    }

    memset(buf, 1, sizeof buf);
    rv = fn(rsaKeyPtr, buf2, buf);
    rv = fn(rsaKeyPtr, buf2, buf);
    if (rv != SECSuccess) {
        PRErrorCode errNum;
        const char *errStr = NULL;

        errNum = PORT_GetError();
        if (errNum)
            errStr = SECU_Strerror(errNum);
            errStr = SECU_Strerror(errNum);
        else
            errNum = rv;
        if (!errStr)
            errStr = "(null)";
        fprintf(stderr, "Error in RSA operation: %d : %s\n", errNum, errStr);
        fprintf(stderr, "Error in RSA operation: %d : %s\n", errNum, errStr);
        exit(1);
    }

    threadsArr = (PRThread **)PORT_Alloc(threadNum * sizeof(PRThread *));
    runDataArr = (ThreadRunData **)PORT_Alloc(threadNum * sizeof(ThreadRunData *));
    runDataArr = (ThreadRunData **)PORT_Alloc(threadNum * sizeof(ThreadRunData *));
    timeCtx = CreateTimingContext();
    TimingBegin(timeCtx, PR_Now());
    for (i = 0; i < threadNum; i++) {
        runDataArr[i] = (ThreadRunData *)PORT_Alloc(sizeof(ThreadRunData));
        runDataArr[i]->fn = fn;
        runDataArr[i]->fn = fn;
        runDataArr[i]->buf = buf;
        runDataArr[i]->doIters = &doIters;
        runDataArr[i]->rsaKey = rsaKeyPtr;
        runDataArr[i]->seconds = seconds;
        runDataArr[i]->iters = iters;
        runDataArr[i]->iters = iters;
        threadsArr[i] =
            PR_CreateThread(PR_USER_THREAD,
                            ThreadExecFunction,
                            (void *)runDataArr[i],
                            (void *)runDataArr[i],
                            PR_PRIORITY_NORMAL,
                            PR_GLOBAL_THREAD,
                            PR_JOINABLE_THREAD,
                            0);
                            0);
    }
    iters = 0;
    calcThreads = 0;
    for (i = 0; i < threadNum; i++, calcThreads++) {
        PR_JoinThread(threadsArr[i]);
        PR_JoinThread(threadsArr[i]);
        if (runDataArr[i]->status != SECSuccess) {
            const char *errStr = SECU_Strerror(runDataArr[i]->errNum);
            fprintf(stderr, "Thread %d: Error in RSA operation: %d : %s\n",
                    i, runDataArr[i]->errNum, errStr);
            calcThreads -= 1;
            calcThreads -= 1;
        } else {
            iters += runDataArr[i]->iterRes;
        }
        PORT_Free((void *)runDataArr[i]);
    }
    }
    PORT_Free(runDataArr);
    PORT_Free(threadsArr);

    TimingEnd(timeCtx, PR_Now());


    printf("%ld iterations in %s\n",
           iters, TimingGenerateString(timeCtx));
    printf("%.2f operations/s .\n", ((double)(iters) * (double)1000000.0) / (double)timeCtx->interval);
    TimingDivide(timeCtx, iters);
    printf("one operation every %s\n", TimingGenerateString(timeCtx));

    if (pubHighKey) {
    if (pubHighKey) {
        SECKEY_DestroyPublicKey(pubHighKey);
    }

    if (privHighKey) {
        SECKEY_DestroyPrivateKey(privHighKey);
        SECKEY_DestroyPrivateKey(privHighKey);
    }

    if (cert) {
        CERT_DestroyCertificate(cert);
    }
    }

    if (NSS_Shutdown() != SECSuccess) {
        exit(1);
    }

    return 0;
    return 0;
}