X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/5c19dc3ae3bd8e40a9c028b0deddd50ff337692c..7e6b461318c8a779d91381531435a68ee4e8b6ed:/OSX/libsecurity_cryptkit/lib/feeRandom.c?ds=inline diff --git a/OSX/libsecurity_cryptkit/lib/feeRandom.c b/OSX/libsecurity_cryptkit/lib/feeRandom.c index 1fedc6ca..83ba0a92 100644 --- a/OSX/libsecurity_cryptkit/lib/feeRandom.c +++ b/OSX/libsecurity_cryptkit/lib/feeRandom.c @@ -23,184 +23,46 @@ */ #include "feeRandom.h" -#include "giantIntegers.h" -#include "elliptic.h" -#include "falloc.h" -#include "feeDebug.h" -#include "byteRep.h" -#include #include "platform.h" +#include -/* - * 1 ==> do extra nextNum on feeRandAllocWithSeed() - */ -#define EXTRA_NEXT_NUM 0 - -#define RANDBITS 128 /* must be 0 mod GIANT_BITS_PER_DIGIT */ -#define RAND_GIANT_DIGITS (RANDBITS/GIANT_BITS_PER_DIGIT) - -typedef struct { - giant A; - giant C; - giant SEED; - giant x; -} randInst; - -#if GIANTS_VIA_STACK - -/* - * Prime the curveParams and giants modules for quick allocs of giants. - */ -static int giantsInitd = 0; - -static void feeRandInitGiants() +feeRand feeRandAllocWithSeed(__attribute__((unused)) unsigned seed) { - if(giantsInitd) { - return; - } - curveParamsInitGiants(); - giantsInitd = 1; -} -#endif - -static void pmod(giant x, int bits) { - /* Force x to be x (mod 2^bits). */ - int j; - int digits = bits / GIANT_BITS_PER_DIGIT; - - for(j = (digits-1); j >= 0; j--) { - if(x->n[j] != 0) break; - } - x->sign = j+1; -} - - -feeRand feeRandAllocWithSeed(unsigned seed) -{ - randInst *rinst = (randInst *) fmalloc(sizeof(randInst)); - int digits = RAND_GIANT_DIGITS * 4; - unsigned j; - - #if GIANTS_VIA_STACK - feeRandInitGiants(); - #endif - rinst->SEED = newGiant(digits); - rinst->C = newGiant(digits); - rinst->A = newGiant(digits); - rinst->x = newGiant(digits); - rinst->C->sign = rinst->A->sign = rinst->SEED->sign = RAND_GIANT_DIGITS; - for(j=0; jC->n[j] = (giantDigit)(seed + 0xdddddddd - j); - rinst->A->n[j] = (giantDigit)(seed + 0xfff12223 + j); - rinst->SEED->n[j] = (giantDigit)(seed + j); - } - - /* - * on the first feeRandBytes or feeRandNextNum, bytes 4 and 5 of - * the result are duplicated 4.5 times (up to byte 15). Subsequent - * data is indeed random. Thus... - */ - #if EXTRA_NEXT_NUM - feeRandNextNum(rinst); - #endif // EXTRA_NEXT_NUM - return rinst; + return NULL; } feeRand feeRandAlloc(void) { - return feeRandAllocWithSeed(createRandomSeed()); + return NULL; } -void feeRandFree(feeRand frand) +void feeRandFree(__attribute__((unused)) feeRand frand) { - randInst *rinst = (randInst *) frand; - clearGiant(rinst->A); - freeGiant(rinst->A); - clearGiant(rinst->C); - freeGiant(rinst->C); - clearGiant(rinst->SEED); - freeGiant(rinst->SEED); - clearGiant(rinst->x); - freeGiant(rinst->x); - ffree(rinst); } unsigned feeRandNextNum(feeRand frand) { - randInst *rinst = (randInst *) frand; - unsigned rtn; + unsigned rand; - mulg(rinst->A, rinst->SEED); - addg(rinst->C, rinst->SEED); - pmod(rinst->SEED, RANDBITS); - gtog(rinst->SEED, rinst->x); + feeRandBytes(frand, &rand, sizeof(rand)); - /* - * FIXME - this is not quite correct; rinst->x only has 4 bytes - * of valid data if RANDBITS is known to be greater than or equal - * to 32. - */ - rtn = byteRepToInt((unsigned char *)&rinst->x->n); - return rtn; + return rand; } -void feeRandBytes(feeRand frand, - unsigned char *bytes, /* must be alloc'd by caller */ - unsigned numBytes) +void feeRandBytes(__attribute__((unused)) feeRand frand, void *bytes, unsigned numBytes) { - randInst *rinst = (randInst *) frand; - int length; - unsigned toCopy; - unsigned char *cp = bytes; + int err; - for (length = numBytes; length > 0; length -= RANDBITS/8) { - mulg(rinst->A, rinst->SEED); - addg(rinst->C, rinst->SEED); - pmod(rinst->SEED, RANDBITS); - gtog(rinst->SEED, rinst->x); - - toCopy = RANDBITS/8; - if(length < toCopy) { - toCopy = length; - } - - /* - * FIXME - not 100% platform independent.... - */ - bcopy(rinst->x->n, cp, toCopy); - cp += toCopy; - } + err = SecRandomCopyBytes(kSecRandomDefault, numBytes, bytes); + if (err != errSecSuccess) { + CKRaise("feeRandBytes"); + } } /* new function, 5 March 1999 - dmitch */ -void feeRandAddEntropy(feeRand frand, unsigned entropy) +void feeRandAddEntropy(__attribute__((unused)) feeRand frand, __attribute__((unused)) unsigned entropy) { - randInst *rinst = (randInst *) frand; - giant tmp = borrowGiant(RAND_GIANT_DIGITS); - unsigned i; - - if(entropy == 0) { - /* boy would that be a mistake */ - entropy = 0x12345; - } - for(i=0; in[i] = (giantDigit)entropy; - } - tmp->sign = RAND_GIANT_DIGITS; - mulg(tmp, rinst->SEED); - addg(rinst->C, rinst->SEED); - pmod(rinst->SEED, RANDBITS); - entropy ^= 0xff0ff0ff; - if(entropy == 0) { - entropy = 0x12345; - } - for(i=0; in[i] = (giantDigit)entropy; - } - mulg(tmp, rinst->A); - addg(rinst->C, rinst->A); - pmod(rinst->A, RANDBITS); - /* leave C alone */ - returnGiant(tmp); + } +