rsa.c
Go to the documentation of this file.
1 /**
2  * @file rsa.c
3  * @brief RSA public-key cryptography standard
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  * @section Description
28  *
29  * RSA is an algorithm for public-key cryptography which is suitable for signing
30  * as well as encryption. Refer to the following RFCs for complete details:
31  * - RFC 2313: PKCS #1: RSA Encryption Version 1.5
32  * - RFC 3447: PKCS #1: RSA Cryptography Specifications Version 2.1
33  * - RFC 8017: PKCS #1: RSA Cryptography Specifications Version 2.2
34  *
35  * @author Oryx Embedded SARL (www.oryx-embedded.com)
36  * @version 2.6.8
37  **/
38 
39 //Switch to the appropriate trace level
40 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
41 
42 //Dependencies
43 #include "core/crypto.h"
44 #include "pkc/rsa.h"
45 #include "pkc/rsa_misc.h"
46 #include "debug.h"
47 
48 //Check crypto library configuration
49 #if (RSA_SUPPORT == ENABLED)
50 
51 //PKCS #1 OID (1.2.840.113549.1.1)
52 const uint8_t PKCS1_OID[8] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01};
53 //RSA encryption OID (1.2.840.113549.1.1.1)
54 const uint8_t RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01};
55 
56 //MD2 with RSA encryption OID (1.2.840.113549.1.1.2)
57 const uint8_t MD2_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x02};
58 //MD5 with RSA encryption OID (1.2.840.113549.1.1.4)
59 const uint8_t MD5_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04};
60 //SHA-1 with RSA encryption OID (1.2.840.113549.1.1.5)
61 const uint8_t SHA1_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05};
62 //SHA-224 with RSA encryption OID (1.2.840.113549.1.1.14)
63 const uint8_t SHA224_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0E};
64 //SHA-256 with RSA encryption OID (1.2.840.113549.1.1.11)
65 const uint8_t SHA256_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B};
66 //SHA-384 with RSA encryption OID (1.2.840.113549.1.1.12)
67 const uint8_t SHA384_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C};
68 //SHA-512 with RSA encryption OID (1.2.840.113549.1.1.13)
69 const uint8_t SHA512_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0D};
70 //SHA-512/224 with RSA encryption OID (1.2.840.113549.1.1.15)
71 const uint8_t SHA512_224_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0F};
72 //SHA-512/256 with RSA encryption OID (1.2.840.113549.1.1.16)
73 const uint8_t SHA512_256_WITH_RSA_ENCRYPTION_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x10};
74 
75 //RSASSA-PKCS1-v1_5 signature with SHA-3-224 OID (2.16.840.1.101.3.4.3.13)
76 const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_224_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0D};
77 //RSASSA-PKCS1-v1_5 signature with SHA-3-256 OID (2.16.840.1.101.3.4.3.14)
78 const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_256_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0E};
79 //RSASSA-PKCS1-v1_5 signature with SHA-3-384 OID (2.16.840.1.101.3.4.3.15)
80 const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_384_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x0F};
81 //RSASSA-PKCS1-v1_5 signature with SHA-3-512 OID (2.16.840.1.101.3.4.3.16)
82 const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_512_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x10};
83 
84 //RSASSA-PSS OID (1.2.840.113549.1.1.10)
85 const uint8_t RSASSA_PSS_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A};
86 //RSASSA-PSS-SHAKE128 OID (1.3.6.1.5.5.7.6.30)
87 const uint8_t RSASSA_PSS_SHAKE128_OID[8] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x06, 0x1E};
88 //RSASSA-PSS-SHAKE256 OID (1.3.6.1.5.5.7.6.31)
89 const uint8_t RSASSA_PSS_SHAKE256_OID[8] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x06, 0x1F};
90 
91 //MGF1 OID (1.2.840.113549.1.1.8)
92 const uint8_t MGF1_OID[9] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08};
93 
94 
95 /**
96  * @brief Initialize an RSA public key
97  * @param[in] key Pointer to the RSA public key to initialize
98  **/
99 
101 {
102  //Initialize multiple precision integers
103  mpiInit(&key->n);
104  mpiInit(&key->e);
105 }
106 
107 
108 /**
109  * @brief Release an RSA public key
110  * @param[in] key Pointer to the RSA public key to free
111  **/
112 
114 {
115  //Free multiple precision integers
116  mpiFree(&key->n);
117  mpiFree(&key->e);
118 }
119 
120 
121 /**
122  * @brief Initialize an RSA private key
123  * @param[in] key Pointer to the RSA private key to initialize
124  **/
125 
127 {
128  //Initialize multiple precision integers
129  mpiInit(&key->n);
130  mpiInit(&key->e);
131  mpiInit(&key->d);
132  mpiInit(&key->p);
133  mpiInit(&key->q);
134  mpiInit(&key->dp);
135  mpiInit(&key->dq);
136  mpiInit(&key->qinv);
137 
138  //Initialize private key slot
139  key->slot = -1;
140 }
141 
142 
143 /**
144  * @brief Release an RSA private key
145  * @param[in] key Pointer to the RSA private key to free
146  **/
147 
149 {
150  //Free multiple precision integers
151  mpiFree(&key->n);
152  mpiFree(&key->e);
153  mpiFree(&key->d);
154  mpiFree(&key->p);
155  mpiFree(&key->q);
156  mpiFree(&key->dp);
157  mpiFree(&key->dq);
158  mpiFree(&key->qinv);
159 }
160 
161 
162 /**
163  * @brief RSA key pair generation
164  * @param[in] prngAlgo PRNG algorithm
165  * @param[in] prngContext Pointer to the PRNG context
166  * @param[in] k Required bit length of the modulus n
167  * @param[in] e Public exponent (3, 5, 17, 257 or 65537)
168  * @param[out] privateKey RSA private key
169  * @param[out] publicKey RSA public key
170  * @return Error code
171  **/
172 
173 error_t rsaGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext,
174  size_t k, uint_t e, RsaPrivateKey *privateKey, RsaPublicKey *publicKey)
175 {
176  error_t error;
177 
178  //Generate a private key
179  error = rsaGeneratePrivateKey(prngAlgo, prngContext, k, e, privateKey);
180 
181  //Check status code
182  if(!error)
183  {
184  //Derive the public key from the private key
185  error = rsaGeneratePublicKey(privateKey, publicKey);
186  }
187 
188  //Return status code
189  return error;
190 }
191 
192 
193 /**
194  * @brief RSA private key generation
195  * @param[in] prngAlgo PRNG algorithm
196  * @param[in] prngContext Pointer to the PRNG context
197  * @param[in] k Required bit length of the modulus n
198  * @param[in] e Public exponent (3, 5, 17, 257 or 65537)
199  * @param[out] privateKey RSA private key
200  * @return Error code
201  **/
202 
203 __weak_func error_t rsaGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext,
204  size_t k, uint_t e, RsaPrivateKey *privateKey)
205 {
206  error_t error;
207  Mpi t1;
208  Mpi t2;
209  Mpi phy;
210 
211  //Check parameters
212  if(prngAlgo == NULL || prngContext == NULL || privateKey == NULL)
213  {
215  }
216 
217  //Check the length of the modulus
218  if(k < 8)
219  {
221  }
222 
223  //Check the value of the public exponent
224  if(e != 3 && e != 5 && e != 17 && e != 257 && e != 65537)
225  {
227  }
228 
229  //Initialize multiple precision integers
230  mpiInit(&t1);
231  mpiInit(&t2);
232  mpiInit(&phy);
233 
234  //Save public exponent
235  MPI_CHECK(mpiSetValue(&privateKey->e, e));
236 
237  //Generate a large random prime p
238  do
239  {
240  do
241  {
242  //Generate a random number of bit length k/2
243  MPI_CHECK(mpiRand(&privateKey->p, k / 2, prngAlgo, prngContext));
244  //Set the low bit (this ensures the number is odd)
245  MPI_CHECK(mpiSetBitValue(&privateKey->p, 0, 1));
246  //Set the two highest bits (this ensures that the high bit of n is also set)
247  MPI_CHECK(mpiSetBitValue(&privateKey->p, k / 2 - 1, 1));
248  MPI_CHECK(mpiSetBitValue(&privateKey->p, k / 2 - 2, 1));
249 
250  //Test whether p is a probable prime
251  error = mpiCheckProbablePrime(&privateKey->p);
252 
253  //Repeat until an acceptable value is found
254  } while(error == ERROR_INVALID_VALUE);
255 
256  //Check status code
257  MPI_CHECK(error);
258 
259  //Compute p mod e
260  MPI_CHECK(mpiMod(&t1, &privateKey->p, &privateKey->e));
261 
262  //Repeat as long as p mod e = 1
263  } while(mpiCompInt(&t1, 1) == 0);
264 
265  //Generate a large random prime q
266  do
267  {
268  do
269  {
270  //Generate random number of bit length k - k/2
271  MPI_CHECK(mpiRand(&privateKey->q, k - (k / 2), prngAlgo, prngContext));
272  //Set the low bit (this ensures the number is odd)
273  MPI_CHECK(mpiSetBitValue(&privateKey->q, 0, 1));
274  //Set the two highest bits (this ensures that the high bit of n is also set)
275  MPI_CHECK(mpiSetBitValue(&privateKey->q, k - (k / 2) - 1, 1));
276  MPI_CHECK(mpiSetBitValue(&privateKey->q, k - (k / 2) - 2, 1));
277 
278  //Test whether q is a probable prime
279  error = mpiCheckProbablePrime(&privateKey->q);
280 
281  //Repeat until an acceptable value is found
282  } while(error == ERROR_INVALID_VALUE);
283 
284  //Check status code
285  MPI_CHECK(error);
286 
287  //Compute q mod e
288  MPI_CHECK(mpiMod(&t2, &privateKey->q, &privateKey->e));
289 
290  //Repeat as long as p mod e = 1
291  } while(mpiCompInt(&t2, 1) == 0);
292 
293  //Make sure p an q are distinct
294  if(mpiComp(&privateKey->p, &privateKey->q) == 0)
295  {
297  }
298 
299  //If p < q, then swap p and q (this only matters if the CRT form of
300  //the private key is used)
301  if(mpiComp(&privateKey->p, &privateKey->q) < 0)
302  {
303  //Swap primes
304  mpiCopy(&t1, &privateKey->p);
305  mpiCopy(&privateKey->p, &privateKey->q);
306  mpiCopy(&privateKey->q, &t1);
307  }
308 
309  //Compute the modulus n = pq
310  MPI_CHECK(mpiMul(&privateKey->n, &privateKey->p, &privateKey->q));
311 
312  //Compute phy = (p-1)(q-1)
313  MPI_CHECK(mpiSubInt(&t1, &privateKey->p, 1));
314  MPI_CHECK(mpiSubInt(&t2, &privateKey->q, 1));
315  MPI_CHECK(mpiMul(&phy, &t1, &t2));
316 
317  //Compute d = e^-1 mod phy
318  MPI_CHECK(mpiInvMod(&privateKey->d, &privateKey->e, &phy));
319  //Compute dP = d mod (p-1)
320  MPI_CHECK(mpiMod(&privateKey->dp, &privateKey->d, &t1));
321  //Compute dQ = d mod (q-1)
322  MPI_CHECK(mpiMod(&privateKey->dq, &privateKey->d, &t2));
323  //Compute qInv = q^-1 mod p
324  MPI_CHECK(mpiInvMod(&privateKey->qinv, &privateKey->q, &privateKey->p));
325 
326  //Debug message
327  TRACE_DEBUG("RSA private key:\r\n");
328  TRACE_DEBUG(" Modulus:\r\n");
329  TRACE_DEBUG_MPI(" ", &privateKey->n);
330  TRACE_DEBUG(" Public exponent:\r\n");
331  TRACE_DEBUG_MPI(" ", &privateKey->e);
332  TRACE_DEBUG(" Private exponent:\r\n");
333  TRACE_DEBUG_MPI(" ", &privateKey->d);
334  TRACE_DEBUG(" Prime 1:\r\n");
335  TRACE_DEBUG_MPI(" ", &privateKey->p);
336  TRACE_DEBUG(" Prime 2:\r\n");
337  TRACE_DEBUG_MPI(" ", &privateKey->q);
338  TRACE_DEBUG(" Prime exponent 1:\r\n");
339  TRACE_DEBUG_MPI(" ", &privateKey->dp);
340  TRACE_DEBUG(" Prime exponent 2:\r\n");
341  TRACE_DEBUG_MPI(" ", &privateKey->dq);
342  TRACE_DEBUG(" Coefficient:\r\n");
343  TRACE_DEBUG_MPI(" ", &privateKey->qinv);
344 
345 end:
346  //Release multiple precision integers
347  mpiFree(&t1);
348  mpiFree(&t2);
349  mpiFree(&phy);
350 
351  //Any error to report?
352  if(error)
353  {
354  //Release RSA private key
355  rsaFreePrivateKey(privateKey);
356  }
357 
358  //Return status code
359  return error;
360 }
361 
362 
363 /**
364  * @brief Derive the public key from an RSA private key
365  * @param[in] privateKey RSA private key
366  * @param[out] publicKey RSA public key
367  * @return Error code
368  **/
369 
371  RsaPublicKey *publicKey)
372 {
373  error_t error;
374 
375  //Check parameters
376  if(privateKey == NULL || publicKey == NULL)
377  {
379  }
380 
381  //The public key is (n, e)
382  MPI_CHECK(mpiCopy(&publicKey->n, &privateKey->n));
383  MPI_CHECK(mpiCopy(&publicKey->e, &privateKey->e));
384 
385  //Debug message
386  TRACE_DEBUG("RSA public key:\r\n");
387  TRACE_DEBUG(" Modulus:\r\n");
388  TRACE_DEBUG_MPI(" ", &publicKey->n);
389  TRACE_DEBUG(" Public exponent:\r\n");
390  TRACE_DEBUG_MPI(" ", &publicKey->e);
391 
392 end:
393  //Any error to report?
394  if(error)
395  {
396  //Release RSA public key
397  rsaFreePublicKey(publicKey);
398  }
399 
400  //Return status code
401  return error;
402 }
403 
404 
405 /**
406  * @brief RSAES-PKCS1-v1_5 encryption operation
407  * @param[in] prngAlgo PRNG algorithm
408  * @param[in] prngContext Pointer to the PRNG context
409  * @param[in] key Recipient's RSA public key
410  * @param[in] message Message to be encrypted
411  * @param[in] messageLen Length of the message to be encrypted
412  * @param[out] ciphertext Ciphertext resulting from the encryption operation
413  * @param[out] ciphertextLen Length of the resulting ciphertext
414  * @return Error code
415  **/
416 
417 __weak_func error_t rsaesPkcs1v15Encrypt(const PrngAlgo *prngAlgo,
418  void *prngContext, const RsaPublicKey *key, const uint8_t *message,
419  size_t messageLen, uint8_t *ciphertext, size_t *ciphertextLen)
420 {
421  error_t error;
422  uint_t k;
423  uint8_t *em;
424  Mpi m;
425  Mpi c;
426 
427  //Check parameters
428  if(prngAlgo == NULL || prngContext == NULL || key == NULL ||
429  message == NULL || ciphertext == NULL || ciphertextLen == NULL)
430  {
432  }
433 
434  //Debug message
435  TRACE_DEBUG("RSAES-PKCS1-v1_5 encryption...\r\n");
436  TRACE_DEBUG(" Modulus:\r\n");
437  TRACE_DEBUG_MPI(" ", &key->n);
438  TRACE_DEBUG(" Public exponent:\r\n");
439  TRACE_DEBUG_MPI(" ", &key->e);
440  TRACE_DEBUG(" Message:\r\n");
441  TRACE_DEBUG_ARRAY(" ", message, messageLen);
442 
443  //Initialize multiple-precision integers
444  mpiInit(&m);
445  mpiInit(&c);
446 
447  //Get the length in octets of the modulus n
448  k = mpiGetByteLength(&key->n);
449 
450  //Point to the buffer where the encoded message EM will be formatted
451  em = ciphertext;
452 
453  //EME-PKCS1-v1_5 encoding
454  error = emePkcs1v15Encode(prngAlgo, prngContext, message, messageLen, em, k);
455  //Any error to report?
456  if(error)
457  {
458  return error;
459  }
460 
461  //Debug message
462  TRACE_DEBUG(" Encoded message:\r\n");
463  TRACE_DEBUG_ARRAY(" ", em, k);
464 
465  //Start of exception handling block
466  do
467  {
468  //Convert the encoded message EM to an integer message representative m
469  error = mpiImport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
470  //Conversion failed?
471  if(error)
472  {
473  break;
474  }
475 
476  //Apply the RSAEP encryption primitive
477  error = rsaep(key, &m, &c);
478  //Any error to report?
479  if(error)
480  {
481  break;
482  }
483 
484  //Convert the ciphertext representative c to a ciphertext of length k octets
485  error = mpiExport(&c, ciphertext, k, MPI_FORMAT_BIG_ENDIAN);
486  //Conversion failed?
487  if(error)
488  {
489  break;
490  }
491 
492  //Length of the resulting ciphertext
493  *ciphertextLen = k;
494 
495  //Debug message
496  TRACE_DEBUG(" Ciphertext:\r\n");
497  TRACE_DEBUG_ARRAY(" ", ciphertext, *ciphertextLen);
498 
499  //End of exception handling block
500  } while(0);
501 
502  //Free previously allocated memory
503  mpiFree(&m);
504  mpiFree(&c);
505 
506  //Return status code
507  return error;
508 }
509 
510 
511 /**
512  * @brief RSAES-PKCS1-v1_5 decryption operation
513  * @param[in] key Recipient's RSA private key
514  * @param[in] ciphertext Ciphertext to be decrypted
515  * @param[in] ciphertextLen Length of the ciphertext to be decrypted
516  * @param[out] message Output buffer where to store the decrypted message
517  * @param[in] messageSize Size of the output buffer
518  * @param[out] messageLen Length of the decrypted message
519  * @return Error code
520  **/
521 
523  const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message,
524  size_t messageSize, size_t *messageLen)
525 {
526  error_t error;
527  uint_t k;
528  size_t i;
529  size_t j;
530  size_t n;
531  uint8_t b;
532  uint32_t a;
533  uint32_t bad;
534  Mpi c;
535  Mpi m;
536  Mpi t;
537 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
538  uint8_t *em;
539 #else
540  uint8_t em[RSA_MAX_MODULUS_SIZE / 8];
541 #endif
542 
543  //Check parameters
544  if(key == NULL || ciphertext == NULL || message == NULL ||
545  messageSize == 0 || messageLen == NULL)
546  {
548  }
549 
550  //Debug message
551  TRACE_DEBUG("RSAES-PKCS1-v1_5 decryption...\r\n");
552  TRACE_DEBUG(" Modulus:\r\n");
553  TRACE_DEBUG_MPI(" ", &key->n);
554  TRACE_DEBUG(" Public exponent:\r\n");
555  TRACE_DEBUG_MPI(" ", &key->e);
556  TRACE_DEBUG(" Private exponent:\r\n");
557  TRACE_DEBUG_MPI(" ", &key->d);
558  TRACE_DEBUG(" Prime 1:\r\n");
559  TRACE_DEBUG_MPI(" ", &key->p);
560  TRACE_DEBUG(" Prime 2:\r\n");
561  TRACE_DEBUG_MPI(" ", &key->q);
562  TRACE_DEBUG(" Prime exponent 1:\r\n");
563  TRACE_DEBUG_MPI(" ", &key->dp);
564  TRACE_DEBUG(" Prime exponent 2:\r\n");
565  TRACE_DEBUG_MPI(" ", &key->dq);
566  TRACE_DEBUG(" Coefficient:\r\n");
567  TRACE_DEBUG_MPI(" ", &key->qinv);
568  TRACE_DEBUG(" Ciphertext:\r\n");
569  TRACE_DEBUG_ARRAY(" ", ciphertext, ciphertextLen);
570 
571  //Initialize multiple-precision integers
572  mpiInit(&c);
573  mpiInit(&m);
574  mpiInit(&t);
575 
576  //Get the length in octets of the modulus n
577  k = mpiGetByteLength(&key->n);
578 
579  //Check the length of the ciphertext
580  if(ciphertextLen != k || ciphertextLen < 11)
581  {
582  return ERROR_INVALID_LENGTH;
583  }
584 
585 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
586  //Allocate a buffer to store the encoded message EM
587  em = cryptoAllocMem(k);
588  //Failed to allocate memory?
589  if(em == NULL)
590  {
591  return ERROR_OUT_OF_MEMORY;
592  }
593 #else
594  //Check the length of the modulus
595  if(k > sizeof(em))
596  {
597  return ERROR_BUFFER_OVERFLOW;
598  }
599 #endif
600 
601  //Start of exception handling block
602  do
603  {
604  //Convert the ciphertext to an integer ciphertext representative c
605  error = mpiImport(&c, ciphertext, ciphertextLen, MPI_FORMAT_BIG_ENDIAN);
606  //Conversion failed?
607  if(error)
608  {
609  break;
610  }
611 
612  //Apply the RSADP decryption primitive
613  error = rsadp(key, &c, &m);
614  //Any error to report?
615  if(error)
616  {
617  break;
618  }
619 
620  //When unprotected, RSA-CRT is vulnerable to the Bellcore attack
621  if(mpiGetLength(&key->n) > 0 && mpiGetLength(&key->e) > 0 &&
622  mpiGetLength(&key->p) > 0 && mpiGetLength(&key->q) > 0 &&
623  mpiGetLength(&key->dp) > 0 && mpiGetLength(&key->dq) > 0 &&
624  mpiGetLength(&key->qinv) > 0)
625  {
626  RsaPublicKey publicKey;
627 
628  //The pair of numbers (n, e) form the RSA public key
629  publicKey.n = key->n;
630  publicKey.e = key->e;
631 
632  //Apply the RSAEP encryption primitive
633  error = rsaep(&publicKey, &m, &t);
634  //Any error to report?
635  if(error)
636  {
637  break;
638  }
639 
640  //Verify the result in order to protect against RSA-CRT key leak
641  if(mpiComp(&t, &c) != 0)
642  {
643  //A decryption fault has been detected
644  error = ERROR_FAILURE;
645  break;
646  }
647  }
648 
649  //Convert the message representative m to an encoded message EM of
650  //length k octets
651  error = mpiExport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
652  //Conversion failed?
653  if(error)
654  {
655  break;
656  }
657 
658  //Debug message
659  TRACE_DEBUG(" Encoded message:\r\n");
660  TRACE_DEBUG_ARRAY(" ", em, k);
661 
662  //EME-PKCS1-v1_5 decoding
663  bad = emePkcs1v15Decode(em, k, &n);
664 
665  //Check whether the output buffer is large enough to hold the decrypted
666  //message
667  bad |= CRYPTO_TEST_LT_32(messageSize, n);
668 
669  //Do not release any part of the message when the decryption fails
670  n = CRYPTO_SELECT_32(n, 0, bad);
671 
672  //Copy the decrypted message, byte per byte
673  for(i = 0; i < messageSize; i++)
674  {
675  //Read the whole encoded message EM
676  for(b = 0, j = 0; j < k; j++)
677  {
678  //Constant time implementation
679  a = CRYPTO_TEST_EQ_32(j, k - n + i);
680  b = CRYPTO_SELECT_8(b, em[j], a);
681  }
682 
683  //Save the value of the current byte
684  message[i] = b;
685  }
686 
687  //Return the length of the decrypted message
688  *messageLen = n;
689 
690  //Check whether the decryption operation is successful
691  error = (bad == 0) ? NO_ERROR : ERROR_DECRYPTION_FAILED;
692 
693  //Debug message
694  TRACE_DEBUG(" Message:\r\n");
695  TRACE_DEBUG_ARRAY(" ", message, *messageLen);
696 
697  //End of exception handling block
698  } while(0);
699 
700 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
701  //Release the encoded message
702  cryptoFreeMem(em);
703 #endif
704 
705  //Release multiple precision integers
706  mpiFree(&c);
707  mpiFree(&m);
708  mpiFree(&t);
709 
710  //Return status code
711  return error;
712 }
713 
714 
715 /**
716  * @brief RSAES-OAEP encryption operation
717  * @param[in] prngAlgo PRNG algorithm
718  * @param[in] prngContext Pointer to the PRNG context
719  * @param[in] key Recipient's RSA public key
720  * @param[in] hash Hash function
721  * @param[in] mgfHash MGF hash function
722  * @param[in] label Optional label to be associated with the message
723  * @param[in] labelLen Length of the label, in bytes
724  * @param[in] message Message to be encrypted
725  * @param[in] messageLen Length of the message to be encrypted
726  * @param[out] ciphertext Ciphertext resulting from the encryption operation
727  * @param[out] ciphertextLen Length of the resulting ciphertext
728  * @return Error code
729  **/
730 
731 __weak_func error_t rsaesOaepEncrypt(const PrngAlgo *prngAlgo,
732  void *prngContext, const RsaPublicKey *key, const HashAlgo *hash,
733  const HashAlgo *mgfHash, const char_t *label, size_t labelLen,
734  const uint8_t *message, size_t messageLen, uint8_t *ciphertext,
735  size_t *ciphertextLen)
736 {
737  error_t error;
738  uint_t k;
739  uint8_t *em;
740  Mpi m;
741  Mpi c;
742 
743  //Check parameters
744  if(prngAlgo == NULL || prngContext == NULL || key == NULL || hash == NULL ||
745  mgfHash == NULL || message == NULL || ciphertext == NULL ||
746  ciphertextLen == NULL)
747  {
749  }
750 
751  //Debug message
752  TRACE_DEBUG("RSAES-OAEP encryption...\r\n");
753  TRACE_DEBUG(" Modulus:\r\n");
754  TRACE_DEBUG_MPI(" ", &key->n);
755  TRACE_DEBUG(" Public exponent:\r\n");
756  TRACE_DEBUG_MPI(" ", &key->e);
757  TRACE_DEBUG(" Message:\r\n");
758  TRACE_DEBUG_ARRAY(" ", message, messageLen);
759 
760  //Initialize multiple-precision integers
761  mpiInit(&m);
762  mpiInit(&c);
763 
764  //Get the length in octets of the modulus n
765  k = mpiGetByteLength(&key->n);
766 
767  //Make sure the modulus is valid
768  if(k == 0)
769  {
771  }
772 
773  //Point to the buffer where the encoded message EM will be formatted
774  em = ciphertext;
775 
776  //EME-OAEP encoding
777  error = emeOaepEncode(prngAlgo, prngContext, hash, mgfHash, label, labelLen,
778  message, messageLen, em, k);
779  //Any error to report?
780  if(error)
781  {
782  return error;
783  }
784 
785  //Debug message
786  TRACE_DEBUG(" Encoded message:\r\n");
787  TRACE_DEBUG_ARRAY(" ", em, k);
788 
789  //Start of exception handling block
790  do
791  {
792  //Convert the encoded message EM to an integer message representative m
793  error = mpiImport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
794  //Conversion failed?
795  if(error)
796  {
797  break;
798  }
799 
800  //Apply the RSAEP encryption primitive
801  error = rsaep(key, &m, &c);
802  //Any error to report?
803  if(error)
804  {
805  break;
806  }
807 
808  //Convert the ciphertext representative c to a ciphertext of length k octets
809  error = mpiExport(&c, ciphertext, k, MPI_FORMAT_BIG_ENDIAN);
810  //Conversion failed?
811  if(error)
812  {
813  break;
814  }
815 
816  //Length of the resulting ciphertext
817  *ciphertextLen = k;
818 
819  //Debug message
820  TRACE_DEBUG(" Ciphertext:\r\n");
821  TRACE_DEBUG_ARRAY(" ", ciphertext, *ciphertextLen);
822 
823  //End of exception handling block
824  } while(0);
825 
826  //Free previously allocated memory
827  mpiFree(&m);
828  mpiFree(&c);
829 
830  //Return status code
831  return error;
832 }
833 
834 
835 /**
836  * @brief RSAES-OAEP decryption operation
837  * @param[in] key Recipient's RSA private key
838  * @param[in] hash Hash function
839  * @param[in] mgfHash MGF hash function
840  * @param[in] label Optional label to be associated with the message
841  * @param[in] labelLen Length of the label, in bytes
842  * @param[in] ciphertext Ciphertext to be decrypted
843  * @param[in] ciphertextLen Length of the ciphertext to be decrypted
844  * @param[out] message Output buffer where to store the decrypted message
845  * @param[in] messageSize Size of the output buffer
846  * @param[out] messageLen Length of the decrypted message
847  * @return Error code
848  **/
849 
850 __weak_func error_t rsaesOaepDecrypt(const RsaPrivateKey *key,
851  const HashAlgo *hash, const HashAlgo *mgfHash, const char_t *label,
852  size_t labelLen, const uint8_t *ciphertext, size_t ciphertextLen,
853  uint8_t *message, size_t messageSize, size_t *messageLen)
854 {
855  error_t error;
856  uint_t k;
857  size_t i;
858  size_t j;
859  size_t n;
860  uint8_t b;
861  uint32_t a;
862  uint32_t bad;
863  Mpi c;
864  Mpi m;
865  Mpi t;
866 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
867  uint8_t *em;
868 #else
869  uint8_t em[RSA_MAX_MODULUS_SIZE / 8];
870 #endif
871 
872  //Check parameters
873  if(key == NULL || hash == NULL || mgfHash == NULL || ciphertext == NULL ||
874  message == NULL || messageSize == 0 || messageLen == NULL)
875  {
877  }
878 
879  //Debug message
880  TRACE_DEBUG("RSAES-OAEP decryption...\r\n");
881  TRACE_DEBUG(" Modulus:\r\n");
882  TRACE_DEBUG_MPI(" ", &key->n);
883  TRACE_DEBUG(" Public exponent:\r\n");
884  TRACE_DEBUG_MPI(" ", &key->e);
885  TRACE_DEBUG(" Private exponent:\r\n");
886  TRACE_DEBUG_MPI(" ", &key->d);
887  TRACE_DEBUG(" Prime 1:\r\n");
888  TRACE_DEBUG_MPI(" ", &key->p);
889  TRACE_DEBUG(" Prime 2:\r\n");
890  TRACE_DEBUG_MPI(" ", &key->q);
891  TRACE_DEBUG(" Prime exponent 1:\r\n");
892  TRACE_DEBUG_MPI(" ", &key->dp);
893  TRACE_DEBUG(" Prime exponent 2:\r\n");
894  TRACE_DEBUG_MPI(" ", &key->dq);
895  TRACE_DEBUG(" Coefficient:\r\n");
896  TRACE_DEBUG_MPI(" ", &key->qinv);
897  TRACE_DEBUG(" Ciphertext:\r\n");
898  TRACE_DEBUG_ARRAY(" ", ciphertext, ciphertextLen);
899 
900  //Initialize multiple-precision integers
901  mpiInit(&c);
902  mpiInit(&m);
903  mpiInit(&t);
904 
905  //Get the length in octets of the modulus n
906  k = mpiGetByteLength(&key->n);
907 
908  //Check the length of the modulus
909  if(k < (2 * hash->digestSize + 2))
910  {
912  }
913 
914  //Check the length of the ciphertext
915  if(ciphertextLen != k)
916  {
917  return ERROR_INVALID_LENGTH;
918  }
919 
920 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
921  //Allocate a buffer to store the encoded message EM
922  em = cryptoAllocMem(k);
923  //Failed to allocate memory?
924  if(em == NULL)
925  {
926  return ERROR_OUT_OF_MEMORY;
927  }
928 #else
929  //Check the length of the modulus
930  if(k > sizeof(em))
931  {
932  return ERROR_BUFFER_OVERFLOW;
933  }
934 #endif
935 
936  //Start of exception handling block
937  do
938  {
939  //Convert the ciphertext to an integer ciphertext representative c
940  error = mpiImport(&c, ciphertext, ciphertextLen, MPI_FORMAT_BIG_ENDIAN);
941  //Conversion failed?
942  if(error)
943  {
944  break;
945  }
946 
947  //Apply the RSADP decryption primitive
948  error = rsadp(key, &c, &m);
949  //Any error to report?
950  if(error)
951  {
952  break;
953  }
954 
955  //When unprotected, RSA-CRT is vulnerable to the Bellcore attack
956  if(mpiGetLength(&key->n) > 0 && mpiGetLength(&key->e) > 0 &&
957  mpiGetLength(&key->p) > 0 && mpiGetLength(&key->q) > 0 &&
958  mpiGetLength(&key->dp) > 0 && mpiGetLength(&key->dq) > 0 &&
959  mpiGetLength(&key->qinv) > 0)
960  {
961  RsaPublicKey publicKey;
962 
963  //The pair of numbers (n, e) form the RSA public key
964  publicKey.n = key->n;
965  publicKey.e = key->e;
966 
967  //Apply the RSAEP encryption primitive
968  error = rsaep(&publicKey, &m, &t);
969  //Any error to report?
970  if(error)
971  {
972  break;
973  }
974 
975  //Verify the result in order to protect against RSA-CRT key leak
976  if(mpiComp(&t, &c) != 0)
977  {
978  //A decryption fault has been detected
979  error = ERROR_FAILURE;
980  break;
981  }
982  }
983 
984  //Convert the message representative m to an encoded message EM of
985  //length k octets
986  error = mpiExport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
987  //Conversion failed?
988  if(error)
989  {
990  break;
991  }
992 
993  //Debug message
994  TRACE_DEBUG(" Encoded message:\r\n");
995  TRACE_DEBUG_ARRAY(" ", em, k);
996 
997  //EME-OAEP decoding
998  bad = emeOaepDecode(hash, mgfHash, label, labelLen, em, k, &n);
999 
1000  //Check whether the output buffer is large enough to hold the decrypted
1001  //message
1002  bad |= CRYPTO_TEST_LT_32(messageSize, n);
1003 
1004  //Do not release any part of the message when the decryption fails
1005  n = CRYPTO_SELECT_32(n, 0, bad);
1006 
1007  //Copy the decrypted message, byte per byte
1008  for(i = 0; i < messageSize; i++)
1009  {
1010  //Read the whole encoded message EM
1011  for(b = 0, j = 0; j < k; j++)
1012  {
1013  //Constant time implementation
1014  a = CRYPTO_TEST_EQ_32(j, k - n + i);
1015  b = CRYPTO_SELECT_8(b, em[j], a);
1016  }
1017 
1018  //Save the value of the current byte
1019  message[i] = b;
1020  }
1021 
1022  //Return the length of the decrypted message
1023  *messageLen = n;
1024 
1025  //Check whether the decryption operation is successful
1026  error = (bad == 0) ? NO_ERROR : ERROR_DECRYPTION_FAILED;
1027 
1028  //Debug message
1029  TRACE_DEBUG(" Message:\r\n");
1030  TRACE_DEBUG_ARRAY(" ", message, *messageLen);
1031 
1032  //End of exception handling block
1033  } while(0);
1034 
1035 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1036  //Release the encoded message
1037  cryptoFreeMem(em);
1038 #endif
1039 
1040  //Release multiple precision integers
1041  mpiFree(&c);
1042  mpiFree(&m);
1043  mpiFree(&t);
1044 
1045  //Return status code
1046  return error;
1047 }
1048 
1049 
1050 /**
1051  * @brief RSASSA-PKCS1-v1_5 signature generation operation
1052  * @param[in] key Signer's RSA private key
1053  * @param[in] hash Hash function used to digest the message
1054  * @param[in] digest Digest of the message to be signed
1055  * @param[out] signature Resulting signature
1056  * @param[out] signatureLen Length of the resulting signature
1057  * @return Error code
1058  **/
1059 
1061  const HashAlgo *hash, const uint8_t *digest, uint8_t *signature,
1062  size_t *signatureLen)
1063 {
1064  error_t error;
1065  uint_t k;
1066  uint8_t *em;
1067  Mpi m;
1068  Mpi s;
1069  Mpi t;
1070 
1071  //Check parameters
1072  if(key == NULL || hash == NULL || digest == NULL || signature == NULL ||
1073  signatureLen == NULL)
1074  {
1075  return ERROR_INVALID_PARAMETER;
1076  }
1077 
1078  //Debug message
1079  TRACE_DEBUG("RSASSA-PKCS1-v1_5 signature generation...\r\n");
1080  TRACE_DEBUG(" Modulus:\r\n");
1081  TRACE_DEBUG_MPI(" ", &key->n);
1082  TRACE_DEBUG(" Public exponent:\r\n");
1083  TRACE_DEBUG_MPI(" ", &key->e);
1084  TRACE_DEBUG(" Private exponent:\r\n");
1085  TRACE_DEBUG_MPI(" ", &key->d);
1086  TRACE_DEBUG(" Prime 1:\r\n");
1087  TRACE_DEBUG_MPI(" ", &key->p);
1088  TRACE_DEBUG(" Prime 2:\r\n");
1089  TRACE_DEBUG_MPI(" ", &key->q);
1090  TRACE_DEBUG(" Prime exponent 1:\r\n");
1091  TRACE_DEBUG_MPI(" ", &key->dp);
1092  TRACE_DEBUG(" Prime exponent 2:\r\n");
1093  TRACE_DEBUG_MPI(" ", &key->dq);
1094  TRACE_DEBUG(" Coefficient:\r\n");
1095  TRACE_DEBUG_MPI(" ", &key->qinv);
1096  TRACE_DEBUG(" Message digest:\r\n");
1097  TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
1098 
1099  //Initialize multiple-precision integers
1100  mpiInit(&m);
1101  mpiInit(&s);
1102  mpiInit(&t);
1103 
1104  //Get the length in octets of the modulus n
1105  k = mpiGetByteLength(&key->n);
1106  //Point to the buffer where the encoded message EM will be formatted
1107  em = signature;
1108 
1109  //Apply the EMSA-PKCS1-v1.5 encoding operation
1110  error = emsaPkcs1v15Encode(hash, digest, em, k);
1111  //Any error to report?
1112  if(error)
1113  {
1114  return error;
1115  }
1116 
1117  //Debug message
1118  TRACE_DEBUG(" Encoded message:\r\n");
1119  TRACE_DEBUG_ARRAY(" ", em, k);
1120 
1121  //Start of exception handling block
1122  do
1123  {
1124  //Convert the encoded message EM to an integer message representative m
1125  error = mpiImport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
1126  //Conversion failed?
1127  if(error)
1128  {
1129  break;
1130  }
1131 
1132  //Apply the RSASP1 signature primitive
1133  error = rsasp1(key, &m, &s);
1134  //Any error to report?
1135  if(error)
1136  {
1137  break;
1138  }
1139 
1140  //When unprotected, RSA-CRT is vulnerable to the Bellcore attack
1141  if(mpiGetLength(&key->n) > 0 && mpiGetLength(&key->e) > 0 &&
1142  mpiGetLength(&key->p) > 0 && mpiGetLength(&key->q) > 0 &&
1143  mpiGetLength(&key->dp) > 0 && mpiGetLength(&key->dq) > 0 &&
1144  mpiGetLength(&key->qinv) > 0)
1145  {
1146  RsaPublicKey publicKey;
1147 
1148  //The pair of numbers (n, e) form the RSA public key
1149  publicKey.n = key->n;
1150  publicKey.e = key->e;
1151 
1152  //Apply the RSAVP1 verification primitive
1153  error = rsavp1(&publicKey, &s, &t);
1154  //Any error to report?
1155  if(error)
1156  {
1157  break;
1158  }
1159 
1160  //Verify the result in order to protect against RSA-CRT key leak
1161  if(mpiComp(&t, &m) != 0)
1162  {
1163  //A signature fault has been detected
1164  error = ERROR_FAILURE;
1165  break;
1166  }
1167  }
1168 
1169  //Convert the signature representative s to a signature of length k octets
1170  error = mpiExport(&s, signature, k, MPI_FORMAT_BIG_ENDIAN);
1171  //Conversion failed?
1172  if(error)
1173  {
1174  break;
1175  }
1176 
1177  //Length of the resulting signature
1178  *signatureLen = k;
1179 
1180  //Debug message
1181  TRACE_DEBUG(" Signature:\r\n");
1182  TRACE_DEBUG_ARRAY(" ", signature, *signatureLen);
1183 
1184  //End of exception handling block
1185  } while(0);
1186 
1187  //Free previously allocated memory
1188  mpiFree(&m);
1189  mpiFree(&s);
1190  mpiFree(&t);
1191 
1192  //Return status code
1193  return error;
1194 }
1195 
1196 
1197 /**
1198  * @brief RSASSA-PKCS1-v1_5 signature verification operation
1199  * @param[in] key Signer's RSA public key
1200  * @param[in] hash Hash function used to digest the message
1201  * @param[in] digest Digest of the message whose signature is to be verified
1202  * @param[in] signature Signature to be verified
1203  * @param[in] signatureLen Length of the signature to be verified
1204  * @return Error code
1205  **/
1206 
1208  const HashAlgo *hash, const uint8_t *digest, const uint8_t *signature,
1209  size_t signatureLen)
1210 {
1211  error_t error;
1212  uint_t k;
1213  Mpi s;
1214  Mpi m;
1215 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1216  uint8_t *em;
1217 #else
1218  uint8_t em[RSA_MAX_MODULUS_SIZE / 8];
1219 #endif
1220 
1221  //Check parameters
1222  if(key == NULL || hash == NULL || digest == NULL || signature == NULL)
1223  {
1224  return ERROR_INVALID_PARAMETER;
1225  }
1226 
1227  //Debug message
1228  TRACE_DEBUG("RSASSA-PKCS1-v1_5 signature verification...\r\n");
1229  TRACE_DEBUG(" Modulus:\r\n");
1230  TRACE_DEBUG_MPI(" ", &key->n);
1231  TRACE_DEBUG(" Public exponent:\r\n");
1232  TRACE_DEBUG_MPI(" ", &key->e);
1233  TRACE_DEBUG(" Message digest:\r\n");
1234  TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
1235  TRACE_DEBUG(" Signature:\r\n");
1236  TRACE_DEBUG_ARRAY(" ", signature, signatureLen);
1237 
1238  //Initialize multiple-precision integers
1239  mpiInit(&s);
1240  mpiInit(&m);
1241 
1242  //Get the length in octets of the modulus n
1243  k = mpiGetByteLength(&key->n);
1244 
1245  //Make sure the modulus is valid
1246  if(k == 0)
1247  {
1248  return ERROR_INVALID_PARAMETER;
1249  }
1250 
1251  //Check the length of the signature
1252  if(signatureLen != k)
1253  {
1254  return ERROR_INVALID_SIGNATURE;
1255  }
1256 
1257 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1258  //Allocate a buffer to store the encoded message EM
1259  em = cryptoAllocMem(k);
1260  //Failed to allocate memory?
1261  if(em == NULL)
1262  {
1263  return ERROR_OUT_OF_MEMORY;
1264  }
1265 #else
1266  //Check the length of the modulus
1267  if(k > sizeof(em))
1268  {
1269  return ERROR_BUFFER_OVERFLOW;
1270  }
1271 #endif
1272 
1273  //Start of exception handling block
1274  do
1275  {
1276  //Convert the signature to an integer signature representative s
1277  error = mpiImport(&s, signature, signatureLen, MPI_FORMAT_BIG_ENDIAN);
1278  //Conversion failed?
1279  if(error)
1280  {
1281  break;
1282  }
1283 
1284  //Apply the RSAVP1 verification primitive
1285  error = rsavp1(key, &s, &m);
1286  //Any error to report?
1287  if(error)
1288  {
1289  break;
1290  }
1291 
1292  //Convert the message representative m to an encoded message EM of
1293  //length k octets
1294  error = mpiExport(&m, em, k, MPI_FORMAT_BIG_ENDIAN);
1295  //Conversion failed?
1296  if(error)
1297  {
1298  break;
1299  }
1300 
1301  //Debug message
1302  TRACE_DEBUG(" Encoded message:\r\n");
1303  TRACE_DEBUG_ARRAY(" ", em, k);
1304 
1305  //Verify the encoded message EM
1306  error = emsaPkcs1v15Verify(hash, digest, em, k);
1307  //Any error to report?
1308  if(error)
1309  {
1310  //The signature is not valid
1311  error = ERROR_INVALID_SIGNATURE;
1312  break;
1313  }
1314 
1315  //End of exception handling block
1316  } while(0);
1317 
1318 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1319  //Release the encoded message
1320  cryptoFreeMem(em);
1321 #endif
1322 
1323  //Release multiple precision integers
1324  mpiFree(&s);
1325  mpiFree(&m);
1326 
1327  //Return status code
1328  return error;
1329 }
1330 
1331 
1332 /**
1333  * @brief RSASSA-PSS signature generation operation
1334  * @param[in] prngAlgo PRNG algorithm
1335  * @param[in] prngContext Pointer to the PRNG context
1336  * @param[in] key Signer's RSA private key
1337  * @param[in] hash Hash function used to digest the message
1338  * @param[in] mgfHash MGF hash function
1339  * @param[in] saltLen Length of the salt, in bytes
1340  * @param[in] digest Digest of the message to be signed
1341  * @param[out] signature Resulting signature
1342  * @param[out] signatureLen Length of the resulting signature
1343  * @return Error code
1344  **/
1345 
1346 __weak_func error_t rsassaPssSign(const PrngAlgo *prngAlgo, void *prngContext,
1347  const RsaPrivateKey *key, const HashAlgo *hash, const HashAlgo *mgfHash,
1348  size_t saltLen, const uint8_t *digest, uint8_t *signature,
1349  size_t *signatureLen)
1350 {
1351  error_t error;
1352  uint_t k;
1353  uint_t modBits;
1354  uint8_t *em;
1355  Mpi m;
1356  Mpi s;
1357  Mpi t;
1358 
1359  //Check parameters
1360  if(prngAlgo == NULL || prngContext == NULL || key == NULL || hash == NULL ||
1361  mgfHash == NULL || digest == NULL || signature == NULL ||
1362  signatureLen == NULL)
1363  {
1364  return ERROR_INVALID_PARAMETER;
1365  }
1366 
1367  //Debug message
1368  TRACE_DEBUG("RSASSA-PSS signature generation...\r\n");
1369  TRACE_DEBUG(" Modulus:\r\n");
1370  TRACE_DEBUG_MPI(" ", &key->n);
1371  TRACE_DEBUG(" Public exponent:\r\n");
1372  TRACE_DEBUG_MPI(" ", &key->e);
1373  TRACE_DEBUG(" Private exponent:\r\n");
1374  TRACE_DEBUG_MPI(" ", &key->d);
1375  TRACE_DEBUG(" Prime 1:\r\n");
1376  TRACE_DEBUG_MPI(" ", &key->p);
1377  TRACE_DEBUG(" Prime 2:\r\n");
1378  TRACE_DEBUG_MPI(" ", &key->q);
1379  TRACE_DEBUG(" Prime exponent 1:\r\n");
1380  TRACE_DEBUG_MPI(" ", &key->dp);
1381  TRACE_DEBUG(" Prime exponent 2:\r\n");
1382  TRACE_DEBUG_MPI(" ", &key->dq);
1383  TRACE_DEBUG(" Coefficient:\r\n");
1384  TRACE_DEBUG_MPI(" ", &key->qinv);
1385  TRACE_DEBUG(" Message digest:\r\n");
1386  TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
1387 
1388  //Initialize multiple-precision integers
1389  mpiInit(&m);
1390  mpiInit(&s);
1391  mpiInit(&t);
1392 
1393  //modBits is the length in bits of the modulus n
1394  modBits = mpiGetBitLength(&key->n);
1395 
1396  //Make sure the modulus is valid
1397  if(modBits == 0)
1398  {
1399  return ERROR_INVALID_PARAMETER;
1400  }
1401 
1402  //Calculate the length in octets of the modulus n
1403  k = (modBits + 7) / 8;
1404 
1405  //Point to the buffer where the encoded message EM will be formatted
1406  em = signature;
1407 
1408  //Apply the EMSA-PSS encoding operation to the message M to produce an
1409  //encoded message EM of length ceil((modBits - 1) / 8) octets
1410  error = emsaPssEncode(prngAlgo, prngContext, hash, mgfHash, saltLen, digest,
1411  em, modBits - 1);
1412  //Any error to report?
1413  if(error)
1414  {
1415  return error;
1416  }
1417 
1418  //Debug message
1419  TRACE_DEBUG(" Encoded message:\r\n");
1420  TRACE_DEBUG_ARRAY(" ", em, (modBits + 6) / 8);
1421 
1422  //Start of exception handling block
1423  do
1424  {
1425  //Convert the encoded message EM to an integer message representative m
1426  error = mpiImport(&m, em, (modBits + 6) / 8, MPI_FORMAT_BIG_ENDIAN);
1427  //Conversion failed?
1428  if(error)
1429  {
1430  break;
1431  }
1432 
1433  //Apply the RSASP1 signature primitive
1434  error = rsasp1(key, &m, &s);
1435  //Any error to report?
1436  if(error)
1437  {
1438  break;
1439  }
1440 
1441  //When unprotected, RSA-CRT is vulnerable to the Bellcore attack
1442  if(mpiGetLength(&key->n) > 0 && mpiGetLength(&key->e) > 0 &&
1443  mpiGetLength(&key->p) > 0 && mpiGetLength(&key->q) > 0 &&
1444  mpiGetLength(&key->dp) > 0 && mpiGetLength(&key->dq) > 0 &&
1445  mpiGetLength(&key->qinv) > 0)
1446  {
1447  RsaPublicKey publicKey;
1448 
1449  //The pair of numbers (n, e) form the RSA public key
1450  publicKey.n = key->n;
1451  publicKey.e = key->e;
1452 
1453  //Apply the RSAVP1 verification primitive
1454  error = rsavp1(&publicKey, &s, &t);
1455  //Any error to report?
1456  if(error)
1457  {
1458  break;
1459  }
1460 
1461  //Verify the result in order to protect against RSA-CRT key leak
1462  if(mpiComp(&t, &m) != 0)
1463  {
1464  //A signature fault has been detected
1465  error = ERROR_FAILURE;
1466  break;
1467  }
1468  }
1469 
1470  //Convert the signature representative s to a signature of length k octets
1471  error = mpiExport(&s, signature, k, MPI_FORMAT_BIG_ENDIAN);
1472  //Conversion failed?
1473  if(error)
1474  {
1475  break;
1476  }
1477 
1478  //Length of the resulting signature
1479  *signatureLen = k;
1480 
1481  //Debug message
1482  TRACE_DEBUG(" Signature:\r\n");
1483  TRACE_DEBUG_ARRAY(" ", signature, *signatureLen);
1484 
1485  //End of exception handling block
1486  } while(0);
1487 
1488  //Free previously allocated memory
1489  mpiFree(&m);
1490  mpiFree(&s);
1491  mpiFree(&t);
1492 
1493  //Return status code
1494  return error;
1495 }
1496 
1497 
1498 /**
1499  * @brief RSASSA-PSS signature verification operation
1500  * @param[in] key Signer's RSA public key
1501  * @param[in] hash Hash function used to digest the message
1502  * @param[in] mgfHash MGF hash function
1503  * @param[in] saltLen Length of the salt, in bytes
1504  * @param[in] digest Digest of the message whose signature is to be verified
1505  * @param[in] signature Signature to be verified
1506  * @param[in] signatureLen Length of the signature to be verified
1507  * @return Error code
1508  **/
1509 
1510 __weak_func error_t rsassaPssVerify(const RsaPublicKey *key,
1511  const HashAlgo *hash, const HashAlgo *mgfHash, size_t saltLen,
1512  const uint8_t *digest, const uint8_t *signature, size_t signatureLen)
1513 {
1514  error_t error;
1515  uint_t k;
1516  uint_t modBits;
1517  Mpi s;
1518  Mpi m;
1519 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1520  uint8_t *em;
1521 #else
1522  uint8_t em[RSA_MAX_MODULUS_SIZE / 8];
1523 #endif
1524 
1525  //Check parameters
1526  if(key == NULL || hash == NULL || mgfHash == NULL || digest == NULL ||
1527  signature == NULL)
1528  {
1529  return ERROR_INVALID_PARAMETER;
1530  }
1531 
1532  //Debug message
1533  TRACE_DEBUG("RSASSA-PSS signature verification...\r\n");
1534  TRACE_DEBUG(" Modulus:\r\n");
1535  TRACE_DEBUG_MPI(" ", &key->n);
1536  TRACE_DEBUG(" Public exponent:\r\n");
1537  TRACE_DEBUG_MPI(" ", &key->e);
1538  TRACE_DEBUG(" Message digest:\r\n");
1539  TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
1540  TRACE_DEBUG(" Signature:\r\n");
1541  TRACE_DEBUG_ARRAY(" ", signature, signatureLen);
1542 
1543  //Initialize multiple-precision integers
1544  mpiInit(&s);
1545  mpiInit(&m);
1546 
1547  //modBits is the length in bits of the modulus n
1548  modBits = mpiGetBitLength(&key->n);
1549 
1550  //Make sure the modulus is valid
1551  if(modBits == 0)
1552  {
1553  return ERROR_INVALID_PARAMETER;
1554  }
1555 
1556  //Calculate the length in octets of the modulus n
1557  k = (modBits + 7) / 8;
1558 
1559  //Check the length of the signature
1560  if(signatureLen != k)
1561  {
1562  return ERROR_INVALID_SIGNATURE;
1563  }
1564 
1565 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1566  //Allocate a buffer to store the encoded message EM
1567  em = cryptoAllocMem(k);
1568  //Failed to allocate memory?
1569  if(em == NULL)
1570  {
1571  return ERROR_OUT_OF_MEMORY;
1572  }
1573 #else
1574  //Check the length of the modulus
1575  if(k > sizeof(em))
1576  {
1577  return ERROR_BUFFER_OVERFLOW;
1578  }
1579 #endif
1580 
1581  //Start of exception handling block
1582  do
1583  {
1584  //Convert the signature to an integer signature representative s
1585  error = mpiImport(&s, signature, signatureLen, MPI_FORMAT_BIG_ENDIAN);
1586  //Conversion failed?
1587  if(error)
1588  {
1589  break;
1590  }
1591 
1592  //Apply the RSAVP1 verification primitive
1593  error = rsavp1(key, &s, &m);
1594  //Any error to report?
1595  if(error)
1596  {
1597  break;
1598  }
1599 
1600  //Convert the message representative m to an encoded message EM of
1601  //length emLen = ceil((modBits - 1) / 8) octets
1602  error = mpiExport(&m, em, (modBits + 6) / 8, MPI_FORMAT_BIG_ENDIAN);
1603  //Conversion failed?
1604  if(error)
1605  {
1606  break;
1607  }
1608 
1609  //Debug message
1610  TRACE_DEBUG(" Encoded message:\r\n");
1611  TRACE_DEBUG_ARRAY(" ", em, (modBits + 6) / 8);
1612 
1613  //Apply the EMSA-PSS verification operation to the message M and the
1614  //encoded message EM to determine whether they are consistent
1615  error = emsaPssVerify(hash, mgfHash, saltLen, digest, em, modBits - 1);
1616  //Any error to report?
1617  if(error)
1618  {
1619  //The signature is not valid
1620  error = ERROR_INVALID_SIGNATURE;
1621  break;
1622  }
1623 
1624  //End of exception handling block
1625  } while(0);
1626 
1627 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
1628  //Release the encoded message
1629  cryptoFreeMem(em);
1630 #endif
1631 
1632  //Release multiple precision integers
1633  mpiFree(&s);
1634  mpiFree(&m);
1635 
1636  //Return status code
1637  return error;
1638 }
1639 
1640 #endif
error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c)
RSA encryption primitive.
__weak_func error_t rsaesPkcs1v15Decrypt(const RsaPrivateKey *key, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen)
RSAES-PKCS1-v1_5 decryption operation.
Definition: rsa.c:522
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_512_OID[9]
Definition: rsa.c:82
uint8_t b
Definition: nbns_common.h:122
void rsaFreePublicKey(RsaPublicKey *key)
Release an RSA public key.
Definition: rsa.c:113
#define RSA_MAX_MODULUS_SIZE
Definition: rsa.h:41
#define CRYPTO_TEST_EQ_32(a, b)
Definition: crypto.h:1128
Mpi p
First factor.
Definition: rsa.h:72
uint8_t a
Definition: ndp.h:411
Arbitrary precision integer.
Definition: mpi.h:102
const uint8_t MD5_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:59
const uint8_t SHA512_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:69
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
const uint8_t RSASSA_PSS_SHAKE256_OID[8]
Definition: rsa.c:89
#define PrngAlgo
Definition: crypto.h:1161
@ ERROR_DECRYPTION_FAILED
Definition: error.h:244
#define CRYPTO_SELECT_32(a, b, c)
Definition: crypto.h:1152
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_384_OID[9]
Definition: rsa.c:80
__weak_func error_t rsaesPkcs1v15Encrypt(const PrngAlgo *prngAlgo, void *prngContext, const RsaPublicKey *key, const uint8_t *message, size_t messageLen, uint8_t *ciphertext, size_t *ciphertextLen)
RSAES-PKCS1-v1_5 encryption operation.
Definition: rsa.c:417
error_t emeOaepEncode(const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, const HashAlgo *mgfHash, const char_t *label, size_t labelLen, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
EME-OAEP encoding operation.
Definition: rsa_misc.c:343
uint8_t message[]
Definition: chap.h:154
uint8_t t
Definition: lldp_ext_med.h:212
error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p)
Modular inverse.
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_224_OID[9]
Definition: rsa.c:76
size_t digestSize
Definition: crypto.h:1272
error_t rsaGeneratePublicKey(const RsaPrivateKey *privateKey, RsaPublicKey *publicKey)
Derive the public key from an RSA private key.
Definition: rsa.c:370
Mpi n
Modulus.
Definition: rsa.h:69
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t mpiSetBitValue(Mpi *r, uint_t index, uint_t value)
Set the bit value at the specified index.
Definition: mpi.c:308
error_t emsaPssEncode(const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, const HashAlgo *mgfHash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
EMSA-PSS encoding operation.
Definition: rsa_misc.c:695
error_t emsaPssVerify(const HashAlgo *hash, const HashAlgo *mgfHash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
EMSA-PSS verification operation.
Definition: rsa_misc.c:792
Mpi e
Public exponent.
Definition: rsa.h:59
const uint8_t RSASSA_PSS_OID[9]
Definition: rsa.c:85
__weak_func error_t rsaGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext, size_t k, uint_t e, RsaPrivateKey *privateKey)
RSA private key generation.
Definition: rsa.c:203
uint32_t emePkcs1v15Decode(uint8_t *em, size_t k, size_t *messageLen)
EME-PKCS1-v1_5 decoding operation.
Definition: rsa_misc.c:287
error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: mpi.c:621
error_t mpiSubInt(Mpi *r, const Mpi *a, mpi_sword_t b)
Subtract an integer from a multiple precision integer.
Definition: mpi.c:1048
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
Definition: mpi.c:49
void rsaInitPrivateKey(RsaPrivateKey *key)
Initialize an RSA private key.
Definition: rsa.c:126
const uint8_t MGF1_OID[9]
Definition: rsa.c:92
Mpi d
Private exponent.
Definition: rsa.h:71
const uint8_t PKCS1_OID[8]
Definition: rsa.c:52
Mpi n
Modulus.
Definition: rsa.h:58
__weak_func error_t rsaesOaepDecrypt(const RsaPrivateKey *key, const HashAlgo *hash, const HashAlgo *mgfHash, const char_t *label, size_t labelLen, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen)
RSAES-OAEP decryption operation.
Definition: rsa.c:850
error_t emePkcs1v15Encode(const PrngAlgo *prngAlgo, void *prngContext, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
EME-PKCS1-v1_5 encoding operation.
Definition: rsa_misc.c:215
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_256_OID[9]
Definition: rsa.c:78
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
Definition: mpi.c:1623
int_t slot
Private key slot.
Definition: rsa.h:77
const uint8_t SHA384_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:67
const uint8_t SHA512_224_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:71
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
error_t emsaPkcs1v15Verify(const HashAlgo *hash, const uint8_t *digest, const uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 verification operation.
Definition: rsa_misc.c:616
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t emsaPkcs1v15Encode(const HashAlgo *hash, const uint8_t *digest, uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 encoding operation.
Definition: rsa_misc.c:550
error_t
Error codes.
Definition: error.h:43
error_t mpiSetValue(Mpi *r, mpi_sword_t a)
Set the value of a multiple precision integer.
Definition: mpi.c:583
void rsaFreePrivateKey(RsaPrivateKey *key)
Release an RSA private key.
Definition: rsa.c:148
#define MPI_CHECK(f)
Definition: mpi.h:74
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
Mpi q
Second factor.
Definition: rsa.h:73
RSA public key.
Definition: rsa.h:57
error_t mpiImport(Mpi *r, const uint8_t *input, size_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:742
error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
RSA decryption primitive.
#define CRYPTO_TEST_LT_32(a, b)
Definition: crypto.h:1136
@ ERROR_INVALID_LENGTH
Definition: error.h:111
General definitions for cryptographic library.
RSA public-key cryptography standard.
#define CRYPTO_SELECT_8(a, b, c)
Definition: crypto.h:1080
error_t rsavp1(const RsaPublicKey *key, const Mpi *s, Mpi *m)
RSA verification primitive.
Definition: rsa_misc.c:195
__weak_func error_t rsaesOaepEncrypt(const PrngAlgo *prngAlgo, void *prngContext, const RsaPublicKey *key, const HashAlgo *hash, const HashAlgo *mgfHash, const char_t *label, size_t labelLen, const uint8_t *message, size_t messageLen, uint8_t *ciphertext, size_t *ciphertextLen)
RSAES-OAEP encryption operation.
Definition: rsa.c:731
error_t mpiExport(const Mpi *a, uint8_t *output, size_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:839
uint32_t t2
Mpi e
Public exponent.
Definition: rsa.h:70
error_t mpiCheckProbablePrime(const Mpi *a)
Test whether a number is probable prime.
const uint8_t SHA256_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:65
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:263
Mpi qinv
CRT coefficient.
Definition: rsa.h:76
Mpi dq
Second factor's CRT exponent.
Definition: rsa.h:75
error_t mpiCopy(Mpi *r, const Mpi *a)
Copy a multiple precision integer.
Definition: mpi.c:533
const uint8_t RSA_ENCRYPTION_OID[9]
Definition: rsa.c:54
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
Definition: mpi.c:189
char char_t
Definition: compiler_port.h:55
Helper routines for RSA.
error_t rsaGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext, size_t k, uint_t e, RsaPrivateKey *privateKey, RsaPublicKey *publicKey)
RSA key pair generation.
Definition: rsa.c:173
__weak_func error_t rsassaPkcs1v15Verify(const RsaPublicKey *key, const HashAlgo *hash, const uint8_t *digest, const uint8_t *signature, size_t signatureLen)
RSASSA-PKCS1-v1_5 signature verification operation.
Definition: rsa.c:1207
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
uint32_t t1
@ ERROR_INVALID_VALUE
Definition: error.h:116
uint8_t m
Definition: ndp.h:304
uint8_t n
RSA private key.
Definition: rsa.h:68
#define cryptoFreeMem(p)
Definition: crypto.h:987
const uint8_t MD2_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:57
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:93
__weak_func error_t rsassaPkcs1v15Sign(const RsaPrivateKey *key, const HashAlgo *hash, const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
RSASSA-PKCS1-v1_5 signature generation operation.
Definition: rsa.c:1060
#define cryptoAllocMem(size)
Definition: crypto.h:982
uint8_t s
Definition: igmp_common.h:234
error_t rsasp1(const RsaPrivateKey *key, const Mpi *m, Mpi *s)
RSA signature primitive.
Definition: rsa_misc.c:174
const uint8_t SHA224_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:63
Common interface for hash algorithms.
Definition: crypto.h:1266
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Definition: mpi.c:375
Mpi dp
First factor's CRT exponent.
Definition: rsa.h:74
int_t mpiCompInt(const Mpi *a, mpi_sword_t b)
Compare a multiple precision integer with an integer.
Definition: mpi.c:446
unsigned int uint_t
Definition: compiler_port.h:57
#define TRACE_DEBUG_MPI(p, a)
Definition: debug.h:122
const uint8_t RSASSA_PSS_SHAKE128_OID[8]
Definition: rsa.c:87
__weak_func error_t rsassaPssVerify(const RsaPublicKey *key, const HashAlgo *hash, const HashAlgo *mgfHash, size_t saltLen, const uint8_t *digest, const uint8_t *signature, size_t signatureLen)
RSASSA-PSS signature verification operation.
Definition: rsa.c:1510
uint32_t emeOaepDecode(const HashAlgo *hash, const HashAlgo *mgfHash, const char_t *label, size_t labelLen, uint8_t *em, size_t k, size_t *messageLen)
EME-OAEP decoding operation.
Definition: rsa_misc.c:449
@ ERROR_INVALID_SIGNATURE
Definition: error.h:229
const uint8_t SHA1_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:61
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.
void rsaInitPublicKey(RsaPublicKey *key)
Initialize an RSA public key.
Definition: rsa.c:100
const uint8_t SHA512_256_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:73
__weak_func error_t rsassaPssSign(const PrngAlgo *prngAlgo, void *prngContext, const RsaPrivateKey *key, const HashAlgo *hash, const HashAlgo *mgfHash, size_t saltLen, const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
RSASSA-PSS signature generation operation.
Definition: rsa.c:1346
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:220
void mpiFree(Mpi *r)
Release a multiple precision integer.
Definition: mpi.c:65