#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include "../bn/internal.h"
#include "../cpucap/internal.h"
#include "../delocate.h"
#include "internal.h"
#include "ec_nistp.h"
#if !defined(OPENSSL_SMALL)
#if defined(EC_NISTP_USE_S2N_BIGNUM)
# include "../../../third_party/s2n-bignum/include/s2n-bignum_aws-lc.h"
#else
# if defined(EC_NISTP_USE_64BIT_LIMB)
# include "../../../third_party/fiat/p521_64.h"
# else
# include "../../../third_party/fiat/p521_32.h"
# endif
#endif
#if defined(EC_NISTP_USE_S2N_BIGNUM)
#define P521_NLIMBS (9)
typedef uint64_t p521_limb_t;
typedef uint64_t p521_felem[P521_NLIMBS];
static const p521_limb_t p521_felem_one[P521_NLIMBS] = {
0x0000000000000001, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000};
static const p521_limb_t p521_felem_p[P521_NLIMBS] = {
0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff, 0xffffffffffffffff,
0x1ff};
#define p521_felem_add(out, in0, in1) bignum_add_p521(out, in0, in1)
#define p521_felem_sub(out, in0, in1) bignum_sub_p521(out, in0, in1)
#define p521_felem_opp(out, in0) bignum_neg_p521(out, in0)
#define p521_felem_to_bytes(out, in0) bignum_tolebytes_p521(out, in0)
#define p521_felem_from_bytes(out, in0) bignum_fromlebytes_p521(out, in0)
#define p521_felem_mul(out, in0, in1) bignum_mul_p521_selector(out, in0, in1)
#define p521_felem_sqr(out, in0) bignum_sqr_p521_selector(out, in0)
#else
#if defined(EC_NISTP_USE_64BIT_LIMB)
#define P521_NLIMBS (9)
typedef uint64_t p521_felem[P521_NLIMBS]; typedef uint64_t p521_limb_t;
static const p521_limb_t p521_felem_one[P521_NLIMBS] = {
0x0000000000000001, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000,
0x0000000000000000};
static const p521_limb_t p521_felem_p[P521_NLIMBS] = {
0x03ffffffffffffff, 0x03ffffffffffffff,
0x03ffffffffffffff, 0x03ffffffffffffff,
0x03ffffffffffffff, 0x03ffffffffffffff,
0x03ffffffffffffff, 0x03ffffffffffffff,
0x01ffffffffffffff};
#else
#define P521_NLIMBS (19)
typedef uint32_t p521_felem[P521_NLIMBS]; typedef uint32_t p521_limb_t;
static const p521_limb_t p521_felem_one[P521_NLIMBS] = {
0x0000001, 0x0000000, 0x0000000, 0x0000000,
0x0000000, 0x0000000, 0x0000000, 0x0000000,
0x0000000, 0x0000000, 0x0000000, 0x0000000,
0x0000000, 0x0000000, 0x0000000, 0x0000000,
0x0000000, 0x0000000, 0x0000000};
static const p521_limb_t p521_felem_p[P521_NLIMBS] = {
0xfffffff, 0x7ffffff, 0xfffffff, 0x7ffffff,
0xfffffff, 0x7ffffff, 0x7ffffff, 0xfffffff,
0x7ffffff, 0xfffffff, 0x7ffffff, 0xfffffff,
0x7ffffff, 0x7ffffff, 0xfffffff, 0x7ffffff,
0xfffffff, 0x7ffffff, 0x7ffffff};
#endif
#define p521_felem_add(out, in0, in1) fiat_secp521r1_carry_add(out, in0, in1)
#define p521_felem_sub(out, in0, in1) fiat_secp521r1_carry_sub(out, in0, in1)
#define p521_felem_opp(out, in0) fiat_secp521r1_carry_opp(out, in0)
#define p521_felem_mul(out, in0, in1) fiat_secp521r1_carry_mul(out, in0, in1)
#define p521_felem_sqr(out, in0) fiat_secp521r1_carry_square(out, in0)
#define p521_felem_to_bytes(out, in0) fiat_secp521r1_to_bytes(out, in0)
#define p521_felem_from_bytes(out, in0) fiat_secp521r1_from_bytes(out, in0)
#endif
static p521_limb_t p521_felem_nz(const p521_limb_t in1[P521_NLIMBS]) {
p521_limb_t is_not_zero = 0;
for (int i = 0; i < P521_NLIMBS; i++) {
is_not_zero |= in1[i];
}
#if defined(EC_NISTP_USE_S2N_BIGNUM)
return is_not_zero;
#else
p521_limb_t is_not_p = 0;
for (int i = 0; i < P521_NLIMBS; i++) {
is_not_p |= (in1[i] ^ p521_felem_p[i]);
}
return ~(constant_time_is_zero_w(is_not_p) |
constant_time_is_zero_w(is_not_zero));
#endif
}
static void p521_felem_copy(p521_limb_t out[P521_NLIMBS],
const p521_limb_t in1[P521_NLIMBS]) {
for (size_t i = 0; i < P521_NLIMBS; i++) {
out[i] = in1[i];
}
}
static void p521_felem_cmovznz(p521_limb_t out[P521_NLIMBS],
p521_limb_t t,
const p521_limb_t z[P521_NLIMBS],
const p521_limb_t nz[P521_NLIMBS]) {
p521_limb_t mask = constant_time_is_zero_w(t);
for (size_t i = 0; i < P521_NLIMBS; i++) {
out[i] = constant_time_select_w(mask, z[i], nz[i]);
}
}
static void p521_from_generic(p521_felem out, const EC_FELEM *in) {
#ifdef OPENSSL_BIG_ENDIAN
uint8_t tmp[P521_EC_FELEM_BYTES];
bn_words_to_little_endian(tmp, P521_EC_FELEM_BYTES, in->words, P521_EC_FELEM_WORDS);
p521_felem_from_bytes(out, tmp);
#else
p521_felem_from_bytes(out, (const uint8_t *)in->words);
#endif
}
static void p521_to_generic(EC_FELEM *out, const p521_felem in) {
#ifdef OPENSSL_BIG_ENDIAN
uint8_t tmp[P521_EC_FELEM_BYTES];
p521_felem_to_bytes(tmp, in);
bn_little_endian_to_words(out->words, P521_EC_FELEM_WORDS, tmp, P521_EC_FELEM_BYTES);
#else
OPENSSL_memset((uint8_t*)out->words, 0, sizeof(out->words));
p521_felem_to_bytes((uint8_t *)out->words, in);
#endif
}
static void p521_felem_inv(p521_felem output, const p521_felem t1) {
p521_felem acc, t2, t4, t8, t16, t32, t64;
p521_felem t128, t256, t512, t516, t518, t519;
p521_felem_sqr(acc, t1);
p521_felem_mul(t2, acc, t1);
p521_felem_sqr(acc, t2);
p521_felem_sqr(acc, acc);
p521_felem_mul(t4, acc, t2);
p521_felem_sqr(acc, t4);
for (int i = 0; i < 3; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t8, acc, t4);
p521_felem_sqr(acc, t8);
for (int i = 0; i < 7; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t16, acc, t8);
p521_felem_sqr(acc, t16);
for (int i = 0; i < 15; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t32, acc, t16);
p521_felem_sqr(acc, t32);
for (int i = 0; i < 31; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t64, acc, t32);
p521_felem_sqr(acc, t64);
for (int i = 0; i < 63; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t128, acc, t64);
p521_felem_sqr(acc, t128);
for (int i = 0; i < 127; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t256, acc, t128);
p521_felem_sqr(acc, t256);
for (int i = 0; i < 255; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t512, acc, t256);
p521_felem_sqr(acc, t512);
for (int i = 0; i < 3; i++) {
p521_felem_sqr(acc, acc);
}
p521_felem_mul(t516, acc, t4);
p521_felem_sqr(acc, t516);
p521_felem_sqr(acc, acc);
p521_felem_mul(t518, acc, t2);
p521_felem_sqr(acc, t518);
p521_felem_mul(t519, acc, t1);
p521_felem_sqr(acc, t519);
p521_felem_sqr(acc, acc);
p521_felem_mul(output, acc, t1);
}
static void p521_point_double(p521_felem x_out,
p521_felem y_out,
p521_felem z_out,
const p521_felem x_in,
const p521_felem y_in,
const p521_felem z_in) {
ec_nistp_point_double(p521_methods(), x_out, y_out, z_out, x_in, y_in, z_in);
}
static void p521_point_add(p521_felem x3, p521_felem y3, p521_felem z3,
const p521_felem x1,
const p521_felem y1,
const p521_felem z1,
const int mixed,
const p521_felem x2,
const p521_felem y2,
const p521_felem z2) {
ec_nistp_point_add(p521_methods(), x3, y3, z3, x1, y1, z1, mixed, x2, y2, z2);
}
#if defined(EC_NISTP_USE_S2N_BIGNUM)
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p521_methods) {
out->felem_num_limbs = P521_NLIMBS;
out->felem_num_bits = 521;
out->felem_add = bignum_add_p521;
out->felem_sub = bignum_sub_p521;
out->felem_mul = bignum_mul_p521_selector;
out->felem_sqr = bignum_sqr_p521_selector;
out->felem_neg = bignum_neg_p521;
out->felem_nz = p521_felem_nz;
out->point_dbl = p521_point_double;
out->point_add = p521_point_add;
}
#else
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p521_methods) {
out->felem_num_limbs = P521_NLIMBS;
out->felem_num_bits = 521;
out->felem_add = fiat_secp521r1_carry_add;
out->felem_sub = fiat_secp521r1_carry_sub;
out->felem_mul = fiat_secp521r1_carry_mul;
out->felem_sqr = fiat_secp521r1_carry_square;
out->felem_neg = fiat_secp521r1_carry_opp;
out->felem_nz = p521_felem_nz;
out->point_dbl = p521_point_double;
out->point_add = p521_point_add;
}
#endif
static int ec_GFp_nistp521_point_get_affine_coordinates(
const EC_GROUP *group, const EC_JACOBIAN *point,
EC_FELEM *x_out, EC_FELEM *y_out) {
if (constant_time_declassify_w(ec_GFp_simple_is_at_infinity(group, point))) {
OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
return 0;
}
p521_felem z1, z2;
p521_from_generic(z1, &point->Z);
p521_felem_inv(z2, z1);
p521_felem_sqr(z2, z2);
if (x_out != NULL) {
p521_felem x;
p521_from_generic(x, &point->X);
p521_felem_mul(x, x, z2);
p521_to_generic(x_out, x);
}
if (y_out != NULL) {
p521_felem y;
p521_from_generic(y, &point->Y);
p521_felem_sqr(z2, z2); p521_felem_mul(y, y, z1); p521_felem_mul(y, y, z2); p521_to_generic(y_out, y);
}
return 1;
}
static void ec_GFp_nistp521_add(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *a, const EC_JACOBIAN *b) {
p521_felem x1, y1, z1, x2, y2, z2;
p521_from_generic(x1, &a->X);
p521_from_generic(y1, &a->Y);
p521_from_generic(z1, &a->Z);
p521_from_generic(x2, &b->X);
p521_from_generic(y2, &b->Y);
p521_from_generic(z2, &b->Z);
p521_point_add(x1, y1, z1, x1, y1, z1, 0 , x2, y2, z2);
p521_to_generic(&r->X, x1);
p521_to_generic(&r->Y, y1);
p521_to_generic(&r->Z, z1);
}
static void ec_GFp_nistp521_dbl(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *a) {
p521_felem x, y, z;
p521_from_generic(x, &a->X);
p521_from_generic(y, &a->Y);
p521_from_generic(z, &a->Z);
p521_point_double(x, y, z, x, y, z);
p521_to_generic(&r->X, x);
p521_to_generic(&r->Y, y);
p521_to_generic(&r->Z, z);
}
#define P521_MUL_WSIZE (5)
OPENSSL_STATIC_ASSERT(P521_MUL_WSIZE == 5,
p521_scalar_mul_window_size_is_not_equal_to_five)
#define P521_MUL_TWO_TO_WSIZE (1 << P521_MUL_WSIZE)
#define P521_MUL_NWINDOWS ((521 + P521_MUL_WSIZE - 1)/P521_MUL_WSIZE)
#define P521_MUL_PUB_WSIZE (5)
#define P521_MUL_TABLE_SIZE (P521_MUL_TWO_TO_WSIZE >> 1)
#define P521_MUL_PUB_TABLE_SIZE (1 << (P521_MUL_PUB_WSIZE - 1))
static void p521_select_point_affine(p521_felem out[2],
size_t idx,
const p521_felem table[][2],
size_t table_size) {
OPENSSL_memset(out, 0, sizeof(p521_felem) * 2);
for (size_t i = 0; i < table_size; i++) {
p521_limb_t mismatch = i ^ idx;
p521_felem_cmovznz(out[0], mismatch, table[i][0], out[0]);
p521_felem_cmovznz(out[1], mismatch, table[i][1], out[1]);
}
}
static void ec_GFp_nistp521_point_mul(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *p,
const EC_SCALAR *scalar) {
p521_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}};
p521_from_generic(tmp[0], &p->X);
p521_from_generic(tmp[1], &p->Y);
p521_from_generic(tmp[2], &p->Z);
ec_nistp_scalar_mul(p521_methods(), res[0], res[1], res[2], tmp[0], tmp[1], tmp[2], scalar);
p521_to_generic(&r->X, res[0]);
p521_to_generic(&r->Y, res[1]);
p521_to_generic(&r->Z, res[2]);
}
#include "p521_table.h"
static void ec_GFp_nistp521_point_mul_base(const EC_GROUP *group,
EC_JACOBIAN *r,
const EC_SCALAR *scalar) {
p521_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}, ftmp;
int16_t rnaf[P521_MUL_NWINDOWS] = {0};
scalar_rwnaf(rnaf, P521_MUL_WSIZE, scalar, 521);
for (int i = 3; i >= 0; i--) {
for (size_t j = 0; i != 3 && j < P521_MUL_WSIZE; j++) {
p521_point_double(res[0], res[1], res[2], res[0], res[1], res[2]);
}
const size_t start_idx = ((P521_MUL_NWINDOWS - i - 1)/4)*4 + i;
for (int j = start_idx; j >= 0; j -= 4) {
int16_t d = rnaf[j];
int16_t is_neg = (d >> 15) & 1;
d = (d ^ -is_neg) + is_neg;
int16_t idx = d >> 1;
p521_select_point_affine(tmp, idx, p521_g_pre_comp[j / 4],
P521_MUL_TABLE_SIZE);
p521_felem_opp(ftmp, tmp[1]);
p521_felem_cmovznz(tmp[1], is_neg, tmp[1], ftmp);
p521_point_add(res[0], res[1], res[2], res[0], res[1], res[2],
1 , tmp[0], tmp[1], p521_felem_one);
}
}
p521_felem_copy(tmp[0], p521_g_pre_comp[0][0][0]);
p521_felem_opp(tmp[1], p521_g_pre_comp[0][0][1]);
p521_point_add(tmp[0], tmp[1], tmp[2], res[0], res[1], res[2],
1 , tmp[0], tmp[1], p521_felem_one);
p521_felem_cmovznz(res[0], scalar->words[0] & 1, tmp[0], res[0]);
p521_felem_cmovznz(res[1], scalar->words[0] & 1, tmp[1], res[1]);
p521_felem_cmovznz(res[2], scalar->words[0] & 1, tmp[2], res[2]);
p521_to_generic(&r->X, res[0]);
p521_to_generic(&r->Y, res[1]);
p521_to_generic(&r->Z, res[2]);
}
static void ec_GFp_nistp521_point_mul_public(const EC_GROUP *group,
EC_JACOBIAN *r,
const EC_SCALAR *g_scalar,
const EC_JACOBIAN *p,
const EC_SCALAR *p_scalar) {
p521_felem res[3] = {{0}, {0}, {0}}, two_p[3] = {{0}, {0}, {0}}, ftmp;
p521_felem p_pre_comp[P521_MUL_PUB_TABLE_SIZE][3];
p521_from_generic(p_pre_comp[0][0], &p->X);
p521_from_generic(p_pre_comp[0][1], &p->Y);
p521_from_generic(p_pre_comp[0][2], &p->Z);
p521_point_double(two_p[0], two_p[1], two_p[2],
p_pre_comp[0][0], p_pre_comp[0][1], p_pre_comp[0][2]);
for (size_t i = 1; i < P521_MUL_PUB_TABLE_SIZE; i++) {
p521_point_add(p_pre_comp[i][0], p_pre_comp[i][1], p_pre_comp[i][2],
two_p[0], two_p[1], two_p[2], 0 ,
p_pre_comp[i - 1][0],
p_pre_comp[i - 1][1],
p_pre_comp[i - 1][2]);
}
int8_t p_wnaf[522] = {0}, g_wnaf[522] = {0};
ec_compute_wNAF(group, p_wnaf, p_scalar, 521, P521_MUL_PUB_WSIZE);
ec_compute_wNAF(group, g_wnaf, g_scalar, 521, P521_MUL_WSIZE);
int16_t res_is_inf = 1;
int16_t d, is_neg, idx;
for (int i = 521; i >= 0; i--) {
if (!res_is_inf) {
p521_point_double(res[0], res[1], res[2], res[0], res[1], res[2]);
}
d = p_wnaf[i];
if (d != 0) {
is_neg = d < 0 ? 1 : 0;
idx = (is_neg) ? (-d - 1) >> 1 : (d - 1) >> 1;
if (res_is_inf) {
p521_felem_copy(res[0], p_pre_comp[idx][0]);
p521_felem_copy(res[1], p_pre_comp[idx][1]);
p521_felem_copy(res[2], p_pre_comp[idx][2]);
res_is_inf = 0;
} else {
if (is_neg) {
p521_felem_opp(ftmp, p_pre_comp[idx][1]);
} else {
p521_felem_copy(ftmp, p_pre_comp[idx][1]);
}
p521_point_add(res[0], res[1], res[2],
res[0], res[1], res[2],
0 ,
p_pre_comp[idx][0], ftmp, p_pre_comp[idx][2]);
}
}
d = g_wnaf[i];
if (d != 0) {
is_neg = d < 0 ? 1 : 0;
idx = (is_neg) ? (-d - 1) >> 1 : (d - 1) >> 1;
if (res_is_inf) {
p521_felem_copy(res[0], p521_g_pre_comp[0][idx][0]);
p521_felem_copy(res[1], p521_g_pre_comp[0][idx][1]);
p521_felem_copy(res[2], p521_felem_one);
res_is_inf = 0;
} else {
if (is_neg) {
p521_felem_opp(ftmp, p521_g_pre_comp[0][idx][1]);
} else {
p521_felem_copy(ftmp, p521_g_pre_comp[0][idx][1]);
}
p521_point_add(res[0], res[1], res[2],
res[0], res[1], res[2],
1 ,
p521_g_pre_comp[0][idx][0], ftmp, p521_felem_one);
}
}
}
p521_to_generic(&r->X, res[0]);
p521_to_generic(&r->Y, res[1]);
p521_to_generic(&r->Z, res[2]);
}
static void ec_GFp_nistp521_felem_mul(const EC_GROUP *group, EC_FELEM *r,
const EC_FELEM *a, const EC_FELEM *b) {
p521_felem felem1, felem2, felem3;
p521_from_generic(felem1, a);
p521_from_generic(felem2, b);
p521_felem_mul(felem3, felem1, felem2);
p521_to_generic(r, felem3);
}
static void ec_GFp_nistp521_felem_sqr(const EC_GROUP *group, EC_FELEM *r,
const EC_FELEM *a) {
p521_felem felem1, felem2;
p521_from_generic(felem1, a);
p521_felem_sqr(felem2, felem1);
p521_to_generic(r, felem2);
}
DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp521_method) {
out->point_get_affine_coordinates =
ec_GFp_nistp521_point_get_affine_coordinates;
out->add = ec_GFp_nistp521_add;
out->dbl = ec_GFp_nistp521_dbl;
out->mul = ec_GFp_nistp521_point_mul;
out->mul_base = ec_GFp_nistp521_point_mul_base;
out->mul_public = ec_GFp_nistp521_point_mul_public;
out->felem_mul = ec_GFp_nistp521_felem_mul;
out->felem_sqr = ec_GFp_nistp521_felem_sqr;
out->felem_to_bytes = ec_GFp_simple_felem_to_bytes;
out->felem_from_bytes = ec_GFp_simple_felem_from_bytes;
out->scalar_inv0_montgomery = ec_simple_scalar_inv0_montgomery;
out->scalar_to_montgomery_inv_vartime =
ec_simple_scalar_to_montgomery_inv_vartime;
out->cmp_x_coordinate = ec_GFp_simple_cmp_x_coordinate;
}
#endif