1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
22 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
57 #include "ecl-curve.h"
58 SECStatus
EC_DecodeParams(const SECItem
*encodedParams
,
60 SECStatus
EC_CopyParams(PRArenaPool
*arena
, ECParams
*dstParams
,
61 const ECParams
*srcParams
);
64 /* Temporary - add debugging ouput on windows for RSA to track QA failure */
66 #define TRACK_BLTEST_BUG
67 char __bltDBG
[] = "BLTEST DEBUG";
73 #define BLTEST_DEFAULT_CHUNKSIZE 4096
75 #define WORDSIZE sizeof(unsigned long)
77 #define CHECKERROR(rv, ln) \
79 PRErrorCode prerror = PR_GetError(); \
80 PR_fprintf(PR_STDERR, "%s: ERR %d (%s) at line %d.\n", progName, \
81 prerror, SECU_Strerror(prerror), ln); \
85 /* Macros for performance timing. */
87 time1 = PR_IntervalNow();
89 #define TIMEFINISH(time, reps) \
90 time2 = (PRIntervalTime)(PR_IntervalNow() - time1); \
91 time1 = PR_IntervalToMilliseconds(time2); \
92 time = ((double)(time1))/reps;
94 #define TIMEMARK(seconds) \
95 time1 = PR_SecondsToInterval(seconds); \
102 LL_DIV(tmp, time1, time2); \
105 opsBetweenChecks = 1; \
107 LL_L2I(opsBetweenChecks, tmp); \
110 opsBetweenChecks = 10; \
114 time1 = PR_IntervalNow();
116 #define TIMETOFINISH() \
117 PR_IntervalNow() - time1 >= time2
121 #define PRINTUSAGE(subject, option, predicate) \
122 fprintf(stderr, "%10s %s\t%s\n", subject, option, predicate);
123 fprintf(stderr
, "\n");
124 PRINTUSAGE(progName
, "[-DEHSV]", "List available cipher modes"); /* XXX */
125 fprintf(stderr
, "\n");
126 PRINTUSAGE(progName
, "-E -m mode ", "Encrypt a buffer");
127 PRINTUSAGE("", "", "[-i plaintext] [-o ciphertext] [-k key] [-v iv]");
128 PRINTUSAGE("", "", "[-b bufsize] [-g keysize] [-e exp] [-r rounds]");
129 PRINTUSAGE("", "", "[-w wordsize] [-p repetitions | -5 time_interval]");
130 PRINTUSAGE("", "", "[-4 th_num]");
131 PRINTUSAGE("", "-m", "cipher mode to use");
132 PRINTUSAGE("", "-i", "file which contains input buffer");
133 PRINTUSAGE("", "-o", "file for output buffer");
134 PRINTUSAGE("", "-k", "file which contains key");
135 PRINTUSAGE("", "-v", "file which contains initialization vector");
136 PRINTUSAGE("", "-b", "size of input buffer");
137 PRINTUSAGE("", "-g", "key size (in bytes)");
138 PRINTUSAGE("", "-p", "do performance test");
139 PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
140 PRINTUSAGE("", "-5", "run test for specified time interval(in seconds)");
141 PRINTUSAGE("(rsa)", "-e", "rsa public exponent");
142 PRINTUSAGE("(rc5)", "-r", "number of rounds");
143 PRINTUSAGE("(rc5)", "-w", "wordsize (32 or 64)");
144 fprintf(stderr
, "\n");
145 PRINTUSAGE(progName
, "-D -m mode", "Decrypt a buffer");
146 PRINTUSAGE("", "", "[-i plaintext] [-o ciphertext] [-k key] [-v iv]");
147 PRINTUSAGE("", "", "[-p repetitions | -5 time_interval] [-4 th_num]");
148 PRINTUSAGE("", "-m", "cipher mode to use");
149 PRINTUSAGE("", "-i", "file which contains input buffer");
150 PRINTUSAGE("", "-o", "file for output buffer");
151 PRINTUSAGE("", "-k", "file which contains key");
152 PRINTUSAGE("", "-v", "file which contains initialization vector");
153 PRINTUSAGE("", "-p", "do performance test");
154 PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
155 PRINTUSAGE("", "-5", "run test for specified time interval(in seconds)");
156 fprintf(stderr
, "\n");
157 PRINTUSAGE(progName
, "-H -m mode", "Hash a buffer");
158 PRINTUSAGE("", "", "[-i plaintext] [-o hash]");
159 PRINTUSAGE("", "", "[-b bufsize]");
160 PRINTUSAGE("", "", "[-p repetitions | -5 time_interval] [-4 th_num]");
161 PRINTUSAGE("", "-m", "cipher mode to use");
162 PRINTUSAGE("", "-i", "file which contains input buffer");
163 PRINTUSAGE("", "-o", "file for hash");
164 PRINTUSAGE("", "-b", "size of input buffer");
165 PRINTUSAGE("", "-p", "do performance test");
166 PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
167 PRINTUSAGE("", "-5", "run test for specified time interval(in seconds)");
168 fprintf(stderr
, "\n");
169 PRINTUSAGE(progName
, "-S -m mode", "Sign a buffer");
170 PRINTUSAGE("", "", "[-i plaintext] [-o signature] [-k key]");
171 PRINTUSAGE("", "", "[-b bufsize]");
172 #ifdef NSS_ENABLE_ECC
173 PRINTUSAGE("", "", "[-n curvename]");
175 PRINTUSAGE("", "", "[-p repetitions | -5 time_interval] [-4 th_num]");
176 PRINTUSAGE("", "-m", "cipher mode to use");
177 PRINTUSAGE("", "-i", "file which contains input buffer");
178 PRINTUSAGE("", "-o", "file for signature");
179 PRINTUSAGE("", "-k", "file which contains key");
180 #ifdef NSS_ENABLE_ECC
181 PRINTUSAGE("", "-n", "name of curve for EC key generation; one of:");
182 PRINTUSAGE("", "", " sect163k1, nistk163, sect163r1, sect163r2,");
183 PRINTUSAGE("", "", " nistb163, sect193r1, sect193r2, sect233k1, nistk233,");
184 PRINTUSAGE("", "", " sect233r1, nistb233, sect239k1, sect283k1, nistk283,");
185 PRINTUSAGE("", "", " sect283r1, nistb283, sect409k1, nistk409, sect409r1,");
186 PRINTUSAGE("", "", " nistb409, sect571k1, nistk571, sect571r1, nistb571,");
187 PRINTUSAGE("", "", " secp160k1, secp160r1, secp160r2, secp192k1, secp192r1,");
188 PRINTUSAGE("", "", " nistp192, secp224k1, secp224r1, nistp224, secp256k1,");
189 PRINTUSAGE("", "", " secp256r1, nistp256, secp384r1, nistp384, secp521r1,");
190 PRINTUSAGE("", "", " nistp521, prime192v1, prime192v2, prime192v3,");
191 PRINTUSAGE("", "", " prime239v1, prime239v2, prime239v3, c2pnb163v1,");
192 PRINTUSAGE("", "", " c2pnb163v2, c2pnb163v3, c2pnb176v1, c2tnb191v1,");
193 PRINTUSAGE("", "", " c2tnb191v2, c2tnb191v3, c2onb191v4, c2onb191v5,");
194 PRINTUSAGE("", "", " c2pnb208w1, c2tnb239v1, c2tnb239v2, c2tnb239v3,");
195 PRINTUSAGE("", "", " c2onb239v4, c2onb239v5, c2pnb272w1, c2pnb304w1,");
196 PRINTUSAGE("", "", " c2tnb359w1, c2pnb368w1, c2tnb431r1, secp112r1,");
197 PRINTUSAGE("", "", " secp112r2, secp128r1, secp128r2, sect113r1, sect113r2,");
198 PRINTUSAGE("", "", " sect131r1, sect131r2");
200 PRINTUSAGE("", "-p", "do performance test");
201 PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
202 PRINTUSAGE("", "-5", "run test for specified time interval(in seconds)");
203 fprintf(stderr
, "\n");
204 PRINTUSAGE(progName
, "-V -m mode", "Verify a signed buffer");
205 PRINTUSAGE("", "", "[-i plaintext] [-s signature] [-k key]");
206 PRINTUSAGE("", "", "[-p repetitions | -5 time_interval] [-4 th_num]");
207 PRINTUSAGE("", "-m", "cipher mode to use");
208 PRINTUSAGE("", "-i", "file which contains input buffer");
209 PRINTUSAGE("", "-s", "file which contains signature of input buffer");
210 PRINTUSAGE("", "-k", "file which contains key");
211 PRINTUSAGE("", "-p", "do performance test");
212 PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
213 PRINTUSAGE("", "-5", "run test for specified time interval(in seconds)");
214 fprintf(stderr
, "\n");
215 PRINTUSAGE(progName
, "-N -m mode -b bufsize",
216 "Create a nonce plaintext and key");
217 PRINTUSAGE("", "", "[-g keysize] [-u cxreps]");
218 PRINTUSAGE("", "-g", "key size (in bytes)");
219 PRINTUSAGE("", "-u", "number of repetitions of context creation");
220 fprintf(stderr
, "\n");
221 PRINTUSAGE(progName
, "-F", "Run the FIPS self-test");
222 fprintf(stderr
, "\n");
223 PRINTUSAGE(progName
, "-T [-m mode1,mode2...]", "Run the BLAPI self-test");
224 fprintf(stderr
, "\n");
228 /* Helper functions for ascii<-->binary conversion/reading/writing */
231 struct item_with_arena
{
237 get_binary(void *arg
, const unsigned char *ibuf
, PRInt32 size
)
239 struct item_with_arena
*it
= arg
;
240 SECItem
*binary
= it
->item
;
243 if (binary
->data
== NULL
) {
244 tmp
= SECITEM_AllocItem(it
->arena
, NULL
, size
);
245 binary
->data
= tmp
->data
;
246 binary
->len
= tmp
->len
;
249 SECITEM_ReallocItem(NULL
, binary
, binary
->len
, binary
->len
+ size
);
252 PORT_Memcpy(&binary
->data
[index
], ibuf
, size
);
257 atob(SECItem
*ascii
, SECItem
*binary
, PRArenaPool
*arena
)
260 NSSBase64Decoder
*cx
;
261 struct item_with_arena it
;
267 len
= (strncmp(&ascii
->data
[ascii
->len
-2],"\r\n",2)) ?
268 ascii
->len
: ascii
->len
-2;
269 cx
= NSSBase64Decoder_Create(get_binary
, &it
);
270 status
= NSSBase64Decoder_Update(cx
, (const char *)ascii
->data
, len
);
271 status
= NSSBase64Decoder_Destroy(cx
, PR_FALSE
);
276 output_ascii(void *arg
, const char *obuf
, PRInt32 size
)
278 PRFileDesc
*outfile
= arg
;
279 PRInt32 nb
= PR_Write(outfile
, obuf
, size
);
281 PORT_SetError(SEC_ERROR_IO
);
288 btoa_file(SECItem
*binary
, PRFileDesc
*outfile
)
291 NSSBase64Encoder
*cx
;
295 if (binary
->len
== 0)
297 cx
= NSSBase64Encoder_Create(output_ascii
, outfile
);
298 status
= NSSBase64Encoder_Update(cx
, binary
->data
, binary
->len
);
299 status
= NSSBase64Encoder_Destroy(cx
, PR_FALSE
);
300 status
= PR_Write(outfile
, "\r\n", 2);
305 hex_from_2char(unsigned char *c2
, unsigned char *byteval
)
308 unsigned char offset
;
310 for (i
=0; i
<2; i
++) {
311 if (c2
[i
] >= '0' && c2
[i
] <= '9') {
312 offset
= c2
[i
] - '0';
313 *byteval
|= offset
<< 4*(1-i
);
314 } else if (c2
[i
] >= 'a' && c2
[i
] <= 'f') {
315 offset
= c2
[i
] - 'a';
316 *byteval
|= (offset
+ 10) << 4*(1-i
);
317 } else if (c2
[i
] >= 'A' && c2
[i
] <= 'F') {
318 offset
= c2
[i
] - 'A';
319 *byteval
|= (offset
+ 10) << 4*(1-i
);
328 char2_from_hex(unsigned char byteval
, unsigned char *c2
)
331 unsigned char offset
;
332 for (i
=0; i
<2; i
++) {
333 offset
= (byteval
>> 4*(1-i
)) & 0x0f;
335 c2
[i
] = '0' + offset
;
337 c2
[i
] = 'A' + offset
- 10;
344 serialize_key(SECItem
*it
, int ni
, PRFileDesc
*file
)
346 unsigned char len
[4];
349 NSSBase64Encoder
*cx
;
353 cx
= NSSBase64Encoder_Create(output_ascii
, file
);
354 for (i
=0; i
<ni
; i
++, it
++) {
355 len
[0] = (it
->len
>> 24) & 0xff;
356 len
[1] = (it
->len
>> 16) & 0xff;
357 len
[2] = (it
->len
>> 8) & 0xff;
358 len
[3] = (it
->len
& 0xff);
359 status
= NSSBase64Encoder_Update(cx
, len
, 4);
360 status
= NSSBase64Encoder_Update(cx
, it
->data
, it
->len
);
362 status
= NSSBase64Encoder_Destroy(cx
, PR_FALSE
);
363 status
= PR_Write(file
, "\r\n", 2);
367 key_from_filedata(PRArenaPool
*arena
, SECItem
*it
, int ns
, int ni
, SECItem
*filedata
)
371 unsigned char *buf
= filedata
->data
;
372 for (i
=0; i
<ni
; i
++) {
373 len
= (buf
[fpos
++] & 0xff) << 24;
374 len
|= (buf
[fpos
++] & 0xff) << 16;
375 len
|= (buf
[fpos
++] & 0xff) << 8;
376 len
|= (buf
[fpos
++] & 0xff);
380 it
->data
= PORT_ArenaAlloc(arena
, it
->len
);
381 PORT_Memcpy(it
->data
, &buf
[fpos
], it
->len
);
392 static RSAPrivateKey
*
393 rsakey_from_filedata(SECItem
*filedata
)
397 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
398 key
= (RSAPrivateKey
*)PORT_ArenaZAlloc(arena
, sizeof(RSAPrivateKey
));
400 key_from_filedata(arena
, &key
->version
, 0, 9, filedata
);
405 pqg_from_filedata(SECItem
*filedata
)
409 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
410 pqg
= (PQGParams
*)PORT_ArenaZAlloc(arena
, sizeof(PQGParams
));
412 key_from_filedata(arena
, &pqg
->prime
, 0, 3, filedata
);
416 static DSAPrivateKey
*
417 dsakey_from_filedata(SECItem
*filedata
)
421 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
422 key
= (DSAPrivateKey
*)PORT_ArenaZAlloc(arena
, sizeof(DSAPrivateKey
));
423 key
->params
.arena
= arena
;
424 key_from_filedata(arena
, &key
->params
.prime
, 0, 5, filedata
);
428 #ifdef NSS_ENABLE_ECC
429 static ECPrivateKey
*
430 eckey_from_filedata(SECItem
*filedata
)
435 ECParams
*tmpECParams
= NULL
;
436 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
437 key
= (ECPrivateKey
*)PORT_ArenaZAlloc(arena
, sizeof(ECPrivateKey
));
438 /* read and convert params */
439 key
->ecParams
.arena
= arena
;
440 key_from_filedata(arena
, &key
->ecParams
.DEREncoding
, 0, 1, filedata
);
442 CHECKERROR(rv
, __LINE__
);
443 rv
= EC_DecodeParams(&key
->ecParams
.DEREncoding
, &tmpECParams
);
444 CHECKERROR(rv
, __LINE__
);
445 rv
= EC_CopyParams(key
->ecParams
.arena
, &key
->ecParams
, tmpECParams
);
446 CHECKERROR(rv
, __LINE__
);
447 rv
= SECOID_Shutdown();
448 CHECKERROR(rv
, __LINE__
);
449 PORT_FreeArena(tmpECParams
->arena
, PR_TRUE
);
451 key_from_filedata(arena
, &key
->publicValue
, 1, 3, filedata
);
455 typedef struct curveNameTagPairStr
{
457 SECOidTag curveOidTag
;
460 #define DEFAULT_CURVE_OID_TAG SEC_OID_SECG_EC_SECP192R1
461 /* #define DEFAULT_CURVE_OID_TAG SEC_OID_SECG_EC_SECP160R1 */
463 static CurveNameTagPair nameTagPair
[] =
465 { "sect163k1", SEC_OID_SECG_EC_SECT163K1
},
466 { "nistk163", SEC_OID_SECG_EC_SECT163K1
},
467 { "sect163r1", SEC_OID_SECG_EC_SECT163R1
},
468 { "sect163r2", SEC_OID_SECG_EC_SECT163R2
},
469 { "nistb163", SEC_OID_SECG_EC_SECT163R2
},
470 { "sect193r1", SEC_OID_SECG_EC_SECT193R1
},
471 { "sect193r2", SEC_OID_SECG_EC_SECT193R2
},
472 { "sect233k1", SEC_OID_SECG_EC_SECT233K1
},
473 { "nistk233", SEC_OID_SECG_EC_SECT233K1
},
474 { "sect233r1", SEC_OID_SECG_EC_SECT233R1
},
475 { "nistb233", SEC_OID_SECG_EC_SECT233R1
},
476 { "sect239k1", SEC_OID_SECG_EC_SECT239K1
},
477 { "sect283k1", SEC_OID_SECG_EC_SECT283K1
},
478 { "nistk283", SEC_OID_SECG_EC_SECT283K1
},
479 { "sect283r1", SEC_OID_SECG_EC_SECT283R1
},
480 { "nistb283", SEC_OID_SECG_EC_SECT283R1
},
481 { "sect409k1", SEC_OID_SECG_EC_SECT409K1
},
482 { "nistk409", SEC_OID_SECG_EC_SECT409K1
},
483 { "sect409r1", SEC_OID_SECG_EC_SECT409R1
},
484 { "nistb409", SEC_OID_SECG_EC_SECT409R1
},
485 { "sect571k1", SEC_OID_SECG_EC_SECT571K1
},
486 { "nistk571", SEC_OID_SECG_EC_SECT571K1
},
487 { "sect571r1", SEC_OID_SECG_EC_SECT571R1
},
488 { "nistb571", SEC_OID_SECG_EC_SECT571R1
},
489 { "secp160k1", SEC_OID_SECG_EC_SECP160K1
},
490 { "secp160r1", SEC_OID_SECG_EC_SECP160R1
},
491 { "secp160r2", SEC_OID_SECG_EC_SECP160R2
},
492 { "secp192k1", SEC_OID_SECG_EC_SECP192K1
},
493 { "secp192r1", SEC_OID_SECG_EC_SECP192R1
},
494 { "nistp192", SEC_OID_SECG_EC_SECP192R1
},
495 { "secp224k1", SEC_OID_SECG_EC_SECP224K1
},
496 { "secp224r1", SEC_OID_SECG_EC_SECP224R1
},
497 { "nistp224", SEC_OID_SECG_EC_SECP224R1
},
498 { "secp256k1", SEC_OID_SECG_EC_SECP256K1
},
499 { "secp256r1", SEC_OID_SECG_EC_SECP256R1
},
500 { "nistp256", SEC_OID_SECG_EC_SECP256R1
},
501 { "secp384r1", SEC_OID_SECG_EC_SECP384R1
},
502 { "nistp384", SEC_OID_SECG_EC_SECP384R1
},
503 { "secp521r1", SEC_OID_SECG_EC_SECP521R1
},
504 { "nistp521", SEC_OID_SECG_EC_SECP521R1
},
506 { "prime192v1", SEC_OID_ANSIX962_EC_PRIME192V1
},
507 { "prime192v2", SEC_OID_ANSIX962_EC_PRIME192V2
},
508 { "prime192v3", SEC_OID_ANSIX962_EC_PRIME192V3
},
509 { "prime239v1", SEC_OID_ANSIX962_EC_PRIME239V1
},
510 { "prime239v2", SEC_OID_ANSIX962_EC_PRIME239V2
},
511 { "prime239v3", SEC_OID_ANSIX962_EC_PRIME239V3
},
513 { "c2pnb163v1", SEC_OID_ANSIX962_EC_C2PNB163V1
},
514 { "c2pnb163v2", SEC_OID_ANSIX962_EC_C2PNB163V2
},
515 { "c2pnb163v3", SEC_OID_ANSIX962_EC_C2PNB163V3
},
516 { "c2pnb176v1", SEC_OID_ANSIX962_EC_C2PNB176V1
},
517 { "c2tnb191v1", SEC_OID_ANSIX962_EC_C2TNB191V1
},
518 { "c2tnb191v2", SEC_OID_ANSIX962_EC_C2TNB191V2
},
519 { "c2tnb191v3", SEC_OID_ANSIX962_EC_C2TNB191V3
},
520 { "c2onb191v4", SEC_OID_ANSIX962_EC_C2ONB191V4
},
521 { "c2onb191v5", SEC_OID_ANSIX962_EC_C2ONB191V5
},
522 { "c2pnb208w1", SEC_OID_ANSIX962_EC_C2PNB208W1
},
523 { "c2tnb239v1", SEC_OID_ANSIX962_EC_C2TNB239V1
},
524 { "c2tnb239v2", SEC_OID_ANSIX962_EC_C2TNB239V2
},
525 { "c2tnb239v3", SEC_OID_ANSIX962_EC_C2TNB239V3
},
526 { "c2onb239v4", SEC_OID_ANSIX962_EC_C2ONB239V4
},
527 { "c2onb239v5", SEC_OID_ANSIX962_EC_C2ONB239V5
},
528 { "c2pnb272w1", SEC_OID_ANSIX962_EC_C2PNB272W1
},
529 { "c2pnb304w1", SEC_OID_ANSIX962_EC_C2PNB304W1
},
530 { "c2tnb359v1", SEC_OID_ANSIX962_EC_C2TNB359V1
},
531 { "c2pnb368w1", SEC_OID_ANSIX962_EC_C2PNB368W1
},
532 { "c2tnb431r1", SEC_OID_ANSIX962_EC_C2TNB431R1
},
534 { "secp112r1", SEC_OID_SECG_EC_SECP112R1
},
535 { "secp112r2", SEC_OID_SECG_EC_SECP112R2
},
536 { "secp128r1", SEC_OID_SECG_EC_SECP128R1
},
537 { "secp128r2", SEC_OID_SECG_EC_SECP128R2
},
539 { "sect113r1", SEC_OID_SECG_EC_SECT113R1
},
540 { "sect113r2", SEC_OID_SECG_EC_SECT113R2
},
541 { "sect131r1", SEC_OID_SECG_EC_SECT131R1
},
542 { "sect131r2", SEC_OID_SECG_EC_SECT131R2
},
545 static SECKEYECParams
*
546 getECParams(const char *curve
)
548 SECKEYECParams
*ecparams
;
549 SECOidData
*oidData
= NULL
;
550 SECOidTag curveOidTag
= SEC_OID_UNKNOWN
; /* default */
554 numCurves
= sizeof(nameTagPair
)/sizeof(CurveNameTagPair
);
555 for (i
= 0; ((i
< numCurves
) && (curveOidTag
== SEC_OID_UNKNOWN
));
557 if (PL_strcmp(curve
, nameTagPair
[i
].curveName
) == 0)
558 curveOidTag
= nameTagPair
[i
].curveOidTag
;
562 /* Return NULL if curve name is not recognized */
563 if ((curveOidTag
== SEC_OID_UNKNOWN
) ||
564 (oidData
= SECOID_FindOIDByTag(curveOidTag
)) == NULL
) {
565 fprintf(stderr
, "Unrecognized elliptic curve %s\n", curve
);
569 ecparams
= SECITEM_AllocItem(NULL
, NULL
, (2 + oidData
->oid
.len
));
572 * ecparams->data needs to contain the ASN encoding of an object ID (OID)
573 * representing the named curve. The actual OID is in
574 * oidData->oid.data so we simply prepend 0x06 and OID length
576 ecparams
->data
[0] = SEC_ASN1_OBJECT_ID
;
577 ecparams
->data
[1] = oidData
->oid
.len
;
578 memcpy(ecparams
->data
+ 2, oidData
->oid
.data
, oidData
->oid
.len
);
582 #endif /* NSS_ENABLE_ECC */
585 dump_pqg(PQGParams
*pqg
)
587 SECU_PrintInteger(stdout
, &pqg
->prime
, "PRIME:", 0);
588 SECU_PrintInteger(stdout
, &pqg
->subPrime
, "SUBPRIME:", 0);
589 SECU_PrintInteger(stdout
, &pqg
->base
, "BASE:", 0);
593 dump_dsakey(DSAPrivateKey
*key
)
595 dump_pqg(&key
->params
);
596 SECU_PrintInteger(stdout
, &key
->publicValue
, "PUBLIC VALUE:", 0);
597 SECU_PrintInteger(stdout
, &key
->privateValue
, "PRIVATE VALUE:", 0);
600 #ifdef NSS_ENABLE_ECC
602 dump_ecp(ECParams
*ecp
)
604 /* TODO other fields */
605 SECU_PrintInteger(stdout
, &ecp
->base
, "BASE POINT:", 0);
609 dump_eckey(ECPrivateKey
*key
)
611 dump_ecp(&key
->ecParams
);
612 SECU_PrintInteger(stdout
, &key
->publicValue
, "PUBLIC VALUE:", 0);
613 SECU_PrintInteger(stdout
, &key
->privateValue
, "PRIVATE VALUE:", 0);
618 dump_rsakey(RSAPrivateKey
*key
)
620 SECU_PrintInteger(stdout
, &key
->version
, "VERSION:", 0);
621 SECU_PrintInteger(stdout
, &key
->modulus
, "MODULUS:", 0);
622 SECU_PrintInteger(stdout
, &key
->publicExponent
, "PUBLIC EXP:", 0);
623 SECU_PrintInteger(stdout
, &key
->privateExponent
, "PRIVATE EXP:", 0);
624 SECU_PrintInteger(stdout
, &key
->prime1
, "CRT PRIME 1:", 0);
625 SECU_PrintInteger(stdout
, &key
->prime2
, "CRT PRIME 2:", 0);
626 SECU_PrintInteger(stdout
, &key
->exponent1
, "CRT EXP 1:", 0);
627 SECU_PrintInteger(stdout
, &key
->exponent2
, "CRT EXP 2:", 0);
628 SECU_PrintInteger(stdout
, &key
->coefficient
, "CRT COEFFICIENT:", 0);
632 bltestBase64Encoded
, /* Base64 encoded ASCII */
633 bltestBinary
, /* straight binary */
634 bltestHexSpaceDelim
, /* 0x12 0x34 0xab 0xCD ... */
635 bltestHexStream
/* 1234abCD ... */
646 typedef SECStatus (* bltestSymmCipherFn
)(void *cx
,
647 unsigned char *output
,
648 unsigned int *outputLen
,
649 unsigned int maxOutputLen
,
650 const unsigned char *input
,
651 unsigned int inputLen
);
653 typedef SECStatus (* bltestPubKeyCipherFn
)(void *key
,
655 const SECItem
*input
);
657 typedef SECStatus (* bltestHashCipherFn
)(unsigned char *dest
,
658 const unsigned char *src
,
663 bltestDES_ECB
, /* Symmetric Key Ciphers */
664 bltestDES_CBC
, /* . */
665 bltestDES_EDE_ECB
, /* . */
666 bltestDES_EDE_CBC
, /* . */
667 bltestRC2_ECB
, /* . */
668 bltestRC2_CBC
, /* . */
670 bltestRC5_ECB
, /* . */
671 bltestRC5_CBC
, /* . */
672 bltestAES_ECB
, /* . */
673 bltestAES_CBC
, /* . */
674 bltestCAMELLIA_ECB
, /* . */
675 bltestCAMELLIA_CBC
, /* . */
676 bltestRSA
, /* Public Key Ciphers */
677 #ifdef NSS_ENABLE_ECC
678 bltestECDSA
, /* . (Public Key Sig.) */
681 bltestMD2
, /* Hash algorithms */
684 bltestSHA256
, /* . */
685 bltestSHA384
, /* . */
686 bltestSHA512
, /* . */
690 static char *mode_strings
[] =
706 #ifdef NSS_ENABLE_ECC
723 } bltestSymmKeyParams
;
737 RSAPrivateKey
*rsakey
;
747 bltestIO sig
; /* if doing verify, have additional input */
749 DSAPrivateKey
*dsakey
;
752 #ifdef NSS_ENABLE_ECC
758 bltestIO sig
; /* if doing verify, have additional input */
765 bltestIO key
; /* unused */
772 bltestSymmKeyParams sk
;
776 #ifdef NSS_ENABLE_ECC
777 bltestECDSAParams ecdsa
;
779 bltestHashParams hash
;
782 typedef struct bltestCipherInfoStr bltestCipherInfo
;
784 struct bltestCipherInfoStr
{
786 /* link to next in multithreaded test */
787 bltestCipherInfo
*next
;
788 PRThread
*cipherThread
;
790 /* MonteCarlo test flag*/
797 /* Cipher-specific parameters */
800 bltestCipherMode mode
;
801 /* Cipher function (encrypt/decrypt/sign/verify/hash) */
803 bltestSymmCipherFn symmkeyCipher
;
804 bltestPubKeyCipherFn pubkeyCipher
;
805 bltestHashCipherFn hashCipher
;
807 /* performance testing */
808 int repetitionsToPerfom
;
817 is_symmkeyCipher(bltestCipherMode mode
)
819 /* change as needed! */
820 if (mode
>= bltestDES_ECB
&& mode
<= bltestCAMELLIA_CBC
)
826 is_pubkeyCipher(bltestCipherMode mode
)
828 /* change as needed! */
829 if (mode
>= bltestRSA
&& mode
<= bltestDSA
)
835 is_hashCipher(bltestCipherMode mode
)
837 /* change as needed! */
838 if (mode
>= bltestMD2
&& mode
<= bltestSHA512
)
844 is_sigCipher(bltestCipherMode mode
)
846 /* change as needed! */
847 #ifdef NSS_ENABLE_ECC
848 if (mode
>= bltestECDSA
&& mode
<= bltestDSA
)
850 if (mode
>= bltestDSA
&& mode
<= bltestDSA
)
857 cipher_requires_IV(bltestCipherMode mode
)
859 /* change as needed! */
860 if (mode
== bltestDES_CBC
|| mode
== bltestDES_EDE_CBC
||
861 mode
== bltestRC2_CBC
|| mode
== bltestRC5_CBC
||
862 mode
== bltestAES_CBC
|| mode
== bltestCAMELLIA_CBC
)
867 SECStatus
finishIO(bltestIO
*output
, PRFileDesc
*file
);
870 setupIO(PRArenaPool
*arena
, bltestIO
*input
, PRFileDesc
*file
,
871 char *str
, int numBytes
)
873 SECStatus rv
= SECSuccess
;
879 if (file
&& (numBytes
== 0 || file
== PR_STDIN
)) {
880 /* grabbing data from a file */
881 rv
= SECU_FileToItem(&fileData
, file
);
882 if (rv
!= SECSuccess
) {
888 /* grabbing data from command line */
890 fileData
.len
= PL_strlen(str
);
894 SECITEM_AllocItem(arena
, &input
->buf
, numBytes
);
895 RNG_GenerateGlobalRandomBytes(input
->buf
.data
, numBytes
);
896 return finishIO(input
, file
);
901 switch (input
->mode
) {
902 case bltestBase64Encoded
:
903 rv
= atob(in
, &input
->buf
, arena
);
906 if (in
->data
[in
->len
-1] == '\n') --in
->len
;
907 if (in
->data
[in
->len
-1] == '\r') --in
->len
;
908 SECITEM_CopyItem(arena
, &input
->buf
, in
);
910 case bltestHexSpaceDelim
:
911 SECITEM_AllocItem(arena
, &input
->buf
, in
->len
/5);
912 for (i
=0, j
=0; i
<in
->len
; i
+=5, j
++) {
914 if (tok
[0] != '0' || tok
[1] != 'x' || tok
[4] != ' ')
918 rv
= hex_from_2char(&tok
[2], input
->buf
.data
+ j
);
923 case bltestHexStream
:
924 SECITEM_AllocItem(arena
, &input
->buf
, in
->len
/2);
925 for (i
=0, j
=0; i
<in
->len
; i
+=2, j
++) {
927 rv
= hex_from_2char(tok
, input
->buf
.data
+ j
);
935 SECITEM_FreeItem(&fileData
, PR_FALSE
);
940 finishIO(bltestIO
*output
, PRFileDesc
*file
)
942 SECStatus rv
= SECSuccess
;
944 unsigned char byteval
;
948 if (output
->pBuf
.len
> 0) {
953 switch (output
->mode
) {
954 case bltestBase64Encoded
:
955 rv
= btoa_file(it
, file
);
958 nb
= PR_Write(file
, it
->data
, it
->len
);
959 rv
= (nb
== (PRInt32
)it
->len
) ? SECSuccess
: SECFailure
;
961 case bltestHexSpaceDelim
:
965 for (i
=0; i
<it
->len
; i
++) {
966 byteval
= it
->data
[i
];
967 rv
= char2_from_hex(byteval
, hexstr
+ 2);
968 nb
= PR_Write(file
, hexstr
, 5);
972 PR_Write(file
, "\n", 1);
974 case bltestHexStream
:
975 for (i
=0; i
<it
->len
; i
++) {
976 byteval
= it
->data
[i
];
977 rv
= char2_from_hex(byteval
, hexstr
);
980 nb
= PR_Write(file
, hexstr
, 2);
982 PR_Write(file
, "\n", 1);
989 bltestCopyIO(PRArenaPool
*arena
, bltestIO
*dest
, bltestIO
*src
)
991 SECITEM_CopyItem(arena
, &dest
->buf
, &src
->buf
);
992 if (src
->pBuf
.len
> 0) {
993 dest
->pBuf
.len
= src
->pBuf
.len
;
994 dest
->pBuf
.data
= dest
->buf
.data
+ (src
->pBuf
.data
- src
->buf
.data
);
996 dest
->mode
= src
->mode
;
997 dest
->file
= src
->file
;
1001 misalignBuffer(PRArenaPool
*arena
, bltestIO
*io
, int off
)
1003 ptrdiff_t offset
= (ptrdiff_t)io
->buf
.data
% WORDSIZE
;
1004 int length
= io
->buf
.len
;
1005 if (offset
!= off
) {
1006 SECITEM_ReallocItem(arena
, &io
->buf
, length
, length
+ 2*WORDSIZE
);
1007 io
->buf
.len
= length
+ 2*WORDSIZE
; /* why doesn't realloc do this? */
1008 /* offset may have changed? */
1009 offset
= (ptrdiff_t)io
->buf
.data
% WORDSIZE
;
1010 if (offset
!= off
) {
1011 memmove(io
->buf
.data
+ off
, io
->buf
.data
, length
);
1012 io
->pBuf
.data
= io
->buf
.data
+ off
;
1013 io
->pBuf
.len
= length
;
1015 io
->pBuf
.data
= io
->buf
.data
;
1016 io
->pBuf
.len
= length
;
1019 io
->pBuf
.data
= io
->buf
.data
;
1020 io
->pBuf
.len
= length
;
1025 des_Encrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1026 unsigned int maxOutputLen
, const unsigned char *input
,
1027 unsigned int inputLen
)
1029 return DES_Encrypt((DESContext
*)cx
, output
, outputLen
, maxOutputLen
,
1034 des_Decrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1035 unsigned int maxOutputLen
, const unsigned char *input
,
1036 unsigned int inputLen
)
1038 return DES_Decrypt((DESContext
*)cx
, output
, outputLen
, maxOutputLen
,
1043 rc2_Encrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1044 unsigned int maxOutputLen
, const unsigned char *input
,
1045 unsigned int inputLen
)
1047 return RC2_Encrypt((RC2Context
*)cx
, output
, outputLen
, maxOutputLen
,
1052 rc2_Decrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1053 unsigned int maxOutputLen
, const unsigned char *input
,
1054 unsigned int inputLen
)
1056 return RC2_Decrypt((RC2Context
*)cx
, output
, outputLen
, maxOutputLen
,
1061 rc4_Encrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1062 unsigned int maxOutputLen
, const unsigned char *input
,
1063 unsigned int inputLen
)
1065 return RC4_Encrypt((RC4Context
*)cx
, output
, outputLen
, maxOutputLen
,
1070 rc4_Decrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1071 unsigned int maxOutputLen
, const unsigned char *input
,
1072 unsigned int inputLen
)
1074 return RC4_Decrypt((RC4Context
*)cx
, output
, outputLen
, maxOutputLen
,
1079 aes_Encrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1080 unsigned int maxOutputLen
, const unsigned char *input
,
1081 unsigned int inputLen
)
1083 return AES_Encrypt((AESContext
*)cx
, output
, outputLen
, maxOutputLen
,
1088 aes_Decrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1089 unsigned int maxOutputLen
, const unsigned char *input
,
1090 unsigned int inputLen
)
1092 return AES_Decrypt((AESContext
*)cx
, output
, outputLen
, maxOutputLen
,
1097 camellia_Encrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1098 unsigned int maxOutputLen
, const unsigned char *input
,
1099 unsigned int inputLen
)
1101 return Camellia_Encrypt((CamelliaContext
*)cx
, output
, outputLen
,
1107 camellia_Decrypt(void *cx
, unsigned char *output
, unsigned int *outputLen
,
1108 unsigned int maxOutputLen
, const unsigned char *input
,
1109 unsigned int inputLen
)
1111 return Camellia_Decrypt((CamelliaContext
*)cx
, output
, outputLen
,
1117 rsa_PublicKeyOp(void *key
, SECItem
*output
, const SECItem
*input
)
1119 return RSA_PublicKeyOp((RSAPublicKey
*)key
, output
->data
, input
->data
);
1123 rsa_PrivateKeyOp(void *key
, SECItem
*output
, const SECItem
*input
)
1125 return RSA_PrivateKeyOp((RSAPrivateKey
*)key
, output
->data
, input
->data
);
1129 dsa_signDigest(void *key
, SECItem
*output
, const SECItem
*input
)
1131 return DSA_SignDigest((DSAPrivateKey
*)key
, output
, input
);
1135 dsa_verifyDigest(void *key
, SECItem
*output
, const SECItem
*input
)
1137 return DSA_VerifyDigest((DSAPublicKey
*)key
, output
, input
);
1140 #ifdef NSS_ENABLE_ECC
1142 ecdsa_signDigest(void *key
, SECItem
*output
, const SECItem
*input
)
1144 return ECDSA_SignDigest((ECPrivateKey
*)key
, output
, input
);
1148 ecdsa_verifyDigest(void *key
, SECItem
*output
, const SECItem
*input
)
1150 return ECDSA_VerifyDigest((ECPublicKey
*)key
, output
, input
);
1155 bltest_des_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1157 PRIntervalTime time1
, time2
;
1158 bltestSymmKeyParams
*desp
= &cipherInfo
->params
.sk
;
1161 switch (cipherInfo
->mode
) {
1162 case bltestDES_ECB
: minorMode
= NSS_DES
; break;
1163 case bltestDES_CBC
: minorMode
= NSS_DES_CBC
; break;
1164 case bltestDES_EDE_ECB
: minorMode
= NSS_DES_EDE3
; break;
1165 case bltestDES_EDE_CBC
: minorMode
= NSS_DES_EDE3_CBC
; break;
1169 cipherInfo
->cx
= (void*)DES_CreateContext(desp
->key
.buf
.data
,
1171 minorMode
, encrypt
);
1172 if (cipherInfo
->cxreps
> 0) {
1173 DESContext
**dummycx
;
1174 dummycx
= PORT_Alloc(cipherInfo
->cxreps
* sizeof(DESContext
*));
1176 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1177 dummycx
[i
] = (void*)DES_CreateContext(desp
->key
.buf
.data
,
1179 minorMode
, encrypt
);
1181 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1182 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1183 DES_DestroyContext(dummycx
[i
], PR_TRUE
);
1188 cipherInfo
->cipher
.symmkeyCipher
= des_Encrypt
;
1190 cipherInfo
->cipher
.symmkeyCipher
= des_Decrypt
;
1195 bltest_rc2_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1197 PRIntervalTime time1
, time2
;
1198 bltestSymmKeyParams
*rc2p
= &cipherInfo
->params
.sk
;
1201 switch (cipherInfo
->mode
) {
1202 case bltestRC2_ECB
: minorMode
= NSS_RC2
; break;
1203 case bltestRC2_CBC
: minorMode
= NSS_RC2_CBC
; break;
1207 cipherInfo
->cx
= (void*)RC2_CreateContext(rc2p
->key
.buf
.data
,
1212 if (cipherInfo
->cxreps
> 0) {
1213 RC2Context
**dummycx
;
1214 dummycx
= PORT_Alloc(cipherInfo
->cxreps
* sizeof(RC2Context
*));
1216 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1217 dummycx
[i
] = (void*)RC2_CreateContext(rc2p
->key
.buf
.data
,
1223 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1224 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1225 RC2_DestroyContext(dummycx
[i
], PR_TRUE
);
1230 cipherInfo
->cipher
.symmkeyCipher
= rc2_Encrypt
;
1232 cipherInfo
->cipher
.symmkeyCipher
= rc2_Decrypt
;
1237 bltest_rc4_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1239 PRIntervalTime time1
, time2
;
1241 bltestSymmKeyParams
*rc4p
= &cipherInfo
->params
.sk
;
1242 cipherInfo
->cx
= (void*)RC4_CreateContext(rc4p
->key
.buf
.data
,
1244 if (cipherInfo
->cxreps
> 0) {
1245 RC4Context
**dummycx
;
1246 dummycx
= PORT_Alloc(cipherInfo
->cxreps
* sizeof(RC4Context
*));
1248 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1249 dummycx
[i
] = (void*)RC4_CreateContext(rc4p
->key
.buf
.data
,
1252 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1253 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1254 RC4_DestroyContext(dummycx
[i
], PR_TRUE
);
1259 cipherInfo
->cipher
.symmkeyCipher
= rc4_Encrypt
;
1261 cipherInfo
->cipher
.symmkeyCipher
= rc4_Decrypt
;
1266 bltest_rc5_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1268 #if NSS_SOFTOKEN_DOES_RC5
1269 PRIntervalTime time1
, time2
;
1270 bltestRC5Params
*rc5p
= &cipherInfo
->params
.rc5
;
1272 switch (cipherInfo
->mode
) {
1273 case bltestRC5_ECB
: minorMode
= NSS_RC5
; break;
1274 case bltestRC5_CBC
: minorMode
= NSS_RC5_CBC
; break;
1279 cipherInfo
->cx
= (void*)RC5_CreateContext(&rc5p
->key
.buf
,
1280 rc5p
->rounds
, rc5p
->wordsize
,
1281 rc5p
->iv
.buf
.data
, minorMode
);
1282 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1284 cipherInfo
->cipher
.symmkeyCipher
= RC5_Encrypt
;
1286 cipherInfo
->cipher
.symmkeyCipher
= RC5_Decrypt
;
1294 bltest_aes_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1296 bltestSymmKeyParams
*aesp
= &cipherInfo
->params
.sk
;
1299 int keylen
= aesp
->key
.buf
.len
;
1300 int blocklen
= AES_BLOCK_SIZE
;
1301 PRIntervalTime time1
, time2
;
1303 switch (cipherInfo
->mode
) {
1304 case bltestAES_ECB
: minorMode
= NSS_AES
; break;
1305 case bltestAES_CBC
: minorMode
= NSS_AES_CBC
; break;
1309 cipherInfo
->cx
= (void*)AES_CreateContext(aesp
->key
.buf
.data
,
1313 if (cipherInfo
->cxreps
> 0) {
1314 AESContext
**dummycx
;
1315 dummycx
= PORT_Alloc(cipherInfo
->cxreps
* sizeof(AESContext
*));
1317 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1318 dummycx
[i
] = (void*)AES_CreateContext(aesp
->key
.buf
.data
,
1323 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1324 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1325 AES_DestroyContext(dummycx
[i
], PR_TRUE
);
1330 cipherInfo
->cipher
.symmkeyCipher
= aes_Encrypt
;
1332 cipherInfo
->cipher
.symmkeyCipher
= aes_Decrypt
;
1337 bltest_camellia_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1339 bltestSymmKeyParams
*camelliap
= &cipherInfo
->params
.sk
;
1342 int keylen
= camelliap
->key
.buf
.len
;
1343 int blocklen
= CAMELLIA_BLOCK_SIZE
;
1344 PRIntervalTime time1
, time2
;
1346 switch (cipherInfo
->mode
) {
1347 case bltestCAMELLIA_ECB
: minorMode
= NSS_CAMELLIA
; break;
1348 case bltestCAMELLIA_CBC
: minorMode
= NSS_CAMELLIA_CBC
; break;
1352 cipherInfo
->cx
= (void*)Camellia_CreateContext(camelliap
->key
.buf
.data
,
1353 camelliap
->iv
.buf
.data
,
1356 if (cipherInfo
->cxreps
> 0) {
1357 CamelliaContext
**dummycx
;
1358 dummycx
= PORT_Alloc(cipherInfo
->cxreps
* sizeof(CamelliaContext
*));
1360 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1361 dummycx
[i
] = (void*)Camellia_CreateContext(camelliap
->key
.buf
.data
,
1362 camelliap
->iv
.buf
.data
,
1366 TIMEFINISH(cipherInfo
->cxtime
, 1.0);
1367 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1368 Camellia_DestroyContext(dummycx
[i
], PR_TRUE
);
1373 cipherInfo
->cipher
.symmkeyCipher
= camellia_Encrypt
;
1375 cipherInfo
->cipher
.symmkeyCipher
= camellia_Decrypt
;
1380 bltest_rsa_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1383 RSAPrivateKey
**dummyKey
;
1384 PRIntervalTime time1
, time2
;
1385 bltestRSAParams
*rsap
= &cipherInfo
->params
.rsa
;
1386 /* RSA key gen was done during parameter setup */
1387 cipherInfo
->cx
= cipherInfo
->params
.rsa
.rsakey
;
1388 /* For performance testing */
1389 if (cipherInfo
->cxreps
> 0) {
1390 /* Create space for n private key objects */
1391 dummyKey
= (RSAPrivateKey
**)PORT_Alloc(cipherInfo
->cxreps
*
1392 sizeof(RSAPrivateKey
*));
1393 /* Time n keygens, storing in the array */
1395 for (i
=0; i
<cipherInfo
->cxreps
; i
++)
1396 dummyKey
[i
] = RSA_NewKey(rsap
->keysizeInBits
,
1397 &rsap
->rsakey
->publicExponent
);
1398 TIMEFINISH(cipherInfo
->cxtime
, cipherInfo
->cxreps
);
1399 /* Free the n key objects */
1400 for (i
=0; i
<cipherInfo
->cxreps
; i
++)
1401 PORT_FreeArena(dummyKey
[i
]->arena
, PR_TRUE
);
1402 PORT_Free(dummyKey
);
1405 /* Have to convert private key to public key. Memory
1406 * is freed with private key's arena */
1407 RSAPublicKey
*pubkey
;
1408 RSAPrivateKey
*key
= (RSAPrivateKey
*)cipherInfo
->cx
;
1409 pubkey
= (RSAPublicKey
*)PORT_ArenaAlloc(key
->arena
,
1410 sizeof(RSAPublicKey
));
1411 pubkey
->modulus
.len
= key
->modulus
.len
;
1412 pubkey
->modulus
.data
= key
->modulus
.data
;
1413 pubkey
->publicExponent
.len
= key
->publicExponent
.len
;
1414 pubkey
->publicExponent
.data
= key
->publicExponent
.data
;
1415 cipherInfo
->cx
= (void *)pubkey
;
1416 cipherInfo
->cipher
.pubkeyCipher
= rsa_PublicKeyOp
;
1418 cipherInfo
->cipher
.pubkeyCipher
= rsa_PrivateKeyOp
;
1424 bltest_pqg_init(bltestDSAParams
*dsap
)
1427 PQGVerify
*vfy
= NULL
;
1428 rv
= PQG_ParamGen(dsap
->j
, &dsap
->pqg
, &vfy
);
1429 CHECKERROR(rv
, __LINE__
);
1430 rv
= PQG_VerifyParams(dsap
->pqg
, vfy
, &res
);
1431 CHECKERROR(res
, __LINE__
);
1432 CHECKERROR(rv
, __LINE__
);
1437 bltest_dsa_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1440 DSAPrivateKey
**dummyKey
;
1441 PQGParams
*dummypqg
;
1442 PRIntervalTime time1
, time2
;
1443 bltestDSAParams
*dsap
= &cipherInfo
->params
.dsa
;
1444 PQGVerify
*ignore
= NULL
;
1445 /* DSA key gen was done during parameter setup */
1446 cipherInfo
->cx
= cipherInfo
->params
.dsa
.dsakey
;
1447 /* For performance testing */
1448 if (cipherInfo
->cxreps
> 0) {
1449 /* Create space for n private key objects */
1450 dummyKey
= (DSAPrivateKey
**)PORT_ZAlloc(cipherInfo
->cxreps
*
1451 sizeof(DSAPrivateKey
*));
1452 /* Time n keygens, storing in the array */
1454 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1456 PQG_ParamGen(dsap
->j
, &dummypqg
, &ignore
);
1457 DSA_NewKey(dummypqg
, &dummyKey
[i
]);
1459 TIMEFINISH(cipherInfo
->cxtime
, cipherInfo
->cxreps
);
1460 /* Free the n key objects */
1461 for (i
=0; i
<cipherInfo
->cxreps
; i
++)
1462 PORT_FreeArena(dummyKey
[i
]->params
.arena
, PR_TRUE
);
1463 PORT_Free(dummyKey
);
1465 if (!dsap
->pqg
&& dsap
->pqgdata
.buf
.len
> 0) {
1466 dsap
->pqg
= pqg_from_filedata(&dsap
->pqgdata
.buf
);
1468 if (!cipherInfo
->cx
&& dsap
->key
.buf
.len
> 0) {
1469 cipherInfo
->cx
= dsakey_from_filedata(&dsap
->key
.buf
);
1472 cipherInfo
->cipher
.pubkeyCipher
= dsa_signDigest
;
1474 /* Have to convert private key to public key. Memory
1475 * is freed with private key's arena */
1476 DSAPublicKey
*pubkey
;
1477 DSAPrivateKey
*key
= (DSAPrivateKey
*)cipherInfo
->cx
;
1478 pubkey
= (DSAPublicKey
*)PORT_ArenaZAlloc(key
->params
.arena
,
1479 sizeof(DSAPublicKey
));
1480 pubkey
->params
.prime
.len
= key
->params
.prime
.len
;
1481 pubkey
->params
.prime
.data
= key
->params
.prime
.data
;
1482 pubkey
->params
.subPrime
.len
= key
->params
.subPrime
.len
;
1483 pubkey
->params
.subPrime
.data
= key
->params
.subPrime
.data
;
1484 pubkey
->params
.base
.len
= key
->params
.base
.len
;
1485 pubkey
->params
.base
.data
= key
->params
.base
.data
;
1486 pubkey
->publicValue
.len
= key
->publicValue
.len
;
1487 pubkey
->publicValue
.data
= key
->publicValue
.data
;
1488 cipherInfo
->cipher
.pubkeyCipher
= dsa_verifyDigest
;
1493 #ifdef NSS_ENABLE_ECC
1495 bltest_ecdsa_init(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1498 ECPrivateKey
**dummyKey
;
1499 PRIntervalTime time1
, time2
;
1500 bltestECDSAParams
*ecdsap
= &cipherInfo
->params
.ecdsa
;
1501 /* ECDSA key gen was done during parameter setup */
1502 cipherInfo
->cx
= cipherInfo
->params
.ecdsa
.eckey
;
1503 /* For performance testing */
1504 if (cipherInfo
->cxreps
> 0) {
1505 /* Create space for n private key objects */
1506 dummyKey
= (ECPrivateKey
**)PORT_ZAlloc(cipherInfo
->cxreps
*
1507 sizeof(ECPrivateKey
*));
1508 /* Time n keygens, storing in the array */
1510 for (i
=0; i
<cipherInfo
->cxreps
; i
++) {
1511 EC_NewKey(&ecdsap
->eckey
->ecParams
, &dummyKey
[i
]);
1513 TIMEFINISH(cipherInfo
->cxtime
, cipherInfo
->cxreps
);
1514 /* Free the n key objects */
1515 for (i
=0; i
<cipherInfo
->cxreps
; i
++)
1516 PORT_FreeArena(dummyKey
[i
]->ecParams
.arena
, PR_TRUE
);
1517 PORT_Free(dummyKey
);
1519 if (!cipherInfo
->cx
&& ecdsap
->key
.buf
.len
> 0) {
1520 cipherInfo
->cx
= eckey_from_filedata(&ecdsap
->key
.buf
);
1523 cipherInfo
->cipher
.pubkeyCipher
= ecdsa_signDigest
;
1525 /* Have to convert private key to public key. Memory
1526 * is freed with private key's arena */
1527 ECPublicKey
*pubkey
;
1528 ECPrivateKey
*key
= (ECPrivateKey
*)cipherInfo
->cx
;
1529 pubkey
= (ECPublicKey
*)PORT_ArenaZAlloc(key
->ecParams
.arena
,
1530 sizeof(ECPublicKey
));
1531 pubkey
->ecParams
.type
= key
->ecParams
.type
;
1532 pubkey
->ecParams
.fieldID
.size
= key
->ecParams
.fieldID
.size
;
1533 pubkey
->ecParams
.fieldID
.type
= key
->ecParams
.fieldID
.type
;
1534 pubkey
->ecParams
.fieldID
.u
.prime
.len
= key
->ecParams
.fieldID
.u
.prime
.len
;
1535 pubkey
->ecParams
.fieldID
.u
.prime
.data
= key
->ecParams
.fieldID
.u
.prime
.data
;
1536 pubkey
->ecParams
.fieldID
.k1
= key
->ecParams
.fieldID
.k1
;
1537 pubkey
->ecParams
.fieldID
.k2
= key
->ecParams
.fieldID
.k2
;
1538 pubkey
->ecParams
.fieldID
.k3
= key
->ecParams
.fieldID
.k3
;
1539 pubkey
->ecParams
.curve
.a
.len
= key
->ecParams
.curve
.a
.len
;
1540 pubkey
->ecParams
.curve
.a
.data
= key
->ecParams
.curve
.a
.data
;
1541 pubkey
->ecParams
.curve
.b
.len
= key
->ecParams
.curve
.b
.len
;
1542 pubkey
->ecParams
.curve
.b
.data
= key
->ecParams
.curve
.b
.data
;
1543 pubkey
->ecParams
.curve
.seed
.len
= key
->ecParams
.curve
.seed
.len
;
1544 pubkey
->ecParams
.curve
.seed
.data
= key
->ecParams
.curve
.seed
.data
;
1545 pubkey
->ecParams
.base
.len
= key
->ecParams
.base
.len
;
1546 pubkey
->ecParams
.base
.data
= key
->ecParams
.base
.data
;
1547 pubkey
->ecParams
.order
.len
= key
->ecParams
.order
.len
;
1548 pubkey
->ecParams
.order
.data
= key
->ecParams
.order
.data
;
1549 pubkey
->ecParams
.cofactor
= key
->ecParams
.cofactor
;
1550 pubkey
->ecParams
.DEREncoding
.len
= key
->ecParams
.DEREncoding
.len
;
1551 pubkey
->ecParams
.DEREncoding
.data
= key
->ecParams
.DEREncoding
.data
;
1552 pubkey
->ecParams
.name
= key
->ecParams
.name
;
1553 pubkey
->publicValue
.len
= key
->publicValue
.len
;
1554 pubkey
->publicValue
.data
= key
->publicValue
.data
;
1555 cipherInfo
->cipher
.pubkeyCipher
= ecdsa_verifyDigest
;
1561 /* XXX unfortunately, this is not defined in blapi.h */
1563 md2_HashBuf(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1566 MD2Context
*cx
= MD2_NewContext();
1567 if (cx
== NULL
) return SECFailure
;
1569 MD2_Update(cx
, src
, src_length
);
1570 MD2_End(cx
, dest
, &len
, MD2_LENGTH
);
1571 MD2_DestroyContext(cx
, PR_TRUE
);
1576 md2_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1578 MD2Context
*cx
, *cx_cpy
;
1579 unsigned char *cxbytes
;
1581 unsigned int i
, quarter
;
1582 SECStatus rv
= SECSuccess
;
1583 cx
= MD2_NewContext();
1585 /* divide message by 4, restarting 3 times */
1586 quarter
= (src_length
+ 3)/ 4;
1587 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1588 MD2_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1589 len
= MD2_FlattenSize(cx
);
1590 cxbytes
= PORT_Alloc(len
);
1591 MD2_Flatten(cx
, cxbytes
);
1592 cx_cpy
= MD2_Resurrect(cxbytes
, NULL
);
1594 PR_fprintf(PR_STDERR
, "%s: MD2_Resurrect failed!\n", progName
);
1597 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1599 MD2_DestroyContext(cx_cpy
, PR_TRUE
);
1600 PR_fprintf(PR_STDERR
, "%s: MD2_restart failed!\n", progName
);
1603 MD2_DestroyContext(cx_cpy
, PR_TRUE
);
1605 src_length
-= quarter
;
1607 MD2_End(cx
, dest
, &len
, MD2_LENGTH
);
1609 MD2_DestroyContext(cx
, PR_TRUE
);
1614 md5_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1616 SECStatus rv
= SECSuccess
;
1617 MD5Context
*cx
, *cx_cpy
;
1618 unsigned char *cxbytes
;
1620 unsigned int i
, quarter
;
1621 cx
= MD5_NewContext();
1623 /* divide message by 4, restarting 3 times */
1624 quarter
= (src_length
+ 3)/ 4;
1625 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1626 MD5_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1627 len
= MD5_FlattenSize(cx
);
1628 cxbytes
= PORT_Alloc(len
);
1629 MD5_Flatten(cx
, cxbytes
);
1630 cx_cpy
= MD5_Resurrect(cxbytes
, NULL
);
1632 PR_fprintf(PR_STDERR
, "%s: MD5_Resurrect failed!\n", progName
);
1636 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1638 MD5_DestroyContext(cx_cpy
, PR_TRUE
);
1639 PR_fprintf(PR_STDERR
, "%s: MD5_restart failed!\n", progName
);
1642 MD5_DestroyContext(cx_cpy
, PR_TRUE
);
1644 src_length
-= quarter
;
1646 MD5_End(cx
, dest
, &len
, MD5_LENGTH
);
1648 MD5_DestroyContext(cx
, PR_TRUE
);
1653 sha1_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1655 SECStatus rv
= SECSuccess
;
1656 SHA1Context
*cx
, *cx_cpy
;
1657 unsigned char *cxbytes
;
1659 unsigned int i
, quarter
;
1660 cx
= SHA1_NewContext();
1662 /* divide message by 4, restarting 3 times */
1663 quarter
= (src_length
+ 3)/ 4;
1664 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1665 SHA1_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1666 len
= SHA1_FlattenSize(cx
);
1667 cxbytes
= PORT_Alloc(len
);
1668 SHA1_Flatten(cx
, cxbytes
);
1669 cx_cpy
= SHA1_Resurrect(cxbytes
, NULL
);
1671 PR_fprintf(PR_STDERR
, "%s: SHA1_Resurrect failed!\n", progName
);
1675 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1677 SHA1_DestroyContext(cx_cpy
, PR_TRUE
);
1678 PR_fprintf(PR_STDERR
, "%s: SHA1_restart failed!\n", progName
);
1681 SHA1_DestroyContext(cx_cpy
, PR_TRUE
);
1683 src_length
-= quarter
;
1685 SHA1_End(cx
, dest
, &len
, MD5_LENGTH
);
1687 SHA1_DestroyContext(cx
, PR_TRUE
);
1692 SHA256_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1694 SECStatus rv
= SECSuccess
;
1695 SHA256Context
*cx
, *cx_cpy
;
1696 unsigned char *cxbytes
;
1698 unsigned int i
, quarter
;
1699 cx
= SHA256_NewContext();
1701 /* divide message by 4, restarting 3 times */
1702 quarter
= (src_length
+ 3)/ 4;
1703 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1704 SHA256_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1705 len
= SHA256_FlattenSize(cx
);
1706 cxbytes
= PORT_Alloc(len
);
1707 SHA256_Flatten(cx
, cxbytes
);
1708 cx_cpy
= SHA256_Resurrect(cxbytes
, NULL
);
1710 PR_fprintf(PR_STDERR
, "%s: SHA256_Resurrect failed!\n", progName
);
1714 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1716 SHA256_DestroyContext(cx_cpy
, PR_TRUE
);
1717 PR_fprintf(PR_STDERR
, "%s: SHA256_restart failed!\n", progName
);
1720 SHA256_DestroyContext(cx_cpy
, PR_TRUE
);
1722 src_length
-= quarter
;
1724 SHA256_End(cx
, dest
, &len
, MD5_LENGTH
);
1726 SHA256_DestroyContext(cx
, PR_TRUE
);
1731 SHA384_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1733 SECStatus rv
= SECSuccess
;
1734 SHA384Context
*cx
, *cx_cpy
;
1735 unsigned char *cxbytes
;
1737 unsigned int i
, quarter
;
1738 cx
= SHA384_NewContext();
1740 /* divide message by 4, restarting 3 times */
1741 quarter
= (src_length
+ 3)/ 4;
1742 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1743 SHA384_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1744 len
= SHA384_FlattenSize(cx
);
1745 cxbytes
= PORT_Alloc(len
);
1746 SHA384_Flatten(cx
, cxbytes
);
1747 cx_cpy
= SHA384_Resurrect(cxbytes
, NULL
);
1749 PR_fprintf(PR_STDERR
, "%s: SHA384_Resurrect failed!\n", progName
);
1753 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1755 SHA384_DestroyContext(cx_cpy
, PR_TRUE
);
1756 PR_fprintf(PR_STDERR
, "%s: SHA384_restart failed!\n", progName
);
1759 SHA384_DestroyContext(cx_cpy
, PR_TRUE
);
1761 src_length
-= quarter
;
1763 SHA384_End(cx
, dest
, &len
, MD5_LENGTH
);
1765 SHA384_DestroyContext(cx
, PR_TRUE
);
1770 SHA512_restart(unsigned char *dest
, const unsigned char *src
, uint32 src_length
)
1772 SECStatus rv
= SECSuccess
;
1773 SHA512Context
*cx
, *cx_cpy
;
1774 unsigned char *cxbytes
;
1776 unsigned int i
, quarter
;
1777 cx
= SHA512_NewContext();
1779 /* divide message by 4, restarting 3 times */
1780 quarter
= (src_length
+ 3)/ 4;
1781 for (i
=0; i
< 4 && src_length
> 0; i
++) {
1782 SHA512_Update(cx
, src
+ i
*quarter
, PR_MIN(quarter
, src_length
));
1783 len
= SHA512_FlattenSize(cx
);
1784 cxbytes
= PORT_Alloc(len
);
1785 SHA512_Flatten(cx
, cxbytes
);
1786 cx_cpy
= SHA512_Resurrect(cxbytes
, NULL
);
1788 PR_fprintf(PR_STDERR
, "%s: SHA512_Resurrect failed!\n", progName
);
1792 rv
= PORT_Memcmp(cx
, cx_cpy
, len
);
1794 SHA512_DestroyContext(cx_cpy
, PR_TRUE
);
1795 PR_fprintf(PR_STDERR
, "%s: SHA512_restart failed!\n", progName
);
1798 SHA512_DestroyContext(cx_cpy
, PR_TRUE
);
1800 src_length
-= quarter
;
1802 SHA512_End(cx
, dest
, &len
, MD5_LENGTH
);
1804 SHA512_DestroyContext(cx
, PR_TRUE
);
1809 pubkeyInitKey(bltestCipherInfo
*cipherInfo
, PRFileDesc
*file
,
1810 #ifdef NSS_ENABLE_ECC
1811 int keysize
, int exponent
, char *curveName
)
1813 int keysize
, int exponent
)
1817 SECStatus rv
= SECSuccess
;
1818 bltestRSAParams
*rsap
;
1819 bltestDSAParams
*dsap
;
1820 #ifdef NSS_ENABLE_ECC
1821 bltestECDSAParams
*ecdsap
;
1822 SECItem
*tmpECParamsDER
;
1823 ECParams
*tmpECParams
= NULL
;
1824 SECItem ecSerialize
[3];
1826 switch (cipherInfo
->mode
) {
1828 rsap
= &cipherInfo
->params
.rsa
;
1830 SECItem expitem
= { 0, 0, 0 };
1831 SECITEM_AllocItem(cipherInfo
->arena
, &expitem
, sizeof(int));
1832 for (i
= 1; i
<= sizeof(int); i
++)
1833 expitem
.data
[i
-1] = exponent
>> (8*(sizeof(int) - i
));
1834 rsap
->rsakey
= RSA_NewKey(keysize
* 8, &expitem
);
1835 serialize_key(&rsap
->rsakey
->version
, 9, file
);
1836 rsap
->keysizeInBits
= keysize
* 8;
1838 setupIO(cipherInfo
->arena
, &cipherInfo
->params
.key
, file
, NULL
, 0);
1839 rsap
->rsakey
= rsakey_from_filedata(&cipherInfo
->params
.key
.buf
);
1840 rsap
->keysizeInBits
= rsap
->rsakey
->modulus
.len
* 8;
1844 dsap
= &cipherInfo
->params
.dsa
;
1846 dsap
->j
= PQG_PBITS_TO_INDEX(8*keysize
);
1848 bltest_pqg_init(dsap
);
1849 rv
= DSA_NewKey(dsap
->pqg
, &dsap
->dsakey
);
1850 CHECKERROR(rv
, __LINE__
);
1851 serialize_key(&dsap
->dsakey
->params
.prime
, 5, file
);
1853 setupIO(cipherInfo
->arena
, &cipherInfo
->params
.key
, file
, NULL
, 0);
1854 dsap
->dsakey
= dsakey_from_filedata(&cipherInfo
->params
.key
.buf
);
1855 dsap
->j
= PQG_PBITS_TO_INDEX(8*dsap
->dsakey
->params
.prime
.len
);
1858 #ifdef NSS_ENABLE_ECC
1860 ecdsap
= &cipherInfo
->params
.ecdsa
;
1861 if (curveName
!= NULL
) {
1862 tmpECParamsDER
= getECParams(curveName
);
1864 CHECKERROR(rv
, __LINE__
);
1865 rv
= EC_DecodeParams(tmpECParamsDER
, &tmpECParams
) == SECFailure
;
1866 CHECKERROR(rv
, __LINE__
);
1867 rv
= EC_NewKey(tmpECParams
, &ecdsap
->eckey
);
1868 CHECKERROR(rv
, __LINE__
);
1869 ecSerialize
[0].type
= tmpECParamsDER
->type
;
1870 ecSerialize
[0].data
= tmpECParamsDER
->data
;
1871 ecSerialize
[0].len
= tmpECParamsDER
->len
;
1872 ecSerialize
[1].type
= ecdsap
->eckey
->publicValue
.type
;
1873 ecSerialize
[1].data
= ecdsap
->eckey
->publicValue
.data
;
1874 ecSerialize
[1].len
= ecdsap
->eckey
->publicValue
.len
;
1875 ecSerialize
[2].type
= ecdsap
->eckey
->privateValue
.type
;
1876 ecSerialize
[2].data
= ecdsap
->eckey
->privateValue
.data
;
1877 ecSerialize
[2].len
= ecdsap
->eckey
->privateValue
.len
;
1878 serialize_key(&(ecSerialize
[0]), 3, file
);
1879 SECITEM_FreeItem(tmpECParamsDER
, PR_TRUE
);
1880 PORT_FreeArena(tmpECParams
->arena
, PR_TRUE
);
1881 rv
= SECOID_Shutdown();
1882 CHECKERROR(rv
, __LINE__
);
1884 setupIO(cipherInfo
->arena
, &cipherInfo
->params
.key
, file
, NULL
, 0);
1885 ecdsap
->eckey
= eckey_from_filedata(&cipherInfo
->params
.key
.buf
);
1896 cipherInit(bltestCipherInfo
*cipherInfo
, PRBool encrypt
)
1899 switch (cipherInfo
->mode
) {
1902 case bltestDES_EDE_ECB
:
1903 case bltestDES_EDE_CBC
:
1904 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1905 cipherInfo
->input
.pBuf
.len
);
1906 return bltest_des_init(cipherInfo
, encrypt
);
1910 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1911 cipherInfo
->input
.pBuf
.len
);
1912 return bltest_rc2_init(cipherInfo
, encrypt
);
1915 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1916 cipherInfo
->input
.pBuf
.len
);
1917 return bltest_rc4_init(cipherInfo
, encrypt
);
1921 #if NSS_SOFTOKEN_DOES_RC5
1922 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1923 cipherInfo
->input
.pBuf
.len
);
1925 return bltest_rc5_init(cipherInfo
, encrypt
);
1929 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1930 cipherInfo
->input
.pBuf
.len
);
1931 return bltest_aes_init(cipherInfo
, encrypt
);
1933 case bltestCAMELLIA_ECB
:
1934 case bltestCAMELLIA_CBC
:
1935 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1936 cipherInfo
->input
.pBuf
.len
);
1937 return bltest_camellia_init(cipherInfo
, encrypt
);
1940 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1941 cipherInfo
->input
.pBuf
.len
);
1942 return bltest_rsa_init(cipherInfo
, encrypt
);
1945 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1947 return bltest_dsa_init(cipherInfo
, encrypt
);
1949 #ifdef NSS_ENABLE_ECC
1951 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1953 return bltest_ecdsa_init(cipherInfo
, encrypt
);
1957 restart
= cipherInfo
->params
.hash
.restart
;
1958 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1960 cipherInfo
->cipher
.hashCipher
= (restart
) ? md2_restart
: md2_HashBuf
;
1964 restart
= cipherInfo
->params
.hash
.restart
;
1965 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1967 cipherInfo
->cipher
.hashCipher
= (restart
) ? md5_restart
: MD5_HashBuf
;
1971 restart
= cipherInfo
->params
.hash
.restart
;
1972 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1974 cipherInfo
->cipher
.hashCipher
= (restart
) ? sha1_restart
: SHA1_HashBuf
;
1978 restart
= cipherInfo
->params
.hash
.restart
;
1979 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1981 cipherInfo
->cipher
.hashCipher
= (restart
) ? SHA256_restart
1986 restart
= cipherInfo
->params
.hash
.restart
;
1987 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1989 cipherInfo
->cipher
.hashCipher
= (restart
) ? SHA384_restart
1994 restart
= cipherInfo
->params
.hash
.restart
;
1995 SECITEM_AllocItem(cipherInfo
->arena
, &cipherInfo
->output
.buf
,
1997 cipherInfo
->cipher
.hashCipher
= (restart
) ? SHA512_restart
2008 dsaOp(bltestCipherInfo
*cipherInfo
)
2010 PRIntervalTime time1
, time2
;
2011 SECStatus rv
= SECSuccess
;
2013 int maxLen
= cipherInfo
->output
.pBuf
.len
;
2014 SECItem dummyOut
= { 0, 0, 0 };
2015 SECITEM_AllocItem(NULL
, &dummyOut
, maxLen
);
2016 if (cipherInfo
->cipher
.pubkeyCipher
== dsa_signDigest
) {
2017 if (cipherInfo
->params
.dsa
.sigseed
.buf
.len
> 0) {
2018 bltestDSAParams
*dsa
= &cipherInfo
->params
.dsa
;
2019 DSAPrivateKey
*key
= (DSAPrivateKey
*)cipherInfo
->cx
;
2022 rv
= DSA_SignDigestWithSeed(key
,
2023 &cipherInfo
->output
.pBuf
,
2024 &cipherInfo
->input
.pBuf
,
2025 dsa
->sigseed
.buf
.data
);
2026 TIMEFINISH(cipherInfo
->optime
, 1.0);
2027 CHECKERROR(rv
, __LINE__
);
2028 cipherInfo
->repetitions
= 0;
2029 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2031 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2032 i
++, cipherInfo
->repetitions
++) {
2033 rv
= DSA_SignDigestWithSeed(key
, &dummyOut
,
2034 &cipherInfo
->input
.pBuf
,
2035 dsa
->sigseed
.buf
.data
);
2036 CHECKERROR(rv
, __LINE__
);
2039 int opsBetweenChecks
= 0;
2040 TIMEMARK(cipherInfo
->seconds
);
2041 while (! (TIMETOFINISH())) {
2043 for (;j
< opsBetweenChecks
;j
++) {
2044 rv
= DSA_SignDigestWithSeed(key
, &dummyOut
,
2045 &cipherInfo
->input
.pBuf
,
2046 dsa
->sigseed
.buf
.data
);
2047 CHECKERROR(rv
, __LINE__
);
2049 cipherInfo
->repetitions
+= j
;
2052 TIMEFINISH(cipherInfo
->optime
, 1.0);
2055 rv
= DSA_SignDigest((DSAPrivateKey
*)cipherInfo
->cx
,
2056 &cipherInfo
->output
.pBuf
,
2057 &cipherInfo
->input
.pBuf
);
2058 TIMEFINISH(cipherInfo
->optime
, 1.0);
2059 CHECKERROR(rv
, __LINE__
);
2060 cipherInfo
->repetitions
= 0;
2061 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2063 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2064 i
++, cipherInfo
->repetitions
++) {
2065 rv
= DSA_SignDigest((DSAPrivateKey
*)cipherInfo
->cx
,
2067 &cipherInfo
->input
.pBuf
);
2068 CHECKERROR(rv
, __LINE__
);
2071 int opsBetweenChecks
= 0;
2072 TIMEMARK(cipherInfo
->seconds
);
2073 while (! (TIMETOFINISH())) {
2075 for (;j
< opsBetweenChecks
;j
++) {
2076 rv
= DSA_SignDigest((DSAPrivateKey
*)cipherInfo
->cx
,
2078 &cipherInfo
->input
.pBuf
);
2079 CHECKERROR(rv
, __LINE__
);
2081 cipherInfo
->repetitions
+= j
;
2084 TIMEFINISH(cipherInfo
->optime
, 1.0);
2086 bltestCopyIO(cipherInfo
->arena
, &cipherInfo
->params
.dsa
.sig
,
2087 &cipherInfo
->output
);
2090 rv
= DSA_VerifyDigest((DSAPublicKey
*)cipherInfo
->cx
,
2091 &cipherInfo
->params
.dsa
.sig
.buf
,
2092 &cipherInfo
->input
.pBuf
);
2093 TIMEFINISH(cipherInfo
->optime
, 1.0);
2094 CHECKERROR(rv
, __LINE__
);
2095 cipherInfo
->repetitions
= 0;
2096 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2098 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2099 i
++, cipherInfo
->repetitions
++) {
2100 rv
= DSA_VerifyDigest((DSAPublicKey
*)cipherInfo
->cx
,
2101 &cipherInfo
->params
.dsa
.sig
.buf
,
2102 &cipherInfo
->input
.pBuf
);
2103 CHECKERROR(rv
, __LINE__
);
2106 int opsBetweenChecks
= 0;
2107 TIMEMARK(cipherInfo
->seconds
);
2108 while (! (TIMETOFINISH())) {
2110 for (;j
< opsBetweenChecks
;j
++) {
2111 rv
= DSA_VerifyDigest((DSAPublicKey
*)cipherInfo
->cx
,
2112 &cipherInfo
->params
.dsa
.sig
.buf
,
2113 &cipherInfo
->input
.pBuf
);
2114 CHECKERROR(rv
, __LINE__
);
2116 cipherInfo
->repetitions
+= j
;
2119 TIMEFINISH(cipherInfo
->optime
, 1.0);
2121 SECITEM_FreeItem(&dummyOut
, PR_FALSE
);
2125 #ifdef NSS_ENABLE_ECC
2127 ecdsaOp(bltestCipherInfo
*cipherInfo
)
2129 PRIntervalTime time1
, time2
;
2130 SECStatus rv
= SECSuccess
;
2132 int maxLen
= cipherInfo
->output
.pBuf
.len
;
2133 SECItem dummyOut
= { 0, 0, 0 };
2134 SECITEM_AllocItem(NULL
, &dummyOut
, maxLen
);
2135 if (cipherInfo
->cipher
.pubkeyCipher
== ecdsa_signDigest
) {
2136 if (cipherInfo
->params
.ecdsa
.sigseed
.buf
.len
> 0) {
2137 ECPrivateKey
*key
= (ECPrivateKey
*)cipherInfo
->cx
;
2138 bltestECDSAParams
*ecdsa
= &cipherInfo
->params
.ecdsa
;
2141 rv
= ECDSA_SignDigestWithSeed(key
,
2142 &cipherInfo
->output
.pBuf
,
2143 &cipherInfo
->input
.pBuf
,
2144 ecdsa
->sigseed
.buf
.data
,
2145 ecdsa
->sigseed
.buf
.len
);
2146 TIMEFINISH(cipherInfo
->optime
, 1.0);
2147 CHECKERROR(rv
, __LINE__
);
2148 cipherInfo
->repetitions
= 0;
2149 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2151 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2152 i
++, cipherInfo
->repetitions
++) {
2153 rv
= ECDSA_SignDigestWithSeed(key
, &dummyOut
,
2154 &cipherInfo
->input
.pBuf
,
2155 ecdsa
->sigseed
.buf
.data
,
2156 ecdsa
->sigseed
.buf
.len
);
2157 CHECKERROR(rv
, __LINE__
);
2160 int opsBetweenChecks
= 0;
2161 TIMEMARK(cipherInfo
->seconds
);
2162 while (! (TIMETOFINISH())) {
2164 for (;j
< opsBetweenChecks
;j
++) {
2165 rv
= ECDSA_SignDigestWithSeed(key
, &dummyOut
,
2166 &cipherInfo
->input
.pBuf
,
2167 ecdsa
->sigseed
.buf
.data
,
2168 ecdsa
->sigseed
.buf
.len
);
2169 CHECKERROR(rv
, __LINE__
);
2171 cipherInfo
->repetitions
+= j
;
2174 TIMEFINISH(cipherInfo
->optime
, 1.0);
2177 rv
= ECDSA_SignDigest((ECPrivateKey
*)cipherInfo
->cx
,
2178 &cipherInfo
->output
.pBuf
,
2179 &cipherInfo
->input
.pBuf
);
2180 TIMEFINISH(cipherInfo
->optime
, 1.0);
2181 CHECKERROR(rv
, __LINE__
);
2182 cipherInfo
->repetitions
= 0;
2183 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2185 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2186 i
++, cipherInfo
->repetitions
++) {
2187 rv
= ECDSA_SignDigest((ECPrivateKey
*)cipherInfo
->cx
,
2189 &cipherInfo
->input
.pBuf
);
2190 CHECKERROR(rv
, __LINE__
);
2193 int opsBetweenChecks
= 0;
2194 TIMEMARK(cipherInfo
->seconds
);
2195 while (! (TIMETOFINISH())) {
2197 for (;j
< opsBetweenChecks
;j
++) {
2198 rv
= ECDSA_SignDigest((ECPrivateKey
*)cipherInfo
->cx
,
2200 &cipherInfo
->input
.pBuf
);
2201 CHECKERROR(rv
, __LINE__
);
2203 cipherInfo
->repetitions
+= j
;
2206 TIMEFINISH(cipherInfo
->optime
, 1.0);
2208 bltestCopyIO(cipherInfo
->arena
, &cipherInfo
->params
.ecdsa
.sig
,
2209 &cipherInfo
->output
);
2212 rv
= ECDSA_VerifyDigest((ECPublicKey
*)cipherInfo
->cx
,
2213 &cipherInfo
->params
.ecdsa
.sig
.buf
,
2214 &cipherInfo
->input
.pBuf
);
2215 TIMEFINISH(cipherInfo
->optime
, 1.0);
2216 CHECKERROR(rv
, __LINE__
);
2217 cipherInfo
->repetitions
= 0;
2218 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2220 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2221 i
++, cipherInfo
->repetitions
++) {
2222 rv
= ECDSA_VerifyDigest((ECPublicKey
*)cipherInfo
->cx
,
2223 &cipherInfo
->params
.ecdsa
.sig
.buf
,
2224 &cipherInfo
->input
.pBuf
);
2225 CHECKERROR(rv
, __LINE__
);
2228 int opsBetweenChecks
= 0;
2229 TIMEMARK(cipherInfo
->seconds
);
2230 while (! (TIMETOFINISH())) {
2232 for (;j
< opsBetweenChecks
;j
++) {
2233 rv
= ECDSA_VerifyDigest((ECPublicKey
*)cipherInfo
->cx
,
2234 &cipherInfo
->params
.ecdsa
.sig
.buf
,
2235 &cipherInfo
->input
.pBuf
);
2236 CHECKERROR(rv
, __LINE__
);
2238 cipherInfo
->repetitions
+= j
;
2241 TIMEFINISH(cipherInfo
->optime
, 1.0);
2243 SECITEM_FreeItem(&dummyOut
, PR_FALSE
);
2249 cipherDoOp(bltestCipherInfo
*cipherInfo
)
2251 PRIntervalTime time1
, time2
;
2252 SECStatus rv
= SECSuccess
;
2254 int maxLen
= cipherInfo
->output
.pBuf
.len
;
2255 unsigned char *dummyOut
;
2256 if (cipherInfo
->mode
== bltestDSA
)
2257 return dsaOp(cipherInfo
);
2258 #ifdef NSS_ENABLE_ECC
2259 else if (cipherInfo
->mode
== bltestECDSA
)
2260 return ecdsaOp(cipherInfo
);
2262 dummyOut
= PORT_Alloc(maxLen
);
2263 if (is_symmkeyCipher(cipherInfo
->mode
)) {
2265 rv
= (*cipherInfo
->cipher
.symmkeyCipher
)(cipherInfo
->cx
,
2266 cipherInfo
->output
.pBuf
.data
,
2268 cipherInfo
->input
.pBuf
.data
,
2269 cipherInfo
->input
.pBuf
.len
);
2270 TIMEFINISH(cipherInfo
->optime
, 1.0);
2271 CHECKERROR(rv
, __LINE__
);
2272 cipherInfo
->repetitions
= 0;
2273 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2275 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
; i
++,
2276 cipherInfo
->repetitions
++) {
2277 (*cipherInfo
->cipher
.symmkeyCipher
)(cipherInfo
->cx
, dummyOut
,
2279 cipherInfo
->input
.pBuf
.data
,
2280 cipherInfo
->input
.pBuf
.len
);
2282 CHECKERROR(rv
, __LINE__
);
2285 int opsBetweenChecks
= 0;
2286 bltestIO
*input
= &cipherInfo
->input
;
2287 TIMEMARK(cipherInfo
->seconds
);
2288 while (! (TIMETOFINISH())) {
2290 for (;j
< opsBetweenChecks
;j
++) {
2291 (*cipherInfo
->cipher
.symmkeyCipher
)(cipherInfo
->cx
,
2297 cipherInfo
->repetitions
+= j
;
2300 TIMEFINISH(cipherInfo
->optime
, 1.0);
2301 } else if (is_pubkeyCipher(cipherInfo
->mode
)) {
2303 rv
= (*cipherInfo
->cipher
.pubkeyCipher
)(cipherInfo
->cx
,
2304 &cipherInfo
->output
.pBuf
,
2305 &cipherInfo
->input
.pBuf
);
2306 TIMEFINISH(cipherInfo
->optime
, 1.0);
2307 CHECKERROR(rv
, __LINE__
);
2308 cipherInfo
->repetitions
= 0;
2309 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2311 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2312 i
++, cipherInfo
->repetitions
++) {
2314 dummy
.data
= dummyOut
;
2316 (*cipherInfo
->cipher
.pubkeyCipher
)(cipherInfo
->cx
, &dummy
,
2317 &cipherInfo
->input
.pBuf
);
2318 CHECKERROR(rv
, __LINE__
);
2321 int opsBetweenChecks
= 0;
2322 TIMEMARK(cipherInfo
->seconds
);
2323 while (! (TIMETOFINISH())) {
2325 for (;j
< opsBetweenChecks
;j
++) {
2327 dummy
.data
= dummyOut
;
2329 (*cipherInfo
->cipher
.pubkeyCipher
)(cipherInfo
->cx
, &dummy
,
2330 &cipherInfo
->input
.pBuf
);
2331 CHECKERROR(rv
, __LINE__
);
2333 cipherInfo
->repetitions
+= j
;
2336 TIMEFINISH(cipherInfo
->optime
, 1.0);
2337 } else if (is_hashCipher(cipherInfo
->mode
)) {
2339 rv
= (*cipherInfo
->cipher
.hashCipher
)(cipherInfo
->output
.pBuf
.data
,
2340 cipherInfo
->input
.pBuf
.data
,
2341 cipherInfo
->input
.pBuf
.len
);
2342 TIMEFINISH(cipherInfo
->optime
, 1.0);
2343 CHECKERROR(rv
, __LINE__
);
2344 cipherInfo
->repetitions
= 0;
2345 if (cipherInfo
->repetitionsToPerfom
!= 0) {
2347 for (i
=0; i
<cipherInfo
->repetitionsToPerfom
;
2348 i
++, cipherInfo
->repetitions
++) {
2349 (*cipherInfo
->cipher
.hashCipher
)(dummyOut
,
2350 cipherInfo
->input
.pBuf
.data
,
2351 cipherInfo
->input
.pBuf
.len
);
2352 CHECKERROR(rv
, __LINE__
);
2355 int opsBetweenChecks
= 0;
2356 TIMEMARK(cipherInfo
->seconds
);
2357 while (! (TIMETOFINISH())) {
2359 for (;j
< opsBetweenChecks
;j
++) {
2360 bltestIO
*input
= &cipherInfo
->input
;
2361 (*cipherInfo
->cipher
.hashCipher
)(dummyOut
,
2364 CHECKERROR(rv
, __LINE__
);
2366 cipherInfo
->repetitions
+= j
;
2369 TIMEFINISH(cipherInfo
->optime
, 1.0);
2371 PORT_Free(dummyOut
);
2376 cipherFinish(bltestCipherInfo
*cipherInfo
)
2378 switch (cipherInfo
->mode
) {
2381 case bltestDES_EDE_ECB
:
2382 case bltestDES_EDE_CBC
:
2383 DES_DestroyContext((DESContext
*)cipherInfo
->cx
, PR_TRUE
);
2387 AES_DestroyContext((AESContext
*)cipherInfo
->cx
, PR_TRUE
);
2389 case bltestCAMELLIA_ECB
:
2390 case bltestCAMELLIA_CBC
:
2391 Camellia_DestroyContext((CamelliaContext
*)cipherInfo
->cx
, PR_TRUE
);
2395 RC2_DestroyContext((RC2Context
*)cipherInfo
->cx
, PR_TRUE
);
2398 RC4_DestroyContext((RC4Context
*)cipherInfo
->cx
, PR_TRUE
);
2400 #if NSS_SOFTOKEN_DOES_RC5
2403 RC5_DestroyContext((RC5Context
*)cipherInfo
->cx
, PR_TRUE
);
2406 case bltestRSA
: /* keys are alloc'ed within cipherInfo's arena, */
2407 case bltestDSA
: /* will be freed with it. */
2408 #ifdef NSS_ENABLE_ECC
2411 case bltestMD2
: /* hash contexts are ephemeral */
2426 print_exponent(SECItem
*exp
)
2430 if (exp
->len
<= 4) {
2431 for (i
=exp
->len
; i
>=0; --i
) e
|= exp
->data
[exp
->len
-i
] << 8*(i
-1);
2432 fprintf(stdout
, "%12d", e
);
2435 fprintf(stdout
, "~2**%-8d", e
);
2440 splitToReportUnit(PRInt64 res
, int *resArr
, int *del
, int size
)
2442 PRInt64 remaining
= res
, tmp
= 0;
2446 while (remaining
> 0 && ++i
< size
) {
2447 LL_I2L(Ldel
, del
[i
]);
2448 LL_MOD(tmp
, remaining
, Ldel
);
2449 LL_L2I(resArr
[i
], tmp
);
2450 LL_DIV(remaining
, remaining
, Ldel
);
2455 getHighUnitBytes(PRInt64 res
)
2457 int spl
[] = {0, 0, 0, 0};
2458 int del
[] = {1024, 1024, 1024, 1024};
2459 char *marks
[] = {"b", "Kb", "Mb", "Gb"};
2462 splitToReportUnit(res
, spl
, del
, 4);
2470 return PR_smprintf("%d%s", spl
[i
], marks
[i
]);
2475 printPR_smpString(const char *sformat
, char *reportStr
,
2476 const char *nformat
, PRInt64 rNum
)
2479 fprintf(stdout
, sformat
, reportStr
);
2480 PR_smprintf_free(reportStr
);
2483 LL_L2I(prnRes
, rNum
);
2484 fprintf(stdout
, nformat
, rNum
);
2489 getHighUnitOps(PRInt64 res
)
2491 int spl
[] = {0, 0, 0, 0};
2492 int del
[] = {1000, 1000, 1000, 1000};
2493 char *marks
[] = {"", "T", "M", "B"};
2496 splitToReportUnit(res
, spl
, del
, 4);
2504 return PR_smprintf("%d%s", spl
[i
], marks
[i
]);
2508 dump_performance_info(bltestCipherInfo
*infoList
, double totalTimeInt
,
2509 PRBool encrypt
, PRBool cxonly
)
2511 bltestCipherInfo
*info
= infoList
;
2513 PRInt64 totalIn
= 0;
2514 PRBool td
= PR_TRUE
;
2516 int repetitions
= 0;
2520 while (info
!= NULL
) {
2521 repetitions
+= info
->repetitions
;
2522 cxreps
+= info
->cxreps
;
2523 cxtime
+= info
->cxtime
;
2524 optime
+= info
->optime
;
2525 totalIn
+= (PRInt64
) info
->input
.buf
.len
* (PRInt64
) info
->repetitions
;
2531 fprintf(stdout
, "#%9s", "mode");
2532 fprintf(stdout
, "%12s", "in");
2534 switch (info
->mode
) {
2537 case bltestDES_EDE_ECB
:
2538 case bltestDES_EDE_CBC
:
2541 case bltestCAMELLIA_ECB
:
2542 case bltestCAMELLIA_CBC
:
2547 fprintf(stdout
, "%8s", "symmkey");
2549 fprintf(stdout
, "%8d", 8*info
->params
.sk
.key
.buf
.len
);
2551 #if NSS_SOFTOKEN_DOES_RC5
2554 if (info
->params
.sk
.key
.buf
.len
> 0)
2555 printf("symmetric key(bytes)=%d,", info
->params
.sk
.key
.buf
.len
);
2556 if (info
->rounds
> 0)
2557 printf("rounds=%d,", info
->params
.rc5
.rounds
);
2558 if (info
->wordsize
> 0)
2559 printf("wordsize(bytes)=%d,", info
->params
.rc5
.wordsize
);
2564 fprintf(stdout
, "%8s", "rsa_mod");
2565 fprintf(stdout
, "%12s", "rsa_pe");
2567 fprintf(stdout
, "%8d", info
->params
.rsa
.keysizeInBits
);
2568 print_exponent(&info
->params
.rsa
.rsakey
->publicExponent
);
2573 fprintf(stdout
, "%8s", "pqg_mod");
2575 fprintf(stdout
, "%8d", PQG_INDEX_TO_PBITS(info
->params
.dsa
.j
));
2577 #ifdef NSS_ENABLE_ECC
2580 fprintf(stdout
, "%12s", "ec_curve");
2582 ECCurveName curveName
= info
->params
.ecdsa
.eckey
->ecParams
.name
;
2583 fprintf(stdout
, "%12s",
2584 ecCurve_map
[curveName
]? ecCurve_map
[curveName
]->text
:
2585 "Unsupported curve");
2599 PRInt64 totalThroughPut
;
2601 printPR_smpString("%8s", getHighUnitOps(repetitions
),
2602 "%8d", repetitions
);
2604 printPR_smpString("%8s", getHighUnitOps(cxreps
), "%8d", cxreps
);
2606 fprintf(stdout
, "%12.3f", cxtime
);
2607 fprintf(stdout
, "%12.3f", optime
);
2608 fprintf(stdout
, "%12.03f", totalTimeInt
/ 1000);
2610 totalThroughPut
= (PRInt64
)(totalIn
/ totalTimeInt
* 1000);
2611 printPR_smpString("%12s", getHighUnitBytes(totalThroughPut
),
2612 "%12d", totalThroughPut
);
2614 fprintf(stdout
, "\n");
2618 fprintf(stdout
, "%8s", "opreps");
2619 fprintf(stdout
, "%8s", "cxreps");
2620 fprintf(stdout
, "%12s", "context");
2621 fprintf(stdout
, "%12s", "op");
2622 fprintf(stdout
, "%12s", "time(sec)");
2623 fprintf(stdout
, "%12s", "thrgput");
2624 fprintf(stdout
, "\n");
2625 fprintf(stdout
, "%8s", mode_strings
[info
->mode
]);
2626 fprintf(stdout
, "_%c", (cxonly
) ? 'c' : (encrypt
) ? 'e' : 'd');
2627 printPR_smpString("%12s", getHighUnitBytes(totalIn
), "%12d", totalIn
);
2636 bltestCipherMode mode
;
2637 int nummodes
= sizeof(mode_strings
) / sizeof(char *);
2638 fprintf(stderr
, "%s: Available modes (specify with -m):\n", progName
);
2639 for (mode
=0; mode
<nummodes
; mode
++)
2640 fprintf(stderr
, "%s\n", mode_strings
[mode
]);
2644 get_mode(const char *modestring
)
2646 bltestCipherMode mode
;
2647 int nummodes
= sizeof(mode_strings
) / sizeof(char *);
2648 for (mode
=0; mode
<nummodes
; mode
++)
2649 if (PL_strcmp(modestring
, mode_strings
[mode
]) == 0)
2651 fprintf(stderr
, "%s: invalid mode: %s\n", progName
, modestring
);
2652 return bltestINVALID
;
2656 load_file_data(PRArenaPool
*arena
, bltestIO
*data
,
2657 char *fn
, bltestIOMode ioMode
)
2660 data
->mode
= ioMode
;
2661 data
->file
= NULL
; /* don't use -- not saving anything */
2662 data
->pBuf
.data
= NULL
;
2664 file
= PR_Open(fn
, PR_RDONLY
, 00660);
2666 setupIO(arena
, data
, file
, NULL
, 0);
2670 get_params(PRArenaPool
*arena
, bltestParams
*params
,
2671 bltestCipherMode mode
, int j
)
2674 char *modestr
= mode_strings
[mode
];
2675 #if NSS_SOFTOKEN_DOES_RC5
2677 char *mark
, *param
, *val
;
2682 case bltestDES_EDE_CBC
:
2685 case bltestCAMELLIA_CBC
:
2686 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "iv", j
);
2687 load_file_data(arena
, ¶ms
->sk
.iv
, filename
, bltestBinary
);
2689 case bltestDES_EDE_ECB
:
2693 case bltestCAMELLIA_ECB
:
2694 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "key", j
);
2695 load_file_data(arena
, ¶ms
->sk
.key
, filename
, bltestBinary
);
2697 #if NSS_SOFTOKEN_DOES_RC5
2700 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "iv", j
);
2701 load_file_data(arena
, ¶ms
->sk
.iv
, filename
, bltestBinary
);
2702 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "key", j
);
2703 load_file_data(arena
, ¶ms
->sk
.key
, filename
, bltestBinary
);
2704 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
,
2706 file
= fopen(filename
, "r");
2708 param
= malloc(100);
2709 len
= fread(param
, 1, 100, file
);
2710 while (index
< len
) {
2711 mark
= PL_strchr(param
, '=');
2714 mark
= PL_strchr(val
, '\n');
2716 if (PL_strcmp(param
, "rounds") == 0) {
2717 params
->rc5
.rounds
= atoi(val
);
2718 } else if (PL_strcmp(param
, "wordsize") == 0) {
2719 params
->rc5
.wordsize
= atoi(val
);
2721 index
+= PL_strlen(param
) + PL_strlen(val
) + 2;
2727 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "key", j
);
2728 load_file_data(arena
, ¶ms
->rsa
.key
, filename
, bltestBase64Encoded
);
2729 params
->rsa
.rsakey
= rsakey_from_filedata(¶ms
->key
.buf
);
2732 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "key", j
);
2733 load_file_data(arena
, ¶ms
->dsa
.key
, filename
, bltestBase64Encoded
);
2734 params
->dsa
.dsakey
= dsakey_from_filedata(¶ms
->key
.buf
);
2735 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "pqg", j
);
2736 load_file_data(arena
, ¶ms
->dsa
.pqgdata
, filename
,
2737 bltestBase64Encoded
);
2738 params
->dsa
.pqg
= pqg_from_filedata(¶ms
->dsa
.pqgdata
.buf
);
2739 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "keyseed", j
);
2740 load_file_data(arena
, ¶ms
->dsa
.keyseed
, filename
,
2741 bltestBase64Encoded
);
2742 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "sigseed", j
);
2743 load_file_data(arena
, ¶ms
->dsa
.sigseed
, filename
,
2744 bltestBase64Encoded
);
2745 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "ciphertext",j
);
2746 load_file_data(arena
, ¶ms
->dsa
.sig
, filename
, bltestBase64Encoded
);
2748 #ifdef NSS_ENABLE_ECC
2750 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "key", j
);
2751 load_file_data(arena
, ¶ms
->ecdsa
.key
, filename
, bltestBase64Encoded
);
2752 params
->ecdsa
.eckey
= eckey_from_filedata(¶ms
->key
.buf
);
2753 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "sigseed", j
);
2754 load_file_data(arena
, ¶ms
->ecdsa
.sigseed
, filename
,
2755 bltestBase64Encoded
);
2756 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
, "ciphertext",j
);
2757 load_file_data(arena
, ¶ms
->ecdsa
.sig
, filename
, bltestBase64Encoded
);
2766 /*params->hash.restart = PR_TRUE;*/
2767 params
->hash
.restart
= PR_FALSE
;
2775 verify_self_test(bltestIO
*result
, bltestIO
*cmp
, bltestCipherMode mode
,
2776 PRBool forward
, SECStatus sigstatus
)
2779 char *modestr
= mode_strings
[mode
];
2780 res
= SECITEM_CompareItem(&result
->pBuf
, &cmp
->buf
);
2781 if (is_sigCipher(mode
)) {
2784 printf("Signature self-test for %s passed.\n", modestr
);
2786 printf("Signature self-test for %s failed!\n", modestr
);
2789 if (sigstatus
== SECSuccess
) {
2790 printf("Verification self-test for %s passed.\n", modestr
);
2792 printf("Verification self-test for %s failed!\n", modestr
);
2796 } else if (is_hashCipher(mode
)) {
2798 printf("Hash self-test for %s passed.\n", modestr
);
2800 printf("Hash self-test for %s failed!\n", modestr
);
2805 printf("Encryption self-test for %s passed.\n", modestr
);
2807 printf("Encryption self-test for %s failed!\n", modestr
);
2811 printf("Decryption self-test for %s passed.\n", modestr
);
2813 printf("Decryption self-test for %s failed!\n", modestr
);
2821 blapi_selftest(bltestCipherMode
*modes
, int numModes
, int inoff
, int outoff
,
2822 PRBool encrypt
, PRBool decrypt
)
2824 bltestCipherInfo cipherInfo
;
2826 bltestCipherMode mode
;
2827 bltestParams
*params
;
2828 int i
, j
, nummodes
, numtests
;
2835 SECStatus rv
= SECSuccess
, srv
;
2837 PORT_Memset(&cipherInfo
, 0, sizeof(cipherInfo
));
2838 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
2839 cipherInfo
.arena
= arena
;
2841 finished
= PR_FALSE
;
2842 nummodes
= (numModes
== 0) ? NUMMODES
: numModes
;
2843 for (i
=0; i
< nummodes
&& !finished
; i
++) {
2844 if (i
== bltestRC5_ECB
|| i
== bltestRC5_CBC
) continue;
2849 if (mode
== bltestINVALID
) {
2850 fprintf(stderr
, "%s: Skipping invalid mode.\n",progName
);
2853 modestr
= mode_strings
[mode
];
2854 cipherInfo
.mode
= mode
;
2855 params
= &cipherInfo
.params
;
2856 #ifdef TRACK_BLTEST_BUG
2857 if (mode
== bltestRSA
) {
2858 fprintf(stderr
, "[%s] Self-Testing RSA\n", __bltDBG
);
2861 /* get the number of tests in the directory */
2862 sprintf(filename
, "%s/tests/%s/%s", testdir
, modestr
, "numtests");
2863 file
= PR_Open(filename
, PR_RDONLY
, 00660);
2865 fprintf(stderr
, "%s: File %s does not exist.\n", progName
,filename
);
2868 rv
= SECU_FileToItem(&item
, file
);
2869 #ifdef TRACK_BLTEST_BUG
2870 if (mode
== bltestRSA
) {
2871 fprintf(stderr
, "[%s] Loaded data from %s\n", __bltDBG
, filename
);
2875 /* loop over the tests in the directory */
2877 for (j
=0; j
<item
.len
; j
++) {
2878 if (!isdigit(item
.data
[j
])) {
2882 numtests
+= (int) (item
.data
[j
] - '0');
2884 for (j
=0; j
<numtests
; j
++) {
2885 #ifdef TRACK_BLTEST_BUG
2886 if (mode
== bltestRSA
) {
2887 fprintf(stderr
, "[%s] Executing self-test #%d\n", __bltDBG
, j
);
2890 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
,
2892 load_file_data(arena
, &pt
, filename
,
2893 #ifdef NSS_ENABLE_ECC
2894 ((mode
== bltestDSA
) || (mode
== bltestECDSA
))
2898 ? bltestBase64Encoded
: bltestBinary
);
2899 sprintf(filename
, "%s/tests/%s/%s%d", testdir
, modestr
,
2901 load_file_data(arena
, &ct
, filename
, bltestBase64Encoded
);
2902 #ifdef TRACK_BLTEST_BUG
2903 if (mode
== bltestRSA
) {
2904 fprintf(stderr
, "[%s] Loaded data for self-test #%d\n", __bltDBG
, j
);
2907 get_params(arena
, params
, mode
, j
);
2908 #ifdef TRACK_BLTEST_BUG
2909 if (mode
== bltestRSA
) {
2910 fprintf(stderr
, "[%s] Got parameters for #%d\n", __bltDBG
, j
);
2913 /* Forward Operation (Encrypt/Sign/Hash)
2914 ** Align the input buffer (plaintext) according to request
2915 ** then perform operation and compare to ciphertext
2920 bltestCopyIO(arena
, &cipherInfo
.input
, &pt
);
2921 misalignBuffer(arena
, &cipherInfo
.input
, inoff
);
2922 memset(&cipherInfo
.output
.buf
, 0, sizeof cipherInfo
.output
.buf
);
2923 rv
|= cipherInit(&cipherInfo
, PR_TRUE
);
2924 misalignBuffer(arena
, &cipherInfo
.output
, outoff
);
2925 #ifdef TRACK_BLTEST_BUG
2926 if (mode
== bltestRSA
) {
2927 fprintf(stderr
, "[%s] Inited cipher context and buffers for #%d\n", __bltDBG
, j
);
2930 rv
|= cipherDoOp(&cipherInfo
);
2931 #ifdef TRACK_BLTEST_BUG
2932 if (mode
== bltestRSA
) {
2933 fprintf(stderr
, "[%s] Performed encrypt for #%d\n", __bltDBG
, j
);
2936 rv
|= cipherFinish(&cipherInfo
);
2937 #ifdef TRACK_BLTEST_BUG
2938 if (mode
== bltestRSA
) {
2939 fprintf(stderr
, "[%s] Finished encrypt for #%d\n", __bltDBG
, j
);
2942 rv
|= verify_self_test(&cipherInfo
.output
,
2943 &ct
, mode
, PR_TRUE
, 0);
2944 #ifdef TRACK_BLTEST_BUG
2945 if (mode
== bltestRSA
) {
2946 fprintf(stderr
, "[%s] Verified self-test for #%d\n", __bltDBG
, j
);
2949 /* If testing hash, only one op to test */
2950 if (is_hashCipher(mode
))
2952 /*if (rv) return rv;*/
2958 /* Reverse Operation (Decrypt/Verify)
2959 ** Align the input buffer (ciphertext) according to request
2960 ** then perform operation and compare to plaintext
2962 #ifdef NSS_ENABLE_ECC
2963 if ((mode
!= bltestDSA
) && (mode
!= bltestECDSA
))
2965 if (mode
!= bltestDSA
)
2967 bltestCopyIO(arena
, &cipherInfo
.input
, &ct
);
2969 bltestCopyIO(arena
, &cipherInfo
.input
, &pt
);
2970 misalignBuffer(arena
, &cipherInfo
.input
, inoff
);
2971 memset(&cipherInfo
.output
.buf
, 0, sizeof cipherInfo
.output
.buf
);
2972 rv
|= cipherInit(&cipherInfo
, PR_FALSE
);
2973 misalignBuffer(arena
, &cipherInfo
.output
, outoff
);
2974 #ifdef TRACK_BLTEST_BUG
2975 if (mode
== bltestRSA
) {
2976 fprintf(stderr
, "[%s] Inited cipher context and buffers for #%d\n", __bltDBG
, j
);
2980 srv
|= cipherDoOp(&cipherInfo
);
2981 #ifdef TRACK_BLTEST_BUG
2982 if (mode
== bltestRSA
) {
2983 fprintf(stderr
, "[%s] Performed decrypt for #%d\n", __bltDBG
, j
);
2986 rv
|= cipherFinish(&cipherInfo
);
2987 #ifdef TRACK_BLTEST_BUG
2988 if (mode
== bltestRSA
) {
2989 fprintf(stderr
, "[%s] Finished decrypt for #%d\n", __bltDBG
, j
);
2992 rv
|= verify_self_test(&cipherInfo
.output
,
2993 &pt
, mode
, PR_FALSE
, srv
);
2994 #ifdef TRACK_BLTEST_BUG
2995 if (mode
== bltestRSA
) {
2996 fprintf(stderr
, "[%s] Verified self-test for #%d\n", __bltDBG
, j
);
2999 /*if (rv) return rv;*/
3006 dump_file(bltestCipherMode mode
, char *filename
)
3009 PRArenaPool
*arena
= NULL
;
3010 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
3011 if (mode
== bltestRSA
) {
3013 load_file_data(arena
, &keydata
, filename
, bltestBase64Encoded
);
3014 key
= rsakey_from_filedata(&keydata
.buf
);
3016 } else if (mode
== bltestDSA
) {
3019 get_file_data(filename
, &item
, PR_TRUE
);
3020 pqg
= pqg_from_filedata(&item
);
3024 load_file_data(arena
, &keydata
, filename
, bltestBase64Encoded
);
3025 key
= dsakey_from_filedata(&keydata
.buf
);
3027 #ifdef NSS_ENABLE_ECC
3028 } else if (mode
== bltestECDSA
) {
3030 load_file_data(arena
, &keydata
, filename
, bltestBase64Encoded
);
3031 key
= eckey_from_filedata(&keydata
.buf
);
3035 PORT_FreeArena(arena
, PR_FALSE
);
3039 void ThreadExecTest(void *data
)
3041 bltestCipherInfo
*cipherInfo
= (bltestCipherInfo
*)data
;
3043 if (cipherInfo
->mCarlo
== PR_TRUE
) {
3045 for (mciter
=0; mciter
<10000; mciter
++) {
3046 cipherDoOp(cipherInfo
);
3047 memcpy(cipherInfo
->input
.buf
.data
,
3048 cipherInfo
->output
.buf
.data
,
3049 cipherInfo
->input
.buf
.len
);
3052 cipherDoOp(cipherInfo
);
3054 cipherFinish(cipherInfo
);
3057 /* bltest commands */
3070 /* bltest options */
3085 #ifdef NSS_ENABLE_ECC
3108 static secuCommandFlag bltest_commands
[] =
3110 { /* cmd_Decrypt */ 'D', PR_FALSE
, 0, PR_FALSE
},
3111 { /* cmd_Encrypt */ 'E', PR_FALSE
, 0, PR_FALSE
},
3112 { /* cmd_FIPS */ 'F', PR_FALSE
, 0, PR_FALSE
},
3113 { /* cmd_Hash */ 'H', PR_FALSE
, 0, PR_FALSE
},
3114 { /* cmd_Nonce */ 'N', PR_FALSE
, 0, PR_FALSE
},
3115 { /* cmd_Dump */ 'P', PR_FALSE
, 0, PR_FALSE
},
3116 { /* cmd_Sign */ 'S', PR_FALSE
, 0, PR_FALSE
},
3117 { /* cmd_SelfTest */ 'T', PR_FALSE
, 0, PR_FALSE
},
3118 { /* cmd_Verify */ 'V', PR_FALSE
, 0, PR_FALSE
}
3121 static secuCommandFlag bltest_options
[] =
3123 { /* opt_B64 */ 'a', PR_FALSE
, 0, PR_FALSE
},
3124 { /* opt_BufSize */ 'b', PR_TRUE
, 0, PR_FALSE
},
3125 { /* opt_Restart */ 'c', PR_FALSE
, 0, PR_FALSE
},
3126 { /* opt_SelfTestDir */ 'd', PR_TRUE
, 0, PR_FALSE
},
3127 { /* opt_Exponent */ 'e', PR_TRUE
, 0, PR_FALSE
},
3128 { /* opt_SigFile */ 'f', PR_TRUE
, 0, PR_FALSE
},
3129 { /* opt_KeySize */ 'g', PR_TRUE
, 0, PR_FALSE
},
3130 { /* opt_Hex */ 'h', PR_FALSE
, 0, PR_FALSE
},
3131 { /* opt_Input */ 'i', PR_TRUE
, 0, PR_FALSE
},
3132 { /* opt_PQGFile */ 'j', PR_TRUE
, 0, PR_FALSE
},
3133 { /* opt_Key */ 'k', PR_TRUE
, 0, PR_FALSE
},
3134 { /* opt_HexWSpc */ 'l', PR_FALSE
, 0, PR_FALSE
},
3135 { /* opt_Mode */ 'm', PR_TRUE
, 0, PR_FALSE
},
3136 #ifdef NSS_ENABLE_ECC
3137 { /* opt_CurveName */ 'n', PR_TRUE
, 0, PR_FALSE
},
3139 { /* opt_Output */ 'o', PR_TRUE
, 0, PR_FALSE
},
3140 { /* opt_Repetitions */ 'p', PR_TRUE
, 0, PR_FALSE
},
3141 { /* opt_ZeroBuf */ 'q', PR_FALSE
, 0, PR_FALSE
},
3142 { /* opt_Rounds */ 'r', PR_TRUE
, 0, PR_FALSE
},
3143 { /* opt_Seed */ 's', PR_TRUE
, 0, PR_FALSE
},
3144 { /* opt_SigSeedFile */ 't', PR_TRUE
, 0, PR_FALSE
},
3145 { /* opt_CXReps */ 'u', PR_TRUE
, 0, PR_FALSE
},
3146 { /* opt_IV */ 'v', PR_TRUE
, 0, PR_FALSE
},
3147 { /* opt_WordSize */ 'w', PR_TRUE
, 0, PR_FALSE
},
3148 { /* opt_UseSeed */ 'x', PR_FALSE
, 0, PR_FALSE
},
3149 { /* opt_UseSigSeed */ 'y', PR_FALSE
, 0, PR_FALSE
},
3150 { /* opt_SeedFile */ 'z', PR_FALSE
, 0, PR_FALSE
},
3151 { /* opt_InputOffset */ '1', PR_TRUE
, 0, PR_FALSE
},
3152 { /* opt_OutputOffset */ '2', PR_TRUE
, 0, PR_FALSE
},
3153 { /* opt_MonteCarlo */ '3', PR_FALSE
, 0, PR_FALSE
},
3154 { /* opt_ThreadNum */ '4', PR_TRUE
, 0, PR_FALSE
},
3155 { /* opt_SecondsToRun */ '5', PR_TRUE
, 0, PR_FALSE
},
3156 { /* opt_CmdLine */ '-', PR_FALSE
, 0, PR_FALSE
}
3159 int main(int argc
, char **argv
)
3161 char *infileName
, *outfileName
, *keyfileName
, *ivfileName
;
3162 SECStatus rv
= SECFailure
;
3165 PRIntervalTime time1
, time2
;
3166 PRFileDesc
*outfile
;
3167 bltestCipherInfo
*cipherInfoListHead
, *cipherInfo
;
3168 bltestIOMode ioMode
;
3169 int bufsize
, exponent
, curThrdNum
;
3170 #ifdef NSS_ENABLE_ECC
3171 char *curveName
= NULL
;
3173 int i
, commandsEntered
;
3178 bltest
.numCommands
= sizeof(bltest_commands
) / sizeof(secuCommandFlag
);
3179 bltest
.numOptions
= sizeof(bltest_options
) / sizeof(secuCommandFlag
);
3180 bltest
.commands
= bltest_commands
;
3181 bltest
.options
= bltest_options
;
3183 progName
= strrchr(argv
[0], '/');
3185 progName
= strrchr(argv
[0], '\\');
3186 progName
= progName
? progName
+1 : argv
[0];
3189 if (rv
!= SECSuccess
) {
3190 SECU_PrintPRandOSError(progName
);
3193 RNG_SystemInfoForRNG();
3195 rv
= SECU_ParseCommandLine(argc
, argv
, progName
, &bltest
);
3196 if (rv
== SECFailure
) {
3197 fprintf(stderr
, "%s: command line parsing error!\n", progName
);
3202 cipherInfo
= PORT_ZNew(bltestCipherInfo
);
3203 cipherInfoListHead
= cipherInfo
;
3204 /* set some defaults */
3205 infileName
= outfileName
= keyfileName
= ivfileName
= NULL
;
3207 /* Check the number of commands entered on the command line. */
3208 commandsEntered
= 0;
3209 for (i
=0; i
<bltest
.numCommands
; i
++)
3210 if (bltest
.commands
[i
].activated
)
3213 if (commandsEntered
> 1 &&
3214 !(commandsEntered
== 2 && bltest
.commands
[cmd_SelfTest
].activated
)) {
3215 fprintf(stderr
, "%s: one command at a time!\n", progName
);
3219 if (commandsEntered
== 0) {
3220 fprintf(stderr
, "%s: you must enter a command!\n", progName
);
3224 if (bltest
.commands
[cmd_Sign
].activated
)
3225 bltest
.commands
[cmd_Encrypt
].activated
= PR_TRUE
;
3226 if (bltest
.commands
[cmd_Verify
].activated
)
3227 bltest
.commands
[cmd_Decrypt
].activated
= PR_TRUE
;
3228 if (bltest
.commands
[cmd_Hash
].activated
)
3229 bltest
.commands
[cmd_Encrypt
].activated
= PR_TRUE
;
3232 if (bltest
.options
[opt_InputOffset
].activated
)
3233 inoff
= PORT_Atoi(bltest
.options
[opt_InputOffset
].arg
);
3234 if (bltest
.options
[opt_OutputOffset
].activated
)
3235 outoff
= PORT_Atoi(bltest
.options
[opt_OutputOffset
].arg
);
3237 testdir
= (bltest
.options
[opt_SelfTestDir
].activated
) ?
3238 strdup(bltest
.options
[opt_SelfTestDir
].arg
) : ".";
3241 * Handle three simple cases first
3244 /* Do BLAPI self-test */
3245 if (bltest
.commands
[cmd_SelfTest
].activated
) {
3246 PRBool encrypt
= PR_TRUE
, decrypt
= PR_TRUE
;
3247 /* user may specified a set of ciphers to test. parse them. */
3248 bltestCipherMode modesToTest
[NUMMODES
];
3249 int numModesToTest
= 0;
3251 str
= bltest
.options
[opt_Mode
].arg
;
3253 tok
= strchr(str
, ',');
3254 if (tok
) *tok
= '\0';
3255 modesToTest
[numModesToTest
++] = get_mode(str
);
3263 if (bltest
.commands
[cmd_Decrypt
].activated
&&
3264 !bltest
.commands
[cmd_Encrypt
].activated
)
3266 if (bltest
.commands
[cmd_Encrypt
].activated
&&
3267 !bltest
.commands
[cmd_Decrypt
].activated
)
3269 rv
= blapi_selftest(modesToTest
, numModesToTest
, inoff
, outoff
,
3271 PORT_Free(cipherInfo
);
3275 /* Do FIPS self-test */
3276 if (bltest
.commands
[cmd_FIPS
].activated
) {
3277 CK_RV ckrv
= sftk_fipsPowerUpSelfTest();
3278 fprintf(stdout
, "CK_RV: %ld.\n", ckrv
);
3279 PORT_Free(cipherInfo
);
3286 * Check command line arguments for Encrypt/Decrypt/Hash/Sign/Verify
3289 if ((bltest
.commands
[cmd_Decrypt
].activated
||
3290 bltest
.commands
[cmd_Verify
].activated
) &&
3291 bltest
.options
[opt_BufSize
].activated
) {
3292 fprintf(stderr
, "%s: Cannot use a nonce as input to decrypt/verify.\n",
3297 if (bltest
.options
[opt_Mode
].activated
) {
3298 cipherInfo
->mode
= get_mode(bltest
.options
[opt_Mode
].arg
);
3299 if (cipherInfo
->mode
== bltestINVALID
) {
3303 fprintf(stderr
, "%s: You must specify a cipher mode with -m.\n",
3309 if (bltest
.options
[opt_Repetitions
].activated
&&
3310 bltest
.options
[opt_SecondsToRun
].activated
) {
3311 fprintf(stderr
, "%s: Operation time should be defined in either "
3312 "repetitions(-p) or seconds(-5) not both",
3317 if (bltest
.options
[opt_Repetitions
].activated
) {
3318 cipherInfo
->repetitionsToPerfom
=
3319 PORT_Atoi(bltest
.options
[opt_Repetitions
].arg
);
3321 cipherInfo
->repetitionsToPerfom
= 0;
3324 if (bltest
.options
[opt_SecondsToRun
].activated
) {
3325 cipherInfo
->seconds
= PORT_Atoi(bltest
.options
[opt_SecondsToRun
].arg
);
3327 cipherInfo
->seconds
= 0;
3331 if (bltest
.options
[opt_CXReps
].activated
) {
3332 cipherInfo
->cxreps
= PORT_Atoi(bltest
.options
[opt_CXReps
].arg
);
3334 cipherInfo
->cxreps
= 0;
3337 if (bltest
.options
[opt_ThreadNum
].activated
) {
3338 threads
= PORT_Atoi(bltest
.options
[opt_ThreadNum
].arg
);
3344 /* Dump a file (rsakey, dsakey, etc.) */
3345 if (bltest
.commands
[cmd_Dump
].activated
) {
3346 rv
= dump_file(cipherInfo
->mode
, bltest
.options
[opt_Input
].arg
);
3347 PORT_Free(cipherInfo
);
3351 /* default input mode is binary */
3352 ioMode
= (bltest
.options
[opt_B64
].activated
) ? bltestBase64Encoded
:
3353 (bltest
.options
[opt_Hex
].activated
) ? bltestHexStream
:
3354 (bltest
.options
[opt_HexWSpc
].activated
) ? bltestHexSpaceDelim
:
3357 if (bltest
.options
[opt_Exponent
].activated
)
3358 exponent
= PORT_Atoi(bltest
.options
[opt_Exponent
].arg
);
3362 #ifdef NSS_ENABLE_ECC
3363 if (bltest
.options
[opt_CurveName
].activated
)
3364 curveName
= PORT_Strdup(bltest
.options
[opt_CurveName
].arg
);
3369 if (bltest
.commands
[cmd_Verify
].activated
&&
3370 !bltest
.options
[opt_SigFile
].activated
) {
3371 fprintf(stderr
, "%s: You must specify a signature file with -f.\n",
3375 PORT_Free(cipherInfo
);
3379 if (bltest
.options
[opt_MonteCarlo
].activated
) {
3380 cipherInfo
->mCarlo
= PR_TRUE
;
3382 cipherInfo
->mCarlo
= PR_FALSE
;
3385 for (curThrdNum
= 0;curThrdNum
< threads
;curThrdNum
++) {
3387 PRFileDesc
*file
= NULL
, *infile
;
3388 bltestParams
*params
;
3392 if (curThrdNum
> 0) {
3393 bltestCipherInfo
*newCInfo
= PORT_ZNew(bltestCipherInfo
);
3395 fprintf(stderr
, "%s: Can not allocate memory.\n", progName
);
3398 newCInfo
->mode
= cipherInfo
->mode
;
3399 newCInfo
->mCarlo
= cipherInfo
->mCarlo
;
3400 newCInfo
->repetitionsToPerfom
=
3401 cipherInfo
->repetitionsToPerfom
;
3402 newCInfo
->seconds
= cipherInfo
->seconds
;
3403 newCInfo
->cxreps
= cipherInfo
->cxreps
;
3404 cipherInfo
->next
= newCInfo
;
3405 cipherInfo
= newCInfo
;
3407 arena
= PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE
);
3409 fprintf(stderr
, "%s: Can not allocate memory.\n", progName
);
3412 cipherInfo
->arena
= arena
;
3413 params
= &cipherInfo
->params
;
3415 /* Set up an encryption key. */
3418 if (is_symmkeyCipher(cipherInfo
->mode
)) {
3419 char *keystr
= NULL
; /* if key is on command line */
3420 if (bltest
.options
[opt_Key
].activated
) {
3421 if (bltest
.options
[opt_CmdLine
].activated
) {
3422 keystr
= bltest
.options
[opt_Key
].arg
;
3424 file
= PR_Open(bltest
.options
[opt_Key
].arg
,
3428 if (bltest
.options
[opt_KeySize
].activated
)
3429 keysize
= PORT_Atoi(bltest
.options
[opt_KeySize
].arg
);
3431 keysize
= 8; /* use 64-bit default (DES) */
3432 /* save the random key for reference */
3433 file
= PR_Open("tmp.key", PR_WRONLY
|PR_CREATE_FILE
, 00660);
3435 params
->key
.mode
= ioMode
;
3436 setupIO(cipherInfo
->arena
, ¶ms
->key
, file
, keystr
, keysize
);
3439 } else if (is_pubkeyCipher(cipherInfo
->mode
)) {
3440 if (bltest
.options
[opt_Key
].activated
) {
3441 file
= PR_Open(bltest
.options
[opt_Key
].arg
, PR_RDONLY
, 00660);
3443 if (bltest
.options
[opt_KeySize
].activated
)
3444 keysize
= PORT_Atoi(bltest
.options
[opt_KeySize
].arg
);
3446 keysize
= 64; /* use 512-bit default */
3447 file
= PR_Open("tmp.key", PR_WRONLY
|PR_CREATE_FILE
, 00660);
3449 params
->key
.mode
= bltestBase64Encoded
;
3450 #ifdef NSS_ENABLE_ECC
3451 pubkeyInitKey(cipherInfo
, file
, keysize
, exponent
, curveName
);
3453 pubkeyInitKey(cipherInfo
, file
, keysize
, exponent
);
3458 /* set up an initialization vector. */
3459 if (cipher_requires_IV(cipherInfo
->mode
)) {
3461 bltestSymmKeyParams
*skp
;
3463 if (cipherInfo
->mode
== bltestRC5_CBC
)
3464 skp
= (bltestSymmKeyParams
*)¶ms
->rc5
;
3467 if (bltest
.options
[opt_IV
].activated
) {
3468 if (bltest
.options
[opt_CmdLine
].activated
) {
3469 ivstr
= bltest
.options
[opt_IV
].arg
;
3471 file
= PR_Open(bltest
.options
[opt_IV
].arg
,
3475 /* save the random iv for reference */
3476 file
= PR_Open("tmp.iv", PR_WRONLY
|PR_CREATE_FILE
, 00660);
3478 memset(&skp
->iv
, 0, sizeof skp
->iv
);
3479 skp
->iv
.mode
= ioMode
;
3480 setupIO(cipherInfo
->arena
, &skp
->iv
, file
, ivstr
, keysize
);
3486 if (bltest
.commands
[cmd_Verify
].activated
) {
3487 file
= PR_Open(bltest
.options
[opt_SigFile
].arg
, PR_RDONLY
, 00660);
3488 if (cipherInfo
->mode
== bltestDSA
) {
3489 memset(&cipherInfo
->params
.dsa
.sig
, 0, sizeof(bltestIO
));
3490 cipherInfo
->params
.dsa
.sig
.mode
= ioMode
;
3491 setupIO(cipherInfo
->arena
, &cipherInfo
->params
.dsa
.sig
,
3493 #ifdef NSS_ENABLE_ECC
3494 } else if (cipherInfo
->mode
== bltestECDSA
) {
3495 memset(&cipherInfo
->params
.ecdsa
.sig
, 0, sizeof(bltestIO
));
3496 cipherInfo
->params
.ecdsa
.sig
.mode
= ioMode
;
3497 setupIO(cipherInfo
->arena
, &cipherInfo
->params
.ecdsa
.sig
,
3506 if (bltest
.options
[opt_PQGFile
].activated
) {
3507 file
= PR_Open(bltest
.options
[opt_PQGFile
].arg
, PR_RDONLY
, 00660);
3508 params
->dsa
.pqgdata
.mode
= bltestBase64Encoded
;
3509 setupIO(cipherInfo
->arena
, ¶ms
->dsa
.pqgdata
, file
, NULL
, 0);
3515 /* Set up the input buffer */
3516 if (bltest
.options
[opt_Input
].activated
) {
3517 if (bltest
.options
[opt_CmdLine
].activated
) {
3518 instr
= bltest
.options
[opt_Input
].arg
;
3521 /* form file name from testdir and input arg. */
3522 char * filename
= bltest
.options
[opt_Input
].arg
;
3523 if (bltest
.options
[opt_SelfTestDir
].activated
&&
3524 testdir
&& filename
&& filename
[0] != '/') {
3525 filename
= PR_smprintf("%s/tests/%s/%s", testdir
,
3526 mode_strings
[cipherInfo
->mode
],
3529 fprintf(stderr
, "%s: Can not allocate memory.\n",
3533 infile
= PR_Open(filename
, PR_RDONLY
, 00660);
3534 PR_smprintf_free(filename
);
3536 infile
= PR_Open(filename
, PR_RDONLY
, 00660);
3539 } else if (bltest
.options
[opt_BufSize
].activated
) {
3540 /* save the random plaintext for reference */
3541 char *tmpFName
= PR_smprintf("tmp.in.%d", curThrdNum
);
3543 fprintf(stderr
, "%s: Can not allocate memory.\n", progName
);
3546 infile
= PR_Open(tmpFName
, PR_WRONLY
|PR_CREATE_FILE
, 00660);
3547 PR_smprintf_free(tmpFName
);
3552 fprintf(stderr
, "%s: Failed to open input file.\n", progName
);
3555 cipherInfo
->input
.mode
= ioMode
;
3557 /* Set up the output stream */
3558 if (bltest
.options
[opt_Output
].activated
) {
3559 /* form file name from testdir and input arg. */
3560 char * filename
= bltest
.options
[opt_Output
].arg
;
3561 if (bltest
.options
[opt_SelfTestDir
].activated
&&
3562 testdir
&& filename
&& filename
[0] != '/') {
3563 filename
= PR_smprintf("%s/tests/%s/%s", testdir
,
3564 mode_strings
[cipherInfo
->mode
],
3567 fprintf(stderr
, "%s: Can not allocate memory.\n", progName
);
3570 outfile
= PR_Open(filename
, PR_WRONLY
|PR_CREATE_FILE
, 00660);
3571 PR_smprintf_free(filename
);
3573 outfile
= PR_Open(filename
, PR_WRONLY
|PR_CREATE_FILE
, 00660);
3576 outfile
= PR_STDOUT
;
3579 fprintf(stderr
, "%s: Failed to open output file.\n", progName
);
3583 cipherInfo
->output
.mode
= ioMode
;
3584 if (bltest
.options
[opt_SelfTestDir
].activated
&& ioMode
== bltestBinary
)
3585 cipherInfo
->output
.mode
= bltestBase64Encoded
;
3587 if (is_hashCipher(cipherInfo
->mode
))
3588 cipherInfo
->params
.hash
.restart
=
3589 bltest
.options
[opt_Restart
].activated
;
3592 if (bltest
.options
[opt_BufSize
].activated
)
3593 bufsize
= PORT_Atoi(bltest
.options
[opt_BufSize
].arg
);
3596 setupIO(cipherInfo
->arena
, &cipherInfo
->input
, infile
, instr
, bufsize
);
3597 if (infile
&& infile
!= PR_STDIN
)
3599 misalignBuffer(cipherInfo
->arena
, &cipherInfo
->input
, inoff
);
3601 cipherInit(cipherInfo
, bltest
.commands
[cmd_Encrypt
].activated
);
3602 misalignBuffer(cipherInfo
->arena
, &cipherInfo
->output
, outoff
);
3605 if (!bltest
.commands
[cmd_Nonce
].activated
) {
3607 cipherInfo
= cipherInfoListHead
;
3608 while (cipherInfo
!= NULL
) {
3609 cipherInfo
->cipherThread
=
3610 PR_CreateThread(PR_USER_THREAD
,
3617 cipherInfo
= cipherInfo
->next
;
3620 cipherInfo
= cipherInfoListHead
;
3621 while (cipherInfo
!= NULL
) {
3622 PR_JoinThread(cipherInfo
->cipherThread
);
3623 finishIO(&cipherInfo
->output
, outfile
);
3624 cipherInfo
= cipherInfo
->next
;
3626 TIMEFINISH(totalTime
, 1);
3629 cipherInfo
= cipherInfoListHead
;
3630 if (cipherInfo
->repetitions
> 0 || cipherInfo
->cxreps
> 0 ||
3632 dump_performance_info(cipherInfoListHead
, totalTime
,
3633 bltest
.commands
[cmd_Encrypt
].activated
,
3634 (cipherInfo
->repetitions
== 0));
3639 if (outfile
&& outfile
!= PR_STDOUT
)
3641 cipherInfo
= cipherInfoListHead
;
3642 while (cipherInfo
!= NULL
) {
3643 bltestCipherInfo
*tmpInfo
= cipherInfo
;
3645 if (cipherInfo
->arena
)
3646 PORT_FreeArena(cipherInfo
->arena
, PR_TRUE
);
3647 cipherInfo
= cipherInfo
->next
;