]> git.saurik.com Git - apple/security.git/blame - libsecurity_cryptkit/ckutils/blobtest/blobtest.c
Security-55471.14.18.tar.gz
[apple/security.git] / libsecurity_cryptkit / ckutils / blobtest / blobtest.c
CommitLineData
b1ab9ed8
A
1/* Copyright 1998 Apple Computer, Inc.
2 *
3 * blobtest.c - test key blob functions
4 *
5 * Revision History
6 * ----------------
7 * 23 Mar 1998 Doug Mitchell
8 * Created.
9 */
10
11#include "Crypt.h"
12#include "falloc.h"
13#include <stdlib.h>
14#include <stdio.h>
15#include <time.h>
16#include "ckutilsPlatform.h"
17
18#define MIN_PASSWD_LENGTH 4
19#define MAX_PASSWD_LENGTH 20
20#define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
21
22#undef BOOL
23#undef YES
24#undef NO
25#define BOOL int
26#define YES 1
27#define NO 0
28
29static unsigned char *passwdPool;
30
31static unsigned doBlobTest(unsigned minPasswdLen,
32 unsigned maxPasswdLen,
33 BOOL verbose,
34 unsigned depth);
35static void usage(char **argv);
36
37int main(int argc, char **argv)
38{
39 BOOL seedSpec = NO; // YES ==> user specified
40 unsigned loopNum;
41 int arg;
42 char *argp;
43
44 /*
45 * User-spec'd variables
46 */
47 unsigned minPasswordLen = MIN_PASSWD_LENGTH;
48 unsigned maxPasswordLen = MAX_PASSWD_LENGTH;
49 int seed = 0;
50 unsigned loops = 1;
51 BOOL quiet = NO;
52 BOOL verbose = NO;
53 unsigned depth = DEPTH_DEFAULT;
54
55 #if macintosh
56 argc = ccommand(&argv);
57 #endif
58 for(arg=1; arg<argc; arg++) {
59 argp = argv[arg];
60 switch(argp[0]) {
61 case 'n':
62 minPasswordLen = atoi(&argp[2]);
63 break;
64 case 'x':
65 maxPasswordLen = atoi(&argp[2]);
66 break;
67 case 's':
68 seed = atoi(&argp[2]);
69 seedSpec = YES;
70 break;
71 case 'l':
72 loops = atoi(&argp[2]);
73 break;
74 case 'D':
75 depth = atoi(&argp[2]);
76 break;
77 case 'q':
78 quiet = YES;
79 break;
80 case 'v':
81 verbose = YES;
82 break;
83 case 'h':
84 default:
85 usage(argv);
86 }
87 }
88 if(seedSpec == NO) {
89 unsigned long tim;
90 time(&tim);
91 seed = (unsigned)tim;
92 }
93 SRAND(seed);
94
95 passwdPool = fmalloc(maxPasswordLen + 4);
96
97 printf("Starting %s: minPasswd %d maxPasswd %d seed %d depth %d\n",
98 argv[0],
99 minPasswordLen, maxPasswordLen, seed, depth);
100
101 for(loopNum=1; ; loopNum++) {
102 if(!quiet) {
103 printf("..loop %d\n", loopNum);
104 }
105 if(doBlobTest(minPasswordLen, maxPasswordLen, verbose,
106 depth)) {
107 return 1;
108 }
109 if(loops && (loopNum == loops)) {
110 break;
111 }
112 }
113 if(!quiet) {
114 printf("%s test complete\n", argv[0]);
115 }
116 return 0;
117}
118
119static void usage(char **argv)
120{
121 printf("usage: %s [options]\n", argv[0]);
122 printf(" Options:\n");
123 printf(" l=loops (0=forever)\n");
124 printf(" n=minPasswordLen\n");
125 printf(" x=maxPasswdLen\n");
126 printf(" s=seed\n");
127 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT);
128 printf(" q(uiet)\n");
129 printf(" v(erbose)\n");
130 printf(" h(elp)\n");
131 exit(1);
132}
133
134static unsigned char *genPasswd(unsigned passwdLength)
135{
136 unsigned *ip = (unsigned *)passwdPool;
137 unsigned intCount = (passwdLength + 3) / 4;
138 int i;
139 unsigned char *rtn;
140
141 for (i=0; i<intCount; i++) {
142 *ip++ = RAND();
143 }
144 rtn = fmalloc(passwdLength);
145 bcopy(passwdPool, rtn, passwdLength);
146 return rtn;
147}
148
149static unsigned doBlobTest(unsigned minPasswdLen,
150 unsigned maxPasswdLen,
151 BOOL verbose,
152 unsigned depth)
153{
154 unsigned char *myPasswd = NULL;
155 unsigned myPasswdLen;
156 feePubKey myPrivate = NULL;
157 feePubKey myPrivateCopy = NULL; // from blob
158 feePubKey myPublic = NULL; // from blob from myPrivate
159 feePubKey myPublicCopy = NULL; // from blob from myPublic
160 unsigned rtn = 0;
161 feeReturn frtn;
162 unsigned char *privBlob = NULL;
163 unsigned privBlobLen;
164 unsigned char *pubBlob = NULL;
165 unsigned pubBlobLen;
166
167 for(myPasswdLen=minPasswdLen;
168 myPasswdLen<maxPasswdLen;
169 myPasswdLen++) {
170
171 if(verbose) {
172 printf("....myPasswdLen %d\n", myPasswdLen);
173 }
174
175 /*
176 * my private password
177 */
178 myPasswd = genPasswd(myPasswdLen);
179
180 /*
181 * Fully capable Public Key object
182 */
183 myPrivate = feePubKeyAlloc();
184 frtn = feePubKeyInitFromPrivDataDepth(myPrivate,
185 myPasswd,
186 myPasswdLen,
187 depth,
188 1);
189 if(frtn) {
190 printf("feePubKeyInitFromPrivDataDepth: %s\n",
191 feeReturnString(frtn));
192 rtn = 1;
193 goto out;
194 }
195
196 /* private blob */
197 frtn = feePubKeyCreatePrivBlob(myPrivate,
198 &privBlob,
199 &privBlobLen);
200 if(frtn) {
201 printf("feePubKeyCreatePrivBlob: %s\n",
202 feeReturnString(frtn));
203 rtn = 1;
204 goto out;
205 }
206
207 /* private key from private blob */
208 myPrivateCopy = feePubKeyAlloc();
209 frtn = feePubKeyInitFromPrivBlob(myPrivateCopy,
210 privBlob,
211 privBlobLen);
212 if(frtn) {
213 printf("feePubKeyInitFromKeyBlob (private): %s\n",
214 feeReturnString(frtn));
215 rtn = 1;
216 goto out;
217 }
218 if(!feePubKeyIsPrivate(myPrivateCopy)) {
219 printf("Unexpected !feePubKeyIsPrivate!\n");
220 rtn = 1;
221 goto out;
222 }
223
224 /* public blob from private key */
225 frtn = feePubKeyCreatePubBlob(myPrivate,
226 &pubBlob,
227 &pubBlobLen);
228 if(frtn) {
229 printf("feePubKeyCreatePubBlob (1): %s\n",
230 feeReturnString(frtn));
231 rtn = 1;
232 goto out;
233 }
234
235 /* public key from public blob */
236 myPublic = feePubKeyAlloc();
237 frtn = feePubKeyInitFromPubBlob(myPublic,
238 pubBlob,
239 pubBlobLen);
240 if(frtn) {
241 printf("feePubKeyInitFromKeyBlob (pub 1): %s\n",
242 feeReturnString(frtn));
243 rtn = 1;
244 goto out;
245 }
246 if(feePubKeyIsPrivate(myPublic)) {
247 printf("Unexpected feePubKeyIsPrivate (1)!\n");
248 rtn = 1;
249 goto out;
250 }
251 ffree(pubBlob);
252 pubBlob = NULL;
253
254 /* public blob from public key */
255 frtn = feePubKeyCreatePubBlob(myPublic,
256 &pubBlob,
257 &pubBlobLen);
258 if(frtn) {
259 printf("feePubKeyCreatePubBlob (2): %s\n",
260 feeReturnString(frtn));
261 rtn = 1;
262 goto out;
263 }
264
265 /* public key from public blob */
266 myPublicCopy = feePubKeyAlloc();
267 frtn = feePubKeyInitFromPubBlob(myPublicCopy,
268 pubBlob,
269 pubBlobLen);
270 if(frtn) {
271 printf("feePubKeyInitFromKeyBlob (pub 2): %s\n",
272 feeReturnString(frtn));
273 rtn = 1;
274 goto out;
275 }
276 if(feePubKeyIsPrivate(myPublicCopy)) {
277 printf("Unexpected feePubKeyIsPrivate (2)!\n");
278 rtn = 1;
279 goto out;
280 }
281
282 /* private blob from pub key - should fail */
283 frtn = feePubKeyCreatePrivBlob(myPublic,
284 &pubBlob,
285 &pubBlobLen);
286 if(frtn == FR_Success) {
287 printf("Unexpected feePubKeyCreatePrivBlob success\n");
288 rtn = 1;
289 goto out;
290 }
291
292 /*
293 * OK, we have four keys; they should all be equal (in
294 * terms of their actual public data).
295 */
296 if(!feePubKeyIsEqual(myPrivate, myPrivateCopy)) {
297 printf("myPrivate != myPrivateCopy\n");
298 rtn = 1;
299 goto out;
300 }
301 if(!feePubKeyIsEqual(myPrivate, myPublic)) {
302 printf("myPrivate != myPublic\n");
303 rtn = 1;
304 goto out;
305 }
306 if(!feePubKeyIsEqual(myPrivate, myPublicCopy)) {
307 printf("myPrivate != myPublicCopy\n");
308 rtn = 1;
309 goto out;
310 }
311 if(!feePubKeyIsEqual(myPublic, myPublicCopy)) {
312 printf("myPublic != myPublicCopy\n");
313 rtn = 1;
314 goto out;
315 }
316 out:
317 if(myPasswd) {
318 ffree(myPasswd);
319 }
320 if(myPrivate) {
321 feePubKeyFree(myPrivate);
322 }
323 if(myPrivateCopy) {
324 feePubKeyFree(myPrivateCopy);
325 }
326 if(myPublic) {
327 feePubKeyFree(myPublic);
328 }
329 if(myPublic) {
330 feePubKeyFree(myPublicCopy);
331 }
332 if(privBlob) {
333 ffree(privBlob);
334 }
335 if(pubBlob) {
336 ffree(pubBlob);
337 }
338 if(rtn) {
339 break;
340 }
341 }
342 return rtn;
343}
344