X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/cspxutils/sha2Time/sha2Time.cpp diff --git a/SecurityTests/cspxutils/sha2Time/sha2Time.cpp b/SecurityTests/cspxutils/sha2Time/sha2Time.cpp new file mode 100644 index 00000000..6b9f6bb5 --- /dev/null +++ b/SecurityTests/cspxutils/sha2Time/sha2Time.cpp @@ -0,0 +1,126 @@ +/* + * Measure performance of SHA using raw CommonCrypto digests. + * Use hashTime to measure performance thru the CSP. + */ + +#include +#include +#include +#include +#include "cputime.h" +#include "common.h" + +static void usage(char **argv) +{ + printf("Usage: %s bytecount [q(uiet)]\n", argv[0]); + exit(1); +} + +#define BUFSIZE (8 * 1024) + +int main(int argc, char **argv) +{ + bool quiet = false; + unsigned byteCount; + + if(argc < 2) { + usage(argv); + } + byteCount = atoi(argv[1]); + for(int arg=2; arg BUFSIZE) { + thisMove = BUFSIZE; + } + else { + thisMove = toMove; + } + toMove -= thisMove; + CC_SHA1_Update(&ctx1, text, thisMove); + } while(toMove); + CC_SHA1_Final(dig1, &ctx1); + timeSpentMs = CPUTimeDeltaMs(startTime, CPUTimeRead()); + printf("SHA1: Digest %u bytes : %.2f ms\n", byteCount, timeSpentMs); + + /* SHA256 */ + if(!quiet) { + printf("...testing SHA256\n"); + } + + CC_SHA256_CTX ctx256; + unsigned char dig256[CC_SHA256_DIGEST_LENGTH]; + toMove = byteCount; + CC_SHA256_Init(&ctx256); + + /* start critical timing loop */ + startTime = CPUTimeRead(); + do { + if(toMove > BUFSIZE) { + thisMove = BUFSIZE; + } + else { + thisMove = toMove; + } + toMove -= thisMove; + CC_SHA256_Update(&ctx256, text, thisMove); + } while(toMove); + CC_SHA256_Final(dig256, &ctx256); + timeSpentMs = CPUTimeDeltaMs(startTime, CPUTimeRead()); + printf("SHA256: Digest %u bytes : %.2f ms\n", byteCount, timeSpentMs); + + /* SHA256 */ + if(!quiet) { + printf("...testing SHA512\n"); + } + + CC_SHA512_CTX ctx512; + unsigned char dig512[CC_SHA512_DIGEST_LENGTH]; + toMove = byteCount; + CC_SHA512_Init(&ctx512); + + /* start critical timing loop */ + startTime = CPUTimeRead(); + do { + if(toMove > BUFSIZE) { + thisMove = BUFSIZE; + } + else { + thisMove = toMove; + } + toMove -= thisMove; + CC_SHA512_Update(&ctx512, text, thisMove); + } while(toMove); + CC_SHA512_Final(dig512, &ctx512); + timeSpentMs = CPUTimeDeltaMs(startTime, CPUTimeRead()); + printf("SHA512: Digest %u bytes : %.2f ms\n", byteCount, timeSpentMs); + + return 0; +}