2 * Simple DES encrypt/decrypt test using raw BSAFE
4 #include "testParams.h"
5 #include <Security/cssm.h>
9 #include <utilLib/common.h>
10 #include <utilLib/cspwrap.h>
11 #include <BSafe/bsafe.h>
13 /* for memory leak debug only, with only one thread running */
15 #define SAFE_RAND_DATA 0
17 #define PTEXT_SIZE 1024
18 #define KEY_SIZE_BITS 64
19 #define KEY_SIZE_BYTES (KEY_SIZE_BITS / 8)
20 #define DES_BLOCK_SIZE 8
22 static B_ALGORITHM_METHOD
*BSAFE_ALGORITHM_CHOOSER
[] = {
32 (B_ALGORITHM_METHOD
*)NULL_PTR
35 /* generate DES key */
37 const TestParams
*testParams
,
41 uint8 keyBytes
[KEY_SIZE_BYTES
];
42 CSSM_DATA keyData
= {KEY_SIZE_BYTES
, keyBytes
};
44 B_DestroyKeyObject(desKey
);
45 brtn
= B_CreateKeyObject(desKey
);
47 printf("***Error on B_CreateKeyObject (%d)\n", brtn
);
51 threadGetRandData(testParams
, &keyData
, KEY_SIZE_BYTES
);
53 simpleGenData(&keyData
, KEY_SIZE_BYTES
, KEY_SIZE_BYTES
);
56 brtn
= B_SetKeyInfo(*desKey
, KI_DES8
, (POINTER
)keyBytes
);
58 printf("***Error on B_SetKeyInfo (%d)\n", brtn
);
66 /* common code to set up encrypt/decrypt */
67 static int desEncDecSetup(
69 B_BLK_CIPHER_W_FEEDBACK_PARAMS
*spec
,
74 spec
->encryptionMethodName
= POINTER("des");
75 spec
->feedbackMethodName
= POINTER("cbc");
76 spec
->feedbackParams
= POINTER(iv
);
77 spec
->paddingParams
= NULL_PTR
;
78 spec
->encryptionParams
= NULL_PTR
;
79 spec
->paddingMethodName
= POINTER("pad");
81 brtn
= B_CreateAlgorithmObject(alg
);
83 printf("***B_CreateAlgorithmObject error (%d)\n", brtn
);
86 brtn
= B_SetAlgorithmInfo(*alg
, AI_FeedbackCipher
, (POINTER
)spec
);
88 printf("***B_SetAlgorithmInfo error (%d)\n", brtn
);
94 static int desEncrypt(
95 TestParams
*testParams
,
102 B_BLK_CIPHER_W_FEEDBACK_PARAMS spec
;
103 B_ALGORITHM_OBJ alg
= NULL
;
106 unsigned remCtext
= ctext
->Length
;
108 iv
.data
= initVector
;
109 iv
.len
= DES_BLOCK_SIZE
;
110 brtn
= desEncDecSetup(&iv
, &spec
, &alg
);
114 brtn
= B_EncryptInit(alg
,
116 BSAFE_ALGORITHM_CHOOSER
,
117 (A_SURRENDER_CTX
*)NULL_PTR
);
119 printf("***B_EncryptInit error (%d)\n", brtn
);
122 brtn
= B_EncryptUpdate(alg
,
128 (B_ALGORITHM_OBJ
)NULL_PTR
,
129 (A_SURRENDER_CTX
*)NULL_PTR
);
131 printf("***B_EncryptUpdate error (%d)\n", brtn
);
135 ctext
->Length
= actLen
;
136 brtn
= B_EncryptFinal(alg
,
137 ctext
->Data
+ actLen
,
140 (B_ALGORITHM_OBJ
)NULL_PTR
,
141 (A_SURRENDER_CTX
*)NULL_PTR
);
143 printf("***B_EncryptFinal error (%d)\n", brtn
);
146 ctext
->Length
+= actLen
;
147 B_DestroyAlgorithmObject (&alg
);
151 static int desDecrypt(
152 TestParams
*testParams
,
159 B_BLK_CIPHER_W_FEEDBACK_PARAMS spec
;
160 B_ALGORITHM_OBJ alg
= NULL
;
163 unsigned remPtext
= ptext
->Length
;
165 iv
.data
= initVector
;
166 iv
.len
= DES_BLOCK_SIZE
;
167 brtn
= desEncDecSetup(&iv
, &spec
, &alg
);
171 brtn
= B_DecryptInit(alg
,
173 BSAFE_ALGORITHM_CHOOSER
,
174 (A_SURRENDER_CTX
*)NULL_PTR
);
176 printf("***B_DecryptInit error (%d)\n", brtn
);
179 brtn
= B_DecryptUpdate(alg
,
185 (B_ALGORITHM_OBJ
)NULL_PTR
,
186 (A_SURRENDER_CTX
*)NULL_PTR
);
188 printf("***B_DecryptUpdate error (%d)\n", brtn
);
192 ptext
->Length
= actLen
;
193 brtn
= B_DecryptFinal(alg
,
194 ptext
->Data
+ actLen
,
197 (B_ALGORITHM_OBJ
)NULL_PTR
,
198 (A_SURRENDER_CTX
*)NULL_PTR
);
200 printf("***B_DecryptFinal error (%d)\n", brtn
);
203 ptext
->Length
+= actLen
;
204 B_DestroyAlgorithmObject (&alg
);
208 int desInit(TestParams
*testParams
)
210 /* nothing for now */
214 int desTest(TestParams
*testParams
)
218 CSSM_DATA ptext
= {0, NULL
};
219 CSSM_DATA ctext
= {0, NULL
};
220 CSSM_DATA rptext
= {0, NULL
};
221 B_KEY_OBJ desKey
= NULL
;
222 uint8
*initVector
= (uint8
*)"someStrangeInitVect";
225 ptext
.Data
= (uint8
*)CSSM_MALLOC(PTEXT_SIZE
);
226 ptext
.Length
= PTEXT_SIZE
;
227 rptext
.Data
= (uint8
*)CSSM_MALLOC(PTEXT_SIZE
);
228 rptext
.Length
= PTEXT_SIZE
;
229 ctext
.Data
= (uint8
*)CSSM_MALLOC(PTEXT_SIZE
+ DES_BLOCK_SIZE
);
230 ctext
.Length
= PTEXT_SIZE
+ DES_BLOCK_SIZE
;
232 for(loop
=0; loop
<testParams
->numLoops
; loop
++) {
233 if(testParams
->verbose
) {
234 printf("symTest thread %d: loop %d\n",
235 testParams
->threadNum
, loop
);
237 else if(!testParams
->quiet
) {
238 printChar(testParams
->progressChar
);
241 /* random plaintext */
243 crtn
= threadGetRandData(testParams
, &ptext
, PTEXT_SIZE
);
248 simpleGenData(&ptext
, PTEXT_SIZE
, PTEXT_SIZE
);
252 rtn
= desGenKey(testParams
, &desKey
);
257 /* encrypt, decrypt */
258 ctext
.Length
= PTEXT_SIZE
+ DES_BLOCK_SIZE
;
259 rtn
= desEncrypt(testParams
,
267 rptext
.Length
= PTEXT_SIZE
+ DES_BLOCK_SIZE
;
268 rtn
= desDecrypt(testParams
,
278 if(ptext
.Length
!= rptext
.Length
) {
279 printf("Ptext length mismatch: expect %d, got %d\n",
280 ptext
.Length
, rptext
.Length
);
283 if(memcmp(ptext
.Data
, rptext
.Data
, ptext
.Length
)) {
284 printf("***data miscompare\n");
290 printf("Hit CR to proceed: ");