X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/cspxutils/randTest/randTest.c?ds=sidebyside diff --git a/SecurityTests/cspxutils/randTest/randTest.c b/SecurityTests/cspxutils/randTest/randTest.c new file mode 100644 index 00000000..5466d62f --- /dev/null +++ b/SecurityTests/cspxutils/randTest/randTest.c @@ -0,0 +1,219 @@ +/* + * Simple interactive CSP RNG exerciser. + */ +#include +#include +#include +#include +#include +#include "cspwrap.h" +#include "common.h" + +#define RAND_ALG CSSM_ALGID_APPLE_YARROW +#define BUFSIZE 32 + +static void usage(char **argv) +{ + printf("usage: %s [options]\n", argv[0]); + printf("Options:\n"); + printf(" D (CSP/DL; default = bare CSP)\n"); + exit(1); +} + +static void dumpBuf(uint8 *buf, + unsigned len) +{ + unsigned i; + + printf(" "); + for(i=0; i gen a new one + // else use this + CSSM_CRYPTO_DATA_PTR seed, // optional + unsigned len, + CSSM_BOOL weMalloc) +{ + CSSM_RETURN crtn; + CSSM_DATA data = {0, NULL}; + + /* optional existing context */ + if(ccHand == 0) { + crtn = CSSM_CSP_CreateRandomGenContext( + cspHand, + RAND_ALG, + seed, + len, + &ccHand); + if(crtn) { + printError("CSSM_CSP_CreateRandomGenContext", crtn); + return; + } + } + + /* who mallocs the data? */ + if(weMalloc) { + data.Data = (uint8 *)appMalloc(len, NULL); + if(data.Data == NULL) { + printf("***malloc failure\n"); + return; + } + data.Length = len; + } + + /* go for it */ + crtn = CSSM_GenerateRandom(ccHand, &data); + if(crtn) { + printError("CSSM_GenerateRandom", crtn); + return; + } + + dumpBuf(data.Data, data.Length); + appFree(data.Data, NULL); + return; +} + +#define SEED_SIZE 32 + +/* + * CryptoData callback for optional random seed. + */ +CSSM_RETURN seedCallback( + CSSM_DATA_PTR OutData, + void *CallerCtx) +{ + int i, j; + static unsigned char seed[SEED_SIZE]; + + OutData->Length = SEED_SIZE; + OutData->Data = seed; + for(i=SEED_SIZE, j=0; i>0; i--, j++) { + seed[j] = i; + } + return CSSM_OK; +} + +int main(int argc, char **argv) +{ + int arg; + char *argp; + CSSM_CSP_HANDLE cspHand; + CSSM_CC_HANDLE ccHand = 0; + CSSM_RETURN crtn; + CSSM_BOOL bareCsp = CSSM_TRUE; + char resp = 'n'; + // initial op = get random data + CSSM_BOOL weMalloc = CSSM_FALSE; + unsigned char seed[SEED_SIZE]; + CSSM_CRYPTO_DATA cseed; + int i; + unsigned reqLen = 16; + CSSM_BOOL explicitSeed; + + for(arg=1; arg