#include <openssl/ec.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include "internal.h"
#include "../../internal.h"
int ec_GFp_simple_group_init(EC_GROUP *group) {
BN_init(&group->field);
group->a_is_minus3 = 0;
return 1;
}
void ec_GFp_simple_group_finish(EC_GROUP *group) {
BN_free(&group->field);
}
int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
const BIGNUM *a, const BIGNUM *b,
BN_CTX *ctx) {
if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
return 0;
}
int ret = 0;
BN_CTX_start(ctx);
BIGNUM *tmp = BN_CTX_get(ctx);
if (tmp == NULL) {
goto err;
}
if (!BN_copy(&group->field, p)) {
goto err;
}
BN_set_negative(&group->field, 0);
bn_set_minimal_width(&group->field);
if (!ec_bignum_to_felem(group, &group->a, a) ||
!ec_bignum_to_felem(group, &group->b, b) ||
!ec_bignum_to_felem(group, &group->one, BN_value_one())) {
goto err;
}
if (!BN_copy(tmp, a) ||
!BN_add_word(tmp, 3)) {
goto err;
}
group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field));
ret = 1;
err:
BN_CTX_end(ctx);
return ret;
}
int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
BIGNUM *b) {
if ((p != NULL && !BN_copy(p, &group->field)) ||
(a != NULL && !ec_felem_to_bignum(group, a, &group->a)) ||
(b != NULL && !ec_felem_to_bignum(group, b, &group->b))) {
return 0;
}
return 1;
}
void ec_GFp_simple_point_init(EC_RAW_POINT *point) {
OPENSSL_memset(&point->X, 0, sizeof(EC_FELEM));
OPENSSL_memset(&point->Y, 0, sizeof(EC_FELEM));
OPENSSL_memset(&point->Z, 0, sizeof(EC_FELEM));
}
void ec_GFp_simple_point_copy(EC_RAW_POINT *dest, const EC_RAW_POINT *src) {
OPENSSL_memcpy(&dest->X, &src->X, sizeof(EC_FELEM));
OPENSSL_memcpy(&dest->Y, &src->Y, sizeof(EC_FELEM));
OPENSSL_memcpy(&dest->Z, &src->Z, sizeof(EC_FELEM));
}
void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
EC_RAW_POINT *point) {
ec_GFp_simple_point_init(point);
}
void ec_GFp_simple_invert(const EC_GROUP *group, EC_RAW_POINT *point) {
ec_felem_neg(group, &point->Y, &point->Y);
}
int ec_GFp_simple_is_at_infinity(const EC_GROUP *group,
const EC_RAW_POINT *point) {
return ec_felem_non_zero_mask(group, &point->Z) == 0;
}
int ec_GFp_simple_is_on_curve(const EC_GROUP *group,
const EC_RAW_POINT *point) {
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
const EC_FELEM *b) = group->meth->felem_mul;
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
group->meth->felem_sqr;
EC_FELEM rh;
felem_sqr(group, &rh, &point->X);
EC_FELEM tmp, Z4, Z6;
felem_sqr(group, &tmp, &point->Z);
felem_sqr(group, &Z4, &tmp);
felem_mul(group, &Z6, &Z4, &tmp);
if (group->a_is_minus3) {
ec_felem_add(group, &tmp, &Z4, &Z4);
ec_felem_add(group, &tmp, &tmp, &Z4);
ec_felem_sub(group, &rh, &rh, &tmp);
} else {
felem_mul(group, &tmp, &Z4, &group->a);
ec_felem_add(group, &rh, &rh, &tmp);
}
felem_mul(group, &rh, &rh, &point->X);
felem_mul(group, &tmp, &group->b, &Z6);
ec_felem_add(group, &rh, &rh, &tmp);
felem_sqr(group, &tmp, &point->Y);
ec_felem_sub(group, &tmp, &tmp, &rh);
BN_ULONG not_equal = ec_felem_non_zero_mask(group, &tmp);
BN_ULONG not_infinity = ec_felem_non_zero_mask(group, &point->Z);
return 1 & ~(not_infinity & not_equal);
}
int ec_GFp_simple_points_equal(const EC_GROUP *group, const EC_RAW_POINT *a,
const EC_RAW_POINT *b) {
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
const EC_FELEM *b) = group->meth->felem_mul;
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
group->meth->felem_sqr;
EC_FELEM tmp1, tmp2, Za23, Zb23;
felem_sqr(group, &Zb23, &b->Z); felem_mul(group, &tmp1, &a->X, &Zb23); felem_sqr(group, &Za23, &a->Z); felem_mul(group, &tmp2, &b->X, &Za23); ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp1);
felem_mul(group, &Zb23, &Zb23, &b->Z); felem_mul(group, &tmp1, &a->Y, &Zb23); felem_mul(group, &Za23, &Za23, &a->Z); felem_mul(group, &tmp2, &b->Y, &Za23); ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp1);
const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
const BN_ULONG a_not_infinity = ec_felem_non_zero_mask(group, &a->Z);
const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
const BN_ULONG a_and_b_infinity = ~(a_not_infinity | b_not_infinity);
const BN_ULONG equal =
a_and_b_infinity | (a_not_infinity & b_not_infinity & x_and_y_equal);
return equal & 1;
}
int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a,
const EC_RAW_POINT *b) {
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
const EC_FELEM *b) = group->meth->felem_mul;
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
group->meth->felem_sqr;
EC_FELEM tmp, Zb2;
felem_sqr(group, &Zb2, &b->Z); felem_mul(group, &tmp, &a->X, &Zb2); ec_felem_sub(group, &tmp, &tmp, &b->X);
const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp);
felem_mul(group, &tmp, &a->Y, &Zb2); felem_mul(group, &tmp, &tmp, &b->Z); ec_felem_sub(group, &tmp, &tmp, &b->Y);
const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp);
const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
const BN_ULONG equal = b_not_infinity & x_and_y_equal;
return equal & 1;
}
int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p,
const EC_SCALAR *r) {
if (ec_GFp_simple_is_at_infinity(group, p)) {
return 0;
}
EC_SCALAR x;
return ec_get_x_coordinate_as_scalar(group, &x, p) &&
ec_scalar_equal_vartime(group, &x, r);
}
void ec_GFp_simple_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);
for (size_t i = 0; i < len; i++) {
out[i] = in->bytes[len - 1 - i];
}
*out_len = len;
}
int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
const uint8_t *in, size_t len) {
if (len != BN_num_bytes(&group->field)) {
OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
return 0;
}
OPENSSL_memset(out, 0, sizeof(EC_FELEM));
for (size_t i = 0; i < len; i++) {
out->bytes[i] = in[len - 1 - i];
}
if (!bn_less_than_words(out->words, group->field.d, group->field.width)) {
OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
return 0;
}
return 1;
}