]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/sigPerform/sigPerform.c
Security-57031.10.10.tar.gz
[apple/security.git] / SecurityTests / cspxutils / sigPerform / sigPerform.c
1 /*
2 * sigPerform.c - measure performance of raw sign and verify
3 */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <time.h>
8 #include <Security/cssm.h>
9 #include <Security/cssmapple.h>
10 #include "cspwrap.h"
11 #include "common.h"
12 #include <string.h>
13 #include <CoreFoundation/CoreFoundation.h>
14
15 /*
16 * Defaults.
17 */
18 #define SIG_LOOPS_DEF 1000 /* sig loops */
19 #define KEYSIZE_DEF 512
20 #define PTEXT_SIZE 20 /* e.g., a SHA1 digest */
21
22 static void usage(char **argv)
23 {
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");
33 printf(" q(uiet)\n");
34 printf(" h(elp)\n");
35 exit(1);
36 }
37
38
39 int main(int argc, char **argv)
40 {
41 int arg;
42 char *argp;
43 CSSM_CSP_HANDLE cspHand;
44 unsigned i;
45 CSSM_KEY pubKey;
46 CSSM_KEY privKey;
47 CSSM_DATA_PTR ptext; // different for each sign/vfy
48 CSSM_DATA_PTR sig; // ditto
49 unsigned sigSize;
50 CSSM_RETURN crtn;
51 CFAbsoluteTime start, end;
52 CSSM_CC_HANDLE sigHand;
53
54 /*
55 * User-spec'd params
56 */
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;
66
67 for(arg=1; arg<argc; arg++) {
68 argp = argv[arg];
69 switch(argp[0]) {
70 case 'a':
71 if(argp[1] != '=') {
72 usage(argv);
73 }
74 switch(argp[2]) {
75 case 'r':
76 sigAlg = keyAlg = CSSM_ALGID_RSA;
77 break;
78 case 'd':
79 sigAlg = keyAlg = CSSM_ALGID_DSA;
80 break;
81 case 's':
82 sigAlg = CSSM_ALGID_SHA1WithRSA;
83 keyAlg = CSSM_ALGID_RSA;
84 digestAlg = CSSM_ALGID_NONE;
85 break;
86 case 'f':
87 sigAlg = CSSM_ALGID_FEE_SHA1;
88 keyAlg = CSSM_ALGID_FEE;
89 digestAlg = CSSM_ALGID_NONE;
90 break;
91 case 'F':
92 sigAlg = CSSM_ALGID_FEE_MD5;
93 keyAlg = CSSM_ALGID_FEE;
94 digestAlg = CSSM_ALGID_NONE;
95 break;
96 case 'e':
97 sigAlg = CSSM_ALGID_SHA1WithECDSA;
98 keyAlg = CSSM_ALGID_FEE;
99 digestAlg = CSSM_ALGID_NONE;
100 break;
101 case 'E':
102 sigAlg = CSSM_ALGID_SHA1WithECDSA;
103 keyAlg = CSSM_ALGID_ECDSA;
104 digestAlg = CSSM_ALGID_NONE;
105 break;
106 default:
107 usage(argv);
108 }
109 break;
110 case 'l':
111 sigLoops = atoi(&argp[2]);
112 break;
113 case 'k':
114 keySizeInBits = atoi(&argp[2]);
115 break;
116 case 'v':
117 verbose = CSSM_TRUE;
118 break;
119 case 'D':
120 bareCsp = CSSM_FALSE;
121 break;
122 case 'b':
123 rsaBlinding = CSSM_TRUE;
124 break;
125 case 'q':
126 quiet = CSSM_TRUE;
127 break;
128 case 'h':
129 default:
130 usage(argv);
131 }
132 }
133
134 cspHand = cspDlDbStartup(bareCsp, NULL);
135 if(cspHand == 0) {
136 exit(1);
137 }
138
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) {
146 sigSize *= 3;
147 }
148 for(i=0; i<sigLoops; i++) {
149 appSetupCssmData(&ptext[i], PTEXT_SIZE);
150 appSetupCssmData(&sig[i], sigSize);
151 }
152
153 /* generate random "digests" */
154 for(i=0; i<sigLoops; i++) {
155 simpleGenData(&ptext[i], PTEXT_SIZE, PTEXT_SIZE);
156 }
157
158 printf("Generating keys....\n");
159 crtn = cspGenKeyPair(cspHand,
160 keyAlg,
161 "foo",
162 3,
163 keySizeInBits,
164 &pubKey,
165 CSSM_TRUE, // reference key for speed
166 CSSM_KEYUSE_VERIFY,
167 CSSM_KEYBLOB_RAW_FORMAT_NONE,
168 &privKey,
169 CSSM_TRUE,
170 CSSM_KEYUSE_SIGN,
171 CSSM_KEYBLOB_RAW_FORMAT_NONE,
172 CSSM_FALSE); // genSeed not used
173 if(crtn) {
174 return testError(quiet);
175 }
176
177 printf("Signing....\n");
178
179 /* set up a reusable signature context */
180 crtn = CSSM_CSP_CreateSignatureContext(cspHand,
181 sigAlg,
182 NULL, // passPhrase
183 &privKey,
184 &sigHand);
185 if(crtn) {
186 printError("CSSM_CSP_CreateSignatureContext (1)", crtn);
187 return 1;
188 }
189 if(rsaBlinding) {
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);
195 if(crtn) {
196 printError("CSSM_UpdateContextAttributes", crtn);
197 return crtn;
198 }
199 }
200
201 /* go - critical signing loop */
202 start = CFAbsoluteTimeGetCurrent();
203 for(i=0; i<sigLoops; i++) {
204 crtn = CSSM_SignData(sigHand,
205 &ptext[i],
206 1,
207 digestAlg,
208 &sig[i]);
209 if(crtn) {
210 printError("CSSM_SignData", crtn);
211 return 1;
212 }
213 }
214 end = CFAbsoluteTimeGetCurrent();
215 printf("%d sign ops in %f seconds, %f ms/op\n", sigLoops, end-start,
216 ((end - start) * 1000.0) / sigLoops);
217
218 CSSM_DeleteContext(sigHand);
219
220 /* set up a reusable signature context */
221 crtn = CSSM_CSP_CreateSignatureContext(cspHand,
222 sigAlg,
223 NULL, // passPhrase
224 &pubKey,
225 &sigHand);
226 if(crtn) {
227 printError("CSSM_CSP_CreateSignatureContext (2)", crtn);
228 return 1;
229 }
230
231 /* go - critical verifying loop */
232 start = CFAbsoluteTimeGetCurrent();
233 for(i=0; i<sigLoops; i++) {
234 crtn = CSSM_VerifyData(sigHand,
235 &ptext[i],
236 1,
237 digestAlg,
238 &sig[i]);
239 if(crtn) {
240 printError("CSSM_VerifyData", crtn);
241 return 1;
242 }
243 }
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);
248
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);
254 }
255 CSSM_FREE(ptext);
256 CSSM_FREE(sig);
257 cspShutdown(cspHand, bareCsp);
258 return 0;
259 }
260
261