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