X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/Security/libsecurity_cryptkit/ckutils/cfileTest/cfileTest.c diff --git a/Security/libsecurity_cryptkit/ckutils/cfileTest/cfileTest.c b/Security/libsecurity_cryptkit/ckutils/cfileTest/cfileTest.c new file mode 100644 index 00000000..e0951b68 --- /dev/null +++ b/Security/libsecurity_cryptkit/ckutils/cfileTest/cfileTest.c @@ -0,0 +1,589 @@ +/* + * Copyright (c) 1997,2011,2014 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + + +#include "ckutilsPlatform.h" +#include "Crypt.h" +#include "feeCipherFile.h" +#include +#include +#include + +static unsigned char *dataPool; /* plaintext comes from here */ + +#undef BOOL +#undef YES +#undef NO +#define BOOL int +#define YES 1 +#define NO 0 + +#define LOOPS_DEF 100 +#define MIN_EXP 2 /* for data size 10**exp */ +#define MAX_EXP 3 /* FEED is very slow with ptext larger than this... */ +#define DEPTH_DEFAULT FEE_DEPTH_DEFAULT +#define MIN_OFFSET 0 +#define MAX_OFFSET 99 + +#define PASSWD_LENGTH 10 + +static void usage(char **argv) +{ + printf("usage: %s [options]\n", argv[0]); + printf("Options:\n"); + printf(" l==loops (default=%d)\n", LOOPS_DEF); + printf(" n=minExp (default=%d)\n", MIN_EXP); + printf(" x=maxExp (default=max=%d)\n", MAX_EXP); + printf(" D=depth (default=%d)\n", DEPTH_DEFAULT); + printf(" N=minOffset (default=%d)\n", MIN_OFFSET); + printf(" q(uiet) v(erbose)\n"); + printf(" h(elp) I(ncrementing offset)\n"); + exit(1); +} + +/* + * ...min <= return <= max + */ +static int genRand(int min, int max) +{ + + /* note random() only yields a 31-bit number... */ + + if(max == min) /* avoid % 1 ! */ + return(max); + else + return(min + (RAND() % (max-min+1))); +} + +/* end of feeLib routines */ + +#define MIN_ASCII ' ' +#define MAX_ASCII '~' + +static void genPasswd(unsigned char *passwd, + unsigned passwdLen, BOOL ascii) +{ + unsigned *ip = (unsigned *)passwd; + unsigned intCount = passwdLen / 4; + int i; + unsigned char *cp; + unsigned residue = passwdLen & 0x3; + char ac; + + if(ascii) { + cp = passwd; + ac = MIN_ASCII; + for(i=0; i MAX_ASCII) { + ac = MIN_ASCII; + } + } + } + else { + for (i=0; i MAX_ASCII) { + ac = MIN_ASCII; + } + } + break; + case DT_Random: + #ifdef __LITTLE_ENDIAN__ + intCount = size >> 2; + ip = (unsigned *)dataPool; + for(i=0; i MAX_OFFSET) { + minOffset = MIN_OFFSET; + } + sizeOffset = minOffset; + break; + case 'x': + maxExp = atoi(&argp[2]); + if(maxExp > MAX_EXP) { + usage(argv); + } + break; + case 's': + seed = atoi(&argp[2]); + seedSpec = YES; + break; + case 'I': + incrOffset = YES; + break; + case 'q': + quiet = YES; + break; + case 'v': + verbose = YES; + break; + case 'h': + default: + usage(argv); + } + } + + if(seedSpec == NO) { + time((unsigned long *)(&seed)); + } + SRAND(seed); + maxSize = dataSizeFromExp(maxExp) + MAX_OFFSET + 8; + dataPool = fmalloc(maxSize); + + printf("Starting cfileTest: loops %d seed %d depth %d\n", + loops, seed, depth); + + for(loop=1; ; loop++) { + + ptext = genData(minExp, maxExp, DT_Random, incrOffset, + minOffset, &ptextLen); + if(!quiet) { + printf("..loop %d plaintext size %d\n", loop, ptextLen); + } + + /* + * Generate a whole bunch of keys + */ + genPasswd(passwd1, PASSWD_LENGTH, NO); // not ascii! + genPasswd(passwd2, PASSWD_LENGTH, NO); + myPrivKey = genPrivKey(passwd1, PASSWD_LENGTH, depth); + theirPrivKey = genPrivKey(passwd2, PASSWD_LENGTH, depth); + myPubKey = genPubKey(myPrivKey); + theirPubKey = genPubKey(theirPrivKey); + + for(encrType=CFE_PublicDES; + encrType<=CFE_FEEDExp; + encrType++) { + + if(verbose) { + printf(" ..%s\n", stringFromEncrType(encrType)); + } + for(doEnc64=0; doEnc64<2; doEnc64++) { + if(verbose) { + printf(" ..doEnc64 %d\n", doEnc64); + } + + if(verbose) { + printf(" ..no sig\n"); + } + doTest(ptext, ptextLen, myPrivKey, myPubKey, + theirPrivKey, theirPubKey, + encrType, doEnc64, SIG_NO, EXPLICIT_NO); + + if(verbose) { + printf(" ..sig, implicit sendPubKey\n"); + } + doTest(ptext, ptextLen, myPrivKey, myPubKey, + theirPrivKey, theirPubKey, + encrType, doEnc64, SIG_YES, EXPLICIT_NO); + + if(verbose) { + printf(" ..sig, explicit sendPubKey\n"); + } + doTest(ptext, ptextLen, myPrivKey, myPubKey, + theirPrivKey, theirPubKey, + encrType, doEnc64, SIG_YES, EXPLICIT_YES); + + if(verbose) { + printf(" ..sig, force error\n"); + } + doTest(ptext, ptextLen, myPrivKey, myPubKey, + theirPrivKey, theirPubKey, + encrType, doEnc64, SIG_YES, EXPLICIT_ERR); + + } /* for doEnc64 */ + } /* for encrType */ + + feePubKeyFree(myPrivKey); + feePubKeyFree(myPubKey); + feePubKeyFree(theirPrivKey); + feePubKeyFree(theirPubKey); + if(loops) { + if(loop == loops) { + break; + } + } + } /* main loop */ + + if(!quiet) { + printf("cfile test complete\n"); + } + return 0; +}