2 * rawSig.c - Test compatiblity of (hash+sign) ops vs. manual digest followed
9 #include <Security/cssm.h>
10 #include <Security/cssmapple.h>
13 #include "bsafeUtils.h"
14 #include "cspdlTesting.h"
20 #define OLOOPS_DEF 10 /* outer loops, one key pair per loop */
21 #define ILOOPS_DEF 10 /* sig loops */
22 #define MAX_TEXT_SIZE 1024
24 #define LOOP_NOTIFY 20
27 * Enumerate algs our own way to allow iteration.
30 #define ALG_RSA_SHA1 2
33 #define ALG_FEE_SHA1 5
34 #define ALG_ECDSA_SHA1 6
35 #define ALG_DSA_SHA1 7
36 #define ALG_ANSI_ECDSA_SHA1 8
37 #define ALG_ECDSA_SHA256 9
38 #define ALG_ECDSA_SHA384 10
39 #define ALG_ECDSA_SHA512 11
40 #define ALG_FIRST ALG_RSA_MD5
41 #define ALG_LAST ALG_ECDSA_SHA512
43 static void usage(char **argv
)
45 printf("usage: %s [options]\n", argv
[0]);
46 printf(" Options:\n");
47 printf(" a=alg (5=RSA/MD5; 2=RSA/MD2; R=RSA/SHA1; d=DSA/SHA1; f=FEE/MD5; F=FEE/SHA1; \n");
48 printf(" e=ECDSA/SHA1; E=ANSI_ECDSA/SHA1; 7=ECDSA/SHA256; 8=ECDSA/SHA384; 9=ECDSA/512; default=all)\n");
49 printf(" l=outerloops (default=%d; 0=forever)\n", OLOOPS_DEF
);
50 printf(" i=innerLoops (default=%d)\n", ILOOPS_DEF
);
51 printf(" k=keySizeInBits; default is random\n");
52 printf(" p=pauseInterval (default=0, no pause)\n");
53 printf(" s(mall keys)\n");
54 printf(" D (CSP/DL; default = bare CSP)\n");
55 printf(" v(erbose)\n");
61 int main(int argc
, char **argv
)
67 CSSM_DATA ptext
= {0, NULL
};
68 CSSM_CSP_HANDLE cspHand
;
71 uint32 keySizeInBits
= 0;
74 CSSM_DATA sig
= {0, NULL
};
75 CSSM_DATA digest
= {0, NULL
};
79 /* current alg (e.g. ALG_FEE_SHA1) parsed to come up with these */
80 CSSM_ALGORITHMS digestAlg
;
81 CSSM_ALGORITHMS rawSigAlg
;
82 CSSM_ALGORITHMS sigAlg
;
83 CSSM_ALGORITHMS keyGenAlg
;
84 const char *sigAlgStr
;
89 CSSM_BOOL keySizeSpec
= CSSM_FALSE
;
90 unsigned oloops
= OLOOPS_DEF
;
91 unsigned iloops
= ILOOPS_DEF
;
92 CSSM_BOOL verbose
= CSSM_FALSE
;
93 CSSM_BOOL quiet
= CSSM_FALSE
;
94 unsigned pauseInterval
= 0;
95 CSSM_BOOL bareCsp
= CSSM_TRUE
;
96 unsigned minAlg
= ALG_FIRST
;
97 uint32 maxAlg
= ALG_LAST
;
98 CSSM_BOOL smallKeys
= CSSM_FALSE
;
100 for(arg
=1; arg
<argc
; arg
++) {
106 minAlg
= maxAlg
= ALG_FEE_MD5
;
109 minAlg
= maxAlg
= ALG_FEE_SHA1
;
112 minAlg
= maxAlg
= ALG_ECDSA_SHA1
;
115 minAlg
= maxAlg
= ALG_RSA_MD5
;
118 minAlg
= maxAlg
= ALG_RSA_MD2
;
121 minAlg
= maxAlg
= ALG_RSA_SHA1
;
124 minAlg
= maxAlg
= ALG_DSA_SHA1
;
127 minAlg
= maxAlg
= ALG_ANSI_ECDSA_SHA1
;
130 minAlg
= maxAlg
= ALG_ECDSA_SHA256
;
133 minAlg
= maxAlg
= ALG_ECDSA_SHA384
;
136 minAlg
= maxAlg
= ALG_ECDSA_SHA512
;
143 oloops
= atoi(&argp
[2]);
146 iloops
= atoi(&argp
[2]);
149 keySizeInBits
= atoi(&argp
[2]);
150 keySizeSpec
= CSSM_TRUE
;
156 bareCsp
= CSSM_FALSE
;
162 smallKeys
= CSSM_TRUE
;
165 pauseInterval
= atoi(&argp
[2]);;
173 ptext
.Data
= (uint8
*)CSSM_MALLOC(MAX_TEXT_SIZE
);
174 if(ptext
.Data
== NULL
) {
175 printf("Insufficient heap space\n");
178 /* ptext length set in inner test loop */
180 printf("Starting rawSig; args: ");
181 for(i
=1; i
<argc
; i
++) {
182 printf("%s ", argv
[i
]);
185 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
191 printf("Top of test; hit CR to proceed: ");
194 for(currAlg
=minAlg
; currAlg
<=maxAlg
; currAlg
++) {
195 /* get current algs */
198 digestAlg
= CSSM_ALGID_MD5
;
199 rawSigAlg
= CSSM_ALGID_RSA
;
200 sigAlg
= CSSM_ALGID_MD5WithRSA
;
201 keyGenAlg
= CSSM_ALGID_RSA
;
202 sigAlgStr
= "MD5WithRSA";
205 digestAlg
= CSSM_ALGID_MD2
;
206 rawSigAlg
= CSSM_ALGID_RSA
;
207 sigAlg
= CSSM_ALGID_MD2WithRSA
;
208 keyGenAlg
= CSSM_ALGID_RSA
;
209 sigAlgStr
= "MD2WithRSA";
212 digestAlg
= CSSM_ALGID_SHA1
;
213 rawSigAlg
= CSSM_ALGID_RSA
;
214 sigAlg
= CSSM_ALGID_SHA1WithRSA
;
215 keyGenAlg
= CSSM_ALGID_RSA
;
216 sigAlgStr
= "SHA1WithRSA";
219 digestAlg
= CSSM_ALGID_SHA1
;
220 rawSigAlg
= CSSM_ALGID_DSA
;
221 sigAlg
= CSSM_ALGID_SHA1WithDSA
;
222 keyGenAlg
= CSSM_ALGID_DSA
;
223 sigAlgStr
= "SHA1WithDSA";
226 digestAlg
= CSSM_ALGID_MD5
;
227 rawSigAlg
= CSSM_ALGID_FEE
;
228 sigAlg
= CSSM_ALGID_FEE_MD5
;
229 keyGenAlg
= CSSM_ALGID_FEE
;
230 sigAlgStr
= "MD5WithFEE";
233 digestAlg
= CSSM_ALGID_SHA1
;
234 rawSigAlg
= CSSM_ALGID_FEE
;
235 sigAlg
= CSSM_ALGID_FEE_SHA1
;
236 keyGenAlg
= CSSM_ALGID_FEE
;
237 sigAlgStr
= "SHA1WithFEE";
240 digestAlg
= CSSM_ALGID_SHA1
;
241 rawSigAlg
= CSSM_ALGID_ECDSA
;
242 sigAlg
= CSSM_ALGID_SHA1WithECDSA
;
243 keyGenAlg
= CSSM_ALGID_FEE
;
244 sigAlgStr
= "SHA1WithECDSA";
246 case ALG_ANSI_ECDSA_SHA1
:
247 digestAlg
= CSSM_ALGID_SHA1
;
248 rawSigAlg
= CSSM_ALGID_ECDSA
;
249 sigAlg
= CSSM_ALGID_SHA1WithECDSA
;
250 keyGenAlg
= CSSM_ALGID_ECDSA
;
251 sigAlgStr
= "ANSI ECDSA";
253 case ALG_ECDSA_SHA256
:
254 digestAlg
= CSSM_ALGID_SHA256
;
255 rawSigAlg
= CSSM_ALGID_ECDSA
;
256 sigAlg
= CSSM_ALGID_SHA256WithECDSA
;
257 keyGenAlg
= CSSM_ALGID_ECDSA
;
258 sigAlgStr
= "ECDSA/SHA256";
260 case ALG_ECDSA_SHA384
:
261 digestAlg
= CSSM_ALGID_SHA384
;
262 rawSigAlg
= CSSM_ALGID_ECDSA
;
263 sigAlg
= CSSM_ALGID_SHA384WithECDSA
;
264 keyGenAlg
= CSSM_ALGID_ECDSA
;
265 sigAlgStr
= "ECDSA/SHA384";
267 case ALG_ECDSA_SHA512
:
268 digestAlg
= CSSM_ALGID_SHA512
;
269 rawSigAlg
= CSSM_ALGID_ECDSA
;
270 sigAlg
= CSSM_ALGID_SHA512WithECDSA
;
271 keyGenAlg
= CSSM_ALGID_ECDSA
;
272 sigAlgStr
= "ECDSA/SHA512";
279 printf("Testing alg %s...\n", sigAlgStr
);
281 for(oloop
=0; ; oloop
++) {
285 keySizeInBits
= cspDefaultKeySize(keyGenAlg
);
287 else if(!keySizeSpec
) {
288 /* random key size */
289 keySizeInBits
= randKeySizeBits(rawSigAlg
, OT_Sign
);
293 if(verbose
|| ((oloop
% LOOP_NOTIFY
) == 0)) {
294 printf(" ...oloop %d keySize %u\n", oloop
, (unsigned)keySizeInBits
);
298 /* generate a key pair */
299 if(currAlg
== ALG_DSA_SHA1
) {
300 CSSM_BOOL doGenParams
;
302 if(bareCsp
|| CSPDL_DSA_GEN_PARAMS
) {
303 doGenParams
= CSSM_TRUE
;
306 /* CSPDL - no gen params */
307 doGenParams
= CSSM_FALSE
;
309 crtn
= cspGenDSAKeyPair(cspHand
,
314 CSSM_TRUE
, // all keys ref for speed
316 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
320 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
321 doGenParams
, // genParams
325 crtn
= cspGenKeyPair(cspHand
,
331 CSSM_TRUE
, // all keys ref for speed
333 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
337 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
338 CSSM_FALSE
); // genSeed not used
341 return testError(quiet
);
344 for(iloop
=0; iloop
<iloops
; iloop
++) {
345 CSSM_CC_HANDLE sigHand
;
347 /* new plaintext each inner loop */
348 simpleGenData(&ptext
, 1, MAX_TEXT_SIZE
);
350 if(verbose
|| ((iloop
% LOOP_NOTIFY
) == 0)) {
351 printf(" ...iloop %d text size %lu\n",
352 iloop
, ptext
.Length
);
359 crtn
= cspStagedSign(cspHand
,
363 CSSM_TRUE
, // multiUpdates
365 if(crtn
&& testError(quiet
)) {
370 crtn
= cspStagedDigest(cspHand
,
372 CSSM_FALSE
, // mallocDigest
373 CSSM_TRUE
, // multiUpdates
376 if(crtn
&& testError(quiet
)) {
380 /* raw verify of the digest */
381 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
387 printError("CSSM_CSP_CreateSignatureContext (1)", crtn
);
390 crtn
= CSSM_VerifyData(sigHand
,
396 printError("CSSM_VerifyData(raw)", crtn
);
397 if(testError(quiet
)) {
402 /* free resources - reuse the digest for raw sign */
403 appFreeCssmData(&sig
, CSSM_FALSE
);
404 CSSM_DeleteContext(sigHand
);
408 /* raw sign the digest */
409 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
415 printError("CSSM_CSP_CreateSignatureContext (2)", crtn
);
418 crtn
= CSSM_SignData(sigHand
,
424 printError("CSSM_SignData(raw)", crtn
);
425 if(testError(quiet
)) {
430 /* all-in-one verify */
431 crtn
= cspStagedSigVerify(cspHand
,
436 CSSM_TRUE
, // multiUpdates
438 if(crtn
&& testError(quiet
)) {
443 appFreeCssmData(&sig
, CSSM_FALSE
);
444 appFreeCssmData(&digest
, CSSM_FALSE
);
445 CSSM_DeleteContext(sigHand
);
446 } /* end of inner loop */
449 cspFreeKey(cspHand
, &pubKey
);
450 cspFreeKey(cspHand
, &privKey
);
452 if(oloops
&& (oloop
== oloops
)) {
455 if(pauseInterval
&& ((oloop
% pauseInterval
) == 0)) {
457 printf("hit CR to proceed: ");
464 cspShutdown(cspHand
, bareCsp
);
467 printf("ModuleDetach/Unload complete; hit CR to exit: ");
470 if((rtn
== 0) && !quiet
) {
471 printf("%s test complete\n", argv
[0]);
473 CSSM_FREE(ptext
.Data
);