]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/hashTimeLibCrypt/hashTimeLibCrypt.cpp
2 * hashTimeLibCrypt.cpp - measure performance of libcrypt digest ops.
4 * Thjis is obsolete; hashTime does this a lot better,a dn it also measures raw
5 * CommonCrypto and CryptKit versions.
8 #include <openssl/sha.h>
9 #include <openssl/md5.h>
16 #include "pbkdDigest.h"
18 /* enumerate digest algorithms our way */
25 #define FIRST_ALG HA_MD5
26 #define LAST_ALG HA_SHA1
28 static void usage(char **argv
)
30 printf("Usage: %s [option ...]\n", argv
[0]);
32 printf(" t=testspec; default=all\n");
33 printf(" test specs: c digest context setup/teardown\n");
34 printf(" b basic single block digest\n");
35 printf(" d digest lots of data\n");
36 printf(" l=loops (only valid if testspec is given)\n");
40 /* passed to each test */
46 /* just digest context setup/teardown */
47 /* returns nonzero on error */
48 static int hashContext(
57 startTime
= CPUTimeRead();
58 for(loop
=0; loop
<params
->loops
; loop
++) {
59 rtn
= DigestCtxInit(&ctx
, params
->isSha
);
64 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
66 printf(" context setup/delete : %u ops in %.2f ms; %f ms/op\n",
67 params
->loops
, timeSpentMs
, timeSpentMs
/ (double)params
->loops
);
71 /* Minimal init/digest/final */
72 #define BASIC_BLOCK_SIZE 64 // to digest in bytes
73 #define MAX_DIGEST_SIZE 20 // we provide, no malloc below CSSM
81 uint8 ptext
[BASIC_BLOCK_SIZE
];
82 uint8 digest
[MAX_DIGEST_SIZE
];
86 /* random data, const thru the loops */
87 appGetRandomBytes(ptext
, BASIC_BLOCK_SIZE
);
89 /* start critical timing loop */
90 startTime
= CPUTimeRead();
91 for(loop
=0; loop
<params
->loops
; loop
++) {
92 rtn
= DigestCtxInit(&ctx
, params
->isSha
);
96 rtn
= DigestCtxUpdate(&ctx
, ptext
, BASIC_BLOCK_SIZE
);
100 rtn
= DigestCtxFinal(&ctx
, digest
);
106 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
107 printf(" Digest one %u byte block : %u ops in %.2f ms; %f ms/op\n",
108 BASIC_BLOCK_SIZE
, params
->loops
,
109 timeSpentMs
, timeSpentMs
/ (double)params
->loops
);
114 #define PTEXT_SIZE 1000 // to digest in bytes
115 #define INNER_LOOPS 1000
117 static int hashDataRate(
123 double timeSpent
, timeSpentMs
;
124 uint8 ptext
[PTEXT_SIZE
];
125 uint8 digest
[MAX_DIGEST_SIZE
];
129 /* random data, const thru the loops */
130 appGetRandomBytes(ptext
, PTEXT_SIZE
);
132 /* start critical timing loop */
133 startTime
= CPUTimeRead();
134 for(loop
=0; loop
<params
->loops
; loop
++) {
135 rtn
= DigestCtxInit(&ctx
, params
->isSha
);
139 for(iloop
=0; iloop
<INNER_LOOPS
; iloop
++) {
140 rtn
= DigestCtxUpdate(&ctx
, ptext
, PTEXT_SIZE
);
145 rtn
= DigestCtxFinal(&ctx
, digest
);
150 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
151 timeSpent
= timeSpentMs
/ 1000.0;
153 float bytesPerLoop
= INNER_LOOPS
* PTEXT_SIZE
;
154 float totalBytes
= params
->loops
* bytesPerLoop
;
156 /* careful, KByte = 1024, ms = 1/1000 */
157 printf(" Digest %.0f bytes : %u ops in %.2f ms; %f ms/op, %.0f KBytes/s\n",
158 bytesPerLoop
, params
->loops
,
159 timeSpentMs
, timeSpentMs
/ (double)params
->loops
,
160 ((float)totalBytes
/ 1024.0) / timeSpent
);
165 typedef int (*testRunFcn
)(TestParams
*testParams
);
168 * Static declaration of a test
171 const char *testName
;
174 char testSpec
; // for t=xxx cmd line opt
177 static TestDefs testDefs
[] =
179 { "Digest context setup/teardown",
184 { "Basic single block digest",
189 { "Large data digest",
196 static void algToAlgId(
211 printf("***algToAlgId screwup\n");
216 #define NUM_TESTS (sizeof(testDefs) / sizeof(testDefs[0]))
218 int main(int argc
, char **argv
)
220 TestParams testParams
;
225 unsigned cmdLoops
= 0; // can be specified in cmd line
226 // if not, use TestDefs.loops
227 char testSpec
= '\0'; // allows specification of one test
232 for(arg
=1; arg
<argc
; arg
++) {
239 cmdLoops
= atoi(&argp
[2]);
246 for(unsigned testNum
=0; testNum
<NUM_TESTS
; testNum
++) {
247 testDef
= &testDefs
[testNum
];
249 if(testSpec
&& (testDef
->testSpec
!= testSpec
)) {
252 printf("%s:\n", testDef
->testName
);
255 testParams
.loops
= cmdLoops
;
259 testParams
.loops
= testDef
->loops
;
261 for(alg
=FIRST_ALG
; alg
<=LAST_ALG
; alg
++) {
262 algToAlgId(alg
, &testParams
.isSha
, &algStr
);
263 printf(" === %s ===\n", algStr
);
264 rtn
= testDef
->run(&testParams
);
266 printf("Test returned error\n");