1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0 2*5c591343SA. Cody Schuffelen * 3*5c591343SA. Cody Schuffelen * The copyright in this software is being made available under the BSD License, 4*5c591343SA. Cody Schuffelen * included below. This software may be subject to other third party and 5*5c591343SA. Cody Schuffelen * contributor rights, including patent rights, and no such rights are granted 6*5c591343SA. Cody Schuffelen * under this license. 7*5c591343SA. Cody Schuffelen * 8*5c591343SA. Cody Schuffelen * Copyright (c) Microsoft Corporation 9*5c591343SA. Cody Schuffelen * 10*5c591343SA. Cody Schuffelen * All rights reserved. 11*5c591343SA. Cody Schuffelen * 12*5c591343SA. Cody Schuffelen * BSD License 13*5c591343SA. Cody Schuffelen * 14*5c591343SA. Cody Schuffelen * Redistribution and use in source and binary forms, with or without modification, 15*5c591343SA. Cody Schuffelen * are permitted provided that the following conditions are met: 16*5c591343SA. Cody Schuffelen * 17*5c591343SA. Cody Schuffelen * Redistributions of source code must retain the above copyright notice, this list 18*5c591343SA. Cody Schuffelen * of conditions and the following disclaimer. 19*5c591343SA. Cody Schuffelen * 20*5c591343SA. Cody Schuffelen * Redistributions in binary form must reproduce the above copyright notice, this 21*5c591343SA. Cody Schuffelen * list of conditions and the following disclaimer in the documentation and/or 22*5c591343SA. Cody Schuffelen * other materials provided with the distribution. 23*5c591343SA. Cody Schuffelen * 24*5c591343SA. Cody Schuffelen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" 25*5c591343SA. Cody Schuffelen * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26*5c591343SA. Cody Schuffelen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27*5c591343SA. Cody Schuffelen * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 28*5c591343SA. Cody Schuffelen * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29*5c591343SA. Cody Schuffelen * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30*5c591343SA. Cody Schuffelen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 31*5c591343SA. Cody Schuffelen * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32*5c591343SA. Cody Schuffelen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*5c591343SA. Cody Schuffelen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34*5c591343SA. Cody Schuffelen */ 35*5c591343SA. Cody Schuffelen //** Introduction 36*5c591343SA. Cody Schuffelen 37*5c591343SA. Cody Schuffelen // This file contains the definitions needed for defining the internal BIGNUM 38*5c591343SA. Cody Schuffelen // structure. 39*5c591343SA. Cody Schuffelen 40*5c591343SA. Cody Schuffelen // A BIGNUM is a pointer to a structure. The structure has three fields. The 41*5c591343SA. Cody Schuffelen // last field is and array (d) of crypt_uword_t. Each word is in machine format 42*5c591343SA. Cody Schuffelen // (big- or little-endian) with the words in ascending significance (i.e. words 43*5c591343SA. Cody Schuffelen // in little-endian order). This is the order that seems to be used in every 44*5c591343SA. Cody Schuffelen // big number library in the worlds, so... 45*5c591343SA. Cody Schuffelen // 46*5c591343SA. Cody Schuffelen // The first field in the structure (allocated) is the number of words in 'd'. 47*5c591343SA. Cody Schuffelen // This is the upper limit on the size of the number that can be held in the 48*5c591343SA. Cody Schuffelen // structure. This differs from libraries like OpenSSL as this is not intended 49*5c591343SA. Cody Schuffelen // to deal with numbers of arbitrary size; just numbers that are needed to deal 50*5c591343SA. Cody Schuffelen // with the algorithms that are defined in the TPM implementation. 51*5c591343SA. Cody Schuffelen // 52*5c591343SA. Cody Schuffelen // The second field in the structure (size) is the number of significant words 53*5c591343SA. Cody Schuffelen // in 'n'. When this number is zero, the number is zero. The word at used-1 should 54*5c591343SA. Cody Schuffelen // never be zero. All words between d[size] and d[allocated-1] should be zero. 55*5c591343SA. Cody Schuffelen 56*5c591343SA. Cody Schuffelen //** Defines 57*5c591343SA. Cody Schuffelen 58*5c591343SA. Cody Schuffelen #ifndef _BN_NUMBERS_H 59*5c591343SA. Cody Schuffelen #define _BN_NUMBERS_H 60*5c591343SA. Cody Schuffelen 61*5c591343SA. Cody Schuffelen #if RADIX_BITS == 64 62*5c591343SA. Cody Schuffelen # define RADIX_LOG2 6 63*5c591343SA. Cody Schuffelen #elif RADIX_BITS == 32 64*5c591343SA. Cody Schuffelen #define RADIX_LOG2 5 65*5c591343SA. Cody Schuffelen #else 66*5c591343SA. Cody Schuffelen # error "Unsupported radix" 67*5c591343SA. Cody Schuffelen #endif 68*5c591343SA. Cody Schuffelen 69*5c591343SA. Cody Schuffelen #define RADIX_MOD(x) ((x) & ((1 << RADIX_LOG2) - 1)) 70*5c591343SA. Cody Schuffelen #define RADIX_DIV(x) ((x) >> RADIX_LOG2) 71*5c591343SA. Cody Schuffelen #define RADIX_MASK ((((crypt_uword_t)1) << RADIX_LOG2) - 1) 72*5c591343SA. Cody Schuffelen 73*5c591343SA. Cody Schuffelen #define BITS_TO_CRYPT_WORDS(bits) RADIX_DIV((bits) + (RADIX_BITS - 1)) 74*5c591343SA. Cody Schuffelen #define BYTES_TO_CRYPT_WORDS(bytes) BITS_TO_CRYPT_WORDS(bytes * 8) 75*5c591343SA. Cody Schuffelen #define SIZE_IN_CRYPT_WORDS(thing) BYTES_TO_CRYPT_WORDS(sizeof(thing)) 76*5c591343SA. Cody Schuffelen 77*5c591343SA. Cody Schuffelen #if RADIX_BITS == 64 78*5c591343SA. Cody Schuffelen #define SWAP_CRYPT_WORD(x) REVERSE_ENDIAN_64(x) 79*5c591343SA. Cody Schuffelen typedef uint64_t crypt_uword_t; 80*5c591343SA. Cody Schuffelen typedef int64_t crypt_word_t; 81*5c591343SA. Cody Schuffelen # define TO_CRYPT_WORD_64 BIG_ENDIAN_BYTES_TO_UINT64 82*5c591343SA. Cody Schuffelen # define TO_CRYPT_WORD_32(a, b, c, d) TO_CRYPT_WORD_64(0, 0, 0, 0, a, b, c, d) 83*5c591343SA. Cody Schuffelen #elif RADIX_BITS == 32 84*5c591343SA. Cody Schuffelen # define SWAP_CRYPT_WORD(x) REVERSE_ENDIAN_32((x)) 85*5c591343SA. Cody Schuffelen typedef uint32_t crypt_uword_t; 86*5c591343SA. Cody Schuffelen typedef int32_t crypt_word_t; 87*5c591343SA. Cody Schuffelen # define TO_CRYPT_WORD_64(a, b, c, d, e, f, g, h) \ 88*5c591343SA. Cody Schuffelen BIG_ENDIAN_BYTES_TO_UINT32(e, f, g, h), \ 89*5c591343SA. Cody Schuffelen BIG_ENDIAN_BYTES_TO_UINT32(a, b, c, d) 90*5c591343SA. Cody Schuffelen #endif 91*5c591343SA. Cody Schuffelen 92*5c591343SA. Cody Schuffelen #define MAX_CRYPT_UWORD (~((crypt_uword_t)0)) 93*5c591343SA. Cody Schuffelen #define MAX_CRYPT_WORD ((crypt_word_t)(MAX_CRYPT_UWORD >> 1)) 94*5c591343SA. Cody Schuffelen #define MIN_CRYPT_WORD (~MAX_CRYPT_WORD) 95*5c591343SA. Cody Schuffelen 96*5c591343SA. Cody Schuffelen #define LARGEST_NUMBER (MAX((ALG_RSA * MAX_RSA_KEY_BYTES), \ 97*5c591343SA. Cody Schuffelen MAX((ALG_ECC * MAX_ECC_KEY_BYTES), MAX_DIGEST_SIZE))) 98*5c591343SA. Cody Schuffelen #define LARGEST_NUMBER_BITS (LARGEST_NUMBER * 8) 99*5c591343SA. Cody Schuffelen 100*5c591343SA. Cody Schuffelen #define MAX_ECC_PARAMETER_BYTES (MAX_ECC_KEY_BYTES * ALG_ECC) 101*5c591343SA. Cody Schuffelen 102*5c591343SA. Cody Schuffelen // These are the basic big number formats. This is convertible to the library- 103*5c591343SA. Cody Schuffelen // specific format without too much difficulty. For the math performed using 104*5c591343SA. Cody Schuffelen // these numbers, the value is always positive. 105*5c591343SA. Cody Schuffelen #define BN_STRUCT_DEF(count) struct { \ 106*5c591343SA. Cody Schuffelen crypt_uword_t allocated; \ 107*5c591343SA. Cody Schuffelen crypt_uword_t size; \ 108*5c591343SA. Cody Schuffelen crypt_uword_t d[count]; \ 109*5c591343SA. Cody Schuffelen } 110*5c591343SA. Cody Schuffelen 111*5c591343SA. Cody Schuffelen typedef BN_STRUCT_DEF(1) bignum_t; 112*5c591343SA. Cody Schuffelen #ifndef bigNum 113*5c591343SA. Cody Schuffelen typedef bignum_t *bigNum; 114*5c591343SA. Cody Schuffelen typedef const bignum_t *bigConst; 115*5c591343SA. Cody Schuffelen #endif 116*5c591343SA. Cody Schuffelen 117*5c591343SA. Cody Schuffelen extern const bignum_t BnConstZero; 118*5c591343SA. Cody Schuffelen 119*5c591343SA. Cody Schuffelen // The Functions to access the properties of a big number. 120*5c591343SA. Cody Schuffelen // Get number of allocated words 121*5c591343SA. Cody Schuffelen #define BnGetAllocated(x) (unsigned)((x)->allocated) 122*5c591343SA. Cody Schuffelen 123*5c591343SA. Cody Schuffelen // Get number of words used 124*5c591343SA. Cody Schuffelen #define BnGetSize(x) ((x)->size) 125*5c591343SA. Cody Schuffelen 126*5c591343SA. Cody Schuffelen // Get a pointer to the data array 127*5c591343SA. Cody Schuffelen #define BnGetArray(x) ((crypt_uword_t *)&((x)->d[0])) 128*5c591343SA. Cody Schuffelen 129*5c591343SA. Cody Schuffelen // Get the nth word of a BIGNUM (zero-based) 130*5c591343SA. Cody Schuffelen #define BnGetWord(x, i) (crypt_uword_t)((x)->d[i]) 131*5c591343SA. Cody Schuffelen 132*5c591343SA. Cody Schuffelen // Some things that are done often. 133*5c591343SA. Cody Schuffelen 134*5c591343SA. Cody Schuffelen // Test to see if a bignum_t is equal to zero 135*5c591343SA. Cody Schuffelen #define BnEqualZero(bn) (BnGetSize(bn) == 0) 136*5c591343SA. Cody Schuffelen 137*5c591343SA. Cody Schuffelen // Test to see if a bignum_t is equal to a word type 138*5c591343SA. Cody Schuffelen #define BnEqualWord(bn, word) \ 139*5c591343SA. Cody Schuffelen ((BnGetSize(bn) == 1) && (BnGetWord(bn, 0) == (crypt_uword_t)word)) 140*5c591343SA. Cody Schuffelen 141*5c591343SA. Cody Schuffelen // Determine if a BIGNUM is even. A zero is even. Although the 142*5c591343SA. Cody Schuffelen // indication that a number is zero is that its size is zero, 143*5c591343SA. Cody Schuffelen // all words of the number are 0 so this test works on zero. 144*5c591343SA. Cody Schuffelen #define BnIsEven(n) ((BnGetWord(n, 0) & 1) == 0) 145*5c591343SA. Cody Schuffelen 146*5c591343SA. Cody Schuffelen // The macros below are used to define BIGNUM values of the required 147*5c591343SA. Cody Schuffelen // size. The values are allocated on the stack so they can be 148*5c591343SA. Cody Schuffelen // treated like simple local values. 149*5c591343SA. Cody Schuffelen 150*5c591343SA. Cody Schuffelen // This will call the initialization function for a defined bignum_t. 151*5c591343SA. Cody Schuffelen // This sets the allocated and used fields and clears the words of 'n'. 152*5c591343SA. Cody Schuffelen #define BN_INIT(name) \ 153*5c591343SA. Cody Schuffelen (bigNum)BnInit((bigNum)&(name), \ 154*5c591343SA. Cody Schuffelen BYTES_TO_CRYPT_WORDS(sizeof(name.d))) 155*5c591343SA. Cody Schuffelen 156*5c591343SA. Cody Schuffelen // In some cases, a function will need the address of the structure 157*5c591343SA. Cody Schuffelen // associated with a variable. The structure for a BIGNUM variable 158*5c591343SA. Cody Schuffelen // of 'name' is 'name_'. Generally, when the structure is created, it 159*5c591343SA. Cody Schuffelen // is initialized and a parameter is created with a pointer to the 160*5c591343SA. Cody Schuffelen // structure. The pointer has the 'name' and the structure it points 161*5c591343SA. Cody Schuffelen // to is 'name_' 162*5c591343SA. Cody Schuffelen #define BN_ADDRESS(name) (bigNum)&name##_ 163*5c591343SA. Cody Schuffelen 164*5c591343SA. Cody Schuffelen #define BN_CONST(name, words, initializer) \ 165*5c591343SA. Cody Schuffelen typedef const struct name##_type { \ 166*5c591343SA. Cody Schuffelen crypt_uword_t allocated; \ 167*5c591343SA. Cody Schuffelen crypt_uword_t size; \ 168*5c591343SA. Cody Schuffelen crypt_uword_t d[words < 1 ? 1 : words]; \ 169*5c591343SA. Cody Schuffelen } name##_type; \ 170*5c591343SA. Cody Schuffelen name##_type name = {(words < 1 ? 1 : words), words, {initializer}}; 171*5c591343SA. Cody Schuffelen 172*5c591343SA. Cody Schuffelen #define BN_STRUCT_ALLOCATION(bits) (BITS_TO_CRYPT_WORDS(bits) + 1) 173*5c591343SA. Cody Schuffelen 174*5c591343SA. Cody Schuffelen // Create a structure of the correct size. 175*5c591343SA. Cody Schuffelen #define BN_STRUCT(bits) \ 176*5c591343SA. Cody Schuffelen BN_STRUCT_DEF(BN_STRUCT_ALLOCATION(bits)) 177*5c591343SA. Cody Schuffelen 178*5c591343SA. Cody Schuffelen // Define a BIGNUM type with a specific allocation 179*5c591343SA. Cody Schuffelen #define BN_TYPE(name, bits) \ 180*5c591343SA. Cody Schuffelen typedef BN_STRUCT(bits) bn_##name##_t 181*5c591343SA. Cody Schuffelen 182*5c591343SA. Cody Schuffelen // This creates a local BIGNUM variable of a specific size and 183*5c591343SA. Cody Schuffelen // initializes it from a TPM2B input parameter. 184*5c591343SA. Cody Schuffelen #define BN_INITIALIZED(name, bits, initializer) \ 185*5c591343SA. Cody Schuffelen BN_STRUCT(bits) name##_; \ 186*5c591343SA. Cody Schuffelen bigNum name = BnFrom2B(BN_INIT(name##_), \ 187*5c591343SA. Cody Schuffelen (const TPM2B *)initializer) 188*5c591343SA. Cody Schuffelen 189*5c591343SA. Cody Schuffelen // Create a local variable that can hold a number with 'bits' 190*5c591343SA. Cody Schuffelen #define BN_VAR(name, bits) \ 191*5c591343SA. Cody Schuffelen BN_STRUCT(bits) _##name; \ 192*5c591343SA. Cody Schuffelen bigNum name = BN_INIT(_##name) 193*5c591343SA. Cody Schuffelen 194*5c591343SA. Cody Schuffelen // Create a type that can hold the largest number defined by the 195*5c591343SA. Cody Schuffelen // implementation. 196*5c591343SA. Cody Schuffelen #define BN_MAX(name) BN_VAR(name, LARGEST_NUMBER_BITS) 197*5c591343SA. Cody Schuffelen #define BN_MAX_INITIALIZED(name, initializer) \ 198*5c591343SA. Cody Schuffelen BN_INITIALIZED(name, LARGEST_NUMBER_BITS, initializer) 199*5c591343SA. Cody Schuffelen 200*5c591343SA. Cody Schuffelen // A word size value is useful 201*5c591343SA. Cody Schuffelen #define BN_WORD(name) BN_VAR(name, RADIX_BITS) 202*5c591343SA. Cody Schuffelen 203*5c591343SA. Cody Schuffelen // This is used to create a word-size BIGNUM and initialize it with 204*5c591343SA. Cody Schuffelen // an input parameter to a function. 205*5c591343SA. Cody Schuffelen #define BN_WORD_INITIALIZED(name, initial) \ 206*5c591343SA. Cody Schuffelen BN_STRUCT(RADIX_BITS) name##_; \ 207*5c591343SA. Cody Schuffelen bigNum name = BnInitializeWord((bigNum)&name##_, \ 208*5c591343SA. Cody Schuffelen BN_STRUCT_ALLOCATION(RADIX_BITS), initial) 209*5c591343SA. Cody Schuffelen 210*5c591343SA. Cody Schuffelen // ECC-Specific Values 211*5c591343SA. Cody Schuffelen 212*5c591343SA. Cody Schuffelen // This is the format for a point. It is always in affine format. The Z value is 213*5c591343SA. Cody Schuffelen // carried as part of the point, primarily to simplify the interface to the support 214*5c591343SA. Cody Schuffelen // library. Rather than have the interface layer have to create space for the 215*5c591343SA. Cody Schuffelen // point each time it is used... 216*5c591343SA. Cody Schuffelen // The x, y, and z values are pointers to bigNum values and not in-line versions of 217*5c591343SA. Cody Schuffelen // the numbers. This is a relic of the days when there was no standard TPM format 218*5c591343SA. Cody Schuffelen // for the numbers 219*5c591343SA. Cody Schuffelen typedef struct _bn_point_t 220*5c591343SA. Cody Schuffelen { 221*5c591343SA. Cody Schuffelen bigNum x; 222*5c591343SA. Cody Schuffelen bigNum y; 223*5c591343SA. Cody Schuffelen bigNum z; 224*5c591343SA. Cody Schuffelen } bn_point_t; 225*5c591343SA. Cody Schuffelen 226*5c591343SA. Cody Schuffelen typedef bn_point_t *bigPoint; 227*5c591343SA. Cody Schuffelen typedef const bn_point_t *pointConst; 228*5c591343SA. Cody Schuffelen 229*5c591343SA. Cody Schuffelen typedef struct constant_point_t 230*5c591343SA. Cody Schuffelen { 231*5c591343SA. Cody Schuffelen bigConst x; 232*5c591343SA. Cody Schuffelen bigConst y; 233*5c591343SA. Cody Schuffelen bigConst z; 234*5c591343SA. Cody Schuffelen } constant_point_t; 235*5c591343SA. Cody Schuffelen 236*5c591343SA. Cody Schuffelen #define ECC_BITS (MAX_ECC_KEY_BYTES * 8) 237*5c591343SA. Cody Schuffelen BN_TYPE(ecc, ECC_BITS); 238*5c591343SA. Cody Schuffelen #define ECC_NUM(name) BN_VAR(name, ECC_BITS) 239*5c591343SA. Cody Schuffelen #define ECC_INITIALIZED(name, initializer) \ 240*5c591343SA. Cody Schuffelen BN_INITIALIZED(name, ECC_BITS, initializer) 241*5c591343SA. Cody Schuffelen 242*5c591343SA. Cody Schuffelen #define POINT_INSTANCE(name, bits) \ 243*5c591343SA. Cody Schuffelen BN_STRUCT (bits) name##_x = \ 244*5c591343SA. Cody Schuffelen {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \ 245*5c591343SA. Cody Schuffelen BN_STRUCT ( bits ) name##_y = \ 246*5c591343SA. Cody Schuffelen {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \ 247*5c591343SA. Cody Schuffelen BN_STRUCT ( bits ) name##_z = \ 248*5c591343SA. Cody Schuffelen {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \ 249*5c591343SA. Cody Schuffelen bn_point_t name##_ 250*5c591343SA. Cody Schuffelen 251*5c591343SA. Cody Schuffelen #define POINT_INITIALIZER(name) \ 252*5c591343SA. Cody Schuffelen BnInitializePoint(&name##_, (bigNum)&name##_x, \ 253*5c591343SA. Cody Schuffelen (bigNum)&name##_y, (bigNum)&name##_z) 254*5c591343SA. Cody Schuffelen 255*5c591343SA. Cody Schuffelen #define POINT_INITIALIZED(name, initValue) \ 256*5c591343SA. Cody Schuffelen POINT_INSTANCE(name, MAX_ECC_KEY_BITS); \ 257*5c591343SA. Cody Schuffelen bigPoint name = BnPointFrom2B( \ 258*5c591343SA. Cody Schuffelen POINT_INITIALIZER(name), \ 259*5c591343SA. Cody Schuffelen initValue) 260*5c591343SA. Cody Schuffelen 261*5c591343SA. Cody Schuffelen #define POINT_VAR(name, bits) \ 262*5c591343SA. Cody Schuffelen POINT_INSTANCE (name, bits); \ 263*5c591343SA. Cody Schuffelen bigPoint name = POINT_INITIALIZER(name) 264*5c591343SA. Cody Schuffelen 265*5c591343SA. Cody Schuffelen #define POINT(name) POINT_VAR(name, MAX_ECC_KEY_BITS) 266*5c591343SA. Cody Schuffelen 267*5c591343SA. Cody Schuffelen // Structure for the curve parameters. This is an analog to the 268*5c591343SA. Cody Schuffelen // TPMS_ALGORITHM_DETAIL_ECC 269*5c591343SA. Cody Schuffelen typedef struct 270*5c591343SA. Cody Schuffelen { 271*5c591343SA. Cody Schuffelen bigConst prime; // a prime number 272*5c591343SA. Cody Schuffelen bigConst order; // the order of the curve 273*5c591343SA. Cody Schuffelen bigConst h; // cofactor 274*5c591343SA. Cody Schuffelen bigConst a; // linear coefficient 275*5c591343SA. Cody Schuffelen bigConst b; // constant term 276*5c591343SA. Cody Schuffelen constant_point_t base; // base point 277*5c591343SA. Cody Schuffelen } ECC_CURVE_DATA; 278*5c591343SA. Cody Schuffelen 279*5c591343SA. Cody Schuffelen // Access macros for the ECC_CURVE structure. The parameter 'C' is a pointer 280*5c591343SA. Cody Schuffelen // to an ECC_CURVE_DATA structure. In some libraries, the curve structure contains 281*5c591343SA. Cody Schuffelen // a pointer to an ECC_CURVE_DATA structure as well as some other bits. For those 282*5c591343SA. Cody Schuffelen // cases, the AccessCurveData macro is used in the code to first get the pointer 283*5c591343SA. Cody Schuffelen // to the ECC_CURVE_DATA for access. In some cases, the macro does nothing. 284*5c591343SA. Cody Schuffelen #define CurveGetPrime(C) ((C)->prime) 285*5c591343SA. Cody Schuffelen #define CurveGetOrder(C) ((C)->order) 286*5c591343SA. Cody Schuffelen #define CurveGetCofactor(C) ((C)->h) 287*5c591343SA. Cody Schuffelen #define CurveGet_a(C) ((C)->a) 288*5c591343SA. Cody Schuffelen #define CurveGet_b(C) ((C)->b) 289*5c591343SA. Cody Schuffelen #define CurveGetG(C) ((pointConst)&((C)->base)) 290*5c591343SA. Cody Schuffelen #define CurveGetGx(C) ((C)->base.x) 291*5c591343SA. Cody Schuffelen #define CurveGetGy(C) ((C)->base.y) 292*5c591343SA. Cody Schuffelen 293*5c591343SA. Cody Schuffelen 294*5c591343SA. Cody Schuffelen // Convert bytes in initializers 295*5c591343SA. Cody Schuffelen // This is used for CryptEccData.c. 296*5c591343SA. Cody Schuffelen #define BIG_ENDIAN_BYTES_TO_UINT32(a, b, c, d) \ 297*5c591343SA. Cody Schuffelen ( ((UINT32)(a) << 24) \ 298*5c591343SA. Cody Schuffelen + ((UINT32)(b) << 16) \ 299*5c591343SA. Cody Schuffelen + ((UINT32)(c) << 8) \ 300*5c591343SA. Cody Schuffelen + ((UINT32)(d)) \ 301*5c591343SA. Cody Schuffelen ) 302*5c591343SA. Cody Schuffelen 303*5c591343SA. Cody Schuffelen #define BIG_ENDIAN_BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \ 304*5c591343SA. Cody Schuffelen ( ((UINT64)(a) << 56) \ 305*5c591343SA. Cody Schuffelen + ((UINT64)(b) << 48) \ 306*5c591343SA. Cody Schuffelen + ((UINT64)(c) << 40) \ 307*5c591343SA. Cody Schuffelen + ((UINT64)(d) << 32) \ 308*5c591343SA. Cody Schuffelen + ((UINT64)(e) << 24) \ 309*5c591343SA. Cody Schuffelen + ((UINT64)(f) << 16) \ 310*5c591343SA. Cody Schuffelen + ((UINT64)(g) << 8) \ 311*5c591343SA. Cody Schuffelen + ((UINT64)(h)) \ 312*5c591343SA. Cody Schuffelen ) 313*5c591343SA. Cody Schuffelen 314*5c591343SA. Cody Schuffelen #ifndef RADIX_BYTES 315*5c591343SA. Cody Schuffelen # if RADIX_BITS == 32 316*5c591343SA. Cody Schuffelen # define RADIX_BYTES 4 317*5c591343SA. Cody Schuffelen # elif RADIX_BITS == 64 318*5c591343SA. Cody Schuffelen # define RADIX_BYTES 8 319*5c591343SA. Cody Schuffelen # else 320*5c591343SA. Cody Schuffelen # error "RADIX_BITS must either be 32 or 64" 321*5c591343SA. Cody Schuffelen # endif 322*5c591343SA. Cody Schuffelen #endif 323*5c591343SA. Cody Schuffelen 324*5c591343SA. Cody Schuffelen // These macros are used for data initialization of big number ECC constants 325*5c591343SA. Cody Schuffelen // These two macros combine a macro for data definition with a macro for 326*5c591343SA. Cody Schuffelen // structure initilization. The 'a' parameter is a macro that gives numbers to 327*5c591343SA. Cody Schuffelen // each of the bytes of the initializer and defines where each of the numberd 328*5c591343SA. Cody Schuffelen // bytes will show up in the final structure. The 'b' value is a structure that 329*5c591343SA. Cody Schuffelen // contains the requisite number of bytes in big endian order. S, the MJOIN 330*5c591343SA. Cody Schuffelen // and JOIND macros will combine a macro defining a data layout with a macro defining 331*5c591343SA. Cody Schuffelen // the data to be places. Generally, these macros will only need expansion when 332*5c591343SA. Cody Schuffelen // CryptEccData.c gets compiled. 333*5c591343SA. Cody Schuffelen #define JOINED(a,b) a b 334*5c591343SA. Cody Schuffelen #define MJOIN(a,b) a b 335*5c591343SA. Cody Schuffelen 336*5c591343SA. Cody Schuffelen #define B4_TO_BN(a, b, c, d) (((((a << 8) + b) << 8) + c) + d) 337*5c591343SA. Cody Schuffelen #if RADIX_BYTES == 64 338*5c591343SA. Cody Schuffelen #define B8_TO_BN(a, b, c, d, e, f, g, h) \ 339*5c591343SA. Cody Schuffelen (UINT64)(((((((((((((((a) << 8) | b) << 8) | c) << 8) | d) << 8) \ 340*5c591343SA. Cody Schuffelen e) << 8) | f) << 8) | g) << 8) | h) 341*5c591343SA. Cody Schuffelen #define B1_TO_BN(a) B8_TO_BN(0, 0, 0, 0, 0, 0, 0, a) 342*5c591343SA. Cody Schuffelen #define B2_TO_BN(a, b) B8_TO_BN(0, 0, 0, 0, 0, 0, a, b) 343*5c591343SA. Cody Schuffelen #define B3_TO_BN(a, b, c) B8_TO_BN(0, 0, 0, 0, 0, a, b, c) 344*5c591343SA. Cody Schuffelen #define B4_TO_BN(a, b, c, d) B8_TO_BN(0, 0, 0, 0, a, b, c, d) 345*5c591343SA. Cody Schuffelen #define B5_TO_BN(a, b, c, d, e) B8_TO_BN(0, 0, 0, a, b, c, d, e) 346*5c591343SA. Cody Schuffelen #define B6_TO_BN(a, b, c, d, e, f) B8_TO_BN(0, 0, a, b, c, d, e, f) 347*5c591343SA. Cody Schuffelen #define B7_TO_BN(a, b, c, d, e, f, g) B8_TO_BN(0, a, b, c, d, e, f, g) 348*5c591343SA. Cody Schuffelen #else 349*5c591343SA. Cody Schuffelen #define B1_TO_BN(a) B4_TO_BN(0, 0, 0, a) 350*5c591343SA. Cody Schuffelen #define B2_TO_BN(a, b) B4_TO_BN(0, 0, a, b) 351*5c591343SA. Cody Schuffelen #define B3_TO_BN(a, b, c) B4_TO_BN(0, a, b, c) 352*5c591343SA. Cody Schuffelen #define B4_TO_BN(a, b, c, d) (((((a << 8) + b) << 8) + c) + d) 353*5c591343SA. Cody Schuffelen #define B5_TO_BN(a, b, c, d, e) B4_TO_BN(b, c, d, e), B1_TO_BN(a) 354*5c591343SA. Cody Schuffelen #define B6_TO_BN(a, b, c, d, e, f) B4_TO_BN(c, d, e, f), B2_TO_BN(a, b) 355*5c591343SA. Cody Schuffelen #define B7_TO_BN(a, b, c, d, e, f, g) B4_TO_BN(d, e, f, g), B3_TO_BN(a, b, c) 356*5c591343SA. Cody Schuffelen #define B8_TO_BN(a, b, c, d, e, f, g, h) B4_TO_BN(e, f, g, h), B4_TO_BN(a, b, c, d) 357*5c591343SA. Cody Schuffelen 358*5c591343SA. Cody Schuffelen #endif 359*5c591343SA. Cody Schuffelen 360*5c591343SA. Cody Schuffelen // Add implementation dependent definitions for other ECC Values and for linkages. 361*5c591343SA. Cody Schuffelen #include LIB_INCLUDE(MATH_LIB, Math) 362*5c591343SA. Cody Schuffelen 363*5c591343SA. Cody Schuffelen 364*5c591343SA. Cody Schuffelen #endif // _BN_NUMBERS_H