| 1 | /* |
| 2 | * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * Copyright (c) 2014, Intel Corporation. All Rights Reserved. |
| 4 | * |
| 5 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 6 | * this file except in compliance with the License. You can obtain a copy |
| 7 | * in the file LICENSE in the source distribution or at |
| 8 | * https://www.openssl.org/source/license.html |
| 9 | * |
| 10 | * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1) |
| 11 | * (1) Intel Corporation, Israel Development Center, Haifa, Israel |
| 12 | * (2) University of Haifa, Israel |
| 13 | * |
| 14 | * Reference: |
| 15 | * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with |
| 16 | * 256 Bit Primes" |
| 17 | */ |
| 18 | |
| 19 | #ifndef OPENSSL_HEADER_EC_P256_X86_64_H |
| 20 | #define |
| 21 | |
| 22 | #include <openssl/base.h> |
| 23 | |
| 24 | #include <openssl/bn.h> |
| 25 | |
| 26 | #include "../bn/internal.h" |
| 27 | |
| 28 | #if defined(__cplusplus) |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \ |
| 34 | !defined(OPENSSL_SMALL) |
| 35 | |
| 36 | // P-256 field operations. |
| 37 | // |
| 38 | // An element mod P in P-256 is represented as a little-endian array of |
| 39 | // |P256_LIMBS| |BN_ULONG|s, spanning the full range of values. |
| 40 | // |
| 41 | // The following functions take fully-reduced inputs mod P and give |
| 42 | // fully-reduced outputs. They may be used in-place. |
| 43 | |
| 44 | #define P256_LIMBS (256 / BN_BITS2) |
| 45 | |
| 46 | // ecp_nistz256_neg sets |res| to -|a| mod P. |
| 47 | void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]); |
| 48 | |
| 49 | // ecp_nistz256_mul_mont sets |res| to |a| * |b| * 2^-256 mod P. |
| 50 | void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS], |
| 51 | const BN_ULONG a[P256_LIMBS], |
| 52 | const BN_ULONG b[P256_LIMBS]); |
| 53 | |
| 54 | // ecp_nistz256_sqr_mont sets |res| to |a| * |a| * 2^-256 mod P. |
| 55 | void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS], |
| 56 | const BN_ULONG a[P256_LIMBS]); |
| 57 | |
| 58 | // ecp_nistz256_from_mont sets |res| to |in|, converted from Montgomery domain |
| 59 | // by multiplying with 1. |
| 60 | static inline void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS], |
| 61 | const BN_ULONG in[P256_LIMBS]) { |
| 62 | static const BN_ULONG ONE[P256_LIMBS] = { 1 }; |
| 63 | ecp_nistz256_mul_mont(res, in, ONE); |
| 64 | } |
| 65 | |
| 66 | // ecp_nistz256_to_mont sets |res| to |in|, converted to Montgomery domain |
| 67 | // by multiplying with RR = 2^512 mod P precomputed for NIST P256 curve. |
| 68 | static inline void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS], |
| 69 | const BN_ULONG in[P256_LIMBS]) { |
| 70 | static const BN_ULONG RR[P256_LIMBS] = { |
| 71 | TOBN(0x00000000, 0x00000003), TOBN(0xfffffffb, 0xffffffff), |
| 72 | TOBN(0xffffffff, 0xfffffffe), TOBN(0x00000004, 0xfffffffd)}; |
| 73 | ecp_nistz256_mul_mont(res, in, RR); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // P-256 scalar operations. |
| 78 | // |
| 79 | // The following functions compute modulo N, where N is the order of P-256. They |
| 80 | // take fully-reduced inputs and give fully-reduced outputs. |
| 81 | |
| 82 | // ecp_nistz256_ord_mul_mont sets |res| to |a| * |b| where inputs and outputs |
| 83 | // are in Montgomery form. That is, |res| is |a| * |b| * 2^-256 mod N. |
| 84 | void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS], |
| 85 | const BN_ULONG a[P256_LIMBS], |
| 86 | const BN_ULONG b[P256_LIMBS]); |
| 87 | |
| 88 | // ecp_nistz256_ord_sqr_mont sets |res| to |a|^(2*|rep|) where inputs and |
| 89 | // outputs are in Montgomery form. That is, |res| is |
| 90 | // (|a| * 2^-256)^(2*|rep|) * 2^256 mod N. |
| 91 | void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS], |
| 92 | const BN_ULONG a[P256_LIMBS], BN_ULONG rep); |
| 93 | |
| 94 | // beeu_mod_inverse_vartime sets out = a^-1 mod p using a Euclidean algorithm. |
| 95 | // Assumption: 0 < a < p < 2^(256) and p is odd. |
| 96 | int beeu_mod_inverse_vartime(BN_ULONG out[P256_LIMBS], |
| 97 | const BN_ULONG a[P256_LIMBS], |
| 98 | const BN_ULONG p[P256_LIMBS]); |
| 99 | |
| 100 | |
| 101 | // P-256 point operations. |
| 102 | // |
| 103 | // The following functions may be used in-place. All coordinates are in the |
| 104 | // Montgomery domain. |
| 105 | |
| 106 | // A P256_POINT represents a P-256 point in Jacobian coordinates. |
| 107 | typedef struct { |
| 108 | BN_ULONG X[P256_LIMBS]; |
| 109 | BN_ULONG Y[P256_LIMBS]; |
| 110 | BN_ULONG Z[P256_LIMBS]; |
| 111 | } P256_POINT; |
| 112 | |
| 113 | // A P256_POINT_AFFINE represents a P-256 point in affine coordinates. Infinity |
| 114 | // is encoded as (0, 0). |
| 115 | typedef struct { |
| 116 | BN_ULONG X[P256_LIMBS]; |
| 117 | BN_ULONG Y[P256_LIMBS]; |
| 118 | } P256_POINT_AFFINE; |
| 119 | |
| 120 | // ecp_nistz256_select_w5 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 16 |
| 121 | // and all zeros (the point at infinity) if |index| is 0. This is done in |
| 122 | // constant time. |
| 123 | void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT in_t[16], |
| 124 | int index); |
| 125 | |
| 126 | // ecp_nistz256_select_w7 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 64 |
| 127 | // and all zeros (the point at infinity) if |index| is 0. This is done in |
| 128 | // constant time. |
| 129 | void ecp_nistz256_select_w7(P256_POINT_AFFINE *val, |
| 130 | const P256_POINT_AFFINE in_t[64], int index); |
| 131 | |
| 132 | // ecp_nistz256_point_double sets |r| to |a| doubled. |
| 133 | void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a); |
| 134 | |
| 135 | // ecp_nistz256_point_add adds |a| to |b| and places the result in |r|. |
| 136 | void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a, |
| 137 | const P256_POINT *b); |
| 138 | |
| 139 | // ecp_nistz256_point_add_affine adds |a| to |b| and places the result in |
| 140 | // |r|. |a| and |b| must not represent the same point unless they are both |
| 141 | // infinity. |
| 142 | void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a, |
| 143 | const P256_POINT_AFFINE *b); |
| 144 | |
| 145 | #endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \ |
| 146 | !defined(OPENSSL_SMALL) */ |
| 147 | |
| 148 | |
| 149 | #if defined(__cplusplus) |
| 150 | } // extern C++ |
| 151 | #endif |
| 152 | |
| 153 | #endif // OPENSSL_HEADER_EC_P256_X86_64_H |
| 154 | |