2 * sigPerform.c - measure performance of raw sign and verify
8 #include <Security/cssm.h>
9 #include <Security/cssmapple.h>
13 #include <CoreFoundation/CoreFoundation.h>
18 #define SIG_LOOPS_DEF 1000 /* sig loops */
19 #define KEYSIZE_DEF 512
20 #define PTEXT_SIZE 20 /* e.g., a SHA1 digest */
22 static void usage(char **argv
)
24 printf("usage: %s [options]\n", argv
[0]);
25 printf(" Options:\n");
26 printf(" a=algorithm (r=RSA; d=DSA; s=SHA1/RSA; f=FEE/SHA1; F=FEE/MD5; e=ECDSA;\n");
27 printf(" E=ECDSA/ANSI; default=RSA)\n");
28 printf(" l=numLoop (default=%d)\n", SIG_LOOPS_DEF
);
29 printf(" k=keySizeInBits; default=%d\n", KEYSIZE_DEF
);
30 printf(" D (CSP/DL; default = bare CSP)\n");
31 printf(" b (RSA blinding enabled)\n");
32 printf(" v(erbose)\n");
39 int main(int argc
, char **argv
)
43 CSSM_CSP_HANDLE cspHand
;
47 CSSM_DATA_PTR ptext
; // different for each sign/vfy
48 CSSM_DATA_PTR sig
; // ditto
51 CFAbsoluteTime start
, end
;
52 CSSM_CC_HANDLE sigHand
;
57 uint32 keySizeInBits
= KEYSIZE_DEF
;
58 unsigned sigLoops
= SIG_LOOPS_DEF
;
59 CSSM_BOOL verbose
= CSSM_FALSE
;
60 CSSM_BOOL quiet
= CSSM_FALSE
;
61 CSSM_BOOL bareCsp
= CSSM_TRUE
;
62 CSSM_ALGORITHMS sigAlg
= CSSM_ALGID_RSA
;
63 CSSM_ALGORITHMS keyAlg
= CSSM_ALGID_RSA
;
64 CSSM_ALGORITHMS digestAlg
= CSSM_ALGID_SHA1
;
65 CSSM_BOOL rsaBlinding
= CSSM_FALSE
;
67 for(arg
=1; arg
<argc
; arg
++) {
76 sigAlg
= keyAlg
= CSSM_ALGID_RSA
;
79 sigAlg
= keyAlg
= CSSM_ALGID_DSA
;
82 sigAlg
= CSSM_ALGID_SHA1WithRSA
;
83 keyAlg
= CSSM_ALGID_RSA
;
84 digestAlg
= CSSM_ALGID_NONE
;
87 sigAlg
= CSSM_ALGID_FEE_SHA1
;
88 keyAlg
= CSSM_ALGID_FEE
;
89 digestAlg
= CSSM_ALGID_NONE
;
92 sigAlg
= CSSM_ALGID_FEE_MD5
;
93 keyAlg
= CSSM_ALGID_FEE
;
94 digestAlg
= CSSM_ALGID_NONE
;
97 sigAlg
= CSSM_ALGID_SHA1WithECDSA
;
98 keyAlg
= CSSM_ALGID_FEE
;
99 digestAlg
= CSSM_ALGID_NONE
;
102 sigAlg
= CSSM_ALGID_SHA1WithECDSA
;
103 keyAlg
= CSSM_ALGID_ECDSA
;
104 digestAlg
= CSSM_ALGID_NONE
;
111 sigLoops
= atoi(&argp
[2]);
114 keySizeInBits
= atoi(&argp
[2]);
120 bareCsp
= CSSM_FALSE
;
123 rsaBlinding
= CSSM_TRUE
;
134 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
139 /* malloc sigLoops ptext and data structs and the data they contain */
140 ptext
= (CSSM_DATA_PTR
)CSSM_MALLOC(sigLoops
* sizeof(CSSM_DATA
));
141 sig
= (CSSM_DATA_PTR
)CSSM_MALLOC(sigLoops
* sizeof(CSSM_DATA
));
142 memset(ptext
, 0, sigLoops
* sizeof(CSSM_DATA
));
143 memset(sig
, 0, sigLoops
* sizeof(CSSM_DATA
));
144 sigSize
= (keySizeInBits
+ 7) / 8;
145 if(sigAlg
!= CSSM_ALGID_RSA
) {
148 for(i
=0; i
<sigLoops
; i
++) {
149 appSetupCssmData(&ptext
[i
], PTEXT_SIZE
);
150 appSetupCssmData(&sig
[i
], sigSize
);
153 /* generate random "digests" */
154 for(i
=0; i
<sigLoops
; i
++) {
155 simpleGenData(&ptext
[i
], PTEXT_SIZE
, PTEXT_SIZE
);
158 printf("Generating keys....\n");
159 crtn
= cspGenKeyPair(cspHand
,
165 CSSM_TRUE
, // reference key for speed
167 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
171 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
172 CSSM_FALSE
); // genSeed not used
174 return testError(quiet
);
177 printf("Signing....\n");
179 /* set up a reusable signature context */
180 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
186 printError("CSSM_CSP_CreateSignatureContext (1)", crtn
);
190 CSSM_CONTEXT_ATTRIBUTE newAttr
;
191 newAttr
.AttributeType
= CSSM_ATTRIBUTE_RSA_BLINDING
;
192 newAttr
.AttributeLength
= sizeof(uint32
);
193 newAttr
.Attribute
.Uint32
= 1;
194 crtn
= CSSM_UpdateContextAttributes(sigHand
, 1, &newAttr
);
196 printError("CSSM_UpdateContextAttributes", crtn
);
201 /* go - critical signing loop */
202 start
= CFAbsoluteTimeGetCurrent();
203 for(i
=0; i
<sigLoops
; i
++) {
204 crtn
= CSSM_SignData(sigHand
,
210 printError("CSSM_SignData", crtn
);
214 end
= CFAbsoluteTimeGetCurrent();
215 printf("%d sign ops in %f seconds, %f ms/op\n", sigLoops
, end
-start
,
216 ((end
- start
) * 1000.0) / sigLoops
);
218 CSSM_DeleteContext(sigHand
);
220 /* set up a reusable signature context */
221 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
227 printError("CSSM_CSP_CreateSignatureContext (2)", crtn
);
231 /* go - critical verifying loop */
232 start
= CFAbsoluteTimeGetCurrent();
233 for(i
=0; i
<sigLoops
; i
++) {
234 crtn
= CSSM_VerifyData(sigHand
,
240 printError("CSSM_VerifyData", crtn
);
244 end
= CFAbsoluteTimeGetCurrent();
245 printf("%d vfy ops in %f seconds, %f ms/op\n", sigLoops
, end
-start
,
246 ((end
- start
) * 1000.0) / sigLoops
);
247 CSSM_DeleteContext(sigHand
);
249 cspFreeKey(cspHand
, &privKey
);
250 cspFreeKey(cspHand
, &pubKey
);
251 for(i
=0; i
<sigLoops
; i
++) {
252 appFreeCssmData(&ptext
[i
], CSSM_FALSE
);
253 appFreeCssmData(&sig
[i
], CSSM_FALSE
);
257 cspShutdown(cspHand
, bareCsp
);