| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | // Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1) |
| 17 | // (1) Intel Corporation, Israel Development Center, Haifa, Israel |
| 18 | // (2) University of Haifa, Israel |
| 19 | // |
| 20 | // Reference: |
| 21 | // S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with |
| 22 | // 256 Bit Primes" |
| 23 |
| 24 | #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_P256_NISTZ_H |
| 25 | #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_P256_NISTZ_H |
| 26 |
| 27 | #include <openssl/base.h> |
| 28 |
| 29 | #include <openssl/bn.h> |
| 30 |
| 31 | #include "../bn/internal.h" |
| 32 |
| 33 | #if defined(__cplusplus) |
| 34 | extern "C" { |
| 35 | #endif |
| 36 |
| 37 |
| 38 | #if !defined(OPENSSL_NO_ASM) && \ |
| 39 | (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \ |
| 40 | !defined(OPENSSL_SMALL) |
| 41 |
| 42 | // P-256 field operations. |
| 43 | // |
| 44 | // An element mod P in P-256 is represented as a little-endian array of |
| 45 | // |P256_LIMBS| |BN_ULONG|s, spanning the full range of values. |
| 46 | // |
| 47 | // The following functions take fully-reduced inputs mod P and give |
| 48 | // fully-reduced outputs. They may be used in-place. |
| 49 |
| 50 | #define P256_LIMBS (256 / BN_BITS2) |
| 51 |
| 52 | // ecp_nistz256_neg sets |res| to -|a| mod P. |
| 53 | void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]); |
| 54 |
| 55 | // ecp_nistz256_mul_mont sets |res| to |a| * |b| * 2^-256 mod P. |
| 56 | #if defined(OPENSSL_X86_64) |
| 57 | void ecp_nistz256_mul_mont_nohw(BN_ULONG res[P256_LIMBS], |
| 58 | const BN_ULONG a[P256_LIMBS], |
| 59 | const BN_ULONG b[P256_LIMBS]); |
| 60 | void ecp_nistz256_mul_mont_adx(BN_ULONG res[P256_LIMBS], |
| 61 | const BN_ULONG a[P256_LIMBS], |
| 62 | const BN_ULONG b[P256_LIMBS]); |
| 63 | #else |
| 64 | void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS], |
| 65 | const BN_ULONG a[P256_LIMBS], |
| 66 | const BN_ULONG b[P256_LIMBS]); |
| 67 | #endif |
| 68 |
| 69 | // ecp_nistz256_sqr_mont sets |res| to |a| * |a| * 2^-256 mod P. |
| 70 | #if defined(OPENSSL_X86_64) |
| 71 | void ecp_nistz256_sqr_mont_nohw(BN_ULONG res[P256_LIMBS], |
| 72 | const BN_ULONG a[P256_LIMBS]); |
| 73 | void ecp_nistz256_sqr_mont_adx(BN_ULONG res[P256_LIMBS], |
| 74 | const BN_ULONG a[P256_LIMBS]); |
| 75 | #else |
| 76 | void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS], |
| 77 | const BN_ULONG a[P256_LIMBS]); |
| 78 | #endif |
| 79 |
| 80 |
| 81 | // P-256 scalar operations. |
| 82 | // |
| 83 | // The following functions compute modulo N, where N is the order of P-256. They |
| 84 | // take fully-reduced inputs and give fully-reduced outputs. |
| 85 |
| 86 | // ecp_nistz256_ord_mul_mont sets |res| to |a| * |b| where inputs and outputs |
| 87 | // are in Montgomery form. That is, |res| is |a| * |b| * 2^-256 mod N. |
| 88 | #if defined(OPENSSL_X86_64) |
| 89 | void ecp_nistz256_ord_mul_mont_nohw(BN_ULONG res[P256_LIMBS], |
| 90 | const BN_ULONG a[P256_LIMBS], |
| 91 | const BN_ULONG b[P256_LIMBS]); |
| 92 | void ecp_nistz256_ord_mul_mont_adx(BN_ULONG res[P256_LIMBS], |
| 93 | const BN_ULONG a[P256_LIMBS], |
| 94 | const BN_ULONG b[P256_LIMBS]); |
| 95 | #else |
| 96 | void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS], |
| 97 | const BN_ULONG a[P256_LIMBS], |
| 98 | const BN_ULONG b[P256_LIMBS]); |
| 99 | #endif |
| 100 |
| 101 | // ecp_nistz256_ord_sqr_mont sets |res| to |a|^(2*|rep|) where inputs and |
| 102 | // outputs are in Montgomery form. That is, |res| is |
| 103 | // (|a| * 2^-256)^(2*|rep|) * 2^256 mod N. |
| 104 | #if defined(OPENSSL_X86_64) |
| 105 | void ecp_nistz256_ord_sqr_mont_nohw(BN_ULONG res[P256_LIMBS], |
| 106 | const BN_ULONG a[P256_LIMBS], BN_ULONG rep); |
| 107 | void ecp_nistz256_ord_sqr_mont_adx(BN_ULONG res[P256_LIMBS], |
| 108 | const BN_ULONG a[P256_LIMBS], BN_ULONG rep); |
| 109 | #else |
| 110 | void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS], |
| 111 | const BN_ULONG a[P256_LIMBS], BN_ULONG rep); |
| 112 | #endif |
| 113 |
| 114 | // beeu_mod_inverse_vartime sets out = a^-1 mod p using a Euclidean algorithm. |
| 115 | // Assumption: 0 < a < p < 2^(256) and p is odd. |
| 116 | int beeu_mod_inverse_vartime(BN_ULONG out[P256_LIMBS], |
| 117 | const BN_ULONG a[P256_LIMBS], |
| 118 | const BN_ULONG p[P256_LIMBS]); |
| 119 |
| 120 |
| 121 | // P-256 point operations. |
| 122 | // |
| 123 | // The following functions may be used in-place. All coordinates are in the |
| 124 | // Montgomery domain. |
| 125 |
| 126 | // A P256_POINT represents a P-256 point in Jacobian coordinates. |
| 127 | typedef struct { |
| 128 | BN_ULONG X[P256_LIMBS]; |
| 129 | BN_ULONG Y[P256_LIMBS]; |
| 130 | BN_ULONG Z[P256_LIMBS]; |
| 131 | } P256_POINT; |
| 132 |
| 133 | // A P256_POINT_AFFINE represents a P-256 point in affine coordinates. Infinity |
| 134 | // is encoded as (0, 0). |
| 135 | typedef struct { |
| 136 | BN_ULONG X[P256_LIMBS]; |
| 137 | BN_ULONG Y[P256_LIMBS]; |
| 138 | } P256_POINT_AFFINE; |
| 139 |
| 140 | // ecp_nistz256_select_w5 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 16 |
| 141 | // and all zeros (the point at infinity) if |index| is 0. This is done in |
| 142 | // constant time. |
| 143 | #if defined(OPENSSL_X86_64) |
| 144 | void ecp_nistz256_select_w5_nohw(P256_POINT *val, const P256_POINT in_t[16], |
| 145 | int index); |
| 146 | void ecp_nistz256_select_w5_avx2(P256_POINT *val, const P256_POINT in_t[16], |
| 147 | int index); |
| 148 | #else |
| 149 | void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT in_t[16], |
| 150 | int index); |
| 151 | #endif |
| 152 |
| 153 | // ecp_nistz256_select_w7 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 64 |
| 154 | // and all zeros (the point at infinity) if |index| is 0. This is done in |
| 155 | // constant time. |
| 156 | #if defined(OPENSSL_X86_64) |
| 157 | void ecp_nistz256_select_w7_nohw(P256_POINT_AFFINE *val, |
| 158 | const P256_POINT_AFFINE in_t[64], int index); |
| 159 | void ecp_nistz256_select_w7_avx2(P256_POINT_AFFINE *val, |
| 160 | const P256_POINT_AFFINE in_t[64], int index); |
| 161 | #else |
| 162 | void ecp_nistz256_select_w7(P256_POINT_AFFINE *val, |
| 163 | const P256_POINT_AFFINE in_t[64], int index); |
| 164 | #endif |
| 165 |
| 166 | // ecp_nistz256_point_double sets |r| to |a| doubled. |
| 167 | #if defined(OPENSSL_X86_64) |
| 168 | void ecp_nistz256_point_double_nohw(P256_POINT *r, const P256_POINT *a); |
| 169 | void ecp_nistz256_point_double_adx(P256_POINT *r, const P256_POINT *a); |
| 170 | #else |
| 171 | void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a); |
| 172 | #endif |
| 173 |
| 174 | // ecp_nistz256_point_add adds |a| to |b| and places the result in |r|. |
| 175 | #if defined(OPENSSL_X86_64) |
| 176 | void ecp_nistz256_point_add_nohw(P256_POINT *r, const P256_POINT *a, |
| 177 | const P256_POINT *b); |
| 178 | void ecp_nistz256_point_add_adx(P256_POINT *r, const P256_POINT *a, |
| 179 | const P256_POINT *b); |
| 180 | #else |
| 181 | void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a, |
| 182 | const P256_POINT *b); |
| 183 | #endif |
| 184 |
| 185 | // ecp_nistz256_point_add_affine adds |a| to |b| and places the result in |
| 186 | // |r|. |a| and |b| must not represent the same point unless they are both |
| 187 | // infinity. |
| 188 | #if defined(OPENSSL_X86_64) |
| 189 | void ecp_nistz256_point_add_affine_adx(P256_POINT *r, const P256_POINT *a, |
| 190 | const P256_POINT_AFFINE *b); |
| 191 | void ecp_nistz256_point_add_affine_nohw(P256_POINT *r, const P256_POINT *a, |
| 192 | const P256_POINT_AFFINE *b); |
| 193 | #else |
| 194 | void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a, |
| 195 | const P256_POINT_AFFINE *b); |
| 196 | #endif |
| 197 |
| 198 | #endif /* !defined(OPENSSL_NO_ASM) && \ |
| 199 | (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \ |
| 200 | !defined(OPENSSL_SMALL) */ |
| 201 |
| 202 |
| 203 | #if defined(__cplusplus) |
| 204 | } // extern C++ |
| 205 | #endif |
| 206 |
| 207 | #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_P256_NISTZ_H |
| 208 |