#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/p384_64.h"
# else
# include "../../../third_party/fiat/p384_32.h"
# endif
#endif
#if defined(EC_NISTP_USE_64BIT_LIMB)
#define P384_NLIMBS (6)
typedef uint64_t p384_limb_t;
typedef uint64_t p384_felem[P384_NLIMBS];
static const p384_felem p384_felem_one = {
0xffffffff00000001, 0xffffffff, 0x1, 0x0, 0x0, 0x0};
#else
#define P384_NLIMBS (12)
typedef uint32_t p384_limb_t;
typedef uint32_t p384_felem[P384_NLIMBS];
static const p384_felem p384_felem_one = {
0x1, 0xffffffff, 0xffffffff, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
#endif
#if defined(EC_NISTP_USE_S2N_BIGNUM)
#define p384_felem_add(out, in0, in1) bignum_add_p384(out, in0, in1)
#define p384_felem_sub(out, in0, in1) bignum_sub_p384(out, in0, in1)
#define p384_felem_opp(out, in0) bignum_neg_p384(out, in0)
#define p384_felem_to_bytes(out, in0) bignum_tolebytes_6(out, in0)
#define p384_felem_from_bytes(out, in0) bignum_fromlebytes_6(out, in0)
#define p384_felem_to_mont(out, in0) bignum_tomont_p384_selector(out, in0)
#define p384_felem_from_mont(out, in0) bignum_deamont_p384_selector(out, in0)
#define p384_felem_mul(out, in0, in1) bignum_montmul_p384_selector(out, in0, in1)
#define p384_felem_sqr(out, in0) bignum_montsqr_p384_selector(out, in0)
static p384_limb_t p384_felem_nz(const p384_limb_t in1[P384_NLIMBS]) {
return bignum_nonzero_6(in1);
}
#else
#define p384_felem_add(out, in0, in1) fiat_p384_add(out, in0, in1)
#define p384_felem_sub(out, in0, in1) fiat_p384_sub(out, in0, in1)
#define p384_felem_opp(out, in0) fiat_p384_opp(out, in0)
#define p384_felem_mul(out, in0, in1) fiat_p384_mul(out, in0, in1)
#define p384_felem_sqr(out, in0) fiat_p384_square(out, in0)
#define p384_felem_to_mont(out, in0) fiat_p384_to_montgomery(out, in0)
#define p384_felem_from_mont(out, in0) fiat_p384_from_montgomery(out, in0)
#define p384_felem_to_bytes(out, in0) fiat_p384_to_bytes(out, in0)
#define p384_felem_from_bytes(out, in0) fiat_p384_from_bytes(out, in0)
static p384_limb_t p384_felem_nz(const p384_limb_t in1[P384_NLIMBS]) {
p384_limb_t ret;
fiat_p384_nonzero(&ret, in1);
return ret;
}
#endif
static void p384_felem_copy(p384_limb_t out[P384_NLIMBS],
const p384_limb_t in1[P384_NLIMBS]) {
for (size_t i = 0; i < P384_NLIMBS; i++) {
out[i] = in1[i];
}
}
static void p384_felem_cmovznz(p384_limb_t out[P384_NLIMBS],
p384_limb_t t,
const p384_limb_t z[P384_NLIMBS],
const p384_limb_t nz[P384_NLIMBS]) {
p384_limb_t mask = constant_time_is_zero_w(t);
for (size_t i = 0; i < P384_NLIMBS; i++) {
out[i] = constant_time_select_w(mask, z[i], nz[i]);
}
}
static void p384_from_generic(p384_felem out, const EC_FELEM *in) {
#ifdef OPENSSL_BIG_ENDIAN
uint8_t tmp[P384_EC_FELEM_BYTES];
bn_words_to_little_endian(tmp, P384_EC_FELEM_BYTES, in->words, P384_EC_FELEM_WORDS);
p384_felem_from_bytes(out, tmp);
#else
p384_felem_from_bytes(out, (const uint8_t *)in->words);
#endif
}
static void p384_to_generic(EC_FELEM *out, const p384_felem in) {
OPENSSL_STATIC_ASSERT(
384 / 8 == sizeof(BN_ULONG) * ((384 + BN_BITS2 - 1) / BN_BITS2),
p384_felem_to_bytes_leaves_bytes_uninitialized);
#ifdef OPENSSL_BIG_ENDIAN
uint8_t tmp[P384_EC_FELEM_BYTES];
p384_felem_to_bytes(tmp, in);
bn_little_endian_to_words(out->words, P384_EC_FELEM_WORDS, tmp, P384_EC_FELEM_BYTES);
#else
p384_felem_to_bytes((uint8_t *)out->words, in);
#endif
}
static void p384_from_scalar(p384_felem out, const EC_SCALAR *in) {
#ifdef OPENSSL_BIG_ENDIAN
uint8_t tmp[P384_EC_FELEM_BYTES];
bn_words_to_little_endian(tmp, P384_EC_FELEM_BYTES, in->words, P384_EC_FELEM_WORDS);
p384_felem_from_bytes(out, tmp);
#else
p384_felem_from_bytes(out, (const uint8_t *)in->words);
#endif
}
static void p384_inv_square(p384_felem out,
const p384_felem in) {
p384_felem x2, x3, x6, x12, x15, x30, x60, x120;
p384_felem_sqr(x2, in); p384_felem_mul(x2, x2, in);
p384_felem_sqr(x3, x2); p384_felem_mul(x3, x3, in);
p384_felem_sqr(x6, x3);
for (int i = 1; i < 3; i++) {
p384_felem_sqr(x6, x6);
} p384_felem_mul(x6, x6, x3);
p384_felem_sqr(x12, x6);
for (int i = 1; i < 6; i++) {
p384_felem_sqr(x12, x12);
} p384_felem_mul(x12, x12, x6);
p384_felem_sqr(x15, x12);
for (int i = 1; i < 3; i++) {
p384_felem_sqr(x15, x15);
} p384_felem_mul(x15, x15, x3);
p384_felem_sqr(x30, x15);
for (int i = 1; i < 15; i++) {
p384_felem_sqr(x30, x30);
} p384_felem_mul(x30, x30, x15);
p384_felem_sqr(x60, x30);
for (int i = 1; i < 30; i++) {
p384_felem_sqr(x60, x60);
} p384_felem_mul(x60, x60, x30);
p384_felem_sqr(x120, x60);
for (int i = 1; i < 60; i++) {
p384_felem_sqr(x120, x120);
} p384_felem_mul(x120, x120, x60);
p384_felem ret;
p384_felem_sqr(ret, x120);
for (int i = 1; i < 120; i++) {
p384_felem_sqr(ret, ret);
} p384_felem_mul(ret, ret, x120);
for (int i = 0; i < 15; i++) {
p384_felem_sqr(ret, ret);
} p384_felem_mul(ret, ret, x15);
for (int i = 0; i < (1 + 30); i++) {
p384_felem_sqr(ret, ret);
} p384_felem_mul(ret, ret, x30);
p384_felem_sqr(ret, ret);
p384_felem_sqr(ret, ret); p384_felem_mul(ret, ret, x2);
for (int i = 0; i < (64 + 30); i++) {
p384_felem_sqr(ret, ret);
} p384_felem_mul(ret, ret, x30);
p384_felem_sqr(ret, ret);
p384_felem_sqr(out, ret); }
static void p384_point_double(p384_felem x_out,
p384_felem y_out,
p384_felem z_out,
const p384_felem x_in,
const p384_felem y_in,
const p384_felem z_in) {
ec_nistp_point_double(p384_methods(), x_out, y_out, z_out, x_in, y_in, z_in);
}
static void p384_point_add(p384_felem x3, p384_felem y3, p384_felem z3,
const p384_felem x1,
const p384_felem y1,
const p384_felem z1,
const int mixed,
const p384_felem x2,
const p384_felem y2,
const p384_felem z2) {
ec_nistp_point_add(p384_methods(), x3, y3, z3, x1, y1, z1, mixed, x2, y2, z2);
}
#if defined(EC_NISTP_USE_S2N_BIGNUM)
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p384_methods) {
out->felem_num_limbs = P384_NLIMBS;
out->felem_num_bits = 384;
out->felem_add = bignum_add_p384;
out->felem_sub = bignum_sub_p384;
out->felem_mul = bignum_montmul_p384_selector;
out->felem_sqr = bignum_montsqr_p384_selector;
out->felem_neg = bignum_neg_p384;
out->felem_nz = p384_felem_nz;
out->point_dbl = p384_point_double;
out->point_add = p384_point_add;
}
#else
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p384_methods) {
out->felem_num_limbs = P384_NLIMBS;
out->felem_num_bits = 384;
out->felem_add = fiat_p384_add;
out->felem_sub = fiat_p384_sub;
out->felem_mul = fiat_p384_mul;
out->felem_sqr = fiat_p384_square;
out->felem_neg = fiat_p384_opp;
out->felem_nz = p384_felem_nz;
out->point_dbl = p384_point_double;
out->point_add = p384_point_add;
}
#endif
static int ec_GFp_nistp384_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;
}
p384_felem z1, z2;
p384_from_generic(z1, &point->Z);
p384_inv_square(z2, z1);
if (x_out != NULL) {
p384_felem x;
p384_from_generic(x, &point->X);
p384_felem_mul(x, x, z2);
p384_to_generic(x_out, x);
}
if (y_out != NULL) {
p384_felem y;
p384_from_generic(y, &point->Y);
p384_felem_sqr(z2, z2); p384_felem_mul(y, y, z1); p384_felem_mul(y, y, z2); p384_to_generic(y_out, y);
}
return 1;
}
static void ec_GFp_nistp384_add(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *a, const EC_JACOBIAN *b) {
p384_felem x1, y1, z1, x2, y2, z2;
p384_from_generic(x1, &a->X);
p384_from_generic(y1, &a->Y);
p384_from_generic(z1, &a->Z);
p384_from_generic(x2, &b->X);
p384_from_generic(y2, &b->Y);
p384_from_generic(z2, &b->Z);
p384_point_add(x1, y1, z1, x1, y1, z1, 0 , x2, y2, z2);
p384_to_generic(&r->X, x1);
p384_to_generic(&r->Y, y1);
p384_to_generic(&r->Z, z1);
}
static void ec_GFp_nistp384_dbl(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *a) {
p384_felem x, y, z;
p384_from_generic(x, &a->X);
p384_from_generic(y, &a->Y);
p384_from_generic(z, &a->Z);
p384_point_double(x, y, z, x, y, z);
p384_to_generic(&r->X, x);
p384_to_generic(&r->Y, y);
p384_to_generic(&r->Z, z);
}
static void ec_GFp_nistp384_mont_felem_to_bytes(
const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_FELEM *in) {
size_t len = BN_num_bytes(&group->field.N);
EC_FELEM felem_tmp;
p384_felem tmp;
p384_from_generic(tmp, in);
p384_felem_from_mont(tmp, tmp);
p384_to_generic(&felem_tmp, tmp);
bn_words_to_big_endian(out, len, felem_tmp.words, group->order.N.width);
*out_len = len;
}
static int ec_GFp_nistp384_mont_felem_from_bytes(
const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, size_t len) {
EC_FELEM felem_tmp;
p384_felem tmp;
if (!ec_GFp_simple_felem_from_bytes(group, &felem_tmp, in, len)) {
return 0;
}
p384_from_generic(tmp, &felem_tmp);
p384_felem_to_mont(tmp, tmp);
p384_to_generic(out, tmp);
return 1;
}
static int ec_GFp_nistp384_cmp_x_coordinate(const EC_GROUP *group,
const EC_JACOBIAN *p,
const EC_SCALAR *r) {
if (ec_GFp_simple_is_at_infinity(group, p)) {
return 0;
}
p384_felem Z2_mont;
p384_from_generic(Z2_mont, &p->Z);
p384_felem_mul(Z2_mont, Z2_mont, Z2_mont);
p384_felem r_Z2;
p384_from_scalar(r_Z2, r); p384_felem_mul(r_Z2, r_Z2, Z2_mont);
p384_felem X;
p384_from_generic(X, &p->X);
p384_felem_from_mont(X, X);
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) {
return 1;
}
assert(group->field.N.width == group->order.N.width);
EC_FELEM tmp;
BN_ULONG carry =
bn_add_words(tmp.words, r->words, group->order.N.d, group->field.N.width);
if (carry == 0 &&
bn_less_than_words(tmp.words, group->field.N.d, group->field.N.width)) {
p384_from_generic(r_Z2, &tmp);
p384_felem_mul(r_Z2, r_Z2, Z2_mont);
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) {
return 1;
}
}
return 0;
}
#define P384_MUL_WSIZE (5)
OPENSSL_STATIC_ASSERT(P384_MUL_WSIZE == 5,
p384_scalar_mul_window_size_is_not_equal_to_five)
#define P384_MUL_TWO_TO_WSIZE (1 << P384_MUL_WSIZE)
#define P384_MUL_NWINDOWS ((384 + P384_MUL_WSIZE - 1)/P384_MUL_WSIZE)
#define P384_MUL_PUB_WSIZE (5)
#define P384_MUL_TABLE_SIZE (P384_MUL_TWO_TO_WSIZE >> 1)
#define P384_MUL_PUB_TABLE_SIZE (1 << (P384_MUL_PUB_WSIZE - 1))
static void p384_select_point_affine(p384_felem out[2],
size_t idx,
const p384_felem table[][2],
size_t table_size) {
OPENSSL_memset(out, 0, sizeof(p384_felem) * 2);
for (size_t i = 0; i < table_size; i++) {
p384_limb_t mismatch = i ^ idx;
p384_felem_cmovznz(out[0], mismatch, table[i][0], out[0]);
p384_felem_cmovznz(out[1], mismatch, table[i][1], out[1]);
}
}
static void ec_GFp_nistp384_point_mul(const EC_GROUP *group, EC_JACOBIAN *r,
const EC_JACOBIAN *p,
const EC_SCALAR *scalar) {
p384_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}};
p384_from_generic(tmp[0], &p->X);
p384_from_generic(tmp[1], &p->Y);
p384_from_generic(tmp[2], &p->Z);
ec_nistp_scalar_mul(p384_methods(), res[0], res[1], res[2], tmp[0], tmp[1], tmp[2], scalar);
p384_to_generic(&r->X, res[0]);
p384_to_generic(&r->Y, res[1]);
p384_to_generic(&r->Z, res[2]);
}
#include "p384_table.h"
static void ec_GFp_nistp384_point_mul_base(const EC_GROUP *group,
EC_JACOBIAN *r,
const EC_SCALAR *scalar) {
p384_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}, ftmp;
int16_t rnaf[P384_MUL_NWINDOWS] = {0};
scalar_rwnaf(rnaf, P384_MUL_WSIZE, scalar, 384);
for (int i = 3; i >= 0; i--) {
for (int j = 0; i != 3 && j < P384_MUL_WSIZE; j++) {
p384_point_double(res[0], res[1], res[2], res[0], res[1], res[2]);
}
const size_t start_idx = ((P384_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;
p384_select_point_affine(tmp, idx, p384_g_pre_comp[j / 4],
P384_MUL_TABLE_SIZE);
p384_felem_opp(ftmp, tmp[1]);
p384_felem_cmovznz(tmp[1], is_neg, tmp[1], ftmp);
p384_point_add(res[0], res[1], res[2], res[0], res[1], res[2],
1 , tmp[0], tmp[1], p384_felem_one);
}
}
p384_felem_copy(tmp[0], p384_g_pre_comp[0][0][0]);
p384_felem_opp(tmp[1], p384_g_pre_comp[0][0][1]);
p384_point_add(tmp[0], tmp[1], tmp[2], res[0], res[1], res[2],
1 , tmp[0], tmp[1], p384_felem_one);
p384_felem_cmovznz(res[0], scalar->words[0] & 1, tmp[0], res[0]);
p384_felem_cmovznz(res[1], scalar->words[0] & 1, tmp[1], res[1]);
p384_felem_cmovznz(res[2], scalar->words[0] & 1, tmp[2], res[2]);
p384_to_generic(&r->X, res[0]);
p384_to_generic(&r->Y, res[1]);
p384_to_generic(&r->Z, res[2]);
}
static void ec_GFp_nistp384_point_mul_public(const EC_GROUP *group,
EC_JACOBIAN *r,
const EC_SCALAR *g_scalar,
const EC_JACOBIAN *p,
const EC_SCALAR *p_scalar) {
p384_felem res[3] = {{0}, {0}, {0}}, two_p[3] = {{0}, {0}, {0}}, ftmp;
p384_felem p_pre_comp[P384_MUL_PUB_TABLE_SIZE][3];
p384_from_generic(p_pre_comp[0][0], &p->X);
p384_from_generic(p_pre_comp[0][1], &p->Y);
p384_from_generic(p_pre_comp[0][2], &p->Z);
p384_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 < P384_MUL_PUB_TABLE_SIZE; i++) {
p384_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[385] = {0}, g_wnaf[385] = {0};
ec_compute_wNAF(group, p_wnaf, p_scalar, 384, P384_MUL_PUB_WSIZE);
ec_compute_wNAF(group, g_wnaf, g_scalar, 384, P384_MUL_WSIZE);
int16_t res_is_inf = 1;
int16_t d, is_neg, idx;
for (int i = 384; i >= 0; i--) {
if (!res_is_inf) {
p384_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) {
p384_felem_copy(res[0], p_pre_comp[idx][0]);
p384_felem_copy(res[1], p_pre_comp[idx][1]);
p384_felem_copy(res[2], p_pre_comp[idx][2]);
res_is_inf = 0;
} else {
if (is_neg) {
p384_felem_opp(ftmp, p_pre_comp[idx][1]);
} else {
p384_felem_copy(ftmp, p_pre_comp[idx][1]);
}
p384_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) {
p384_felem_copy(res[0], p384_g_pre_comp[0][idx][0]);
p384_felem_copy(res[1], p384_g_pre_comp[0][idx][1]);
p384_felem_copy(res[2], p384_felem_one);
res_is_inf = 0;
} else {
if (is_neg) {
p384_felem_opp(ftmp, p384_g_pre_comp[0][idx][1]);
} else {
p384_felem_copy(ftmp, p384_g_pre_comp[0][idx][1]);
}
p384_point_add(res[0], res[1], res[2],
res[0], res[1], res[2],
1 ,
p384_g_pre_comp[0][idx][0], ftmp, p384_felem_one);
}
}
}
p384_to_generic(&r->X, res[0]);
p384_to_generic(&r->Y, res[1]);
p384_to_generic(&r->Z, res[2]);
}
DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp384_method) {
out->point_get_affine_coordinates =
ec_GFp_nistp384_point_get_affine_coordinates;
out->jacobian_to_affine_batch =
ec_GFp_mont_jacobian_to_affine_batch; out->add = ec_GFp_nistp384_add;
out->dbl = ec_GFp_nistp384_dbl;
out->mul = ec_GFp_nistp384_point_mul;
out->mul_base = ec_GFp_nistp384_point_mul_base;
out->mul_public = ec_GFp_nistp384_point_mul_public;
out->mul_batch = ec_GFp_mont_mul_batch; out->mul_public_batch = ec_GFp_mont_mul_public_batch;
out->init_precomp = ec_GFp_mont_init_precomp; out->mul_precomp = ec_GFp_mont_mul_precomp; out->felem_mul = ec_GFp_mont_felem_mul;
out->felem_sqr = ec_GFp_mont_felem_sqr;
out->felem_to_bytes = ec_GFp_nistp384_mont_felem_to_bytes;
out->felem_from_bytes = ec_GFp_nistp384_mont_felem_from_bytes;
out->felem_reduce = ec_GFp_mont_felem_reduce; out->felem_exp = ec_GFp_mont_felem_exp; 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_nistp384_cmp_x_coordinate;
}
#endif