1 /* Copyright (c) 2020, Google Inc. 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15 #ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H 16 #define OPENSSL_HEADER_CURVE25519_INTERNAL_H 17 18 #if defined(__cplusplus) 19 extern "C" { 20 #endif 21 22 #include <openssl/base.h> 23 24 #include "../internal.h" 25 26 27 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE) 28 #define BORINGSSL_X25519_NEON 29 30 // x25519_NEON is defined in asm/x25519-arm.S. 31 void x25519_NEON(uint8_t out[32], const uint8_t scalar[32], 32 const uint8_t point[32]); 33 #endif 34 35 #if defined(BORINGSSL_HAS_UINT128) 36 #define BORINGSSL_CURVE25519_64BIT 37 #endif 38 39 #if defined(BORINGSSL_CURVE25519_64BIT) 40 // fe means field element. Here the field is \Z/(2^255-19). An element t, 41 // entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153 42 // t[3]+2^204 t[4]. 43 // fe limbs are bounded by 1.125*2^51. 44 // Multiplication and carrying produce fe from fe_loose. 45 typedef struct fe { uint64_t v[5]; } fe; 46 47 // fe_loose limbs are bounded by 3.375*2^51. 48 // Addition and subtraction produce fe_loose from (fe, fe). 49 typedef struct fe_loose { uint64_t v[5]; } fe_loose; 50 #else 51 // fe means field element. Here the field is \Z/(2^255-19). An element t, 52 // entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77 53 // t[3]+2^102 t[4]+...+2^230 t[9]. 54 // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc. 55 // Multiplication and carrying produce fe from fe_loose. 56 typedef struct fe { uint32_t v[10]; } fe; 57 58 // fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc. 59 // Addition and subtraction produce fe_loose from (fe, fe). 60 typedef struct fe_loose { uint32_t v[10]; } fe_loose; 61 #endif 62 63 // ge means group element. 64 // 65 // Here the group is the set of pairs (x,y) of field elements (see fe.h) 66 // satisfying -x^2 + y^2 = 1 + d x^2y^2 67 // where d = -121665/121666. 68 // 69 // Representations: 70 // ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z 71 // ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT 72 // ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T 73 // ge_precomp (Duif): (y+x,y-x,2dxy) 74 75 typedef struct { 76 fe X; 77 fe Y; 78 fe Z; 79 } ge_p2; 80 81 typedef struct { 82 fe X; 83 fe Y; 84 fe Z; 85 fe T; 86 } ge_p3; 87 88 typedef struct { 89 fe_loose X; 90 fe_loose Y; 91 fe_loose Z; 92 fe_loose T; 93 } ge_p1p1; 94 95 typedef struct { 96 fe_loose yplusx; 97 fe_loose yminusx; 98 fe_loose xy2d; 99 } ge_precomp; 100 101 typedef struct { 102 fe_loose YplusX; 103 fe_loose YminusX; 104 fe_loose Z; 105 fe_loose T2d; 106 } ge_cached; 107 108 void x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h); 109 int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s); 110 void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p); 111 void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p); 112 void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p); 113 void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 114 void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 115 void x25519_ge_scalarmult_small_precomp( 116 ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]); 117 void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]); 118 void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A); 119 void x25519_sc_reduce(uint8_t s[64]); 120 121 enum spake2_state_t { 122 spake2_state_init = 0, 123 spake2_state_msg_generated, 124 spake2_state_key_generated, 125 }; 126 127 struct spake2_ctx_st { 128 uint8_t private_key[32]; 129 uint8_t my_msg[32]; 130 uint8_t password_scalar[32]; 131 uint8_t password_hash[64]; 132 uint8_t *my_name; 133 size_t my_name_len; 134 uint8_t *their_name; 135 size_t their_name_len; 136 enum spake2_role_t my_role; 137 enum spake2_state_t state; 138 char disable_password_scalar_hack; 139 }; 140 141 142 #if defined(__cplusplus) 143 } // extern C 144 #endif 145 146 #endif // OPENSSL_HEADER_CURVE25519_INTERNAL_H 147