]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/sha2Time/sha2Time.cpp
2 * Measure performance of SHA using raw CommonCrypto digests.
3 * Use hashTime to measure performance thru the CSP.
9 #include <CommonCrypto/CommonDigest.h>
13 static void usage(char **argv
)
15 printf("Usage: %s bytecount [q(uiet)]\n", argv
[0]);
19 #define BUFSIZE (8 * 1024)
21 int main(int argc
, char **argv
)
29 byteCount
= atoi(argv
[1]);
30 for(int arg
=2; arg
<argc
; arg
++) {
31 switch(argv
[arg
][0]) {
40 unsigned char *text
= (unsigned char *)malloc(BUFSIZE
);
41 appGetRandomBytes(text
, BUFSIZE
);
43 unsigned toMove
= byteCount
;
49 printf("...testing SHA1\n");
53 unsigned char dig1
[CC_SHA1_DIGEST_LENGTH
];
57 /* start critical timing loop */
58 startTime
= CPUTimeRead();
60 if(toMove
> BUFSIZE
) {
67 CC_SHA1_Update(&ctx1
, text
, thisMove
);
69 CC_SHA1_Final(dig1
, &ctx1
);
70 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
71 printf("SHA1: Digest %u bytes : %.2f ms\n", byteCount
, timeSpentMs
);
75 printf("...testing SHA256\n");
79 unsigned char dig256
[CC_SHA256_DIGEST_LENGTH
];
81 CC_SHA256_Init(&ctx256
);
83 /* start critical timing loop */
84 startTime
= CPUTimeRead();
86 if(toMove
> BUFSIZE
) {
93 CC_SHA256_Update(&ctx256
, text
, thisMove
);
95 CC_SHA256_Final(dig256
, &ctx256
);
96 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
97 printf("SHA256: Digest %u bytes : %.2f ms\n", byteCount
, timeSpentMs
);
101 printf("...testing SHA512\n");
104 CC_SHA512_CTX ctx512
;
105 unsigned char dig512
[CC_SHA512_DIGEST_LENGTH
];
107 CC_SHA512_Init(&ctx512
);
109 /* start critical timing loop */
110 startTime
= CPUTimeRead();
112 if(toMove
> BUFSIZE
) {
119 CC_SHA512_Update(&ctx512
, text
, thisMove
);
121 CC_SHA512_Final(dig512
, &ctx512
);
122 timeSpentMs
= CPUTimeDeltaMs(startTime
, CPUTimeRead());
123 printf("SHA512: Digest %u bytes : %.2f ms\n", byteCount
, timeSpentMs
);