MPHELL  5.0.0
mphell_tuto_ecgdsa.c
Go to the documentation of this file.
1 /*
2  MPHELL-5.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2021 - Institut Fourier / Univ. Grenoble Alpes (France)
6 
7  This file is part of the MPHELL Library.
8  MPHELL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, version 3 of the License.
11 
12  MPHELL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with MPHELL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
45 #include <stdio.h>
46 #include "mphell/mphell.h"
47 #if MPHELL_USE_MULTITHREADING == 1
48 #include <omp.h>
49 #endif
50 
55 struct ecgdsa_sig
56 {
57  number *r;
58  number *s;
59 };
60 
65 typedef struct ecgdsa_sig ecgdsa_sig_t;
66 
72 
78 void
79 ecgdsa_sign_alloc(ecgdsa_sig *sig, uint8_t size)
80 {
81  (*sig)->r = (number*)malloc(sizeof(number));
82  (*sig)->s = (number*)malloc(sizeof(number));
83  number_init((*sig)->r, size);
84  number_init((*sig)->s, size);
85  number_set_ui(*((*sig)->r), (block)0);
86  number_set_ui(*((*sig)->s), (block)0);
87 }
88 
94 void
96 {
97  if ((*sig)->r)
98  {
99  number_free((*sig)->r);
100  free((*sig)->r);
101  }
102  if ((*sig)->s)
103  {
104  number_free((*sig)->s);
105  free((*sig)->s);
106  }
107 }
108 
119 void
120 ec_point_mul_ecdsa(ec_point dst, number_srcptr u1, ec_point_srcptr G,
121  number_srcptr u2, ec_point_srcptr key, ec_curve_srcptr E)
122 {
123 #if MPHELL_USE_MULTITHREADING == 1
124  omp_set_num_threads(2);
125  ec_point tmp;
126  ec_point_get_pool_elt(tmp, E->k, STACK_1);
127  #pragma omp parallel
128  {
129  #pragma omp sections
130  {
131  #pragma omp section
132  {
133  ec_point_mul(tmp, u2, key, E, STACK_1);
134  }
135  #pragma omp section
136  {
137  ec_point_mul(dst, u1, G, E, STACK_2);
138  }
139  }
140  }
141  ec_point_add(dst, dst, tmp, E, STACK_1);
142  ec_point_relax_pool_elt(tmp, E->k, STACK_1);
143 #else
144  ec_point tmp;
145  ec_point_get_pool_elt(tmp, E->k, STACK_1);
146  ec_point_mul(tmp, u2, key, E, STACK_1);
147  ec_point_mul(dst, u1, G, E, STACK_1);
148  ec_point_add(dst, dst, tmp, E, STACK_1);
149  ec_point_relax_pool_elt(tmp, E->k, STACK_1);
150 #endif
151 }
152 
161 void
162 ecgdsa_sign(unsigned char *hash, ecgdsa_sig *sig, number priv_key, ec_curve *curve)
163 {
164  number *rr = (*sig)->r;
165  number *ss = (*sig)->s;
166 
167  uint8_t size = (*curve)->k->size;
168 
169  number k; number_tmp_alloc(&k, size, STACK_1);
170  number h; number_tmp_alloc(&h, (strlen((char *)hash)/(BLOCK_SIZE/4)), STACK_1);
171  number t; number_tmp_alloc(&t, 2*size+1, STACK_1);
172 
173  number abs;
174  number_tmp_alloc(&abs, size, STACK_1);
175 
176  field_elt xx;
177  field_elt_get_pool_elt(&xx, (*curve)->k, STACK_1);
178 
179  ec_point x;
180  ec_point_get_pool_elt(x, (*curve)->k, STACK_1);
181 
182  bool newk = true;
183  number_set_str(h , (char*)hash, 16);
184  number_mod(h, h, (*curve)->n); /* To keep h small */
185 
186  while(newk)
187  {
188  /* Choose k randomly in [1, n-1] */
189  number_random1(k, (*curve)->n, STACK_1);
190  number test; number_init(&test, bits_to_nblock(256));
191  number_dec(test, (*curve)->n);
192  while(number_cmp(test, k)==0)
193  {
194  number_random1(k, (*curve)->n, STACK_1);
195  }
196  number_free(&test);
197 
198  /* Compute (x,y) = kG */
199  ec_point_mul_unified(x, k, (*curve)->G, *curve, STACK_1);
200  ec_point_get_x_affine(xx, x, *curve, STACK_1);
201 
202  /* r = x mod n */
203  field_elt_get_number(abs, xx, 1, (*curve)->k, STACK_1);
204  number_mod(*rr, abs, (*curve)->n);
205 
206  /* if r == 0 --> new k */
207  if(!number_iszero(*rr))
208  {
209  /* s = (k * r - hash)*priv_key mod n */
210  number_mul(t, *rr, k);
211  number_mod(t, t, (*curve)->n); /* To keep the multiplication small */
212  if (number_cmp(h,t)==1) /* We make sure that h<=t */
213  {
214  number_add(t, t, (*curve)->n);
215  }
216  number_sub(t, t, h);
217  /* not useful anymore number_mod(t, t, (*curve)->n); */ /* To keep the multiplication small */
218  number_mul(t, t, priv_key);
219  number_mod(*ss, t, (*curve)->n);
220  /* if s == 0 --> new k */
221  if(!number_iszero(*ss))
222  {
223  newk = false;
224  }
225  }
226  }
227  (*sig)->r = rr;
228  (*sig)->s = ss;
229 
230  /* Free memory */
231  ec_point_relax_pool_elt(x, (*curve)->k, STACK_1);
232  field_elt_relax_pool_elt(&xx, (*curve)->k, STACK_1);
233  number_tmp_free(&abs, size, STACK_1);
234  number_tmp_free(&t, 2*size+1, STACK_1);
235  number_tmp_free(&h, (strlen((char *)hash)/(BLOCK_SIZE/4)), STACK_1);
236  number_tmp_free(&k, size, STACK_1);
237 }
238 
239 
254 int8_t
255 ecgdsa_verify(unsigned char *hash, ecgdsa_sig *sig, ec_point *pub_key, ec_curve *curve)
256 {
257  /* Just check that r,s \in {1, ... , q-1} */
258  if( number_iszero(*((*sig)->r)) || number_cmp(*((*sig)->r),(*curve)->n)>=0 || number_iszero(*((*sig)->s)) || number_cmp(*((*sig)->s),(*curve)->n)>=0)
259  {
260  return false;
261  }
262 
263  uint8_t size = (*curve)->k->size;
264 
265  number w; number_tmp_alloc(&w, size, STACK_1);
266  number u1; number_tmp_alloc(&u1, size, STACK_1);
267  number u2; number_tmp_alloc(&u2, size, STACK_1);
268  number tt2; number_tmp_alloc(&tt2, size, STACK_1);
269  number t; number_tmp_alloc(&t, 2*size, STACK_1);
270  number f1; number_tmp_alloc(&f1, size, STACK_1);
271 
272  number abs;
273  number_tmp_alloc(&abs, size, STACK_1);
274 
275  field_elt xx;
276  field_elt_get_pool_elt(&xx, (*curve)->k, STACK_1);
277 
278  ec_point x;
279  ec_point_get_pool_elt(x, (*curve)->k, STACK_1);
280 
281  number h; number_init(&h, (strlen((char *)hash)/(BLOCK_SIZE/4)));
282  number_set_str(h , (char*)hash, 16);
283 
284  /* w = r^-1 mod n */
285  number_invmod(w, *((*sig)->r), (*curve)->n);
286 
287  /* u1 = hash * w mod n */
288  number_mod(tt2, h, (*curve)->n);
289  number_mul(t, tt2, w);
290  number_mod(u1, t, (*curve)->n);
291 
292  /* u2 = s*w mod n */
293  number_mul(t, *((*sig)->s), w);
294  number_mod(u2, t, (*curve)->n);
295 
296  /* x = u1*G + u2*Q */
297  ec_point_mul_ecdsa(x, u1, (*curve)->G, u2, *pub_key, *curve);
298  ec_point_get_x_affine(xx, x, *curve, STACK_1);
299 
300  /* Get the result, by comparing x value with r */
301  field_elt_get_number(abs, xx, 1, (*curve)->k, STACK_1);
302  number_mod(f1, abs, (*curve)->n);
303 
304  bool result = number_cmp(f1, *((*sig)->r)) == 0;
305 
306  /* Free memory */
307  number_free(&h);
308  ec_point_relax_pool_elt(x, (*curve)->k, STACK_1);
309  field_elt_relax_pool_elt(&xx, (*curve)->k, STACK_1);
310  number_tmp_free(&abs, size, STACK_1);
311  number_tmp_free(&f1, size, STACK_1);
312  number_tmp_free(&t, 2*size, STACK_1);
313  number_tmp_free(&tt2, size, STACK_1);
314  number_tmp_free(&u2, size, STACK_1);
315  number_tmp_free(&u1, size, STACK_1);
316  number_tmp_free(&w, size, STACK_1);
317 
318  return result;
319 }
320 
329 int8_t
331 {
332 
333  if(!ec_belongs(*pub_key, *curve, STACK_1))
334  {
335  /* pubkey does not belong to the curve */
336  return false;
337  }
338  if(ec_point_is_neutral(*pub_key, *curve, STACK_1))
339  {
340  /* pubkey is the neutral element */
341  return false;
342  }
343  /* We finally test the order of the curve */
344  ec_point x;
345  ec_point_get_pool_elt(x, (*curve)->k, STACK_1);
346  ec_point_mul(x, (*curve)->n, *pub_key, *curve, STACK_1);
347  if(!ec_point_is_neutral(x, *curve, STACK_1))
348  {
349  /* pubkey is the neutral element */
350  return false;
351  }
352  ec_point_relax_pool_elt(x, (*curve)->k, STACK_1);
353 
354  return true;
355 }
356 
357 int main()
358 {
359  /* Initialise MPHELL with 256 bits of security strength for the entropy, RANDOM_AES256 as DRBG and DEVURANDOM as entropy source */
360 
362 
363  /* Allocate a field of size 4*block_SIZE = 4*64 = 256 on 64 bits architecture */
364 
365  field k;
366  field_alloc(k, FP, bits_to_nblock(256), NULL);
367 
368  /* Allocate a number of size 4*block_SIZE = 4*64 = 256 on 64 bits architecture */
369 
370  number p;
371  number_init(&p, bits_to_nblock(256));
372 
373  /* Set the number p from a string in base 16 */
374 
375  number_set_str(p, "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", 16);
376 #if MPHELL_USE_AMNS == 1
377  amns AMNS;
378 #if MPHELL_USE_AMNS_32 == 0
379  amns_alloc_init_str(&AMNS, "[4, 5, [-3, 0, 0, 0, 0, 1], 55, 71533969486545864413516767253794911064875612240194438086363842467605348744666, [281003535425591, -579833024021834, -91487654138292, -1407285223270225, 1257806506188213], [10138092101046066474, 10662180220582548599, 13438525377408553810, 2956505949806397541, 1582018761247275305], [-1488511254885447, 1691948961301838, 368225590485535, 364881739107280, -1251317625290732], [1396607582108050, -3787438073044259, 594275210547971, 1341450284833267, -604094026418167]]", p);
380 #else
381  amns_alloc_init_str(&AMNS, "[1, 13, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 24, 12283929623108920300809241648376276210021878764342577833737044552429073849048, [339101, 37569, 167801, -196152, -417739, -294951, 49819, 364997, 286578, 400226, 258890, -193421, 198477], [2979672525, 235065897, 2631974852, 2919389277, 2172036204, 3746829531, 2056633047, 794426671, 2257704876, 1726773529, 1568503618, 66995355, 3378284366], [954133, 569228, 1096741, 836659, 630615, 1288006, 872327, 1504995, 1367932, 401997, 700080, 158946, 137905], [803354, 622540, 375011, -650534, 854552, 1546271, 1126099, 1770152, 1141472, 187022, 247179, 639661, 675036]]", p);
382 #endif
383  field_set_amns(k, AMNS);
384 #endif
385 
386  /* Create the field of characteristic p */
387 
388  field_create(k, "", STACK_1, 1, p);
389 
390  /* Allocate curve */
391 
392  ec_curve E;
393  ec_alloc (E, k);
394  ec_init(E, k);
395 
396  /* Create curve */
397 
398  field_elt a, b;
399  ec_point G;
400  number n, h;
401 
402  field_elt_alloc(&a, k);
403  field_elt_init(a, k);
404  field_elt_alloc(&b, k);
405  field_elt_init(b, k);
406  ec_point_alloc(G, k);
407  ec_point_init(G, k);
408  number_init(&n, bits_to_nblock(256));
409  number_init(&h, bits_to_nblock(256));
410 
411  field_elt_set_str(a, "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", 16, false, k, STACK_1);
412  field_elt_set_str(b, "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", 16, false, k, STACK_1);
413  ec_point_set_aff_str(G, "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", false, 16, WEIERSTRASS, k, STACK_1);
414  number_set_str(h, "1", 16);
415  number_set_str(n, "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 16);
416 
417  ec_create (E, "Weierstrass_nist_256", k, a, b, G, h, n, WEIERSTRASS, JACOBIAN, STACK_1);
418 
419  printf("E: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
420 
421  field_elt_free(&a, k);
422  field_elt_free(&b, k);
423  ec_point_free(G, k);
424  number_free(&h);
425  number_free(&n);
426 
427  /* Get private key from random value between 0 and the size of the group generated by G:E->n */
428  number priv_key;
429  number_init(&priv_key, bits_to_nblock(256));
430  number_random1(priv_key, E->n, STACK_1);
431 
432  /* Test that the value is not equal to p-1 and change it until it is the case */
433  number test;
434  number_init(&test, bits_to_nblock(256));
435  number_dec(test, E->n);
436  while(number_cmp(test, priv_key)==0)
437  {
438  number_random1(priv_key, E->n, STACK_1);
439  }
440  number_free(&test);
441  printf("\nThe private key has been created its value is:\n");
442  number_print(priv_key, 16);
443 
444  /* Computation of the public key */
445  /* Here is the main difference for ECGDSA pub_key=priv_key^{-1}*G */
446  number inv_priv_key; number_init(&inv_priv_key, k->size);
447  number_invmod(inv_priv_key,priv_key,E->n);
448  ec_point pub_key;
449  ec_point_alloc(pub_key, E->k);
450  ec_point_init(pub_key, E->k);
451  ec_point_mul_unified(pub_key, inv_priv_key, E->G, E, STACK_1);
452  number_free(&inv_priv_key);
453 
454  /* Print the public key */
455  ec_point_norm(pub_key, E, STACK_1);
456  printf("\nThe pubkey has been computed:\n");
457  ec_point_print(pub_key, 16, true, k, STACK_1);
458 
459  /* This test is optional.*/
460  printf("\nThe validity of the public key is a:\n");
461  int ret = ecgdsa_pub_key_validation(&pub_key, &E);
462  if( ret < 1 )
463  {
464  printf("ERROR\n");
465  }
466  else
467  {
468  printf("SUCCESS\n");
469  }
470 
471  /* Digest to sign */
472  char * digest = "982c20c2493fc9ae405b74b65a022662c014a38ef3d707217e56e57afac05994";
473 
474  /* Allocate ecgdsa_sig structure */
475  ecgdsa_sig sig;
476  ecgdsa_sign_alloc(&sig, bits_to_nblock(256));
477 
478  /* Sign the digest */
479  ecgdsa_sign((unsigned char *)digest, &sig, priv_key, &E);
480 
481  printf("\nThe ECGDSA signature is: (");
482  number_print(*(sig->r), 16);
483  printf(", ");
484  number_print(*(sig->s), 16);
485  printf(")\n");
486 
487  /* Verify the signature */
488  printf("\n-> Verify ECGDSA signature \n");
489  ret = ecgdsa_verify((unsigned char *)digest, &sig, &pub_key, &E);
490  if( ret < 1 )
491  {
492  printf("ERROR\n");
493  }
494  else
495  {
496  printf("SUCCESS\n");
497  }
498 
499  /* Free memory */
500 
501  ec_point_free(pub_key, k);
502  number_free(&priv_key);
503  ecgdsa_sign_free(&sig);
504  number_free(&p);
505  field_free(k);
506 #if MPHELL_USE_AMNS == 1
507  amns_free(&AMNS);
508 #endif
509  ec_free(E);
510 
511  free_mphell();
512 
513  return 0;
514 }
void amns_free(amns_ptr *AMNS)
Free the amns system.
Definition: mphell-amns.c:444
void amns_alloc_init_str(amns_ptr *AMNS, char *str, number p)
Allocate and initialise the amns system from the string generated by the Sage AMNS generator from htt...
Definition: mphell-amns.c:1242
void ec_point_get_x_affine(field_elt x, ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Convert P->x to its affine representation.
Definition: mphell-curve.c:972
void ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
void ec_point_print(ec_point_srcptr P, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack)
Print a description of P.
void ec_create(ec_curve_ptr E, const char *id_curve, field_srcptr k, fe_srcptr a, fe_srcptr b, ec_point_srcptr G, number_srcptr h, number_srcptr n, const ec_type type, const ec_formula f, uint8_t stack)
Create an elliptic curve E, the curve must be allocated and initialised (ec_alloc & ec_init)
Definition: mphell-curve.c:65
bool ec_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
void ec_point_mul(ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to n * P1 using Montgomery for Weierstrass elliptic curve, and naive method for other elliptic...
void ec_point_add(ec_point_ptr P3, ec_point_srcptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Set P3 to P1 + P2, using dedicated formulae (not protected against SPA, but faster)
void ec_free(ec_curve_ptr E)
Free the elliptic curve E.
Definition: mphell-curve.c:809
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
Definition: mphell-curve.c:55
bool ec_point_is_neutral(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P is the neutral element.
void ec_point_mul_unified(ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to n * P1 using Montgomery for Weierstrass elliptic curve, and naive method for other elliptic...
void ec_point_set_aff_str(ec_point_ptr P, const char *str_x, const char *str_y, const bool is_reduced, const uint8_t base, const ec_type type, field_srcptr k, uint8_t stack)
Set a point from its affine coordinates under string format.
Definition: mphell-curve.c:913
void ec_point_init(ec_point_ptr P, field_srcptr k)
Initialise an elliptic curve point.
Definition: mphell-curve.c:842
void ec_point_free(ec_point_ptr P, field_srcptr k)
Free the point P.
Definition: mphell-curve.c:858
void ec_point_norm(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Convert a point in projective or jacobian coordinate to an affine point (x,y)
Definition: mphell-curve.c:953
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
Definition: mphell-curve.c:834
void ec_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
static void ec_point_get_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Get an initialised point from the pool.
Definition: mphell-curve.h:206
@ WEIERSTRASS
Definition: mphell-curve.h:41
static void ec_point_relax_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Relax an initialised point from the pool.
Definition: mphell-curve.h:222
@ JACOBIAN
Definition: mphell-curve.h:59
@ DEVURANDOM
void field_elt_free(fe_ptr *src, field_srcptr k)
Free space used by src.
Definition: mphell-field.c:348
void field_alloc(field_ptr k, const field_type type, const uint8_t size, field_ptr base)
Allocates space for the different fields of the structure pointed by k.
Definition: mphell-field.c:37
void field_elt_set_str(fe_ptr dst, const char *str, const uint8_t base, const bool isreduced, field_srcptr k, uint8_t stack)
Set dst to str, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-field.c:516
void field_elt_init(fe_ptr dst, field_srcptr k)
Initialise the field element.
Definition: mphell-field.c:291
void field_elt_get_number(number_ptr dst, fe_srcptr src, uint8_t pos, field_srcptr k, uint8_t stack)
If Montgomery arithmetic is used, lift src (which is into Montgomery form) to classical fp (or its co...
Definition: mphell-field.c:574
void field_elt_alloc(fe_ptr *dst, field_srcptr k)
Allocate space for a field element.
Definition: mphell-field.c:269
void field_free(field_ptr k)
Free the space of the field informations structure.
Definition: mphell-field.c:194
void field_create(field_ptr k, const char *id, uint8_t stack, const uint32_t n,...)
Initialize the different fields of the structure pointed by k.
Definition: mphell-field.c:87
static void field_elt_relax_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Relax an initialised field element from the pool.
Definition: mphell-field.h:143
field_t field[1]
Address of a field structure.
Definition: mphell-field.h:86
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:37
static void field_elt_get_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Get an initialised field element from the pool.
Definition: mphell-field.h:110
@ FP
Definition: mphell-field.h:57
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
void init_mphell(const uint16_t security_strength, const random_type type, const entropy_type entropy)
Initialise MPHELL with security_strength bits of security (for random number only).
Definition: mphell-init.c:35
void number_random1(number_ptr dst, number_srcptr bound, uint8_t stack)
Set dst to a random number_ptr between 0 and bound, the random process is chosen at the MPHELL initia...
void number_mod(number_ptr dst, number_srcptr src1, number_srcptr src2)
Compute dst such that src1 = q * src2 + dst ; dst < src2.
bool number_iszero(number_srcptr src)
Test if src is zero.
void number_invmod(number_ptr dst, number_srcptr src1, number_srcptr src2)
Compute dst such that dst = src1^(-1) mod src2.
void number_set_ui(number_ptr dst, const block src)
Set dst to src.
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
Definition: mphell-number.c:75
void number_set_str(number_ptr dst, const char *str, const uint8_t base)
Set dst to str.
void number_sub(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 - src2 if src1 - src2 fit in dst.
void number_tmp_free(number *t, const uint8_t size, uint8_t stack)
Free a temporary number.
Definition: mphell-number.c:45
void number_add(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 + src2 if src1 + src2 fit in dst.
int8_t number_cmp(number_srcptr src1, number_srcptr src2)
Compare src1 and src2.
void number_tmp_alloc(number *t, const uint8_t size, uint8_t stack)
Allocate a temporary number.
Definition: mphell-number.c:31
void number_print(number_srcptr src, const uint8_t base)
Print src in base "base".
void number_dec(number_ptr dst, number_srcptr src)
Set dst to src - 1 if src - 1 fit in dst.
void number_mul(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 * src2.
void number_init(number *dst, const uint8_t n)
Allocate a number_ptr on the RAM memory (malloc)
Definition: mphell-number.c:59
@ RANDOM_AES256
Definition: mphell-random.h:39
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
Definition: mphell-util.c:29
void ec_point_mul_ecdsa(ec_point dst, number_srcptr u1, ec_point_srcptr G, number_srcptr u2, ec_point_srcptr key, ec_curve_srcptr E)
Compute u1 * G + u2 * key.
int8_t ecgdsa_verify(unsigned char *hash, ecgdsa_sig *sig, ec_point *pub_key, ec_curve *curve)
Verify the signature "sig" of hash "hash" using public key "pub_key" and the EC "curve".
void ecgdsa_sign_alloc(ecgdsa_sig *sig, uint8_t size)
Allocate a signature structure.
int8_t ecgdsa_pub_key_validation(ec_point *pub_key, ec_curve *curve)
Verify that the public key is valid (id est check taht pub_key belongs to the curve,...
void ecgdsa_sign(unsigned char *hash, ecgdsa_sig *sig, number priv_key, ec_curve *curve)
Sign "hash" with ECGDSA algorithm using the EC "curve" and the private key "key".
void ecgdsa_sign_free(ecgdsa_sig *sig)
Free a used signature structure.
Define a AMNS.
Definition: mphell-amns.h:81
Define an elliptic curve.
Definition: mphell-curve.h:141
field_ptr k
Definition: mphell-curve.h:143
ec_point G
Definition: mphell-curve.h:148
number n
Definition: mphell-curve.h:149
Define an elliptic curve point.
Definition: mphell-curve.h:105