xref: /freebsd/crypto/libecc/src/curves/ec_edwards.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2017 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #include <libecc/curves/ec_edwards.h>
17 
18 #define EC_EDWARDS_CRV_MAGIC ((word_t)(0x9c7349a1837c6794ULL))
19 
20 /*
21  * Check pointed Edwards curve structure has already been
22  * initialized.
23  *
24  * Returns 0 on success, -1 on error.
25  */
ec_edwards_crv_check_initialized(ec_edwards_crv_src_t crv)26 int ec_edwards_crv_check_initialized(ec_edwards_crv_src_t crv)
27 {
28 	int ret;
29 
30 	MUST_HAVE((crv != NULL) && (crv->magic == EC_EDWARDS_CRV_MAGIC), ret, err);
31 	ret = 0;
32 
33 err:
34 	return ret;
35 }
36 
37 /*
38  * Initialize pointed Edwards curve structure using given a and d
39  * Fp elements representing curve equation (a x^2 + y^2 = 1 + d x^2 y^2) parameters.
40  *
41  * Returns 0 on success, -1 on error.
42  */
ec_edwards_crv_init(ec_edwards_crv_t crv,fp_src_t a,fp_src_t d,nn_src_t order)43 int ec_edwards_crv_init(ec_edwards_crv_t crv, fp_src_t a, fp_src_t d, nn_src_t order)
44 {
45 	int ret, iszero, cmp;
46 
47 	ret = nn_check_initialized(order); EG(ret, err);
48 	ret = fp_check_initialized(a); EG(ret, err);
49 	ret = fp_check_initialized(d); EG(ret, err);
50 	MUST_HAVE((a->ctx == d->ctx), ret, err);
51 	MUST_HAVE((crv != NULL), ret, err);
52 
53 	/* a and d in Fp, must be distinct and non zero */
54 	MUST_HAVE((!fp_iszero(a, &iszero)) && (!iszero), ret, err);
55 	MUST_HAVE((!fp_iszero(d, &iszero)) && (!iszero), ret, err);
56 	MUST_HAVE((!fp_cmp(a, d, &cmp)) && cmp, ret, err);
57 
58 	ret = fp_init(&(crv->a), a->ctx); EG(ret, err);
59 	ret = fp_init(&(crv->d), d->ctx); EG(ret, err);
60 	ret = fp_copy(&(crv->a), a); EG(ret, err);
61 	ret = fp_copy(&(crv->d), d); EG(ret, err);
62 	ret = nn_copy(&(crv->order), order); EG(ret, err);
63 
64 	crv->magic = EC_EDWARDS_CRV_MAGIC;
65 
66 err:
67 	return ret;
68 }
69 
70 
71 /* Uninitialize curve */
ec_edwards_crv_uninit(ec_edwards_crv_t crv)72 void ec_edwards_crv_uninit(ec_edwards_crv_t crv)
73 {
74 	if ((crv != NULL) && (crv->magic == EC_EDWARDS_CRV_MAGIC)) {
75 		crv->magic = WORD(0);
76 	}
77 
78 	return;
79 }
80