ecdsa.c
Go to the documentation of this file.
1 /**
2  * @file ecdsa.c
3  * @brief ECDSA (Elliptic Curve Digital Signature Algorithm)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "core/crypto_mem.h"
37 #include "ecc/ecdsa.h"
38 #include "ecc/ec_misc.h"
39 #include "encoding/asn1.h"
40 #include "rng/hmac_drbg.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (ECDSA_SUPPORT == ENABLED)
45 
46 //ECDSA with SHA-1 OID (1.2.840.10045.4.1)
47 const uint8_t ECDSA_WITH_SHA1_OID[7] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x01};
48 //ECDSA with SHA-224 OID (1.2.840.10045.4.3.1)
49 const uint8_t ECDSA_WITH_SHA224_OID[8] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x01};
50 //ECDSA with SHA-256 OID (1.2.840.10045.4.3.2)
51 const uint8_t ECDSA_WITH_SHA256_OID[8] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02};
52 //ECDSA with SHA-384 OID (1.2.840.10045.4.3.3)
53 const uint8_t ECDSA_WITH_SHA384_OID[8] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x03};
54 //ECDSA with SHA-512 OID (1.2.840.10045.4.3.4)
55 const uint8_t ECDSA_WITH_SHA512_OID[8] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x04};
56 //ECDSA with SHA-3-224 OID (2.16.840.1.101.3.4.3.9)
57 const uint8_t ECDSA_WITH_SHA3_224_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x09};
58 //ECDSA with SHA-3-256 OID (2.16.840.1.101.3.4.3.10)
59 const uint8_t ECDSA_WITH_SHA3_256_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0A};
60 //ECDSA with SHA-3-384 OID (2.16.840.1.101.3.4.3.11)
61 const uint8_t ECDSA_WITH_SHA3_384_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0B};
62 //ECDSA with SHA-3-512 OID (2.16.840.1.101.3.4.3.12)
63 const uint8_t ECDSA_WITH_SHA3_512_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0C};
64 //ECDSA with SHAKE128 OID (1.3.6.1.5.5.7.6.32)
65 const uint8_t ECDSA_WITH_SHAKE128_OID[8] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x06, 0x20};
66 //ECDSA with SHAKE256 OID (1.3.6.1.5.5.7.6.33)
67 const uint8_t ECDSA_WITH_SHAKE256_OID[8] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x06, 0x21};
68 
69 
70 /**
71  * @brief Initialize an ECDSA signature
72  * @param[in] signature Pointer to the ECDSA signature to initialize
73  **/
74 
76 {
77  //Initialize elliptic curve parameters
78  signature->curve = NULL;
79 
80  //Initialize (R, S) integer pair
81  ecScalarSetInt(signature->r, 0, EC_MAX_ORDER_SIZE);
82  ecScalarSetInt(signature->s, 0, EC_MAX_ORDER_SIZE);
83 }
84 
85 
86 /**
87  * @brief Release an ECDSA signature
88  * @param[in] signature Pointer to the ECDSA signature to free
89  **/
90 
92 {
93  //Clear ECDSA signature
94  cryptoZeroizeMem(signature, sizeof(EcdsaSignature));
95 }
96 
97 
98 /**
99  * @brief Import an ECDSA signature
100  * @param[out] signature ECDSA signature
101  * @param[in] curve Elliptic curve parameters
102  * @param[in] input Pointer to the octet string
103  * @param[in] length Length of the octet string, in bytes
104  * @param[in] format ECDSA signature format (ASN.1 or raw format)
105  * @return Error code
106  **/
107 
109  const uint8_t *input, size_t length, EcdsaSignatureFormat format)
110 {
111  error_t error;
112  size_t n;
113 
114  //Check parameters
115  if(signature == NULL || curve == NULL || input == NULL)
116  {
118  }
119 
120  //Get the length of the order, in bytes
121  n = (curve->orderSize + 7) / 8;
122 
123  //Debug message
124  TRACE_DEBUG("Importing ECDSA signature...\r\n");
125 
126  //Dump ECDSA signature
127  TRACE_DEBUG(" signature:\r\n");
128  TRACE_DEBUG_ARRAY(" ", input, length);
129 
130  //Check the format of the ECDSA signature
131  if(format == ECDSA_SIGNATURE_FORMAT_ASN1)
132  {
133  Asn1Tag tag;
134 
135  //Display ASN.1 structure
136  error = asn1DumpObject(input, length, 0);
137  //Any error to report?
138  if(error)
139  {
140  return error;
141  }
142 
143  //Read the contents of the ASN.1 structure
144  error = asn1ReadSequence(input, length, &tag);
145  //Failed to decode ASN.1 tag?
146  if(error)
147  {
148  return error;
149  }
150 
151  //Malformed ECDSA signature?
152  if(length != tag.totalLength)
153  {
154  return ERROR_INVALID_SYNTAX;
155  }
156 
157  //Point to the first field
158  input = tag.value;
159  length = tag.length;
160 
161  //Read the integer R
162  error = asn1ReadTag(input, length, &tag);
163  //Failed to decode ASN.1 tag?
164  if(error)
165  {
166  return error;
167  }
168 
169  //Enforce encoding, class and type
170  error = asn1CheckTag(&tag, FALSE, ASN1_CLASS_UNIVERSAL,
172  //Invalid tag?
173  if(error)
174  {
175  return error;
176  }
177 
178  //Make sure R is a positive integer
179  if(tag.length == 0 || (tag.value[0] & 0x80) != 0)
180  {
181  return ERROR_INVALID_SYNTAX;
182  }
183 
184  //Convert the octet string to an integer
185  error = ecScalarImport(signature->r, EC_MAX_ORDER_SIZE, tag.value,
187  //Any error to report?
188  if(error)
189  {
190  return error;
191  }
192 
193  //Point to the next field
194  input += tag.totalLength;
195  length -= tag.totalLength;
196 
197  //Read the integer S
198  error = asn1ReadTag(input, length, &tag);
199  //Failed to decode ASN.1 tag?
200  if(error)
201  {
202  return error;
203  }
204 
205  //Enforce encoding, class and type
206  error = asn1CheckTag(&tag, FALSE, ASN1_CLASS_UNIVERSAL,
208  //Invalid tag?
209  if(error)
210  {
211  return error;
212  }
213 
214  //Make sure S is a positive integer
215  if(tag.length == 0 || (tag.value[0] & 0x80) != 0)
216  {
217  return ERROR_INVALID_SYNTAX;
218  }
219 
220  //Convert the octet string to an integer
221  error = ecScalarImport(signature->s, EC_MAX_ORDER_SIZE, tag.value,
223  //Any error to report?
224  if(error)
225  {
226  return error;
227  }
228 
229  //Malformed ECDSA signature?
230  if(length != tag.totalLength)
231  {
232  return ERROR_INVALID_SYNTAX;
233  }
234 
235  //Dump (R, S) integer pair
236  TRACE_DEBUG(" r:\r\n");
237  TRACE_DEBUG_EC_SCALAR(" ", signature->r, EC_MAX_ORDER_SIZE);
238  TRACE_DEBUG(" s:\r\n");
239  TRACE_DEBUG_EC_SCALAR(" ", signature->s, EC_MAX_ORDER_SIZE);
240  }
241  else if(format == ECDSA_SIGNATURE_FORMAT_RAW)
242  {
243  //Check the length of the octet string
244  if(length != (n * 2))
245  {
246  return ERROR_INVALID_LENGTH;
247  }
248 
249  //Convert R to an integer
250  error = ecScalarImport(signature->r, EC_MAX_ORDER_SIZE, input, n,
252  //Any error to report?
253  if(error)
254  {
255  return error;
256  }
257 
258  //Convert S to an integer
259  error = ecScalarImport(signature->s, EC_MAX_ORDER_SIZE,
260  input + n, n, EC_SCALAR_FORMAT_BIG_ENDIAN);
261  //Any error to report?
262  if(error)
263  {
264  return error;
265  }
266  }
267  else if(format == ECDSA_SIGNATURE_FORMAT_RAW_R)
268  {
269  //Convert R to an integer
270  error = ecScalarImport(signature->r, EC_MAX_ORDER_SIZE, input, length,
272  //Any error to report?
273  if(error)
274  {
275  return error;
276  }
277  }
278  else if(format == ECDSA_SIGNATURE_FORMAT_RAW_S)
279  {
280  //Convert S to an integer
281  error = ecScalarImport(signature->s, EC_MAX_ORDER_SIZE, input, length,
283  //Any error to report?
284  if(error)
285  {
286  return error;
287  }
288  }
289  else
290  {
291  //Invalid format
293  }
294 
295  //Save elliptic curve parameters
296  signature->curve = curve;
297 
298  //Successful processing
299  return NO_ERROR;
300 }
301 
302 
303 /**
304  * @brief Export an ECDSA signature
305  * @param[in] signature ECDSA signature
306  * @param[out] output Pointer to the octet string (optional parameter)
307  * @param[out] written Length of the resulting octet string, in bytes
308  * @param[in] format ECDSA signature format (ASN.1 or raw format)
309  * @return Error code
310  **/
311 
312 error_t ecdsaExportSignature(const EcdsaSignature *signature, uint8_t *output,
313  size_t *written, EcdsaSignatureFormat format)
314 {
315  error_t error;
316  size_t k;
317  size_t n;
318  size_t length;
319  size_t orderLen;
320  uint8_t *p;
321  Asn1Tag tag;
322 
323  //Check parameters
324  if(signature == NULL || written == NULL)
325  {
327  }
328 
329  //Invalid elliptic curve?
330  if(signature->curve == NULL)
331  {
333  }
334 
335  //Get the length of the order, in words
336  orderLen = (signature->curve->orderSize + 31) / 32;
337 
338  //Debug message
339  TRACE_DEBUG("Exporting ECDSA signature...\r\n");
340 
341  //Dump (R, S) integer pair
342  TRACE_DEBUG(" r:\r\n");
343  TRACE_DEBUG_EC_SCALAR(" ", signature->r, orderLen);
344  TRACE_DEBUG(" s:\r\n");
345  TRACE_DEBUG_EC_SCALAR(" ", signature->s, orderLen);
346 
347  //Check the format of the ECDSA signature
348  if(format == ECDSA_SIGNATURE_FORMAT_ASN1)
349  {
350  //Point to the buffer where to write the ASN.1 structure
351  p = output;
352  //Length of the ASN.1 structure
353  length = 0;
354 
355  //R is always encoded in the smallest possible number of octets
356  k = ecScalarGetBitLength(signature->r, orderLen) / 8 + 1;
357 
358  //R is represented by an integer
359  tag.constructed = FALSE;
362  tag.length = k;
363 
364  //Write the corresponding ASN.1 tag
365  error = asn1WriteHeader(&tag, FALSE, p, &n);
366  //Any error to report?
367  if(error)
368  {
369  return error;
370  }
371 
372  //Advance data pointer
373  ASN1_INC_POINTER(p, n);
374  length += n;
375 
376  //If the output parameter is NULL, then the function calculates the
377  //length of the ASN.1 structure without copying any data
378  if(p != NULL)
379  {
380  //Convert R to an octet string
381  error = ecScalarExport(signature->r, EC_MAX_ORDER_SIZE, p, k,
383  //Any error to report?
384  if(error)
385  {
386  return error;
387  }
388  }
389 
390  //Advance data pointer
391  ASN1_INC_POINTER(p, k);
392  length += k;
393 
394  //S is always encoded in the smallest possible number of octets
395  k = ecScalarGetBitLength(signature->s, orderLen) / 8 + 1;
396 
397  //S is represented by an integer
398  tag.constructed = FALSE;
401  tag.length = k;
402 
403  //Write the corresponding ASN.1 tag
404  error = asn1WriteHeader(&tag, FALSE, p, &n);
405  //Any error to report?
406  if(error)
407  {
408  return error;
409  }
410 
411  //Advance data pointer
412  ASN1_INC_POINTER(p, n);
413  length += n;
414 
415  //If the output parameter is NULL, then the function calculates the
416  //length of the ASN.1 structure without copying any data
417  if(p != NULL)
418  {
419  //Convert S to an octet string
420  error = ecScalarExport(signature->s, EC_MAX_ORDER_SIZE, p, k,
422  //Any error to report?
423  if(error)
424  {
425  return error;
426  }
427  }
428 
429  //Advance data pointer
430  ASN1_INC_POINTER(p, k);
431  length += k;
432 
433  //The (R, S) integer pair is encapsulated within a sequence
434  tag.constructed = TRUE;
437  tag.length = length;
438 
439  //Write the corresponding ASN.1 tag
440  error = asn1InsertHeader(&tag, output, &n);
441  //Any error to report?
442  if(error)
443  {
444  return error;
445  }
446 
447  //Total length of the ASN.1 structure
448  *written = length + n;
449  }
450  else if(format == ECDSA_SIGNATURE_FORMAT_RAW)
451  {
452  //Get the length of the order, in bytes
453  n = (signature->curve->orderSize + 7) / 8;
454 
455  //If the output parameter is NULL, then the function calculates the
456  //length of the octet string without copying any data
457  if(output != NULL)
458  {
459  //Convert R to an octet string
460  error = ecScalarExport(signature->r, (n + 3) / 4, output, n,
462  //Any error to report?
463  if(error)
464  {
465  return error;
466  }
467 
468  //Convert S to an octet string
469  error = ecScalarExport(signature->s, (n + 3) / 4, output + n, n,
471  //Any error to report?
472  if(error)
473  {
474  return error;
475  }
476  }
477 
478  //Length of the resulting octet string
479  *written = 2 * n;
480  }
481  else if(format == ECDSA_SIGNATURE_FORMAT_RAW_R)
482  {
483  //Get the length of the order, in bytes
484  n = (signature->curve->orderSize + 7) / 8;
485 
486  //If the output parameter is NULL, then the function calculates the
487  //length of the octet string without copying any data
488  if(output != NULL)
489  {
490  //Convert R to an octet string
491  error = ecScalarExport(signature->r, (n + 3) / 4, output, n,
493  //Any error to report?
494  if(error)
495  {
496  return error;
497  }
498  }
499 
500  //Length of the resulting octet string
501  *written = n;
502  }
503  else if(format == ECDSA_SIGNATURE_FORMAT_RAW_S)
504  {
505  //Get the length of the order, in bytes
506  n = (signature->curve->orderSize + 7) / 8;
507 
508  //If the output parameter is NULL, then the function calculates the
509  //length of the octet string without copying any data
510  if(output != NULL)
511  {
512  //Convert S to an octet string
513  error = ecScalarExport(signature->s, (n + 3) / 4, output, n,
515  //Any error to report?
516  if(error)
517  {
518  return error;
519  }
520  }
521 
522  //Length of the resulting octet string
523  *written = n;
524  }
525  else
526  {
527  //Invalid format
529  }
530 
531  //Dump ECDSA signature
532  if(output != NULL)
533  {
534  TRACE_DEBUG(" signature:\r\n");
535  TRACE_DEBUG_ARRAY(" ", output, *written);
536  }
537 
538  //Successful processing
539  return NO_ERROR;
540 }
541 
542 
543 /**
544  * @brief ECDSA signature generation
545  * @param[in] prngAlgo PRNG algorithm
546  * @param[in] prngContext Pointer to the PRNG context
547  * @param[in] privateKey Signer's EC private key
548  * @param[in] digest Digest of the message to be signed
549  * @param[in] digestLen Length in octets of the digest
550  * @param[out] signature (R, S) integer pair
551  * @return Error code
552  **/
553 
554 __weak_func error_t ecdsaGenerateSignature(const PrngAlgo *prngAlgo,
555  void *prngContext, const EcPrivateKey *privateKey, const uint8_t *digest,
556  size_t digestLen, EcdsaSignature *signature)
557 {
558  error_t error;
559  uint_t n;
560  uint_t pLen;
561  uint_t qLen;
562 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
564 #else
566 #endif
567 
568  //Check parameters
569  if(privateKey == NULL || digest == NULL || signature == NULL)
570  {
572  }
573 
574  //Invalid elliptic curve?
575  if(privateKey->curve == NULL)
576  {
578  }
579 
580  //The private key d must be in the range [1, q - 2]. The value q - 1 is
581  //excluded since the scalar multiplication does not handle it
582  error = ecCheckPrivateKey(privateKey);
583  //Any error to report?
584  if(error)
585  {
586  return error;
587  }
588 
589  //Get the length of the modulus, in words
590  pLen = (privateKey->curve->fieldSize + 31) / 32;
591  //Get the length of the order, in words
592  qLen = (privateKey->curve->orderSize + 31) / 32;
593 
594  //Debug message
595  TRACE_DEBUG("ECDSA signature generation...\r\n");
596  TRACE_DEBUG(" curve: %s\r\n", privateKey->curve->name);
597  TRACE_DEBUG(" private key:\r\n");
598  TRACE_DEBUG_EC_SCALAR(" ", privateKey->d, qLen);
599  TRACE_DEBUG(" digest:\r\n");
600  TRACE_DEBUG_ARRAY(" ", digest, digestLen);
601 
602 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
603  //Allocate working state
605  //Failed to allocate memory?
606  if(state == NULL)
607  {
608  return ERROR_OUT_OF_MEMORY;
609  }
610 #endif
611 
612  //Initialize working state
613  osMemset(state, 0, sizeof(EcdsaGenerateSignatureState));
614 
615  //Initialize (R, S) integer pair
616  ecScalarSetInt(signature->r, 0, EC_MAX_ORDER_SIZE);
617  ecScalarSetInt(signature->s, 0, EC_MAX_ORDER_SIZE);
618 
619  //Let N be the bit length of q
620  n = privateKey->curve->orderSize;
621  //Compute N = MIN(N, outlen)
622  n = MIN(n, digestLen * 8);
623 
624  //Convert the digest to an integer
625  error = ecScalarImport(state->z, qLen, digest, (n + 7) / 8,
627 
628  //Check status code
629  if(!error)
630  {
631  uint32_t c;
632  uint32_t z2[EC_MAX_ORDER_SIZE];
633 
634  //Keep the leftmost N bits of the hash value
635  if((n % 8) != 0)
636  {
637  ecScalarShiftRight(state->z, state->z, 8 - (n % 8), qLen);
638  }
639 
640  //Debug message
641  TRACE_DEBUG(" z:\r\n");
642  TRACE_DEBUG_EC_SCALAR(" ", state->z, qLen);
643 
644  //z is reduced modulo q. The modular reduction can be implemented with a
645  //simple conditional subtraction
646  c = ecScalarSub(z2, state->z, privateKey->curve->q, qLen);
647  ecScalarSelect(state->z, z2, state->z, c, qLen);
648  }
649 
650  //ECDSA signature generation process
651  while(!error)
652  {
653  //Generate a random number k such as 0 < k < q - 1
654  error = ecScalarRand(privateKey->curve, state->k, prngAlgo, prngContext);
655 
656  //Check status code
657  if(!error)
658  {
659  //Debug message
660  TRACE_DEBUG(" k:\r\n");
661  TRACE_DEBUG_EC_SCALAR(" ", state->k, qLen);
662 
663  //Compute R1 = (x1, y1) = k.G
664  error = ecMulRegular(privateKey->curve, &state->r1, state->k,
665  &privateKey->curve->g);
666  }
667 
668  //Check status code
669  if(!error)
670  {
671  //Convert R1 to affine representation
672  error = ecAffinify(privateKey->curve, &state->r1, &state->r1);
673  }
674 
675  //Check status code
676  if(!error)
677  {
678  //Debug message
679  TRACE_DEBUG(" x1:\r\n");
680  TRACE_DEBUG_EC_SCALAR(" ", state->r1.x, pLen);
681  TRACE_DEBUG(" y1:\r\n");
682  TRACE_DEBUG_EC_SCALAR(" ", state->r1.y, pLen);
683 
684  //Compute r = x1 mod q
685  ecScalarMod(signature->r, state->r1.x, pLen, privateKey->curve->q, qLen);
686  //Compute k ^ -1 mod q
687  ecScalarInvMod(privateKey->curve, state->k, state->k);
688 
689  //Compute s = k ^ -1 * (z + x * r) mod q
690  ecScalarMulMod(privateKey->curve, signature->s, privateKey->d, signature->r);
691  ecScalarAddMod(privateKey->curve, signature->s, signature->s, state->z);
692  ecScalarMulMod(privateKey->curve, signature->s, signature->s, state->k);
693 
694  //The values of r and s shall be checked to determine if r = 0 or s = 0.
695  //If either r = 0 or s = 0, a new value of k shall be generated, and the
696  //signature shall be recalculated (refer to FIPS 186-5, section 6.4.1)
697  if(ecScalarCompInt(signature->r, 0, qLen) != 0 &&
698  ecScalarCompInt(signature->s, 0, qLen) != 0)
699  {
700  break;
701  }
702  }
703  }
704 
705  //Check status code
706  if(!error)
707  {
708  //Save elliptic curve parameters
709  signature->curve = privateKey->curve;
710 
711  //Debug message
712  TRACE_DEBUG(" r:\r\n");
713  TRACE_DEBUG_EC_SCALAR(" ", signature->r, qLen);
714  TRACE_DEBUG(" s:\r\n");
715  TRACE_DEBUG_EC_SCALAR(" ", signature->s, qLen);
716  }
717 
718  //Erase working state
720 
721 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
722  //Release working state
723  cryptoFreeMem(state);
724 #endif
725 
726  //Return status code
727  return error;
728 }
729 
730 
731 /**
732  * @brief Deterministic ECDSA signature generation
733  * @param[in] privateKey Signer's EC private key
734  * @param[in] hashAlgo Underlying hash function
735  * @param[in] digest Digest of the message to be signed
736  * @param[out] signature (R, S) integer pair
737  * @return Error code
738  **/
739 
741  const HashAlgo *hashAlgo, const uint8_t *digest, EcdsaSignature *signature)
742 {
743  error_t error;
744  uint_t n;
745  uint_t pLen;
746  uint_t qLen;
747 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
749 #else
751 #endif
752 
753  //Check parameters
754  if(hashAlgo == NULL || privateKey == NULL || digest == NULL ||
755  signature == NULL)
756  {
758  }
759 
760  //Invalid elliptic curve?
761  if(privateKey->curve == NULL)
762  {
764  }
765 
766  //Get the length of the modulus, in words
767  pLen = (privateKey->curve->fieldSize + 31) / 32;
768  //Get the length of the order, in words
769  qLen = (privateKey->curve->orderSize + 31) / 32;
770 
771  //Debug message
772  TRACE_DEBUG("ECDSA signature generation...\r\n");
773  TRACE_DEBUG(" curve: %s\r\n", privateKey->curve->name);
774  TRACE_DEBUG(" private key:\r\n");
775  TRACE_DEBUG_EC_SCALAR(" ", privateKey->d, qLen);
776  TRACE_DEBUG(" digest:\r\n");
777  TRACE_DEBUG_ARRAY(" ", digest, hashAlgo->digestSize);
778 
779 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
780  //Allocate working state
782  //Failed to allocate memory?
783  if(state == NULL)
784  {
785  return ERROR_OUT_OF_MEMORY;
786  }
787 #endif
788 
789  //Initialize working state
790  osMemset(state, 0, sizeof(EcdsaGenerateSignatureState));
791 
792  //Initialize (R, S) integer pair
793  ecScalarSetInt(signature->r, 0, EC_MAX_ORDER_SIZE);
794  ecScalarSetInt(signature->s, 0, EC_MAX_ORDER_SIZE);
795 
796  //Let N be the bit length of q
797  n = privateKey->curve->orderSize;
798  //Compute N = MIN(N, outlen)
799  n = MIN(n, hashAlgo->digestSize * 8);
800 
801  //Convert the digest to an integer
802  error = ecScalarImport(state->z, qLen, digest, (n + 7) / 8,
804 
805  //Check status code
806  if(!error)
807  {
808  uint32_t c;
809  uint32_t z2[EC_MAX_ORDER_SIZE];
810 
811  //Keep the leftmost N bits of the hash value
812  if((n % 8) != 0)
813  {
814  ecScalarShiftRight(state->z, state->z, 8 - (n % 8), qLen);
815  }
816 
817  //Debug message
818  TRACE_DEBUG(" z:\r\n");
819  TRACE_DEBUG_EC_SCALAR(" ", state->z, qLen);
820 
821  //z is reduced modulo q. The modular reduction can be implemented with a
822  //simple conditional subtraction
823  c = ecScalarSub(z2, state->z, privateKey->curve->q, qLen);
824  ecScalarSelect(state->z, z2, state->z, c, qLen);
825 
826  //Compute the pseudorandom k for signature generation
827  error = ecdsaGenerateK(privateKey->curve, hashAlgo, privateKey->d,
828  state->z, state->k);
829  }
830 
831  //Check status code
832  if(!error)
833  {
834  //Debug message
835  TRACE_DEBUG(" k:\r\n");
836  TRACE_DEBUG_EC_SCALAR(" ", state->k, qLen);
837 
838  //Compute R1 = (x1, y1) = k.G
839  error = ecMulRegular(privateKey->curve, &state->r1, state->k,
840  &privateKey->curve->g);
841  }
842 
843  //Check status code
844  if(!error)
845  {
846  //Convert R1 to affine representation
847  error = ecAffinify(privateKey->curve, &state->r1, &state->r1);
848  }
849 
850  //Check status code
851  if(!error)
852  {
853  //Debug message
854  TRACE_DEBUG(" x1:\r\n");
855  TRACE_DEBUG_EC_SCALAR(" ", state->r1.x, pLen);
856  TRACE_DEBUG(" y1:\r\n");
857  TRACE_DEBUG_EC_SCALAR(" ", state->r1.y, pLen);
858 
859  //Compute r = x1 mod q
860  ecScalarMod(signature->r, state->r1.x, pLen, privateKey->curve->q, qLen);
861  //Compute k ^ -1 mod q
862  ecScalarInvMod(privateKey->curve, state->k, state->k);
863 
864  //Compute s = k ^ -1 * (z + x * r) mod q
865  ecScalarMulMod(privateKey->curve, signature->s, privateKey->d, signature->r);
866  ecScalarAddMod(privateKey->curve, signature->s, signature->s, state->z);
867  ecScalarMulMod(privateKey->curve, signature->s, signature->s, state->k);
868 
869  //Save elliptic curve parameters
870  signature->curve = privateKey->curve;
871 
872  //Debug message
873  TRACE_DEBUG(" r:\r\n");
874  TRACE_DEBUG_EC_SCALAR(" ", signature->r, qLen);
875  TRACE_DEBUG(" s:\r\n");
876  TRACE_DEBUG_EC_SCALAR(" ", signature->s, qLen);
877 
878  //If r = 0 or if s = 0, and k was generated deterministically, then
879  //output failure (refer to FIPS 186-5, section 6.4.1)
880  if(ecScalarCompInt(signature->r, 0, qLen) == 0 ||
881  ecScalarCompInt(signature->s, 0, qLen) == 0)
882  {
883  error = ERROR_FAILURE;
884  }
885  }
886 
887  //Erase working state
889 
890 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
891  //Release working state
892  cryptoFreeMem(state);
893 #endif
894 
895  //Return status code
896  return error;
897 }
898 
899 
900 /**
901  * @brief Generation of pseudorandom k
902  * @param[in] curve Elliptic curve parameters
903  * @param[in] hashAlgo Underlying hash function
904  * @param[in] x ECDSA private key
905  * @param[in] h Digest of the message to be signed
906  * @param[in] k Pseudorandom value k
907  * @return Error code
908  **/
909 
910 error_t ecdsaGenerateK(const EcCurve *curve, const HashAlgo *hashAlgo,
911  const uint32_t *x, const uint32_t *h, uint32_t *k)
912 {
913 #if (HMAC_DRBG_SUPPORT == ENABLED)
914  error_t error;
915  size_t n;
916  uint8_t seed[EC_MAX_ORDER_SIZE * 8];
917  uint8_t t[EC_MAX_ORDER_SIZE * 4];
918  uint32_t q[EC_MAX_ORDER_SIZE];
919 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
920  HmacDrbgContext *hmacDrbgContext;
921 #else
922  HmacDrbgContext hmacDrbgContext[1];
923 #endif
924 
925 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
926  //Allocate HMAC_DRBG context
927  hmacDrbgContext = cryptoAllocMem(sizeof(HmacDrbgContext));
928  //Failed to allocate memory?
929  if(hmacDrbgContext == NULL)
930  {
931  return ERROR_OUT_OF_MEMORY;
932  }
933 #endif
934 
935  //Get the length of the order, in bytes
936  n = (curve->orderSize + 7) / 8;
937 
938  //Instantiate HMAC_DRBG using HMAC parameterized with the same hash function
939  //H as the one used for processing the message that is to be signed (refer
940  //to RFC 6979, section 3.3)
941  error = hmacDrbgInit(hmacDrbgContext, hashAlgo);
942 
943  //Check status code
944  if(!error)
945  {
946  //The private key is used as entropy string
947  error = ecScalarExport(x, (n + 3) / 4, seed, n,
949 
950  //Check status code
951  if(!error)
952  {
953  //The hashed message (truncated and expanded by bits2octets) is used as
954  //nonce. The entropy string and nonce are simply concatenated into the
955  //initial seed
956  error = ecScalarExport(h, (n + 3) / 4, seed + n, n,
958  }
959 
960  //Check status code
961  if(!error)
962  {
963  //For deterministic ECDSA, we want HMAC_DRBG to run with the entropy
964  //string and nonce that we specify, without accessing an actual entropy
965  //source
966  error = hmacDrbgSeed(hmacDrbgContext, seed, n * 2);
967  }
968 
969  //Repeat this step until an acceptable value is obtained
970  while(!error)
971  {
972  //Generate a candidate value for k by requesting qlen bits from
973  //HMAC_DRBG
974  error = hmacDrbgGenerate(hmacDrbgContext, t, n);
975 
976  //Check status code
977  if(!error)
978  {
979  //The resulting value is then converted to an integer value using
980  //the big-endian convention
981  error = ecScalarImport(k, (n + 3) / 4, t, n,
983  }
984 
985  //Check status code
986  if(!error)
987  {
988  //The qlen leftmost bits are kept, and subsequent bits are discarded
989  if(curve->orderSize < (n * 8))
990  {
991  ecScalarShiftRight(k, k, (n * 8) - curve->orderSize, (n + 3) / 4);
992  }
993 
994  //Precompte q-1
995  ecScalarSubInt(q, curve->q, 1, (n + 3) / 4);
996 
997  //If that value of k is within the [1,q-1] range, and is suitable
998  //for ECDSA, then the generation of k is finished
999  if(ecScalarCompInt(k, 0, (n + 3) / 4) > 0 &&
1000  ecScalarComp(k, q, (n + 3) / 4) < 0)
1001  {
1002  //The obtained value of k can be used in ECDSA
1003  break;
1004  }
1005  }
1006  }
1007 
1008  //Release HMAC_DRBG context
1009  hmacDrbgDeinit(hmacDrbgContext);
1010  }
1011 
1012 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1013  //Release previously allocated memory
1014  cryptoFreeMem(hmacDrbgContext);
1015 #endif
1016 
1017  //Erase working state
1018  cryptoZeroizeMem(seed, sizeof(seed));
1019  cryptoZeroizeMem(t, sizeof(t));
1020 
1021  //Return status code
1022  return error;
1023 #else
1024  //HMAC_DRBG is not implemented
1025  return ERROR_NOT_IMPLEMENTED;
1026 #endif
1027 }
1028 
1029 
1030 /**
1031  * @brief ECDSA signature verification
1032  * @param[in] publicKey Signer's EC public key
1033  * @param[in] digest Digest of the message whose signature is to be verified
1034  * @param[in] digestLen Length in octets of the digest
1035  * @param[in] signature (R, S) integer pair
1036  * @return Error code
1037  **/
1038 
1039 __weak_func error_t ecdsaVerifySignature(const EcPublicKey *publicKey,
1040  const uint8_t *digest, size_t digestLen, const EcdsaSignature *signature)
1041 {
1042  error_t error;
1043  uint_t n;
1044  uint_t pLen;
1045  uint_t qLen;
1046 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1048 #else
1049  EcdsaVerifySignatureState state[1];
1050 #endif
1051 
1052  //Check parameters
1053  if(publicKey == NULL || digest == NULL || signature == NULL)
1054  {
1055  return ERROR_INVALID_PARAMETER;
1056  }
1057 
1058  //Invalid elliptic curve?
1059  if(publicKey->curve == NULL)
1060  {
1062  }
1063 
1064  //Get the length of the modulus, in words
1065  pLen = (publicKey->curve->fieldSize + 31) / 32;
1066  //Get the length of the order, in words
1067  qLen = (publicKey->curve->orderSize + 31) / 32;
1068 
1069  //Debug message
1070  TRACE_DEBUG("ECDSA signature verification...\r\n");
1071  TRACE_DEBUG(" curve: %s\r\n", publicKey->curve->name);
1072  TRACE_DEBUG(" public key X:\r\n");
1073  TRACE_DEBUG_EC_SCALAR(" ", publicKey->q.x, pLen);
1074  TRACE_DEBUG(" public key Y:\r\n");
1075  TRACE_DEBUG_EC_SCALAR(" ", publicKey->q.y, pLen);
1076  TRACE_DEBUG(" digest:\r\n");
1077  TRACE_DEBUG_ARRAY(" ", digest, digestLen);
1078  TRACE_DEBUG(" r:\r\n");
1079  TRACE_DEBUG_EC_SCALAR(" ", signature->r, qLen);
1080  TRACE_DEBUG(" s:\r\n");
1081  TRACE_DEBUG_EC_SCALAR(" ", signature->s, qLen);
1082 
1083  //Verify that the public key is on the curve
1084  if(!ecIsPointAffine(publicKey->curve, &publicKey->q))
1085  {
1086  return ERROR_INVALID_SIGNATURE;
1087  }
1088 
1089  //The verifier shall check that 0 < r < q
1090  if(ecScalarCompInt(signature->r, 0, EC_MAX_ORDER_SIZE) <= 0 ||
1091  ecScalarComp(signature->r, publicKey->curve->q, EC_MAX_ORDER_SIZE) >= 0)
1092  {
1093  //If the condition is violated, the signature shall be rejected as invalid
1094  return ERROR_INVALID_SIGNATURE;
1095  }
1096 
1097  //The verifier shall check that 0 < s < q
1098  if(ecScalarCompInt(signature->s, 0, EC_MAX_ORDER_SIZE) <= 0 ||
1099  ecScalarComp(signature->s, publicKey->curve->q, EC_MAX_ORDER_SIZE) >= 0)
1100  {
1101  //If the condition is violated, the signature shall be rejected as invalid
1102  return ERROR_INVALID_SIGNATURE;
1103  }
1104 
1105 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1106  //Allocate working state
1107  state = cryptoAllocMem(sizeof(EcdsaVerifySignatureState));
1108  //Failed to allocate memory?
1109  if(state == NULL)
1110  {
1111  return ERROR_OUT_OF_MEMORY;
1112  }
1113 #endif
1114 
1115  //Initialize working state
1116  osMemset(state, 0, sizeof(EcdsaVerifySignatureState));
1117 
1118  //Let N be the bit length of q
1119  n = publicKey->curve->orderSize;
1120  //Compute N = MIN(N, outlen)
1121  n = MIN(n, digestLen * 8);
1122 
1123  //Convert the digest to an integer
1124  error = ecScalarImport(state->z, qLen, digest, (n + 7) / 8,
1126 
1127  //Check status code
1128  if(!error)
1129  {
1130  //Keep the leftmost N bits of the hash value
1131  if((n % 8) != 0)
1132  {
1133  ecScalarShiftRight(state->z, state->z, 8 - (n % 8), qLen);
1134  }
1135 
1136  //Compute w = s ^ -1 mod q
1137  ecScalarInvMod(publicKey->curve, state->w, signature->s);
1138 
1139  //Compute u1 = z * w mod q
1140  ecScalarMulMod(publicKey->curve, state->u1, state->z, state->w);
1141  //Compute u2 = r * w mod q
1142  ecScalarMulMod(publicKey->curve, state->u2, signature->r, state->w);
1143 
1144  //Convert the public key to projective representation
1145  ecProjectify(publicKey->curve, &state->v1, &publicKey->q);
1146 
1147  //Compute V0 = (x0, y0) = u1.G + u2.Q
1148  error = ecTwinMul(publicKey->curve, &state->v0, state->u1,
1149  &publicKey->curve->g, state->u2, &state->v1);
1150  }
1151 
1152  //Check status code
1153  if(!error)
1154  {
1155  //Convert V0 to affine representation
1156  error = ecAffinify(publicKey->curve, &state->v0, &state->v0);
1157  }
1158 
1159  //Check status code
1160  if(!error)
1161  {
1162  //Debug message
1163  TRACE_DEBUG(" x0:\r\n");
1164  TRACE_DEBUG_EC_SCALAR(" ", state->v0.x, pLen);
1165  TRACE_DEBUG(" y0:\r\n");
1166  TRACE_DEBUG_EC_SCALAR(" ", state->v0.y, pLen);
1167 
1168  //Compute v = x0 mod q
1169  ecScalarMod(state->v, state->v0.x, pLen, publicKey->curve->q, qLen);
1170 
1171  //Debug message
1172  TRACE_DEBUG(" v:\r\n");
1173  TRACE_DEBUG_EC_SCALAR(" ", state->v, qLen);
1174 
1175  //If v = r, then the signature is verified. If v does not equal r, then the
1176  //message or the signature may have been modified
1177  if(ecScalarComp(state->v, signature->r, qLen) == 0)
1178  {
1179  error = NO_ERROR;
1180  }
1181  else
1182  {
1183  error = ERROR_INVALID_SIGNATURE;
1184  }
1185  }
1186 
1187  //Erase working state
1189 
1190 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1191  //Release working state
1192  cryptoFreeMem(state);
1193 #endif
1194 
1195  //Return status code
1196  return error;
1197 }
1198 
1199 #endif
error_t ecdsaImportSignature(EcdsaSignature *signature, const EcCurve *curve, const uint8_t *input, size_t length, EcdsaSignatureFormat format)
Import an ECDSA signature.
Definition: ecdsa.c:108
error_t ecScalarImport(uint32_t *r, uint_t n, const uint8_t *input, size_t length, EcScalarFormat format)
Octet string to integer conversion.
Definition: ec_misc.c:54
uint32_t u1[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:90
__weak_func error_t ecdsaGenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const EcPrivateKey *privateKey, const uint8_t *digest, size_t digestLen, EcdsaSignature *signature)
ECDSA signature generation.
Definition: ecdsa.c:554
ECDSA signature.
Definition: ecdsa.h:63
__weak_func error_t ecAffinify(const EcCurve *curve, EcPoint3 *r, const EcPoint3 *s)
Recover affine representation.
Definition: ec.c:869
error_t ecScalarExport(const uint32_t *a, uint_t n, uint8_t *output, size_t length, EcScalarFormat format)
Integer to octet string conversion.
Definition: ec_misc.c:150
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
#define PrngAlgo
Definition: crypto.h:1161
void hmacDrbgDeinit(HmacDrbgContext *context)
Release PRNG context.
Definition: hmac_drbg.c:489
ECDSA (Elliptic Curve Digital Signature Algorithm)
uint8_t p
Definition: ndp.h:300
uint8_t x
Definition: lldp_ext_med.h:211
const EcCurve * curve
Elliptic curve parameters.
Definition: ecdsa.h:64
const EcCurve * curve
Elliptic curve parameters.
Definition: ec.h:434
uint8_t t
Definition: lldp_ext_med.h:212
#define TRUE
Definition: os_port.h:50
error_t asn1WriteHeader(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written)
Write an ASN.1 tag header.
Definition: asn1.c:545
error_t asn1InsertHeader(Asn1Tag *tag, uint8_t *data, size_t *written)
Insert an ASN.1 tag header.
Definition: asn1.c:687
size_t digestSize
Definition: crypto.h:1272
const uint8_t ECDSA_WITH_SHA3_512_OID[9]
Definition: ecdsa.c:63
#define EC_MAX_ORDER_SIZE
Definition: ec.h:315
uint_t ecScalarGetBitLength(const uint32_t *a, uint_t n)
Get the actual length in bits.
Definition: ec_misc.c:275
const uint8_t ECDSA_WITH_SHA3_256_OID[9]
Definition: ecdsa.c:59
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:1092
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define TRACE_DEBUG_EC_SCALAR(p, a, n)
Definition: debug.h:123
error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 tag from the input stream.
Definition: asn1.c:52
uint32_t y[EC_MAX_MODULUS_SIZE]
y-coordinate
Definition: ec.h:401
const uint8_t ECDSA_WITH_SHA256_OID[8]
Definition: ecdsa.c:51
uint32_t objType
Definition: asn1.h:108
HMAC_DRBG PRNG context.
Definition: hmac_drbg.h:57
__weak_func error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
Twin multiplication.
Definition: ec.c:1542
error_t ecdsaExportSignature(const EcdsaSignature *signature, uint8_t *output, size_t *written, EcdsaSignatureFormat format)
Export an ECDSA signature.
Definition: ecdsa.c:312
const uint8_t ECDSA_WITH_SHAKE256_OID[8]
Definition: ecdsa.c:67
size_t totalLength
Definition: asn1.h:111
@ ERROR_INVALID_ELLIPTIC_CURVE
Definition: error.h:134
void ecdsaFreeSignature(EcdsaSignature *signature)
Release an ECDSA signature.
Definition: ecdsa.c:91
size_t length
Definition: asn1.h:109
#define FALSE
Definition: os_port.h:46
uint8_t h
Definition: ndp.h:302
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
uint32_t ecScalarSub(uint32_t *r, const uint32_t *a, const uint32_t *b, uint_t n)
Subtraction of two integers.
Definition: ec_misc.c:711
#define ASN1_CLASS_UNIVERSAL
Definition: asn1.h:52
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void ecScalarSetInt(uint32_t *a, uint32_t b, uint_t n)
Set integer value.
Definition: ec_misc.c:509
ASN.1 tag.
Definition: asn1.h:105
HMAC_DRBG pseudorandom number generator.
#define ASN1_INC_POINTER(p, n)
Definition: asn1.h:58
Helper routines for ECC.
void ecdsaInitSignature(EcdsaSignature *signature)
Initialize an ECDSA signature.
Definition: ecdsa.c:75
const uint8_t ECDSA_WITH_SHAKE128_OID[8]
Definition: ecdsa.c:65
uint32_t ecScalarSubInt(uint32_t *r, const uint32_t *a, uint32_t b, uint_t n)
Subtraction of two integers.
Definition: ec_misc.c:740
EcdsaSignatureFormat
ECDSA signature format.
Definition: ecdsa.h:50
uint32_t r[EC_MAX_ORDER_SIZE]
Integer R.
Definition: ecdsa.h:65
@ ECDSA_SIGNATURE_FORMAT_RAW
Definition: ecdsa.h:52
Working state (ECDSA signature generation)
Definition: ecdsa.h:75
const uint8_t ECDSA_WITH_SHA384_OID[8]
Definition: ecdsa.c:53
@ ERROR_INVALID_LENGTH
Definition: error.h:111
uint32_t z[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:89
General definitions for cryptographic library.
void ecScalarInvMod(const EcCurve *curve, uint32_t *r, const uint32_t *a)
Modular inversion.
Definition: ec_misc.c:1185
error_t hmacDrbgInit(HmacDrbgContext *context, const HashAlgo *hashAlgo)
Initialize PRNG context.
Definition: hmac_drbg.c:63
EC private key.
Definition: ec.h:433
uint_t objClass
Definition: asn1.h:107
uint8_t length
Definition: tcp.h:375
const uint8_t ECDSA_WITH_SHA3_224_OID[9]
Definition: ecdsa.c:57
#define MIN(a, b)
Definition: os_port.h:63
void ecScalarSelect(uint32_t *r, const uint32_t *a, const uint32_t *b, uint32_t c, uint_t n)
Select an integer.
Definition: ec_misc.c:580
const uint8_t ECDSA_WITH_SHA1_OID[7]
Definition: ecdsa.c:47
const uint8_t ECDSA_WITH_SHA224_OID[8]
Definition: ecdsa.c:49
const uint8_t ECDSA_WITH_SHA512_OID[8]
Definition: ecdsa.c:55
uint32_t w[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:88
error_t ecdsaGenerateDeterministicSignature(const EcPrivateKey *privateKey, const HashAlgo *hashAlgo, const uint8_t *digest, EcdsaSignature *signature)
Deterministic ECDSA signature generation.
Definition: ecdsa.c:740
EC public key.
Definition: ec.h:422
@ ASN1_TYPE_INTEGER
Definition: asn1.h:73
#define TRACE_DEBUG(...)
Definition: debug.h:119
Secure memory operations.
__weak_func bool_t ecIsPointAffine(const EcCurve *curve, const EcPoint *s)
Check whether the affine point S is on the curve.
Definition: ec.c:918
error_t ecCheckPrivateKey(const EcPrivateKey *privateKey)
Check the range of an EC private key.
Definition: ec.c:680
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
uint32_t z[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:77
@ ECDSA_SIGNATURE_FORMAT_ASN1
Definition: ecdsa.h:51
uint32_t d[EC_MAX_ORDER_SIZE]
Private key.
Definition: ec.h:435
void ecScalarAddMod(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular addition.
Definition: ec_misc.c:1066
uint32_t k[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:76
int_t ecScalarCompInt(const uint32_t *a, uint32_t b, uint_t n)
Compare integers.
Definition: ec_misc.c:378
const uint8_t ECDSA_WITH_SHA3_384_OID[9]
Definition: ecdsa.c:61
uint8_t n
@ EC_SCALAR_FORMAT_BIG_ENDIAN
Definition: ec_misc.h:51
@ ECDSA_SIGNATURE_FORMAT_RAW_R
Definition: ecdsa.h:53
#define cryptoFreeMem(p)
Definition: crypto.h:987
error_t hmacDrbgGenerate(HmacDrbgContext *context, uint8_t *output, size_t length)
Generate pseudorandom data.
Definition: hmac_drbg.c:334
__weak_func void cryptoZeroizeMem(void *p, size_t n)
Securely erase a memory block.
Definition: crypto_mem.c:45
__weak_func error_t ecdsaVerifySignature(const EcPublicKey *publicKey, const uint8_t *digest, size_t digestLen, const EcdsaSignature *signature)
ECDSA signature verification.
Definition: ecdsa.c:1039
void ecProjectify(const EcCurve *curve, EcPoint3 *r, const EcPoint *s)
Compute projective representation.
Definition: ec.c:840
__weak_func error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
Definition: ec.c:1434
EcPoint q
Public key.
Definition: ec.h:424
uint32_t s[EC_MAX_ORDER_SIZE]
Integer S.
Definition: ecdsa.h:66
bool_t constructed
Definition: asn1.h:106
void ecScalarShiftRight(uint32_t *r, const uint32_t *a, uint_t k, uint_t n)
Right shift operation.
Definition: ec_misc.c:944
error_t ecScalarRand(const EcCurve *curve, uint32_t *r, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: ec_misc.c:607
#define cryptoAllocMem(size)
Definition: crypto.h:982
@ ERROR_INVALID_SYNTAX
Definition: error.h:68
error_t ecdsaGenerateK(const EcCurve *curve, const HashAlgo *hashAlgo, const uint32_t *x, const uint32_t *h, uint32_t *k)
Generation of pseudorandom k.
Definition: ecdsa.c:910
@ ASN1_TYPE_SEQUENCE
Definition: asn1.h:83
uint32_t u2[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:91
Common interface for hash algorithms.
Definition: crypto.h:1266
#define EcCurve
Definition: ec.h:347
void ecScalarMod(uint32_t *r, const uint32_t *a, uint_t m, const uint32_t *p, uint_t n)
Modulo operation.
Definition: ec_misc.c:1013
int_t ecScalarComp(const uint32_t *a, const uint32_t *b, uint_t n)
Compare integers.
Definition: ec_misc.c:341
unsigned int uint_t
Definition: compiler_port.h:57
uint32_t x[EC_MAX_MODULUS_SIZE]
x-coordinate
Definition: ec.h:400
@ ECDSA_SIGNATURE_FORMAT_RAW_S
Definition: ecdsa.h:54
#define osMemset(p, value, length)
Definition: os_port.h:141
error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 sequence from the input stream.
Definition: asn1.c:185
Working state (ECDSA signature verification)
Definition: ecdsa.h:87
uint32_t v[EC_MAX_ORDER_SIZE]
Definition: ecdsa.h:92
@ ERROR_INVALID_SIGNATURE
Definition: error.h:229
const EcCurve * curve
Elliptic curve parameters.
Definition: ec.h:423
__weak_func void ecScalarMulMod(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
Definition: ec_misc.c:1117
uint32_t x[EC_MAX_MODULUS_SIZE]
x-coordinate
Definition: ec.h:411
const uint8_t * value
Definition: asn1.h:110
error_t hmacDrbgSeed(HmacDrbgContext *context, const uint8_t *seed, size_t length)
Seed the PRNG state.
Definition: hmac_drbg.c:112
error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass, uint_t objType)
Enforce the type of a specified tag.
Definition: asn1.c:1027
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.
ASN.1 (Abstract Syntax Notation One)
uint32_t y[EC_MAX_MODULUS_SIZE]
y-coordinate
Definition: ec.h:412