1 /* Copyright 1997 Apple Computer, Inc.
3 * cfileTest.c - General standalone CipherFile test
7 * 28 May 98 Doug Mitchell at Apple
8 * Checged to use fmalloc(), ffree(), ccommand()
9 * 24 Jun 97 Doug Mitchell at Apple
10 * Mods for Mac CodeWarrior build - standalone; no feeLib
11 * 7 Mar 97 Doug Mitchell at Apple
15 #include "ckutilsPlatform.h"
17 #include "feeCipherFile.h"
22 static unsigned char *dataPool
; /* plaintext comes from here */
32 #define MIN_EXP 2 /* for data size 10**exp */
33 #define MAX_EXP 3 /* FEED is very slow with ptext larger than this... */
34 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
38 #define PASSWD_LENGTH 10
40 static void usage(char **argv
)
42 printf("usage: %s [options]\n", argv
[0]);
44 printf(" l==loops (default=%d)\n", LOOPS_DEF
);
45 printf(" n=minExp (default=%d)\n", MIN_EXP
);
46 printf(" x=maxExp (default=max=%d)\n", MAX_EXP
);
47 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
48 printf(" N=minOffset (default=%d)\n", MIN_OFFSET
);
49 printf(" q(uiet) v(erbose)\n");
50 printf(" h(elp) I(ncrementing offset)\n");
55 * ...min <= return <= max
57 static int genRand(int min
, int max
)
60 /* note random() only yields a 31-bit number... */
62 if(max
== min
) /* avoid % 1 ! */
65 return(min
+ (RAND() % (max
-min
+1)));
68 /* end of feeLib routines */
73 static void genPasswd(unsigned char *passwd
,
74 unsigned passwdLen
, BOOL ascii
)
76 unsigned *ip
= (unsigned *)passwd
;
77 unsigned intCount
= passwdLen
/ 4;
80 unsigned residue
= passwdLen
& 0x3;
86 for(i
=0; i
<passwdLen
; i
++) {
94 for (i
=0; i
<intCount
; i
++) {
97 cp
= (unsigned char *)ip
;
98 for(i
=0; i
<residue
; i
++) {
99 *cp
= (unsigned char)RAND();
105 * Calculate random data size, fill dataPool with that many random bytes.
111 DT_None
/* data irrelevant; use existing pool */
114 static void fillDataPool(unsigned size
, dataType type
)
116 #ifdef __LITTLE_ENDIAN__
127 bzero(dataPool
, size
);
132 for(i
=0; i
<size
; i
++) {
140 #ifdef __LITTLE_ENDIAN__
141 intCount
= size
>> 2;
142 ip
= (unsigned *)dataPool
;
143 for(i
=0; i
<intCount
; i
++) {
147 residue
= size
& 0x3;
148 cp
= (unsigned char *)ip
;
149 for(i
=0; i
<residue
; i
++) {
150 *cp
++ = (unsigned char)RAND();
152 #else __LITTLE_ENDIAN__
154 for(i
=0; i
<size
; i
++) {
155 *cp
++ = (char)RAND();
157 #endif __LITTLE_ENDIAN__
160 printf("fillDataPool(DT_None)\n");
165 static unsigned dataSizeFromExp(unsigned maxExp
)
168 while(maxExp
--) { // size = 10 ** exp
174 static int sizeOffset
= MIN_OFFSET
;
176 static unsigned char *genData(unsigned minExp
,
181 unsigned *dataLen
) // RETURNED
188 * Calculate "random" size : (10 ** (random exponent)) + random offset
190 exp
= genRand(minExp
, maxExp
);
192 offset
= sizeOffset
++;
193 if(sizeOffset
== MAX_OFFSET
) {
194 sizeOffset
= minOffset
;
198 offset
= genRand(minOffset
, MAX_OFFSET
);
200 size
= dataSizeFromExp(exp
) + offset
;
201 if(type
!= DT_None
) {
202 fillDataPool(size
, type
);
208 static feePubKey
genPrivKey(const unsigned char *privKeyData
,
209 unsigned privDataLen
,
212 feePubKey privKey
; // generic key object
215 privKey
= feePubKeyAlloc();
216 frtn
= feePubKeyInitFromPrivDataDepth(privKey
,
217 (unsigned char *)privKeyData
,
222 printf("pubKeyFromPrivDataDepth: Can't create new key (%s)\n",
223 feeReturnString(frtn
));
229 static feePubKey
genPubKey(feePubKey privKey
)
231 feePubKey pubKey
; // generic key object
234 unsigned pubStringLen
;
236 frtn
= feePubKeyCreateKeyString(privKey
, &pubString
, &pubStringLen
);
238 printf("feePubKeyCreateKeyString: Can't get key string (%s)\n",
239 feeReturnString(frtn
));
242 pubKey
= feePubKeyAlloc();
243 frtn
= feePubKeyInitFromKeyString(pubKey
, pubString
, pubStringLen
);
245 printf("feePubKeyInitFromKeyString: Can't create new key "
247 feeReturnString(frtn
));
248 feePubKeyFree(pubKey
);
255 static char *stringFromEncrType(cipherFileEncrType encrType
)
258 case CFE_PublicDES
: return "CFE_PublicDES";
259 case CFE_RandDES
: return "CFE_RandDES";
260 case CFE_FEED
: return "CFE_FEED";
261 case CFE_FEEDExp
: return "CFE_FEEDExp";
262 default: return "Bogus encrType";
268 #define EXPLICIT_NO 0
269 #define EXPLICIT_YES 1
270 #define EXPLICIT_ERR 2
272 static void doTest(unsigned char *ptext
,
276 feePubKey theirPrivKey
,
277 feePubKey theirPubKey
,
278 cipherFileEncrType encrType
,
281 int doExplicitKey
) /* EXPLICIT_ERR means do one with
285 unsigned char *ctext
;
287 unsigned char *dectext
;
289 unsigned outUserData
= 0x1234567;
291 cipherFileEncrType inEncrType
;
292 feeSigStatus sigStatus
;
295 feeSigStatus expSigStatus
= SS_PresentValid
;
299 * These are tailored to specific encrTypes and doExplicitKeys
301 feePubKey sendPrivKey
= myPrivKey
;
302 feePubKey sendPubKey
= myPubKey
;
303 feePubKey recvPrivKey
= theirPrivKey
;
304 feePubKey recvPubKey
= theirPubKey
;
310 sendPrivKey
= NULL
; // not needed
317 printf("Hey bozo! Give me a real encrType!\n");
321 sendPubKey
= NULL
; // never needed
322 expSigStatus
= SS_NotPresent
;
324 else switch(doExplicitKey
) {
326 sendPubKey
= NULL
; // get it from cipherfile
329 break; // use myPubKey
331 if(feePubKeyIsEqual(myPubKey
, theirPubKey
)) {
332 printf("myPubKey = theirPubKey!\n");
335 sendPubKey
= theirPubKey
; // hopefully != myPubKey!
336 expSigStatus
= SS_PresentInvalid
;
339 printf("BOGUS doExplicitKey\n");
343 frtn
= createCipherFile(sendPrivKey
,
354 printf("createCipherFile: %s\n", feeReturnString(frtn
));
358 valid64
= isValidEnc64(ctext
, ctextLen
);
359 if(valid64
!= doEnc64
) {
360 printf("valid64 mismatch! exp %d got %d\n", doEnc64
, valid64
);
363 frtn
= parseCipherFile(recvPrivKey
,
374 printf("parseCipherFile: %s\n", feeReturnString(frtn
));
377 if(inEncrType
!= encrType
) {
378 printf("encrType mismatch exp %d got %d\n",
379 encrType
, inEncrType
);
382 if(inUserData
!= outUserData
) {
383 printf("userData mismatch exp %d got %d\n",
384 outUserData
, inUserData
);
387 if(sigStatus
!= expSigStatus
) {
388 printf("Bad sigStatus exp %d got %d\n",
389 expSigStatus
, sigStatus
);
392 if(ptextLen
!= dectextLen
) {
393 printf("ptextLen mismatch exp %d got %d\n",
394 ptextLen
, dectextLen
);
397 if(bcmp(ptext
, dectext
, ptextLen
)) {
398 printf("Data Miscompare\n");
408 printf("attach with debugger for more info; enter CR to quit: ");
414 int main(int argc
, char **argv
)
419 unsigned char *ptext
;
421 unsigned char passwd1
[PASSWD_LENGTH
];
422 unsigned char passwd2
[PASSWD_LENGTH
];
426 feePubKey theirPrivKey
;
428 feePubKey theirPubKey
;
434 unsigned loops
= LOOPS_DEF
;
439 unsigned minExp
= MIN_EXP
;
440 unsigned maxExp
= MAX_EXP
;
441 BOOL incrOffset
= NO
;
442 unsigned depth
= DEPTH_DEFAULT
;
443 unsigned minOffset
= MIN_OFFSET
;
446 argc
= ccommand(&argv
);
449 for(arg
=1; arg
<argc
; arg
++) {
453 loops
= atoi(&argp
[2]);
456 minExp
= atoi(&argp
[2]);
459 depth
= atoi(&argp
[2]);
462 minOffset
= atoi(&argp
[2]);
463 if(minOffset
> MAX_OFFSET
) {
464 minOffset
= MIN_OFFSET
;
466 sizeOffset
= minOffset
;
469 maxExp
= atoi(&argp
[2]);
470 if(maxExp
> MAX_EXP
) {
475 seed
= atoi(&argp
[2]);
494 time((unsigned long *)(&seed
));
497 maxSize
= dataSizeFromExp(maxExp
) + MAX_OFFSET
+ 8;
498 dataPool
= fmalloc(maxSize
);
500 printf("Starting cfileTest: loops %d seed %d depth %d\n",
503 for(loop
=1; ; loop
++) {
505 ptext
= genData(minExp
, maxExp
, DT_Random
, incrOffset
,
506 minOffset
, &ptextLen
);
508 printf("..loop %d plaintext size %d\n", loop
, ptextLen
);
512 * Generate a whole bunch of keys
514 genPasswd(passwd1
, PASSWD_LENGTH
, NO
); // not ascii!
515 genPasswd(passwd2
, PASSWD_LENGTH
, NO
);
516 myPrivKey
= genPrivKey(passwd1
, PASSWD_LENGTH
, depth
);
517 theirPrivKey
= genPrivKey(passwd2
, PASSWD_LENGTH
, depth
);
518 myPubKey
= genPubKey(myPrivKey
);
519 theirPubKey
= genPubKey(theirPrivKey
);
521 for(encrType
=CFE_PublicDES
;
522 encrType
<=CFE_FEEDExp
;
526 printf(" ..%s\n", stringFromEncrType(encrType
));
528 for(doEnc64
=0; doEnc64
<2; doEnc64
++) {
530 printf(" ..doEnc64 %d\n", doEnc64
);
534 printf(" ..no sig\n");
536 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
537 theirPrivKey
, theirPubKey
,
538 encrType
, doEnc64
, SIG_NO
, EXPLICIT_NO
);
541 printf(" ..sig, implicit sendPubKey\n");
543 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
544 theirPrivKey
, theirPubKey
,
545 encrType
, doEnc64
, SIG_YES
, EXPLICIT_NO
);
548 printf(" ..sig, explicit sendPubKey\n");
550 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
551 theirPrivKey
, theirPubKey
,
552 encrType
, doEnc64
, SIG_YES
, EXPLICIT_YES
);
555 printf(" ..sig, force error\n");
557 doTest(ptext
, ptextLen
, myPrivKey
, myPubKey
,
558 theirPrivKey
, theirPubKey
,
559 encrType
, doEnc64
, SIG_YES
, EXPLICIT_ERR
);
564 feePubKeyFree(myPrivKey
);
565 feePubKeyFree(myPubKey
);
566 feePubKeyFree(theirPrivKey
);
567 feePubKeyFree(theirPubKey
);
576 printf("cfile test complete\n");