]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/desTest.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / desTest.cpp
1 /*
2 * Simple DES encrypt/decrypt test using raw BSAFE
3 */
4 #include "testParams.h"
5 #include <Security/cssm.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <utilLib/common.h>
10 #include <utilLib/cspwrap.h>
11 #include <BSafe/bsafe.h>
12
13 /* for memory leak debug only, with only one thread running */
14 #define DO_PAUSE 0
15 #define SAFE_RAND_DATA 0
16
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
21
22 static B_ALGORITHM_METHOD *BSAFE_ALGORITHM_CHOOSER[] = {
23 &AM_MD5_RANDOM,
24 &AM_MD,
25 &AM_MD5,
26 &AM_CBC_ENCRYPT,
27 &AM_CBC_DECRYPT,
28 &AM_OFB_ENCRYPT,
29 &AM_OFB_DECRYPT,
30 &AM_DES_DECRYPT,
31 &AM_DES_ENCRYPT,
32 (B_ALGORITHM_METHOD *)NULL_PTR
33 };
34
35 /* generate DES key */
36 static int desGenKey(
37 const TestParams *testParams,
38 B_KEY_OBJ *desKey)
39 {
40 int brtn;
41 uint8 keyBytes[KEY_SIZE_BYTES];
42 CSSM_DATA keyData = {KEY_SIZE_BYTES, keyBytes};
43
44 B_DestroyKeyObject(desKey);
45 brtn = B_CreateKeyObject(desKey);
46 if(brtn) {
47 printf("***Error on B_CreateKeyObject (%d)\n", brtn);
48 return 1;
49 }
50 #if SAFE_RAND_DATA
51 threadGetRandData(testParams, &keyData, KEY_SIZE_BYTES);
52 #else
53 simpleGenData(&keyData, KEY_SIZE_BYTES, KEY_SIZE_BYTES);
54 #endif
55
56 brtn = B_SetKeyInfo(*desKey, KI_DES8, (POINTER)keyBytes);
57 if(brtn) {
58 printf("***Error on B_SetKeyInfo (%d)\n", brtn);
59 return 1;
60 }
61
62 return 0;
63
64 }
65
66 /* common code to set up encrypt/decrypt */
67 static int desEncDecSetup(
68 ITEM *iv,
69 B_BLK_CIPHER_W_FEEDBACK_PARAMS *spec,
70 B_ALGORITHM_OBJ *alg)
71 {
72 int brtn;
73
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");
80
81 brtn = B_CreateAlgorithmObject(alg);
82 if(brtn) {
83 printf("***B_CreateAlgorithmObject error (%d)\n", brtn);
84 return 1;
85 }
86 brtn = B_SetAlgorithmInfo(*alg, AI_FeedbackCipher, (POINTER)spec);
87 if(brtn) {
88 printf("***B_SetAlgorithmInfo error (%d)\n", brtn);
89 return 1;
90 }
91 return 0;
92 }
93
94 static int desEncrypt(
95 TestParams *testParams,
96 B_KEY_OBJ desKey,
97 uint8 *initVector,
98 CSSM_DATA *ptext,
99 CSSM_DATA *ctext)
100 {
101 ITEM iv;
102 B_BLK_CIPHER_W_FEEDBACK_PARAMS spec;
103 B_ALGORITHM_OBJ alg = NULL;
104 int brtn;
105 unsigned actLen;
106 unsigned remCtext = ctext->Length;
107
108 iv.data = initVector;
109 iv.len = DES_BLOCK_SIZE;
110 brtn = desEncDecSetup(&iv, &spec, &alg);
111 if(brtn) {
112 return brtn;
113 }
114 brtn = B_EncryptInit(alg,
115 desKey,
116 BSAFE_ALGORITHM_CHOOSER,
117 (A_SURRENDER_CTX *)NULL_PTR);
118 if(brtn) {
119 printf("***B_EncryptInit error (%d)\n", brtn);
120 return brtn;
121 }
122 brtn = B_EncryptUpdate(alg,
123 ctext->Data,
124 &actLen,
125 remCtext,
126 ptext->Data,
127 ptext->Length,
128 (B_ALGORITHM_OBJ)NULL_PTR,
129 (A_SURRENDER_CTX *)NULL_PTR);
130 if(brtn) {
131 printf("***B_EncryptUpdate error (%d)\n", brtn);
132 return brtn;
133 }
134 remCtext -= actLen;
135 ctext->Length = actLen;
136 brtn = B_EncryptFinal(alg,
137 ctext->Data + actLen,
138 &actLen,
139 remCtext,
140 (B_ALGORITHM_OBJ)NULL_PTR,
141 (A_SURRENDER_CTX *)NULL_PTR);
142 if(brtn) {
143 printf("***B_EncryptFinal error (%d)\n", brtn);
144 return brtn;
145 }
146 ctext->Length += actLen;
147 B_DestroyAlgorithmObject (&alg);
148 return 0;
149 }
150
151 static int desDecrypt(
152 TestParams *testParams,
153 B_KEY_OBJ desKey,
154 uint8 *initVector,
155 CSSM_DATA *ctext,
156 CSSM_DATA *ptext)
157 {
158 ITEM iv;
159 B_BLK_CIPHER_W_FEEDBACK_PARAMS spec;
160 B_ALGORITHM_OBJ alg = NULL;
161 int brtn;
162 unsigned actLen;
163 unsigned remPtext = ptext->Length;
164
165 iv.data = initVector;
166 iv.len = DES_BLOCK_SIZE;
167 brtn = desEncDecSetup(&iv, &spec, &alg);
168 if(brtn) {
169 return brtn;
170 }
171 brtn = B_DecryptInit(alg,
172 desKey,
173 BSAFE_ALGORITHM_CHOOSER,
174 (A_SURRENDER_CTX *)NULL_PTR);
175 if(brtn) {
176 printf("***B_DecryptInit error (%d)\n", brtn);
177 return brtn;
178 }
179 brtn = B_DecryptUpdate(alg,
180 ptext->Data,
181 &actLen,
182 remPtext,
183 ctext->Data,
184 ctext->Length,
185 (B_ALGORITHM_OBJ)NULL_PTR,
186 (A_SURRENDER_CTX *)NULL_PTR);
187 if(brtn) {
188 printf("***B_DecryptUpdate error (%d)\n", brtn);
189 return brtn;
190 }
191 remPtext -= actLen;
192 ptext->Length = actLen;
193 brtn = B_DecryptFinal(alg,
194 ptext->Data + actLen,
195 &actLen,
196 remPtext,
197 (B_ALGORITHM_OBJ)NULL_PTR,
198 (A_SURRENDER_CTX *)NULL_PTR);
199 if(brtn) {
200 printf("***B_DecryptFinal error (%d)\n", brtn);
201 return brtn;
202 }
203 ptext->Length += actLen;
204 B_DestroyAlgorithmObject (&alg);
205 return 0;
206 }
207
208 int desInit(TestParams *testParams)
209 {
210 /* nothing for now */
211 return 0;
212 }
213
214 int desTest(TestParams *testParams)
215 {
216 unsigned loop;
217 CSSM_RETURN crtn;
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";
223 int rtn;
224
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;
231
232 for(loop=0; loop<testParams->numLoops; loop++) {
233 if(testParams->verbose) {
234 printf("symTest thread %d: loop %d\n",
235 testParams->threadNum, loop);
236 }
237 else if(!testParams->quiet) {
238 printChar(testParams->progressChar);
239 }
240
241 /* random plaintext */
242 #if SAFE_RAND_DATA
243 crtn = threadGetRandData(testParams, &ptext, PTEXT_SIZE);
244 if(crtn) {
245 return 1;
246 }
247 #else
248 simpleGenData(&ptext, PTEXT_SIZE, PTEXT_SIZE);
249 #endif
250
251 /* cook up a key */
252 rtn = desGenKey(testParams, &desKey);
253 if(rtn) {
254 return 1;
255 }
256
257 /* encrypt, decrypt */
258 ctext.Length = PTEXT_SIZE + DES_BLOCK_SIZE;
259 rtn = desEncrypt(testParams,
260 desKey,
261 initVector,
262 &ptext,
263 &ctext);
264 if(rtn) {
265 return 1;
266 }
267 rptext.Length = PTEXT_SIZE + DES_BLOCK_SIZE;
268 rtn = desDecrypt(testParams,
269 desKey,
270 initVector,
271 &ctext,
272 &rptext);
273 if(crtn) {
274 return 1;
275 }
276
277 /* compare */
278 if(ptext.Length != rptext.Length) {
279 printf("Ptext length mismatch: expect %d, got %d\n",
280 ptext.Length, rptext.Length);
281 return 1;
282 }
283 if(memcmp(ptext.Data, rptext.Data, ptext.Length)) {
284 printf("***data miscompare\n");
285 return 1;
286 }
287
288 #if DO_PAUSE
289 fpurge(stdin);
290 printf("Hit CR to proceed: ");
291 getchar();
292 #endif
293 }
294 return 0;
295 }
296