]>
git.saurik.com Git - apple/security.git/blob - libsecurity_cryptkit/ckutils/blobtest/blobtest.c
1 /* Copyright 1998 Apple Computer, Inc.
3 * blobtest.c - test key blob functions
7 * 23 Mar 1998 Doug Mitchell
16 #include "ckutilsPlatform.h"
18 #define MIN_PASSWD_LENGTH 4
19 #define MAX_PASSWD_LENGTH 20
20 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
29 static unsigned char *passwdPool
;
31 static unsigned doBlobTest(unsigned minPasswdLen
,
32 unsigned maxPasswdLen
,
35 static void usage(char **argv
);
37 int main(int argc
, char **argv
)
39 BOOL seedSpec
= NO
; // YES ==> user specified
45 * User-spec'd variables
47 unsigned minPasswordLen
= MIN_PASSWD_LENGTH
;
48 unsigned maxPasswordLen
= MAX_PASSWD_LENGTH
;
53 unsigned depth
= DEPTH_DEFAULT
;
56 argc
= ccommand(&argv
);
58 for(arg
=1; arg
<argc
; arg
++) {
62 minPasswordLen
= atoi(&argp
[2]);
65 maxPasswordLen
= atoi(&argp
[2]);
68 seed
= atoi(&argp
[2]);
72 loops
= atoi(&argp
[2]);
75 depth
= atoi(&argp
[2]);
95 passwdPool
= fmalloc(maxPasswordLen
+ 4);
97 printf("Starting %s: minPasswd %d maxPasswd %d seed %d depth %d\n",
99 minPasswordLen
, maxPasswordLen
, seed
, depth
);
101 for(loopNum
=1; ; loopNum
++) {
103 printf("..loop %d\n", loopNum
);
105 if(doBlobTest(minPasswordLen
, maxPasswordLen
, verbose
,
109 if(loops
&& (loopNum
== loops
)) {
114 printf("%s test complete\n", argv
[0]);
119 static void usage(char **argv
)
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");
127 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
128 printf(" q(uiet)\n");
129 printf(" v(erbose)\n");
134 static unsigned char *genPasswd(unsigned passwdLength
)
136 unsigned *ip
= (unsigned *)passwdPool
;
137 unsigned intCount
= (passwdLength
+ 3) / 4;
141 for (i
=0; i
<intCount
; i
++) {
144 rtn
= fmalloc(passwdLength
);
145 bcopy(passwdPool
, rtn
, passwdLength
);
149 static unsigned doBlobTest(unsigned minPasswdLen
,
150 unsigned maxPasswdLen
,
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
162 unsigned char *privBlob
= NULL
;
163 unsigned privBlobLen
;
164 unsigned char *pubBlob
= NULL
;
167 for(myPasswdLen
=minPasswdLen
;
168 myPasswdLen
<maxPasswdLen
;
172 printf("....myPasswdLen %d\n", myPasswdLen
);
176 * my private password
178 myPasswd
= genPasswd(myPasswdLen
);
181 * Fully capable Public Key object
183 myPrivate
= feePubKeyAlloc();
184 frtn
= feePubKeyInitFromPrivDataDepth(myPrivate
,
190 printf("feePubKeyInitFromPrivDataDepth: %s\n",
191 feeReturnString(frtn
));
197 frtn
= feePubKeyCreatePrivBlob(myPrivate
,
201 printf("feePubKeyCreatePrivBlob: %s\n",
202 feeReturnString(frtn
));
207 /* private key from private blob */
208 myPrivateCopy
= feePubKeyAlloc();
209 frtn
= feePubKeyInitFromPrivBlob(myPrivateCopy
,
213 printf("feePubKeyInitFromKeyBlob (private): %s\n",
214 feeReturnString(frtn
));
218 if(!feePubKeyIsPrivate(myPrivateCopy
)) {
219 printf("Unexpected !feePubKeyIsPrivate!\n");
224 /* public blob from private key */
225 frtn
= feePubKeyCreatePubBlob(myPrivate
,
229 printf("feePubKeyCreatePubBlob (1): %s\n",
230 feeReturnString(frtn
));
235 /* public key from public blob */
236 myPublic
= feePubKeyAlloc();
237 frtn
= feePubKeyInitFromPubBlob(myPublic
,
241 printf("feePubKeyInitFromKeyBlob (pub 1): %s\n",
242 feeReturnString(frtn
));
246 if(feePubKeyIsPrivate(myPublic
)) {
247 printf("Unexpected feePubKeyIsPrivate (1)!\n");
254 /* public blob from public key */
255 frtn
= feePubKeyCreatePubBlob(myPublic
,
259 printf("feePubKeyCreatePubBlob (2): %s\n",
260 feeReturnString(frtn
));
265 /* public key from public blob */
266 myPublicCopy
= feePubKeyAlloc();
267 frtn
= feePubKeyInitFromPubBlob(myPublicCopy
,
271 printf("feePubKeyInitFromKeyBlob (pub 2): %s\n",
272 feeReturnString(frtn
));
276 if(feePubKeyIsPrivate(myPublicCopy
)) {
277 printf("Unexpected feePubKeyIsPrivate (2)!\n");
282 /* private blob from pub key - should fail */
283 frtn
= feePubKeyCreatePrivBlob(myPublic
,
286 if(frtn
== FR_Success
) {
287 printf("Unexpected feePubKeyCreatePrivBlob success\n");
293 * OK, we have four keys; they should all be equal (in
294 * terms of their actual public data).
296 if(!feePubKeyIsEqual(myPrivate
, myPrivateCopy
)) {
297 printf("myPrivate != myPrivateCopy\n");
301 if(!feePubKeyIsEqual(myPrivate
, myPublic
)) {
302 printf("myPrivate != myPublic\n");
306 if(!feePubKeyIsEqual(myPrivate
, myPublicCopy
)) {
307 printf("myPrivate != myPublicCopy\n");
311 if(!feePubKeyIsEqual(myPublic
, myPublicCopy
)) {
312 printf("myPublic != myPublicCopy\n");
321 feePubKeyFree(myPrivate
);
324 feePubKeyFree(myPrivateCopy
);
327 feePubKeyFree(myPublic
);
330 feePubKeyFree(myPublicCopy
);