2 * CSP symmetric encryption performance measurement tool
3 * Based on Michael Brouwer's cryptoPerformance.cpp; this one does not
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <security_cdsa_utilities/cssmdata.h>
9 #include <security_cdsa_utilities/cssmerrors.h>
22 #define ALG_DEFAULT CSSM_ALGID_AES
23 #define ALG_STR_DEFAULT "AES"
24 #define CHAIN_DEFAULT CSSM_TRUE
25 #define ALG_MODE_DEFAULT CSSM_ALGMODE_CBC_IV8
26 #define ALG_MODE_STR_DEFAULT "CBC"
27 #define KEY_SIZE_DEFAULT 128
28 #define BLOCK_SIZE_DEFAULT 16
30 static void usage(char **argv
)
32 printf("usage: %s iterations bufsize [options]\n", argv
[0]);
33 printf(" Options:\n");
34 printf(" a=algorithm (s=ASC; d=DES; 3=3DES; 2=RC2; 4=RC4; 5=RC5;\n");
35 printf(" a=AES; b=Blowfish; c=CAST; n=NULL; default=AES)\n");
36 printf(" k=keySizeInBits\n");
37 printf(" e (ECB mode; default is CBC)\n");
38 printf(" c (re-create context in each loop)\n");
39 printf(" v(erbose)\n");
45 CSSM_CSP_HANDLE cspHand
,
47 CSSM_BOOL resetContext
,
50 CSSM_ALGORITHMS encrAlg
,
51 CSSM_ENCRYPT_MODE encrMode
,
52 const CSSM_DATA
*ptext
, // pre-allocd
55 CSSM_CC_HANDLE ccHand
= 0;
56 char *someIv
= "some Initialization vector";
57 CSSM_DATA iv
= {blockSizeBytes
, (uint8
*)someIv
};
58 CSSM_DATA remData
= {0, NULL
};
62 for(unsigned dex
=0; dex
<iterations
; dex
++) {
64 /* always the first time, and each loop if resetContext */
65 crtn
= CSSM_CSP_CreateSymmetricContext(cspHand
,
75 cssmPerror("CSSM_CSP_CreateSymmetricContext", crtn
);
79 crtn
= CSSM_EncryptData(ccHand
, ptext
, 1,
80 ctext
, 1, &moved
, &remData
);
82 cssmPerror("CSSM_EncryptData", crtn
);
86 CSSM_DeleteContext(ccHand
);
91 CSSM_DeleteContext(ccHand
);
97 CSSM_CSP_HANDLE cspHand
,
99 CSSM_BOOL resetContext
,
101 uint8 blockSizeBytes
,
102 CSSM_ALGORITHMS encrAlg
,
103 CSSM_ENCRYPT_MODE encrMode
,
104 const CSSM_DATA
*ctext
, // pre-allocd
107 CSSM_CC_HANDLE ccHand
= 0;
108 char *someIv
= "some Initialization vector";
109 CSSM_DATA iv
= {blockSizeBytes
, (uint8
*)someIv
};
110 CSSM_DATA remData
= {0, NULL
};
114 for(unsigned dex
=0; dex
<iterations
; dex
++) {
116 /* always the first time, and each loop if resetContext */
117 crtn
= CSSM_CSP_CreateSymmetricContext(cspHand
,
127 cssmPerror("CSSM_CSP_CreateSymmetricContext", crtn
);
131 crtn
= CSSM_DecryptData(ccHand
, ctext
, 1,
132 rptext
, 1, &moved
, &remData
);
134 cssmPerror("CSSM_DecryptData", crtn
);
138 CSSM_DeleteContext(ccHand
);
142 CSSM_FreeKey(cspHand
, NULL
, symKey
, CSSM_FALSE
);
144 CSSM_DeleteContext(ccHand
);
149 int main(int argc
, char **argv
)
153 CSSM_ENCRYPT_MODE mode
= ALG_MODE_DEFAULT
;
154 char *modeStr
= ALG_MODE_STR_DEFAULT
;
155 uint32 blockSizeBytes
= BLOCK_SIZE_DEFAULT
;
160 CSSM_BOOL chainEnable
= CHAIN_DEFAULT
;
161 uint32 keySizeInBits
= KEY_SIZE_DEFAULT
;
162 char *algStr
= ALG_STR_DEFAULT
;
163 uint32 keyAlg
= ALG_DEFAULT
; // CSSM_ALGID_xxx of the key
164 uint32 encrAlg
= ALG_DEFAULT
; // CSSM_ALGID_xxx for encrypt
167 CSSM_BOOL resetContext
= CSSM_FALSE
;
168 CSSM_BOOL verbose
= false;
173 iterations
= atoi(argv
[1]);
174 bufSize
= atoi(argv
[2]);
175 for(arg
=3; arg
<argc
; arg
++) {
182 /* only set mode and modeStr if not default (CBC) */
185 encrAlg
= keyAlg
= CSSM_ALGID_ASC
;
187 mode
= CSSM_ALGMODE_NONE
;
192 encrAlg
= keyAlg
= CSSM_ALGID_DES
;
198 keyAlg
= CSSM_ALGID_3DES_3KEY
;
199 encrAlg
= CSSM_ALGID_3DES_3KEY_EDE
;
201 keySizeInBits
= 64 * 3;
205 encrAlg
= keyAlg
= CSSM_ALGID_RC2
;
210 encrAlg
= keyAlg
= CSSM_ALGID_RC4
;
212 /* not a block cipher */
213 chainEnable
= CSSM_FALSE
;
214 mode
= CSSM_ALGMODE_NONE
;
219 encrAlg
= keyAlg
= CSSM_ALGID_RC5
;
224 encrAlg
= keyAlg
= CSSM_ALGID_AES
;
229 encrAlg
= keyAlg
= CSSM_ALGID_BLOWFISH
;
234 encrAlg
= keyAlg
= CSSM_ALGID_CAST
;
239 encrAlg
= keyAlg
= CSSM_ALGID_NONE
;
248 keySizeInBits
= atoi(&argp
[2]);
251 chainEnable
= CSSM_FALSE
;
254 resetContext
= CSSM_TRUE
;
266 case CSSM_ALGMODE_CBC_IV8
:
267 mode
= CSSM_ALGMODE_ECB
;
270 case CSSM_ALGMODE_NONE
:
271 /* stream cipher, we weren't running CBC anyway */
277 printf("Algorithm: %s keySize: %u mode: %s iterations: %d "
279 algStr
, (unsigned)keySizeInBits
, modeStr
, iterations
, bufSize
);
281 CSSM_CSP_HANDLE cspHand
= cspStartup();
286 appSetupCssmData(&ptext
, bufSize
);
287 appSetupCssmData(&ctext
, bufSize
);
288 appSetupCssmData(&rptext
, bufSize
);
289 simpleGenData(&ptext
, ptext
.Length
, ptext
.Length
);
291 CSSM_KEY_PTR symKey
= cspGenSymKey(cspHand
, keyAlg
,
293 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
297 printf("***Error generating symmetric key\n");
301 CFAbsoluteTime start
, end
;
304 printf(" %d * cdsaEncrypt %d bytes", iterations
, bufSize
);
306 start
= CFAbsoluteTimeGetCurrent();
307 rtn
= doEncrypt(cspHand
, symKey
, resetContext
, iterations
,
308 blockSizeBytes
, encrAlg
, mode
, &ptext
, &ctext
);
309 end
= CFAbsoluteTimeGetCurrent();
313 printf(" took: %gs %.1f Kbytes/s\n", end
- start
,
314 (iterations
* bufSize
) / (end
- start
) / 1024.0);
316 printf(" %d * cdsaDecrypt %d bytes", iterations
, bufSize
);
318 start
= CFAbsoluteTimeGetCurrent();
319 rtn
= doDecrypt(cspHand
, symKey
, resetContext
, iterations
,
320 blockSizeBytes
, encrAlg
, mode
, &ctext
, &rptext
);
321 end
= CFAbsoluteTimeGetCurrent();
325 printf(" took: %gs %.1f Kbytes/s\n", end
- start
,
326 (iterations
* bufSize
) / (end
- start
) / 1024.0);