2 * Simple symmetric encrypt/decrypt test
4 #include "testParams.h"
5 #include <Security/cssm.h>
9 #include <security_cdsa_utils/cuFileIo.h>
10 #include <utilLib/common.h>
11 #include <utilLib/cspwrap.h>
14 /* for memory leak debug only, with only one thread running */
17 #define PTEXT_SIZE 1024
18 #define USAGE_DEF "noUsage"
20 int symTestInit(TestParams
*testParams
)
26 int symTest(TestParams
*testParams
)
30 CSSM_DATA ptext
= {0, NULL
};
31 CSSM_DATA ctext
= {0, NULL
};
32 CSSM_DATA rptext
= {0, NULL
};
35 CSSM_ALGORITHMS keyAlg
;
36 CSSM_ALGORITHMS encrAlg
;
37 CSSM_ENCRYPT_MODE encrMode
;
41 initVector
.Data
= (uint8
*)"someStrangeInitVect";
42 ptext
.Data
= (uint8
*)CSSM_MALLOC(PTEXT_SIZE
);
43 ptext
.Length
= PTEXT_SIZE
;
45 for(loop
=0; loop
<testParams
->numLoops
; loop
++) {
46 if(testParams
->verbose
) {
47 printf("symTest thread %d: loop %d\n",
48 testParams
->threadNum
, loop
);
50 else if(!testParams
->quiet
) {
51 printChar(testParams
->progressChar
);
54 /* random plaintext */
55 crtn
= threadGetRandData(testParams
, &ptext
, PTEXT_SIZE
);
60 /* pick algorithm and params */
62 keyAlg
= CSSM_ALGID_DES
;
63 encrAlg
= CSSM_ALGID_DES
;
64 encrMode
= CSSM_ALGMODE_CBCPadIV8
;
65 padding
= CSSM_PADDING_PKCS1
;
66 initVector
.Length
= 8;
69 keyAlg
= CSSM_ALGID_AES
;
70 encrAlg
= CSSM_ALGID_AES
;
71 encrMode
= CSSM_ALGMODE_CBCPadIV8
;
72 padding
= CSSM_PADDING_PKCS5
;
73 initVector
.Length
= 16;
79 keyIsRef
= CSSM_FALSE
;
83 symKey
= cspGenSymKey(testParams
->cspHand
,
87 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
94 /* encrypt, decrypt */
95 crtn
= cspEncrypt(testParams
->cspHand
,
100 NULL
, // second key unused
101 0, // efectiveKeySizeInBits,
106 CSSM_TRUE
); // mallocCtext
110 crtn
= cspDecrypt(testParams
->cspHand
,
115 NULL
, // second key unused
116 0, // efectiveKeySizeInBits
121 CSSM_TRUE
); // mallocCtext
127 if(ptext
.Length
!= rptext
.Length
) {
128 printf("Ptext length mismatch: expect %lu, got %lu\n",
129 ptext
.Length
, rptext
.Length
);
132 if(memcmp(ptext
.Data
, rptext
.Data
, ptext
.Length
)) {
133 printf("***data miscompare\n");
137 /* free everything */
138 appFreeCssmData(&ctext
, CSSM_FALSE
);
139 appFreeCssmData(&rptext
, CSSM_FALSE
);
140 cspFreeKey(testParams
->cspHand
, symKey
);
144 printf("Hit CR to proceed: ");