Path: blob/main/crypto/libecc/src/curves/ec_shortw.c
34869 views
/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/curves/ec_shortw.h>1617#define EC_SHORTW_CRV_MAGIC ((word_t)(0x9c7c46a1a04c6720ULL))1819/*20* Check pointed short Weierstrass curve structure has already been21* initialized. Returns -1 on error, 0 on success.22*/23int ec_shortw_crv_check_initialized(ec_shortw_crv_src_t crv)24{25int ret;2627MUST_HAVE((crv != NULL) && (crv->magic == EC_SHORTW_CRV_MAGIC), ret, err);28ret = 0;2930err:31return ret;32}3334/*35* Initialize pointed short Weierstrass curve structure using given a and b36* Fp elements representing curve equation (y^2 = x^3 + ax + b) parameters.37* 'order' parameter is the generator point order. The function returns 038* on success, -1 on error.39*/40int ec_shortw_crv_init(ec_shortw_crv_t crv, fp_src_t a, fp_src_t b, nn_src_t order)41{42fp tmp, tmp2;43int ret, iszero;44tmp.magic = tmp2.magic = WORD(0);4546ret = nn_check_initialized(order); EG(ret, err);47ret = fp_check_initialized(a); EG(ret, err);48ret = fp_check_initialized(b); EG(ret, err);49MUST_HAVE((a->ctx == b->ctx), ret, err);50MUST_HAVE((crv != NULL), ret, err);5152/* The discriminant (4 a^3 + 27 b^2) must be non zero */53ret = fp_init(&tmp, a->ctx); EG(ret, err);54ret = fp_init(&tmp2, a->ctx); EG(ret, err);55ret = fp_sqr(&tmp, a); EG(ret, err);56ret = fp_mul(&tmp, &tmp, a); EG(ret, err);57ret = fp_set_word_value(&tmp2, WORD(4)); EG(ret, err);58ret = fp_mul(&tmp, &tmp, &tmp2); EG(ret, err);5960ret = fp_set_word_value(&tmp2, WORD(27)); EG(ret, err);61ret = fp_mul(&tmp2, &tmp2, b); EG(ret, err);62ret = fp_mul(&tmp2, &tmp2, b); EG(ret, err);6364ret = fp_add(&tmp, &tmp, &tmp2); EG(ret, err);65ret = fp_iszero(&tmp, &iszero); EG(ret, err);66MUST_HAVE((!iszero), ret, err);6768ret = fp_init(&(crv->a), a->ctx); EG(ret, err);69ret = fp_init(&(crv->b), b->ctx); EG(ret, err);70ret = fp_init(&(crv->a_monty), a->ctx); EG(ret, err);7172ret = fp_copy(&(crv->a), a); EG(ret, err);73ret = fp_copy(&(crv->b), b); EG(ret, err);74ret = fp_redcify(&(crv->a_monty), a); EG(ret, err);7576ret = nn_copy(&(crv->order), order); EG(ret, err);7778#ifndef NO_USE_COMPLETE_FORMULAS79ret = fp_init(&(crv->b3), b->ctx); EG(ret, err);80ret = fp_init(&(crv->b_monty), b->ctx); EG(ret, err);81ret = fp_init(&(crv->b3_monty), b->ctx); EG(ret, err);8283ret = fp_add(&(crv->b3), b, b); EG(ret, err);84ret = fp_add(&(crv->b3), &(crv->b3), b); EG(ret, err);85ret = fp_redcify(&(crv->b_monty), b); EG(ret, err);86ret = fp_redcify(&(crv->b3_monty), &(crv->b3)); EG(ret, err);87#endif8889crv->magic = EC_SHORTW_CRV_MAGIC;9091err:92fp_uninit(&tmp);93fp_uninit(&tmp2);9495return ret;96}9798/* Uninitialize curve */99void ec_shortw_crv_uninit(ec_shortw_crv_t crv)100{101if((crv != NULL) && (crv->magic == EC_SHORTW_CRV_MAGIC)){102crv->magic = WORD(0);103}104105return;106}107108109