]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/ckutils/blobtest/blobtest.c
2 * Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
30 #include "ckutilsPlatform.h"
32 #define MIN_PASSWD_LENGTH 4
33 #define MAX_PASSWD_LENGTH 20
34 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
43 static unsigned char *passwdPool
;
45 static unsigned doBlobTest(unsigned minPasswdLen
,
46 unsigned maxPasswdLen
,
49 static void usage(char **argv
);
51 int main(int argc
, char **argv
)
53 BOOL seedSpec
= NO
; // YES ==> user specified
59 * User-spec'd variables
61 unsigned minPasswordLen
= MIN_PASSWD_LENGTH
;
62 unsigned maxPasswordLen
= MAX_PASSWD_LENGTH
;
67 unsigned depth
= DEPTH_DEFAULT
;
70 argc
= ccommand(&argv
);
72 for(arg
=1; arg
<argc
; arg
++) {
76 minPasswordLen
= atoi(&argp
[2]);
79 maxPasswordLen
= atoi(&argp
[2]);
82 seed
= atoi(&argp
[2]);
86 loops
= atoi(&argp
[2]);
89 depth
= atoi(&argp
[2]);
105 seed
= (unsigned)tim
;
109 passwdPool
= fmalloc(maxPasswordLen
+ 4);
111 printf("Starting %s: minPasswd %d maxPasswd %d seed %d depth %d\n",
113 minPasswordLen
, maxPasswordLen
, seed
, depth
);
115 for(loopNum
=1; ; loopNum
++) {
117 printf("..loop %d\n", loopNum
);
119 if(doBlobTest(minPasswordLen
, maxPasswordLen
, verbose
,
123 if(loops
&& (loopNum
== loops
)) {
128 printf("%s test complete\n", argv
[0]);
133 static void usage(char **argv
)
135 printf("usage: %s [options]\n", argv
[0]);
136 printf(" Options:\n");
137 printf(" l=loops (0=forever)\n");
138 printf(" n=minPasswordLen\n");
139 printf(" x=maxPasswdLen\n");
141 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
142 printf(" q(uiet)\n");
143 printf(" v(erbose)\n");
148 static unsigned char *genPasswd(unsigned passwdLength
)
150 unsigned *ip
= (unsigned *)passwdPool
;
151 unsigned intCount
= (passwdLength
+ 3) / 4;
155 for (i
=0; i
<intCount
; i
++) {
158 rtn
= fmalloc(passwdLength
);
159 bcopy(passwdPool
, rtn
, passwdLength
);
163 static unsigned doBlobTest(unsigned minPasswdLen
,
164 unsigned maxPasswdLen
,
168 unsigned char *myPasswd
= NULL
;
169 unsigned myPasswdLen
;
170 feePubKey myPrivate
= NULL
;
171 feePubKey myPrivateCopy
= NULL
; // from blob
172 feePubKey myPublic
= NULL
; // from blob from myPrivate
173 feePubKey myPublicCopy
= NULL
; // from blob from myPublic
176 unsigned char *privBlob
= NULL
;
177 unsigned privBlobLen
;
178 unsigned char *pubBlob
= NULL
;
181 for(myPasswdLen
=minPasswdLen
;
182 myPasswdLen
<maxPasswdLen
;
186 printf("....myPasswdLen %d\n", myPasswdLen
);
190 * my private password
192 myPasswd
= genPasswd(myPasswdLen
);
195 * Fully capable Public Key object
197 myPrivate
= feePubKeyAlloc();
198 frtn
= feePubKeyInitFromPrivDataDepth(myPrivate
,
204 printf("feePubKeyInitFromPrivDataDepth: %s\n",
205 feeReturnString(frtn
));
211 frtn
= feePubKeyCreatePrivBlob(myPrivate
,
215 printf("feePubKeyCreatePrivBlob: %s\n",
216 feeReturnString(frtn
));
221 /* private key from private blob */
222 myPrivateCopy
= feePubKeyAlloc();
223 frtn
= feePubKeyInitFromPrivBlob(myPrivateCopy
,
227 printf("feePubKeyInitFromKeyBlob (private): %s\n",
228 feeReturnString(frtn
));
232 if(!feePubKeyIsPrivate(myPrivateCopy
)) {
233 printf("Unexpected !feePubKeyIsPrivate!\n");
238 /* public blob from private key */
239 frtn
= feePubKeyCreatePubBlob(myPrivate
,
243 printf("feePubKeyCreatePubBlob (1): %s\n",
244 feeReturnString(frtn
));
249 /* public key from public blob */
250 myPublic
= feePubKeyAlloc();
251 frtn
= feePubKeyInitFromPubBlob(myPublic
,
255 printf("feePubKeyInitFromKeyBlob (pub 1): %s\n",
256 feeReturnString(frtn
));
260 if(feePubKeyIsPrivate(myPublic
)) {
261 printf("Unexpected feePubKeyIsPrivate (1)!\n");
268 /* public blob from public key */
269 frtn
= feePubKeyCreatePubBlob(myPublic
,
273 printf("feePubKeyCreatePubBlob (2): %s\n",
274 feeReturnString(frtn
));
279 /* public key from public blob */
280 myPublicCopy
= feePubKeyAlloc();
281 frtn
= feePubKeyInitFromPubBlob(myPublicCopy
,
285 printf("feePubKeyInitFromKeyBlob (pub 2): %s\n",
286 feeReturnString(frtn
));
290 if(feePubKeyIsPrivate(myPublicCopy
)) {
291 printf("Unexpected feePubKeyIsPrivate (2)!\n");
296 /* private blob from pub key - should fail */
297 frtn
= feePubKeyCreatePrivBlob(myPublic
,
300 if(frtn
== FR_Success
) {
301 printf("Unexpected feePubKeyCreatePrivBlob success\n");
307 * OK, we have four keys; they should all be equal (in
308 * terms of their actual public data).
310 if(!feePubKeyIsEqual(myPrivate
, myPrivateCopy
)) {
311 printf("myPrivate != myPrivateCopy\n");
315 if(!feePubKeyIsEqual(myPrivate
, myPublic
)) {
316 printf("myPrivate != myPublic\n");
320 if(!feePubKeyIsEqual(myPrivate
, myPublicCopy
)) {
321 printf("myPrivate != myPublicCopy\n");
325 if(!feePubKeyIsEqual(myPublic
, myPublicCopy
)) {
326 printf("myPublic != myPublicCopy\n");
335 feePubKeyFree(myPrivate
);
338 feePubKeyFree(myPrivateCopy
);
341 feePubKeyFree(myPublic
);
344 feePubKeyFree(myPublicCopy
);