| *********************************************************************************************/ |
| 6 |
| 7 | #ifndef UTILS_H_ |
| 8 | #define UTILS_H_ |
| 9 |
| 10 | #include <openssl/base.h> |
| 11 |
| 12 | #include "../crypto/internal.h" |
| 13 | #include "sike.h" |
| 14 |
| 15 | // Conversion macro from number of bits to number of bytes |
| 16 | #define BITS_TO_BYTES(nbits) (((nbits)+7)/8) |
| 17 |
| 18 | // Bit size of the field |
| 19 | #define BITS_FIELD 434 |
| 20 | // Byte size of the field |
| 21 | #define FIELD_BYTESZ BITS_TO_BYTES(BITS_FIELD) |
| 22 | // Number of 64-bit words of a 224-bit element |
| 23 | #define NBITS_ORDER 224 |
| 24 | #define NWORDS64_ORDER ((NBITS_ORDER+63)/64) |
| 25 | // Number of elements in Alice's strategy |
| 26 | #define A_max 108 |
| 27 | // Number of elements in Bob's strategy |
| 28 | #define B_max 137 |
| 29 | // Word size size |
| 30 | #define RADIX sizeof(crypto_word_t)*8 |
| 31 | // Byte size of a limb |
| 32 | #define LSZ sizeof(crypto_word_t) |
| 33 |
| 34 | #if defined(OPENSSL_64_BIT) |
| 35 | // Number of words of a 434-bit field element |
| 36 | #define NWORDS_FIELD 7 |
| 37 | // Number of "0" digits in the least significant part of p434 + 1 |
| 38 | #define ZERO_WORDS 3 |
| 39 | // U64_TO_WORDS expands |x| for a |crypto_word_t| array literal. |
| 40 | #define U64_TO_WORDS(x) UINT64_C(x) |
| 41 | #else |
| 42 | // Number of words of a 434-bit field element |
| 43 | #define NWORDS_FIELD 14 |
| 44 | // Number of "0" digits in the least significant part of p434 + 1 |
| 45 | #define ZERO_WORDS 6 |
| 46 | // U64_TO_WORDS expands |x| for a |crypto_word_t| array literal. |
| 47 | #define U64_TO_WORDS(x) \ |
| 48 | (uint32_t)(UINT64_C(x) & 0xffffffff), (uint32_t)(UINT64_C(x) >> 32) |
| 49 | #endif |
| 50 |
| 51 | // Extended datatype support |
| 52 | #if !defined(BORINGSSL_HAS_UINT128) |
| 53 | typedef uint64_t uint128_t[2]; |
| 54 | #endif |
| 55 |
| 56 | // The following functions return 1 (TRUE) if condition is true, 0 (FALSE) otherwise |
| 57 | // Digit multiplication |
| 58 | #define MUL(multiplier, multiplicand, hi, lo) digit_x_digit((multiplier), (multiplicand), &(lo)); |
| 59 |
| 60 | // If mask |x|==0xff.ff set |x| to 1, otherwise 0 |
| 61 | #define M2B(x) ((x)>>(RADIX-1)) |
| 62 |
| 63 | // Digit addition with carry |
| 64 | #define ADDC(carryIn, addend1, addend2, carryOut, sumOut) \ |
| 65 | do { \ |
| 66 | crypto_word_t tempReg = (addend1) + (crypto_word_t)(carryIn); \ |
| 67 | (sumOut) = (addend2) + tempReg; \ |
| 68 | (carryOut) = M2B(constant_time_lt_w(tempReg, (crypto_word_t)(carryIn)) | \ |
| 69 | constant_time_lt_w((sumOut), tempReg)); \ |
| 70 | } while(0) |
| 71 |
| 72 | // Digit subtraction with borrow |
| 73 | #define SUBC(borrowIn, minuend, subtrahend, borrowOut, differenceOut) \ |
| 74 | do { \ |
| 75 | crypto_word_t tempReg = (minuend) - (subtrahend); \ |
| 76 | crypto_word_t borrowReg = M2B(constant_time_lt_w((minuend), (subtrahend))); \ |
| 77 | borrowReg |= ((borrowIn) & constant_time_is_zero_w(tempReg)); \ |
| 78 | (differenceOut) = tempReg - (crypto_word_t)(borrowIn); \ |
| 79 | (borrowOut) = borrowReg; \ |
| 80 | } while(0) |
| 81 |
| 82 | /* Old GCC 4.9 (jessie) doesn't implement {0} initialization properly, |
| 83 | which violates C11 as described in 6.7.9, 21 (similarily C99, 6.7.8). |
| 84 | Defines below are used to work around the bug, and provide a way |
| 85 | to initialize f2elem_t and point_proj_t structs. |
| 86 | Bug has been fixed in GCC6 (debian stretch). |
| 87 | */ |
| 88 | #define F2ELM_INIT {{ {0}, {0} }} |
| 89 | #define POINT_PROJ_INIT {{ F2ELM_INIT, F2ELM_INIT }} |
| 90 |
| 91 | // Datatype for representing 434-bit field elements (448-bit max.) |
| 92 | // Elements over GF(p434) are encoded in 63 octets in little endian format |
| 93 | // (i.e., the least significant octet is located in the lowest memory address). |
| 94 | typedef crypto_word_t felm_t[NWORDS_FIELD]; |
| 95 |
| 96 | // An element in F_{p^2}, is composed of two coefficients from F_p, * i.e. |
| 97 | // Fp2 element = c0 + c1*i in F_{p^2} |
| 98 | // Datatype for representing double-precision 2x434-bit field elements (448-bit max.) |
| 99 | // Elements (a+b*i) over GF(p434^2), where a and b are defined over GF(p434), are |
| 100 | // encoded as {a, b}, with a in the lowest memory portion. |
| 101 | typedef struct { |
| 102 | felm_t c0; |
| 103 | felm_t c1; |
| 104 | } fp2; |
| 105 |
| 106 | // Our F_{p^2} element type is a pointer to the struct. |
| 107 | typedef fp2 f2elm_t[1]; |
| 108 |
| 109 | // Datatype for representing double-precision 2x434-bit |
| 110 | // field elements in contiguous memory. |
| 111 | typedef crypto_word_t dfelm_t[2*NWORDS_FIELD]; |
| 112 |
| 113 | // Constants used during SIKE computation. |
| 114 | struct params_t { |
| 115 | // Stores a prime |
| 116 | const crypto_word_t prime[NWORDS_FIELD]; |
| 117 | // Stores prime + 1 |
| 118 | const crypto_word_t prime_p1[NWORDS_FIELD]; |
| 119 | // Stores prime * 2 |
| 120 | const crypto_word_t prime_x2[NWORDS_FIELD]; |
| 121 | // Alice's generator values {XPA0 + XPA1*i, XQA0 + XQA1*i, XRA0 + XRA1*i} |
| 122 | // in GF(prime^2), expressed in Montgomery representation |
| 123 | const crypto_word_t A_gen[6*NWORDS_FIELD]; |
| 124 | // Bob's generator values {XPB0 + XPB1*i, XQB0 + XQB1*i, XRB0 + XRB1*i} |
| 125 | // in GF(prime^2), expressed in Montgomery representation |
| 126 | const crypto_word_t B_gen[6*NWORDS_FIELD]; |
| 127 | // Montgomery constant mont_R2 = (2^448)^2 mod prime |
| 128 | const crypto_word_t mont_R2[NWORDS_FIELD]; |
| 129 | // Value 'one' in Montgomery representation |
| 130 | const crypto_word_t mont_one[NWORDS_FIELD]; |
| 131 | // Value '6' in Montgomery representation |
| 132 | const crypto_word_t mont_six[NWORDS_FIELD]; |
| 133 | // Fixed parameters for isogeny tree computation |
| 134 | const unsigned int A_strat[A_max-1]; |
| 135 | const unsigned int B_strat[B_max-1]; |
| 136 | }; |
| 137 |
| 138 | // Point representation in projective XZ Montgomery coordinates. |
| 139 | typedef struct { |
| 140 | f2elm_t X; |
| 141 | f2elm_t Z; |
| 142 | } point_proj; |
| 143 | typedef point_proj point_proj_t[1]; |
| 144 |
| 145 | #endif // UTILS_H_ |
| 146 |