2 * Copyright (c) 1997,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@
25 #include "ckutilsPlatform.h"
27 #include "feeCipherFile.h"
32 static unsigned char *dataPool
; /* plaintext comes from here */
42 #define MIN_EXP 2 /* for data size 10**exp */
43 #define MAX_EXP 3 /* FEED is very slow with ptext larger than this... */
44 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
48 #define PASSWD_LENGTH 10
50 static void usage(char **argv
)
52 printf("usage: %s [options]\n", argv
[0]);
54 printf(" l==loops (default=%d)\n", LOOPS_DEF
);
55 printf(" n=minExp (default=%d)\n", MIN_EXP
);
56 printf(" x=maxExp (default=max=%d)\n", MAX_EXP
);
57 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
58 printf(" N=minOffset (default=%d)\n", MIN_OFFSET
);
59 printf(" q(uiet) v(erbose)\n");
60 printf(" h(elp) I(ncrementing offset)\n");
65 * ...min <= return <= max
67 static int genRand(int min
, int max
)
70 /* note random() only yields a 31-bit number... */
72 if(max
== min
) /* avoid % 1 ! */
75 return(min
+ (RAND() % (max
-min
+1)));
78 /* end of feeLib routines */
83 static void genPasswd(unsigned char *passwd
,
84 unsigned passwdLen
, BOOL ascii
)
86 unsigned *ip
= (unsigned *)passwd
;
87 unsigned intCount
= passwdLen
/ 4;
90 unsigned residue
= passwdLen
& 0x3;
96 for(i
=0; i
<passwdLen
; i
++) {
104 for (i
=0; i
<intCount
; i
++) {
107 cp
= (unsigned char *)ip
;
108 for(i
=0; i
<residue
; i
++) {
109 *cp
= (unsigned char)RAND();
115 * Calculate random data size, fill dataPool with that many random bytes.
121 DT_None
/* data irrelevant; use existing pool */
124 static void fillDataPool(unsigned size
, dataType type
)
126 #ifdef __LITTLE_ENDIAN__
137 bzero(dataPool
, size
);
142 for(i
=0; i
<size
; i
++) {
150 #ifdef __LITTLE_ENDIAN__
151 intCount
= size
>> 2;
152 ip
= (unsigned *)dataPool
;
153 for(i
=0; i
<intCount
; i
++) {
157 residue
= size
& 0x3;
158 cp
= (unsigned char *)ip
;
159 for(i
=0; i
<residue
; i
++) {
160 *cp
++ = (unsigned char)RAND();
162 #else __LITTLE_ENDIAN__
164 for(i
=0; i
<size
; i
++) {
165 *cp
++ = (char)RAND();
167 #endif __LITTLE_ENDIAN__
170 printf("fillDataPool(DT_None)\n");
175 static unsigned dataSizeFromExp(unsigned maxExp
)
178 while(maxExp
--) { // size = 10 ** exp
184 static int sizeOffset
= MIN_OFFSET
;
186 static unsigned char *genData(unsigned minExp
,
191 unsigned *dataLen
) // RETURNED
198 * Calculate "random" size : (10 ** (random exponent)) + random offset
200 exp
= genRand(minExp
, maxExp
);
202 offset
= sizeOffset
++;
203 if(sizeOffset
== MAX_OFFSET
) {
204 sizeOffset
= minOffset
;
208 offset
= genRand(minOffset
, MAX_OFFSET
);
210 size
= dataSizeFromExp(exp
) + offset
;
211 if(type
!= DT_None
) {
212 fillDataPool(size
, type
);
218 static feePubKey
genPrivKey(const unsigned char *privKeyData
,
219 unsigned privDataLen
,
222 feePubKey privKey
; // generic key object
225 privKey
= feePubKeyAlloc();
226 frtn
= feePubKeyInitFromPrivDataDepth(privKey
,
227 (unsigned char *)privKeyData
,
232 printf("pubKeyFromPrivDataDepth: Can't create new key (%s)\n",
233 feeReturnString(frtn
));
239 static feePubKey
genPubKey(feePubKey privKey
)
241 feePubKey pubKey
; // generic key object
244 unsigned pubStringLen
;
246 frtn
= feePubKeyCreateKeyString(privKey
, &pubString
, &pubStringLen
);
248 printf("feePubKeyCreateKeyString: Can't get key string (%s)\n",
249 feeReturnString(frtn
));
252 pubKey
= feePubKeyAlloc();
253 frtn
= feePubKeyInitFromKeyString(pubKey
, pubString
, pubStringLen
);
255 printf("feePubKeyInitFromKeyString: Can't create new key "
257 feeReturnString(frtn
));
258 feePubKeyFree(pubKey
);
265 static char *stringFromEncrType(cipherFileEncrType encrType
)
268 case CFE_PublicDES
: return "CFE_PublicDES";
269 case CFE_RandDES
: return "CFE_RandDES";
270 case CFE_FEED
: return "CFE_FEED";
271 case CFE_FEEDExp
: return "CFE_FEEDExp";
272 default: return "Bogus encrType";
278 #define EXPLICIT_NO 0
279 #define EXPLICIT_YES 1
280 #define EXPLICIT_ERR 2
282 static void doTest(unsigned char *ptext
,
286 feePubKey theirPrivKey
,
287 feePubKey theirPubKey
,
288 cipherFileEncrType encrType
,
291 int doExplicitKey
) /* EXPLICIT_ERR means do one with
295 unsigned char *ctext
;
297 unsigned char *dectext
;
299 unsigned outUserData
= 0x1234567;
301 cipherFileEncrType inEncrType
;
302 feeSigStatus sigStatus
;
305 feeSigStatus expSigStatus
= SS_PresentValid
;
309 * These are tailored to specific encrTypes and doExplicitKeys
311 feePubKey sendPrivKey
= myPrivKey
;
312 feePubKey sendPubKey
= myPubKey
;
313 feePubKey recvPrivKey
= theirPrivKey
;
314 feePubKey recvPubKey
= theirPubKey
;
320 sendPrivKey
= NULL
; // not needed
327 printf("Hey bozo! Give me a real encrType!\n");
331 sendPubKey
= NULL
; // never needed
332 expSigStatus
= SS_NotPresent
;
334 else switch(doExplicitKey
) {
336 sendPubKey
= NULL
; // get it from cipherfile
339 break; // use myPubKey
341 if(feePubKeyIsEqual(myPubKey
, theirPubKey
)) {
342 printf("myPubKey = theirPubKey!\n");
345 sendPubKey
= theirPubKey
; // hopefully != myPubKey!
346 expSigStatus
= SS_PresentInvalid
;
349 printf("BOGUS doExplicitKey\n");
353 frtn
= createCipherFile(sendPrivKey
,
364 printf("createCipherFile: %s\n", feeReturnString(frtn
));
368 valid64
= isValidEnc64(ctext
, ctextLen
);
369 if(valid64
!= doEnc64
) {
370 printf("valid64 mismatch! exp %d got %d\n", doEnc64
, valid64
);
373 frtn
= parseCipherFile(recvPrivKey
,
384 printf("parseCipherFile: %s\n", feeReturnString(frtn
));
387 if(inEncrType
!= encrType
) {
388 printf("encrType mismatch exp %d got %d\n",
389 encrType
, inEncrType
);
392 if(inUserData
!= outUserData
) {
393 printf("userData mismatch exp %d got %d\n",
394 outUserData
, inUserData
);
397 if(sigStatus
!= expSigStatus
) {
398 printf("Bad sigStatus exp %d got %d\n",
399 expSigStatus
, sigStatus
);
402 if(ptextLen
!= dectextLen
) {
403 printf("ptextLen mismatch exp %d got %d\n",
404 ptextLen
, dectextLen
);
407 if(bcmp(ptext
, dectext
, ptextLen
)) {
408 printf("Data Miscompare\n");
418 printf("attach with debugger for more info; enter CR to quit: ");
424 int main(int argc
, char **argv
)
429 unsigned char *ptext
;
431 unsigned char passwd1
[PASSWD_LENGTH
];
432 unsigned char passwd2
[PASSWD_LENGTH
];
436 feePubKey theirPrivKey
;
438 feePubKey theirPubKey
;
444 unsigned loops
= LOOPS_DEF
;
449 unsigned minExp
= MIN_EXP
;
450 unsigned maxExp
= MAX_EXP
;
451 BOOL incrOffset
= NO
;
452 unsigned depth
= DEPTH_DEFAULT
;
453 unsigned minOffset
= MIN_OFFSET
;
456 argc
= ccommand(&argv
);
459 for(arg
=1; arg
<argc
; arg
++) {
463 loops
= atoi(&argp
[2]);
466 minExp
= atoi(&argp
[2]);
469 depth
= atoi(&argp
[2]);
472 minOffset
= atoi(&argp
[2]);
473 if(minOffset
> MAX_OFFSET
) {
474 minOffset
= MIN_OFFSET
;
476 sizeOffset
= minOffset
;
479 maxExp
= atoi(&argp
[2]);
480 if(maxExp
> MAX_EXP
) {
485 seed
= atoi(&argp
[2]);
504 time((unsigned long *)(&seed
));
507 maxSize
= dataSizeFromExp(maxExp
) + MAX_OFFSET
+ 8;
508 dataPool
= fmalloc(maxSize
);
510 printf("Starting cfileTest: loops %d seed %d depth %d\n",
513 for(loop
=1; ; loop
++) {
515 ptext
= genData(minExp
, maxExp
, DT_Random
, incrOffset
,
516 minOffset
, &ptextLen
);
518 printf("..loop %d plaintext size %d\n", loop
, ptextLen
);
522 * Generate a whole bunch of keys
524 genPasswd(passwd1
, PASSWD_LENGTH
, NO
); // not ascii!
525 genPasswd(passwd2
, PASSWD_LENGTH
, NO
);
526 myPrivKey
= genPrivKey(passwd1
, PASSWD_LENGTH
, depth
);
527 theirPrivKey
= genPrivKey(passwd2
, PASSWD_LENGTH
, depth
);
528 myPubKey
= genPubKey(myPrivKey
);
529 theirPubKey
= genPubKey(theirPrivKey
);
531 for(encrType
=CFE_PublicDES
;
532 encrType
<=CFE_FEEDExp
;
536 printf(" ..%s\n", stringFromEncrType(encrType
));
538 for(doEnc64
=0; doEnc64
<2; doEnc64
++) {
540 printf(" ..doEnc64 %d\n", doEnc64
);
544 printf(" ..no sig\n");
546 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
547 theirPrivKey
, theirPubKey
,
548 encrType
, doEnc64
, SIG_NO
, EXPLICIT_NO
);
551 printf(" ..sig, implicit sendPubKey\n");
553 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
554 theirPrivKey
, theirPubKey
,
555 encrType
, doEnc64
, SIG_YES
, EXPLICIT_NO
);
558 printf(" ..sig, explicit sendPubKey\n");
560 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
561 theirPrivKey
, theirPubKey
,
562 encrType
, doEnc64
, SIG_YES
, EXPLICIT_YES
);
565 printf(" ..sig, force error\n");
567 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
568 theirPrivKey
, theirPubKey
,
569 encrType
, doEnc64
, SIG_YES
, EXPLICIT_ERR
);
574 feePubKeyFree(myPrivKey
);
575 feePubKeyFree(myPubKey
);
576 feePubKeyFree(theirPrivKey
);
577 feePubKeyFree(theirPubKey
);
586 printf("cfile test complete\n");