]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/ccPerform/ccPerform.cpp
2 * ccPerform.cpp - measure performance of CommonCrypto encryption
9 #include <CoreFoundation/CoreFoundation.h>
10 #include <CommonCrypto/CommonCryptor.h>
12 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
14 #define LOOPS_DEF 1000
15 #define BUFSIZE_DEF 10240
16 #define MAX_KEY_SIZE kCCKeySizeMaxRC4 /* bytes */
20 * Enumerate algs our own way to allow iteration.
31 #define ALG_FIRST ALG_AES_128
32 #define ALG_LAST ALG_RC4
34 static void usage(char **argv
)
36 printf("usage: %s [options]\n", argv
[0]);
38 printf(" -a alg -- alg : d=DES; 3=3DES; a=AES128; n=AES192; A=AES256;\n");
39 printf(" c=CAST; 4=RC4; default=all\n");
40 printf(" -l loops -- loops; default %u\n", LOOPS_DEF
);
41 printf(" -b bufsize -- buffer size; default %u\n", BUFSIZE_DEF
);
42 printf(" -e -- ECB mode; default is CBC\n");
47 static void printCCError(const char *str
, OSStatus ortn
)
49 printf("***%s returned %ld\n", str
, (long)ortn
);
52 int main(int argc
, char **argv
)
54 unsigned loops
= LOOPS_DEF
;
55 unsigned bufSize
= BUFSIZE_DEF
;
56 unsigned algFirst
= ALG_FIRST
;
57 unsigned algLast
= ALG_LAST
;
61 while ((arg
= getopt(argc
, argv
, "a:l:b:eh")) != -1) {
66 algFirst
= algLast
= ALG_AES_128
;
69 algFirst
= algLast
= ALG_AES_192
;
72 algFirst
= algLast
= ALG_AES_256
;
75 algFirst
= algLast
= ALG_DES
;
78 algFirst
= algLast
= ALG_3DES
;
81 algFirst
= algLast
= ALG_CAST
;
83 algFirst
= algLast
= ALG_RC4
;
90 bufSize
= atoi(optarg
);
104 * encrypt and decrypt on workBuf
105 * save original ptext in saveBuf, compare at end as sanity check
108 unsigned char *workBuf
= (unsigned char *)malloc(bufSize
);
109 unsigned char *saveBuf
= (unsigned char *)malloc(bufSize
);
110 if((workBuf
== NULL
) || (saveBuf
== NULL
)) {
111 printf("***malloc failure\n");
114 appGetRandomBytes(workBuf
, bufSize
);
115 memmove(saveBuf
, workBuf
, bufSize
);
117 uint8_t keyBytes
[MAX_KEY_SIZE
];
120 appGetRandomBytes(keyBytes
, MAX_KEY_SIZE
);
122 CCCryptorRef cryptor
;
124 CCOptions options
= 0;
128 options
|= kCCOptionECBMode
;
132 for(currAlg
=algFirst
; currAlg
<=algLast
; currAlg
++) {
133 const char *algStr
= NULL
;
137 keyLength
= kCCKeySizeDES
;
138 alg
= kCCAlgorithmDES
;
142 keyLength
= kCCKeySize3DES
;
143 alg
= kCCAlgorithm3DES
;
147 keyLength
= kCCKeySizeAES128
;
148 alg
= kCCAlgorithmAES128
;
152 keyLength
= kCCKeySizeAES192
;
153 alg
= kCCAlgorithmAES128
;
157 keyLength
= kCCKeySizeAES256
;
158 alg
= kCCAlgorithmAES128
;
162 keyLength
= kCCKeySizeMaxCAST
;
163 alg
= kCCAlgorithmCAST
;
167 keyLength
= kCCKeySizeMaxRC4
;
168 alg
= kCCAlgorithmRC4
;
173 printf("Algorithm: %s keySize: %u mode: %s loops: %u bufSize: %u\n",
174 algStr
, (unsigned)keyLength
, ecbMode
? "ECB" : "CBC",
175 (unsigned)loops
, (unsigned)bufSize
);
177 CFAbsoluteTime start
, end
;
182 start
= CFAbsoluteTimeGetCurrent();
184 ortn
= CCCryptorCreate(kCCEncrypt
, alg
, options
,
185 keyBytes
, keyLength
, NULL
, &cryptor
);
187 printCCError("CCCryptorCreate", ortn
);
191 for(loop
=0; loop
<loops
; loop
++) {
192 ortn
= CCCryptorUpdate(cryptor
, workBuf
, bufSize
,
193 workBuf
, bufSize
, &thisMoved
);
195 printCCError("CCCryptorUpdate", ortn
);
199 /* no padding, CCCryptFinal not needed */
200 end
= CFAbsoluteTimeGetCurrent();
202 printf(" encrypt %u * %u bytes took %gs: %g KBytes/s\n",
203 (unsigned)loops
, (unsigned)bufSize
,
205 (loops
* bufSize
) / (end
- start
) / 1024.0);
208 start
= CFAbsoluteTimeGetCurrent();
210 ortn
= CCCryptorCreate(kCCDecrypt
, alg
, options
,
211 keyBytes
, keyLength
, NULL
, &cryptor
);
213 printCCError("CCCryptorCreate", ortn
);
217 for(loop
=0; loop
<loops
; loop
++) {
218 ortn
= CCCryptorUpdate(cryptor
, workBuf
, bufSize
,
219 workBuf
, bufSize
, &thisMoved
);
221 printCCError("CCCryptorUpdate", ortn
);
225 /* no padding, CCCryptFinal not needed */
226 end
= CFAbsoluteTimeGetCurrent();
228 printf(" decrypt %u * %u bytes took %gs: %g KBytes/s\n",
229 (unsigned)loops
, (unsigned)bufSize
,
231 (loops
* bufSize
) / (end
- start
) / 1024.0);
234 if(memcmp(workBuf
, saveBuf
, bufSize
)) {
235 printf("***plaintext miscompare!\n");