]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/ckutils/sigTime/sigTime.cpp
2 * atomTime.c - measure performance of digital signature primitives (not incluing
7 #include "ckutilsPlatform.h"
8 #include "CryptKitSA.h"
9 #include "curveParams.h"
15 #define SIGN_LOOPS_DEF 100
16 #define VFY_LOOPS_DEF 100
17 #define PRIV_KEY_SIZE_BYTES 32
18 #define DIGEST_SIZE_BYTES 20 /* e.g., SHA1 */
21 static void usage(char **argv
)
23 printf("Usage: %s [option...]\n", argv
[0]);
25 printf(" s=signLoops -- default %d\n", SIGN_LOOPS_DEF
);
26 printf(" v=verifyLoops -- default %d\n", VFY_LOOPS_DEF
);
27 printf(" D=depth -- default is ALL\n");
37 * Fill numDatas with random data of length bits. Caller has mallocd referents.
39 static void genRandData(FeeData
*datas
,
46 for(i
=0; i
<numDatas
; i
++) {
48 fd
->length
= numBytes
;
49 feeRandBytes(rand
, fd
->data
, numBytes
);
54 static void mallocData(
58 fd
->data
= (unsigned char *)fmalloc(numBytes
);
59 fd
->length
= numBytes
;
62 /* common random callback */
63 feeReturn
randCallback(
68 feeRand frand
= (feeRand
)ref
;
69 feeRandBytes(frand
, bytes
, numBytes
);
73 int main(int argc
, char **argv
)
77 unsigned sigLoops
= SIGN_LOOPS_DEF
;
78 unsigned vfyLoops
= VFY_LOOPS_DEF
;
79 unsigned numKeys
= NUM_KEYS
; // might be less for very small loops
83 feePubKey keys
[NUM_KEYS
];
84 /* sigLoops copies of each of {digestData, sigData} */
94 unsigned minDepth
= 0;
95 unsigned maxDepth
= FEE_DEPTH_MAX
;
96 unsigned basePrimeLen
;
100 for(arg
=1; arg
<argc
; arg
++) {
104 sigLoops
= atoi(&argp
[2]);
107 vfyLoops
= atoi(&argp
[2]);
110 minDepth
= maxDepth
= atoi(&argp
[2]);
119 * Common random generator
121 time((time_t *)&seed
);
122 rand
= feeRandAllocWithSeed(seed
);
124 if(numKeys
> sigLoops
) {
127 digestData
= (FeeData
*)fmalloc(sizeof(FeeData
) * sigLoops
);
128 sigData
= (FeeData
*)fmalloc(sizeof(FeeData
) * sigLoops
);
130 /* alloc the data, once, for largest private key or "digest" we'll use */
131 for(i
=0; i
<sigLoops
; i
++) {
132 mallocData(&digestData
[i
], PRIV_KEY_SIZE_BYTES
);
134 for(depth
=minDepth
; depth
<=maxDepth
; depth
++) {
137 * Get curve params for this depth
139 cp
= curveParamsForDepth(depth
);
141 printf("malloc failure\n");
144 switch(cp
->curveType
) {
146 curveType
= "FCT_Montgomery";
148 case FCT_Weierstrass
:
149 curveType
= "FCT_Weierstrass";
152 curveType
= "FCT_General";
155 printf("***Unknown curveType!\n");
159 switch(cp
->primeType
) {
161 printf("depth=%d; FPT_General, %s; keysize=%d;\n",
162 depth
, curveType
, bitlen(cp
->basePrime
));
165 printf("depth=%d; FPT_Mersenne, %s; q=%d\n",
166 depth
, curveType
, cp
->q
);
169 printf("depth=%d; FPT_FEE, %s; q=%d k=%d\n",
170 depth
, curveType
, cp
->q
, cp
->k
);
173 basePrimeLen
= bitlen(cp
->basePrime
);
175 /* one set of random data as private keys */
176 unsigned privSize
= (basePrimeLen
+ 8) / 8;
177 genRandData(digestData
, numKeys
, privSize
, rand
);
179 /* generate the keys (no hash - we've got that covered) */
180 for(i
=0; i
<numKeys
; i
++) {
181 keys
[i
] = feePubKeyAlloc();
182 feePubKeyInitFromPrivDataDepth(keys
[i
], digestData
[i
].data
, privSize
,
186 /* now different data to actually sign */
187 genRandData(digestData
, sigLoops
, DIGEST_SIZE_BYTES
, rand
);
192 PLAT_GET_TIME(startTime
);
193 for(i
=0; i
<sigLoops
; i
++) {
194 FeeData
*digst
= &digestData
[i
];
195 FeeData
*sig
= &sigData
[i
];
196 feePubKey fkey
= keys
[i
% numKeys
];
198 feeSig fs
= feeSigNewWithKey(fkey
, randCallback
, rand
);
199 frtn
= feeSigSign(fs
, digst
->data
, digst
->length
, fkey
);
201 printf("***Error %d on feeSigSign\n", (int)frtn
);
204 frtn
= feeSigData(fs
, &sig
->data
, &sig
->length
);
206 printf("***Error %d on feeSigData\n", (int)frtn
);
211 PLAT_GET_TIME(endTime
);
212 elapsed
= PLAT_GET_US(startTime
, endTime
);
213 printf(" sign: %12.2f us per op\n",
217 * verify - might be doing more of these than we have
218 * valid signatures.....
221 PLAT_GET_TIME(startTime
);
222 for(i
=0; i
<vfyLoops
; i
++) {
223 FeeData
*digst
= &digestData
[dex
];
224 FeeData
*sig
= &sigData
[dex
];
225 feePubKey fkey
= keys
[dex
% numKeys
];
228 frtn
= feeSigParse(sig
->data
, sig
->length
, &fs
);
230 printf("***Error %d on feeSigParse\n", (int)frtn
);
233 frtn
= feeSigVerify(fs
, digst
->data
, digst
->length
, fkey
);
235 printf("***Error %d on feeSigVerify\n", (int)frtn
);
240 if(dex
== sigLoops
) {
241 /* that's all the data we have, recycle */
245 PLAT_GET_TIME(endTime
);
246 elapsed
= PLAT_GET_US(startTime
, endTime
);
247 printf(" verify: %12.2f us per op\n",
251 /* possibly limited number of signatures.... */
252 for(i
=0; i
<sigLoops
; i
++) {
253 ffree(sigData
[i
].data
); // mallocd by feeSigData()
255 for(i
=0; i
<numKeys
; i
++) {
256 feePubKeyFree(keys
[i
]);
261 for(i
=0; i
<sigLoops
; i
++) {
262 ffree(digestData
[i
].data
);