X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/Security/libsecurity_cryptkit/lib/giantIntegers.h diff --git a/Security/libsecurity_cryptkit/lib/giantIntegers.h b/Security/libsecurity_cryptkit/lib/giantIntegers.h new file mode 100644 index 00000000..2352a368 --- /dev/null +++ b/Security/libsecurity_cryptkit/lib/giantIntegers.h @@ -0,0 +1,162 @@ +/* Copyright (c) 1998,2011-2012,2014 Apple Inc. All Rights Reserved. + * + * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT + * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE + * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE + * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE, + * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL + * EXPOSE YOU TO LIABILITY. + *************************************************************************** + * + * giantIntegers.h - large-integer arithmetic library. + * + * Revision History + * ---------------- + * 05 Oct 98 at Apple + * Default "unsigned int" giantDigit for __i386__ and __i486__ + * 08 May 97 at Apple + * Changed size of giantstruct.n to 1 for Mac build + * Created. + */ + +#ifndef _CK_NSGIANTINTS_H_ +#define _CK_NSGIANTINTS_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Size of giant digit. + */ +#if NeXT || __i386__ || __i486__ + +typedef unsigned int giantDigit; + +/* + * used to divide by GIANT_BITS_PER_DIGIT via shift - no easy way to get + * the compiler to calculate this. + */ +#define GIANT_LOG2_BITS_PER_DIGIT 5 + +#elif defined(macintosh) || defined(__ppc__) + +typedef unsigned int giantDigit; +#define GIANT_LOG2_BITS_PER_DIGIT 5 + +#else + +typedef unsigned short giantDigit; +#define GIANT_LOG2_BITS_PER_DIGIT 4 + +#endif + +/* platform-independent digit manipulation macros */ + +#define GIANT_BYTES_PER_DIGIT (sizeof(giantDigit)) +#define GIANT_BITS_PER_DIGIT (8 * GIANT_BYTES_PER_DIGIT) +#define GIANT_DIGIT_MASK ((giantDigit)~0) +#define BYTES_TO_GIANT_DIGITS(x) \ + ((x + GIANT_BYTES_PER_DIGIT - 1) / GIANT_BYTES_PER_DIGIT) + +#define MAX_DIGITS ((1<<18)+(1<<17)) + /* 2^(16*MAX_DIGITS)-1 will fit into a giant. */ + +/* + * The giant stack package is a local cache which allows us to avoid calls + * to malloc() for borrowGiant(). On a 90 Mhz Pentium, enabling the + * giant stack package shows about a 1.35 speedup factor over an identical + * CryptKit without the giant stacks enabled. + */ +#define GIANTS_VIA_STACK CRYPTKIT_GIANT_STACK_ENABLE + +typedef struct { + int sign; /* number of giantDigits = abs(sign) */ + unsigned capacity; /* largest possible number of giantDigits */ + giantDigit n[1]; /* n[0] is l.s. digit */ +} giantstruct; +typedef giantstruct *giant; + +#if GIANTS_VIA_STACK +/* + * For giant stack debug only + * Set default giant size (i.e., for newGiant(0) and borrowGiant(0)) + */ +void setGiantSize(unsigned numDigits); + +/* + * Initialize giant stacks, with up to specified max giant size. + */ +void initGiantStacks(unsigned maxDigits); + +/* + * Free giant stacks on shutdown. + */ +void freeGiantStacks(void); + +#endif /* GIANTS_VIA_STACK */ + +giant newGiant(unsigned numDigits); +giant copyGiant(giant x); +void freeGiant(giant x); + +giant borrowGiant(unsigned numDigits); /* get a temporary */ +void returnGiant(giant); /* return it */ +unsigned bitlen(giant n); /* Returns the bit-length n; + * e.g. n=7 returns 3. */ +int bitval(giant n, int pos); /* Returns the value of bit pos of n */ +int isZero(giant g); /* Returns whether g is zero */ +int isone(giant g); /* Returns whether g is 1 */ +void gtog(giant src, giant dest); /* Copies one giant to another */ +void int_to_giant(int n, giant g); /* Gives a giant an int value */ +int gcompg(giant a, giant b); /* Returns 1, 0, -1 as a>b, a=b, a