ike_sign_generate.c
Go to the documentation of this file.
1 /**
2  * @file ike_sign_generate.c
3  * @brief RSA/DSA/ECDSA/EdDSA signature generation
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneIPSEC 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.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL IKE_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ike/ike.h"
36 #include "ike/ike_algorithms.h"
37 #include "ike/ike_sign_generate.h"
38 #include "pkix/pem_key_import.h"
39 #include "pkix/x509_sign_format.h"
40 #include "debug.h"
41 
42 //Check IKEv2 library configuration
43 #if (IKE_SUPPORT == ENABLED && IKE_CERT_AUTH_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Signature generation
48  * @param[in] sa Pointer to the IKE SA
49  * @param[in] id Pointer to the identification data
50  * @param[in] idLen Length of the identification data, in bytes
51  * @param[out] authMethod Authentication method
52  * @param[out] signature Output stream where to write the signature
53  * @param[out] signatureLen Total number of bytes that have been written
54  * @return Error code
55  **/
56 
57 error_t ikeGenerateSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen,
58  uint8_t *authMethod, uint8_t *signature, size_t *signatureLen)
59 {
60  error_t error;
61  IkeContext *context;
62 
63  //Point to the IKE context
64  context = sa->context;
65 
66 #if (IKE_SIGN_HASH_ALGOS_SUPPORT == ENABLED)
67  //Digital signature method?
68  if(sa->signHashAlgos != 0)
69  {
70  //The peer is only allowed to use the "Digital Signature" authentication
71  //method if the Notify payload of type SIGNATURE_HASH_ALGORITHMS has been
72  //sent and received by each peer (refer to RFC 7427, section 3)
74 
75  //The new digital signature method is flexible enough to include all
76  //current signature methods (RSA, RSA-PSS, DSA, ECDSA and EdDSA) and add
77  //new methods in the future
78  error = ikeGenerateDigitalSignature(sa, id, idLen,
79  (IkeAuthData *) signature, signatureLen);
80  }
81  else
82 #endif
83 #if (IKE_RSA_SIGN_SUPPORT == ENABLED && IKE_SHA1_SUPPORT == ENABLED)
84  //RSA signature algorithm?
85  if(context->certType == IKE_CERT_TYPE_RSA)
86  {
87  //Set authentication method
89 
90  //Generate an RSA signature using the entity's private key
91  error = ikeGenerateRsaSignature(sa, id, idLen, SHA1_HASH_ALGO, signature,
92  signatureLen);
93  }
94  else
95 #endif
96 #if (IKE_DSA_SIGN_SUPPORT == ENABLED && IKE_SHA1_SUPPORT == ENABLED)
97  //DSA signature method?
98  if(context->certType == IKE_CERT_TYPE_DSA)
99  {
100  //Set authentication method
102 
103  //Generate an DSA signature using the entity's private key
104  error = ikeGenerateDsaSignature(sa, id, idLen, SHA1_HASH_ALGO, signature,
105  signatureLen, IKE_SIGN_FORMAT_RAW);
106  }
107  else
108 #endif
109 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_256_SUPPORT == ENABLED && \
110  IKE_SHA256_SUPPORT == ENABLED)
111  //ECDSA with NIST P-256 signature method?
112  if(context->certType == IKE_CERT_TYPE_ECDSA_P256)
113  {
114  //Set authentication method
116 
117  //Generate an ECDSA signature using the entity's private key
118  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP256R1_CURVE,
119  SHA256_HASH_ALGO, signature, signatureLen, IKE_SIGN_FORMAT_RAW);
120  }
121  else
122 #endif
123 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_384_SUPPORT == ENABLED && \
124  IKE_SHA384_SUPPORT == ENABLED)
125  //ECDSA with NIST P-384 signature method?
126  if(context->certType == IKE_CERT_TYPE_ECDSA_P384)
127  {
128  //Set authentication method
130 
131  //Generate an ECDSA signature using the entity's private key
132  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP384R1_CURVE,
133  SHA384_HASH_ALGO, signature, signatureLen, IKE_SIGN_FORMAT_RAW);
134  }
135  else
136 #endif
137 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_521_SUPPORT == ENABLED && \
138  IKE_SHA512_SUPPORT == ENABLED)
139  //ECDSA with NIST P-521 signature method?
140  if(context->certType == IKE_CERT_TYPE_ECDSA_P521)
141  {
142  //Set authentication method
144 
145  //Generate an ECDSA signature using the entity's private key
146  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP521R1_CURVE,
147  SHA512_HASH_ALGO, signature, signatureLen, IKE_SIGN_FORMAT_RAW);
148  }
149  else
150 #endif
151  //Invalid signature method?
152  {
153  //Report an error
155  }
156 
157  //Return status code
158  return error;
159 }
160 
161 
162 /**
163  * @brief Digital signature generation
164  * @param[in] sa Pointer to the IKE SA
165  * @param[in] id Pointer to the identification data
166  * @param[in] idLen Length of the identification data, in bytes
167  * @param[out] authData Output stream where to write the authentication data
168  * @param[out] authDataLen Total number of bytes that have been written
169  * @return Error code
170  **/
171 
173  size_t idLen, IkeAuthData *authData, size_t *authDataLen)
174 {
175  error_t error;
176  size_t n;
177  uint8_t *signature;
178  const HashAlgo *hashAlgo;
179  IkeContext *context;
180  X509SignAlgoId signAlgoId;
181 
182  //Point to the IKE context
183  context = sa->context;
184 
185  //When calculating the digital signature, a peer must pick one algorithm
186  //sent by the other peer (refer to RFC 7427, section 4)
187  if(context->certType == IKE_CERT_TYPE_RSA ||
188  context->certType == IKE_CERT_TYPE_RSA_PSS ||
189  context->certType == IKE_CERT_TYPE_DSA ||
190  context->certType == IKE_CERT_TYPE_ECDSA_P256 ||
191  context->certType == IKE_CERT_TYPE_ECDSA_BRAINPOOLP256R1)
192  {
193  //The preferred signature hash algorithm is SHA-256
195  }
196  else if(context->certType == IKE_CERT_TYPE_ECDSA_P384 ||
197  context->certType == IKE_CERT_TYPE_ECDSA_BRAINPOOLP384R1)
198  {
199  //The preferred signature hash algorithm is SHA-384
201  }
202  else if(context->certType == IKE_CERT_TYPE_ECDSA_P521 ||
203  context->certType == IKE_CERT_TYPE_ECDSA_BRAINPOOLP512R1)
204  {
205  //The preferred signature hash algorithm is SHA-512
207  }
208  else
209  {
210  hashAlgo = NULL;
211  }
212 
213  //Select the algorithm identifier that matches the specified certificate
214  //type and hash algorithms
215  error = ikeSelectSignAlgoId(context->certType, hashAlgo, &signAlgoId);
216 
217  //Check status code
218  if(!error)
219  {
220  //The signature value is prefixed with an ASN.1 object indicating the
221  //algorithm used to generate the signature (refer to RFC 7427, section 3)
222  error = x509FormatSignatureAlgo(&signAlgoId, authData->algoId, &n);
223  }
224 
225  //Check status code
226  if(!error)
227  {
228  //Set the length of the ASN.1 object
229  authData->algoIdLen = (uint8_t) n;
230 
231  //There is no padding between the ASN.1 object and the signature value
232  signature = authData->algoId + authData->algoIdLen;
233 
234 #if (IKE_RSA_SIGN_SUPPORT == ENABLED)
235  //RSA signature algorithm?
236  if(context->certType == IKE_CERT_TYPE_RSA)
237  {
238  //Generate an RSA signature using the entity's private key
239  error = ikeGenerateRsaSignature(sa, id, idLen, hashAlgo, signature,
240  &n);
241  }
242  else
243 #endif
244 #if (IKE_RSA_PSS_SIGN_SUPPORT == ENABLED)
245  //RSA-PSS signature algorithm?
246  if(context->certType == IKE_CERT_TYPE_RSA_PSS)
247  {
248  //Generate an RSA-PSS signature using the entity's private key
249  error = ikeGenerateRsaPssSignature(sa, id, idLen, hashAlgo,
250  signAlgoId.rsaPssParams.saltLen, signature, &n);
251  }
252  else
253 #endif
254 #if (IKE_DSA_SIGN_SUPPORT == ENABLED)
255  //DSA signature method?
256  if(context->certType == IKE_CERT_TYPE_DSA)
257  {
258  //Generate an DSA signature using the entity's private key
259  error = ikeGenerateDsaSignature(sa, id, idLen, hashAlgo, signature,
261  }
262  else
263 #endif
264 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_256_SUPPORT == ENABLED)
265  //ECDSA with NIST P-256 signature method?
266  if(context->certType == IKE_CERT_TYPE_ECDSA_P256)
267  {
268  //Generate an ECDSA signature using the entity's private key
269  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP256R1_CURVE,
270  hashAlgo, signature, &n, IKE_SIGN_FORMAT_ASN1);
271  }
272  else
273 #endif
274 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_384_SUPPORT == ENABLED)
275  //ECDSA with NIST P-384 signature method?
276  if(context->certType == IKE_CERT_TYPE_ECDSA_P384)
277  {
278  //Generate an ECDSA signature using the entity's private key
279  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP384R1_CURVE,
280  hashAlgo, signature, &n, IKE_SIGN_FORMAT_ASN1);
281  }
282  else
283 #endif
284 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED && IKE_ECP_521_SUPPORT == ENABLED)
285  //ECDSA with NIST P-521 signature method?
286  if(context->certType == IKE_CERT_TYPE_ECDSA_P521)
287  {
288  //Generate an ECDSA signature using the entity's private key
289  error = ikeGenerateEcdsaSignature(sa, id, idLen, SECP521R1_CURVE,
290  hashAlgo, signature, &n, IKE_SIGN_FORMAT_ASN1);
291  }
292  else
293 #endif
294 #if (IKE_ED25519_SIGN_SUPPORT == ENABLED)
295  //Ed25519 signature method?
296  if(context->certType == IKE_CERT_TYPE_ED25519)
297  {
298  //Generate an Ed25519 signature using the entity's private key
299  error = ikeGenerateEd25519Signature(sa, id, idLen, signature, &n);
300  }
301  else
302 #endif
303 #if (IKE_ED448_SIGN_SUPPORT == ENABLED)
304  //Ed448 signature method?
305  if(context->certType == IKE_CERT_TYPE_ED448)
306  {
307  //Generate an Ed448 signature using the entity's private key
308  error = ikeGenerateEd448Signature(sa, id, idLen, signature, &n);
309  }
310  else
311 #endif
312  //Invalid signature method?
313  {
314  //Report an error
316  }
317  }
318 
319  //Check status code
320  if(!error)
321  {
322  //Total length of the authentication data
323  *authDataLen = sizeof(IkeAuthData) + authData->algoIdLen + n;
324  }
325 
326  //Return status code
327  return error;
328 }
329 
330 
331 /**
332  * @brief RSA signature generation
333  * @param[in] sa Pointer to the IKE SA
334  * @param[in] id Pointer to the identification data
335  * @param[in] idLen Length of the identification data, in bytes
336  * @param[in] hashAlgo Hash algorithm
337  * @param[out] signature Output stream where to write the signature
338  * @param[out] signatureLen Total number of bytes that have been written
339  * @return Error code
340  **/
341 
343  size_t idLen, const HashAlgo *hashAlgo, uint8_t *signature,
344  size_t *signatureLen)
345 {
346 #if (IKE_RSA_SIGN_SUPPORT == ENABLED)
347  error_t error;
348  IkeContext *context;
349  RsaPrivateKey rsaPrivateKey;
350  uint8_t digest[IKE_MAX_DIGEST_SIZE];
351 
352  //Point to the IKE context
353  context = sa->context;
354 
355  //Initialize RSA private key
356  rsaInitPrivateKey(&rsaPrivateKey);
357 
358  //Digest signed octets
359  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
360  sa->originalInitiator);
361 
362  //Check status code
363  if(!error)
364  {
365  //Import RSA private key
366  error = pemImportRsaPrivateKey(&rsaPrivateKey, context->privateKey,
367  context->privateKeyLen, context->password);
368  }
369 
370  //Check status code
371  if(!error)
372  {
373  //Generate RSA signature (RSASSA-PKCS1-v1_5 signature scheme)
374  error = rsassaPkcs1v15Sign(&rsaPrivateKey, hashAlgo, digest, signature,
375  signatureLen);
376  }
377 
378  //Free previously allocated memory
379  rsaFreePrivateKey(&rsaPrivateKey);
380 
381  //Return status code
382  return error;
383 #else
384  //Not implemented
385  return ERROR_NOT_IMPLEMENTED;
386 #endif
387 }
388 
389 
390 /**
391  * @brief RSA-PSS signature generation
392  * @param[in] sa Pointer to the IKE SA
393  * @param[in] id Pointer to the identification data
394  * @param[in] idLen Length of the identification data, in bytes
395  * @param[in] hashAlgo Hash algorithm
396  * @param[in] saltLen Length of the salt, in bytes
397  * @param[out] signature Output stream where to write the signature
398  * @param[out] signatureLen Total number of bytes that have been written
399  * @return Error code
400  **/
401 
403  size_t idLen, const HashAlgo *hashAlgo, size_t saltLen, uint8_t *signature,
404  size_t *signatureLen)
405 {
406 #if (IKE_RSA_PSS_SIGN_SUPPORT == ENABLED)
407  error_t error;
408  IkeContext *context;
409  RsaPrivateKey rsaPrivateKey;
410  uint8_t digest[IKE_MAX_DIGEST_SIZE];
411 
412  //Point to the IKE context
413  context = sa->context;
414 
415  //Initialize RSA private key
416  rsaInitPrivateKey(&rsaPrivateKey);
417 
418  //Digest signed octets
419  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
420  sa->originalInitiator);
421 
422  //Check status code
423  if(!error)
424  {
425  //Import RSA private key
426  error = pemImportRsaPrivateKey(&rsaPrivateKey, context->privateKey,
427  context->privateKeyLen, context->password);
428  }
429 
430  //Check status code
431  if(!error)
432  {
433  //Generate RSA signature (RSASSA-PSS signature scheme)
434  error = rsassaPssSign(context->prngAlgo, context->prngContext,
435  &rsaPrivateKey, hashAlgo, hashAlgo, saltLen, digest, signature,
436  signatureLen);
437  }
438 
439  //Free previously allocated memory
440  rsaFreePrivateKey(&rsaPrivateKey);
441 
442  //Return status code
443  return error;
444 #else
445  //Not implemented
446  return ERROR_NOT_IMPLEMENTED;
447 #endif
448 }
449 
450 
451 /**
452  * @brief DSA signature generation
453  * @param[in] sa Pointer to the IKE SA
454  * @param[in] id Pointer to the identification data
455  * @param[in] idLen Length of the identification data, in bytes
456  * @param[in] hashAlgo Hash algorithm
457  * @param[out] signature Output stream where to write the signature
458  * @param[out] signatureLen Total number of bytes that have been written
459  * @param[in] format Signature format (raw or ASN.1)
460  * @return Error code
461  **/
462 
464  size_t idLen, const HashAlgo *hashAlgo, uint8_t *signature,
465  size_t *signatureLen, IkeSignFormat format)
466 {
467 #if (IKE_DSA_SIGN_SUPPORT == ENABLED)
468  error_t error;
469  IkeContext *context;
470  DsaPrivateKey dsaPrivateKey;
471  DsaSignature dsaSignature;
472  uint8_t digest[IKE_MAX_DIGEST_SIZE];
473 
474  //Point to the IKE context
475  context = sa->context;
476 
477  //Initialize DSA private key
478  dsaInitPrivateKey(&dsaPrivateKey);
479  //Initialize DSA signature
480  dsaInitSignature(&dsaSignature);
481 
482  //Digest signed octets
483  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
484  sa->originalInitiator);
485 
486  //Check status code
487  if(!error)
488  {
489  //Import DSA private key
490  error = pemImportDsaPrivateKey(&dsaPrivateKey, context->privateKey,
491  context->privateKeyLen, context->password);
492  }
493 
494  //Check status code
495  if(!error)
496  {
497  //Generate DSA signature
498  error = dsaGenerateSignature(context->prngAlgo, context->prngContext,
499  &dsaPrivateKey, digest, hashAlgo->digestSize, &dsaSignature);
500  }
501 
502  //Check status code
503  if(!error)
504  {
505  //Encode (R, S) integer pair
506  error = ikeFormatDsaSignature(&dsaSignature, signature, signatureLen,
507  format);
508  }
509 
510  //Free previously allocated memory
511  dsaFreePrivateKey(&dsaPrivateKey);
512  dsaFreeSignature(&dsaSignature);
513 
514  //Return status code
515  return error;
516 #else
517  //Not implemented
518  return ERROR_NOT_IMPLEMENTED;
519 #endif
520 }
521 
522 
523 /**
524  * @brief ECDSA signature generation
525  * @param[in] sa Pointer to the IKE SA
526  * @param[in] id Pointer to the identification data
527  * @param[in] idLen Length of the identification data, in bytes
528  * @param[in] group Elliptic curve group
529  * @param[in] hashAlgo Hash algorithm
530  * @param[out] signature Output stream where to write the signature
531  * @param[out] signatureLen Total number of bytes that have been written
532  * @param[in] format Signature format (raw or ASN.1)
533  * @return Error code
534  **/
535 
537  size_t idLen, const EcCurve *group, const HashAlgo *hashAlgo,
538  uint8_t *signature, size_t *signatureLen, IkeSignFormat format)
539 {
540 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED)
541  error_t error;
542  IkeContext *context;
543  EcPrivateKey ecPrivateKey;
544  EcdsaSignature ecdsaSignature;
545  uint8_t digest[IKE_MAX_DIGEST_SIZE];
546 
547  //Point to the IKE context
548  context = sa->context;
549 
550  //Initialize EC private key
551  ecInitPrivateKey(&ecPrivateKey);
552  //Initialize ECDSA signature
553  ecdsaInitSignature(&ecdsaSignature);
554 
555  //Digest signed octets
556  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
557  sa->originalInitiator);
558 
559  //Check status code
560  if(!error)
561  {
562  //Import EC private key
563  error = pemImportEcPrivateKey(&ecPrivateKey, context->privateKey,
564  context->privateKeyLen, context->password);
565  }
566 
567  //Check status code
568  if(!error)
569  {
570  //Generate ECDSA signature
571  error = ecdsaGenerateSignature(context->prngAlgo, context->prngContext,
572  &ecPrivateKey, digest, hashAlgo->digestSize, &ecdsaSignature);
573  }
574 
575  //Check status code
576  if(!error)
577  {
578  //Encode (R, S) integer pair
579  error = ikeFormatEcdsaSignature(&ecdsaSignature, signature, signatureLen,
580  format);
581  }
582 
583  //Free previously allocated memory
584  ecFreePrivateKey(&ecPrivateKey);
585  ecdsaFreeSignature(&ecdsaSignature);
586 
587  //Return status code
588  return error;
589 #else
590  //Not implemented
591  return ERROR_NOT_IMPLEMENTED;
592 #endif
593 }
594 
595 
596 /**
597  * @brief Ed25519 signature generation
598  * @param[in] sa Pointer to the IKE SA
599  * @param[in] id Pointer to the identification data
600  * @param[in] idLen Length of the identification data, in bytes
601  * @param[out] signature Output stream where to write the signature
602  * @param[out] signatureLen Total number of bytes that have been written
603  * @return Error code
604  **/
605 
607  size_t idLen, uint8_t *signature, size_t *signatureLen)
608 {
609 #if (IKE_ED25519_SIGN_SUPPORT == ENABLED)
610  error_t error;
611  const uint8_t *q;
612  IkeContext *context;
613  EddsaPrivateKey ed25519PrivateKey;
614  DataFrag messageFrags[3];
615  uint8_t macId[IKE_MAX_DIGEST_SIZE];
616 
617  //Point to the IKE context
618  context = sa->context;
619 
620  //Initialize Ed25519 private key
621  eddsaInitPrivateKey(&ed25519PrivateKey);
622 
623  //Data to be signed is run through the EdDSA algorithm without pre-hashing
624  error = ikeGetSignedOctets(sa, id, idLen, macId, messageFrags,
625  sa->originalInitiator);
626 
627  //Check status code
628  if(!error)
629  {
630  //Import Ed25519 private key
631  error = pemImportEddsaPrivateKey(&ed25519PrivateKey, context->privateKey,
632  context->privateKeyLen, context->password);
633  }
634 
635  //Check status code
636  if(!error)
637  {
638  //The public key is optional
639  q = (ed25519PrivateKey.q.curve != NULL) ? ed25519PrivateKey.q.q : NULL;
640 
641  //Generate Ed25519 signature
642  error = ed25519GenerateSignatureEx(ed25519PrivateKey.d, q,
643  messageFrags, arraysize(messageFrags), NULL, 0, 0, signature);
644  }
645 
646  //Check status code
647  if(!error)
648  {
649  //The Ed25519 signature consists of 32 octets
650  *signatureLen = ED25519_SIGNATURE_LEN;
651  }
652 
653  //Free previously allocated memory
654  eddsaFreePrivateKey(&ed25519PrivateKey);
655 
656  //Return status code
657  return error;
658 #else
659  //Not implemented
660  return ERROR_NOT_IMPLEMENTED;
661 #endif
662 }
663 
664 
665 /**
666  * @brief Ed448 signature generation
667  * @param[in] sa Pointer to the IKE SA
668  * @param[in] id Pointer to the identification data
669  * @param[in] idLen Length of the identification data, in bytes
670  * @param[out] signature Output stream where to write the signature
671  * @param[out] signatureLen Total number of bytes that have been written
672  * @return Error code
673  **/
674 
676  size_t idLen, uint8_t *signature, size_t *signatureLen)
677 {
678 #if (IKE_ED448_SIGN_SUPPORT == ENABLED)
679  error_t error;
680  const uint8_t *q;
681  IkeContext *context;
682  EddsaPrivateKey ed448PrivateKey;
683  DataFrag messageFrags[3];
684  uint8_t macId[IKE_MAX_DIGEST_SIZE];
685 
686  //Point to the IKE context
687  context = sa->context;
688 
689  //Initialize Ed448 private key
690  eddsaInitPrivateKey(&ed448PrivateKey);
691 
692  //Data to be signed is run through the EdDSA algorithm without pre-hashing
693  error = ikeGetSignedOctets(sa, id, idLen, macId, messageFrags,
694  sa->originalInitiator);
695 
696  //Check status code
697  if(!error)
698  {
699  //Import Ed448 private key
700  error = pemImportEddsaPrivateKey(&ed448PrivateKey, context->privateKey,
701  context->privateKeyLen, context->password);
702  }
703 
704  //Check status code
705  if(!error)
706  {
707  //The public key is optional
708  q = (ed448PrivateKey.q.curve != NULL) ? ed448PrivateKey.q.q : NULL;
709 
710  //Generate Ed448 signature
711  error = ed448GenerateSignatureEx(ed448PrivateKey.d, q, messageFrags,
712  arraysize(messageFrags), NULL, 0, 0, signature);
713  }
714 
715  //Check status code
716  if(!error)
717  {
718  //The Ed448 signature consists of 32 octets
719  *signatureLen = ED448_SIGNATURE_LEN;
720  }
721 
722  //Free previously allocated memory
723  eddsaFreePrivateKey(&ed448PrivateKey);
724 
725  //Return status code
726  return error;
727 #else
728  //Not implemented
729  return ERROR_NOT_IMPLEMENTED;
730 #endif
731 }
732 
733 #endif
__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:495
ECDSA signature.
Definition: ecdsa.h:63
error_t ikeGenerateRsaPssSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, const HashAlgo *hashAlgo, size_t saltLen, uint8_t *signature, size_t *signatureLen)
RSA-PSS signature generation.
IkeAuthData
Definition: ike.h:1607
#define SHA256_HASH_ALGO
Definition: sha256.h:49
#define SHA1_HASH_ALGO
Definition: sha1.h:49
@ IKE_CERT_TYPE_ECDSA_BRAINPOOLP384R1
Definition: ike.h:1423
#define SECP521R1_CURVE
Definition: ec_curves.h:53
@ IKE_HASH_ALGO_SHA256
Definition: ike.h:1400
uint8_t d[EDDSA_MAX_PRIVATE_KEY_LEN]
Private key.
Definition: eddsa.h:77
#define SHA512_HASH_ALGO
Definition: sha512.h:49
error_t ikeGenerateSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *authMethod, uint8_t *signature, size_t *signatureLen)
Signature generation.
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
error_t ikeGenerateEd448Signature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *signature, size_t *signatureLen)
Ed448 signature generation.
#define SECP384R1_CURVE
Definition: ec_curves.h:52
void eddsaFreePrivateKey(EddsaPrivateKey *key)
Release an EdDSA private key.
Definition: eddsa.c:95
void eddsaInitPrivateKey(EddsaPrivateKey *key)
Initialize an EdDSA private key.
Definition: eddsa.c:75
@ IKE_CERT_TYPE_RSA_PSS
Definition: ike.h:1417
void dsaFreePrivateKey(DsaPrivateKey *key)
Release a DSA private key.
Definition: dsa.c:152
@ IKE_AUTH_METHOD_ECDSA_P521_SHA512
ECDSA with SHA-512 on the P-521 curve.
Definition: ike.h:1169
#define SECP256R1_CURVE
Definition: ec_curves.h:51
error_t ikeGenerateRsaSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, const HashAlgo *hashAlgo, uint8_t *signature, size_t *signatureLen)
RSA signature generation.
error_t pemImportEcPrivateKey(EcPrivateKey *privateKey, const char_t *input, size_t length, const char_t *password)
Decode a PEM file containing an EC private key.
size_t digestSize
Definition: crypto.h:1249
@ IKE_AUTH_METHOD_RSA
RSA Digital Signature.
Definition: ike.h:1164
#define ED448_SIGNATURE_LEN
Definition: ed448.h:44
error_t ikeDigestSignedOctets(IkeSaEntry *sa, const HashAlgo *hashAlgo, const uint8_t *id, size_t idLen, uint8_t *digest, bool_t initiator)
Digest signed octets.
PEM key file import functions.
#define ED25519_SIGNATURE_LEN
Definition: ed25519.h:44
#define IKE_MAX_DIGEST_SIZE
Definition: ike.h:764
@ IKE_CERT_TYPE_ECDSA_P384
Definition: ike.h:1420
RSA/DSA/ECDSA/EdDSA signature generation.
void rsaInitPrivateKey(RsaPrivateKey *key)
Initialize an RSA private key.
Definition: rsa.c:126
error_t ikeSelectSignAlgoId(IkeCertType certType, const HashAlgo *hashAlgo, X509SignAlgoId *signAlgoId)
Select the algorithm identifier that matches the specified certificate type and hash algorithms.
@ IKE_CERT_TYPE_ECDSA_BRAINPOOLP256R1
Definition: ike.h:1422
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.
#define IkeContext
Definition: ike.h:832
void ecdsaFreeSignature(EcdsaSignature *signature)
Release an ECDSA signature.
Definition: ecdsa.c:90
error_t pemImportRsaPrivateKey(RsaPrivateKey *privateKey, const char_t *input, size_t length, const char_t *password)
Decode a PEM file containing an RSA private key.
uint8_t q[EDDSA_MAX_PUBLIC_KEY_LEN]
Public key.
Definition: eddsa.h:66
error_t ed448GenerateSignatureEx(const uint8_t *privateKey, const uint8_t *publicKey, const DataFrag *messageFrags, uint_t messageNumFrags, const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature)
EdDSA signature generation.
Definition: ed448.c:277
const EcCurve * curve
Elliptic curve parameters.
Definition: eddsa.h:65
error_t
Error codes.
Definition: error.h:43
void dsaInitSignature(DsaSignature *signature)
Initialize a DSA signature.
Definition: dsa.c:168
error_t ikeGenerateDsaSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, const HashAlgo *hashAlgo, uint8_t *signature, size_t *signatureLen, IkeSignFormat format)
DSA signature generation.
void rsaFreePrivateKey(RsaPrivateKey *key)
Release an RSA private key.
Definition: rsa.c:148
@ IKE_AUTH_METHOD_DIGITAL_SIGN
Digital Signature.
Definition: ike.h:1172
error_t ikeGetSignedOctets(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *macId, DataFrag *messageFrags, bool_t initiator)
Retrieve the octets to be signed using EdDSA.
@ IKE_HASH_ALGO_SHA512
Definition: ike.h:1402
void ecdsaInitSignature(EcdsaSignature *signature)
Initialize an ECDSA signature.
Definition: ecdsa.c:74
error_t dsaGenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const DsaPrivateKey *key, const uint8_t *digest, size_t digestLen, DsaSignature *signature)
DSA signature generation.
Definition: dsa.c:457
error_t ikeGenerateDigitalSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, IkeAuthData *authData, size_t *authDataLen)
Digital signature generation.
void dsaInitPrivateKey(DsaPrivateKey *key)
Initialize a DSA private key.
Definition: dsa.c:133
Data fragment descriptor.
Definition: crypto.h:1164
error_t x509FormatSignatureAlgo(const X509SignAlgoId *signatureAlgo, uint8_t *output, size_t *written)
Format SignatureAlgorithm structure.
@ IKE_SIGN_FORMAT_ASN1
Definition: ike_sign_misc.h:50
EC private key.
Definition: ec.h:432
uint8_t authMethod
Definition: ike.h:1593
DSA private key.
Definition: dsa.h:72
void ecFreePrivateKey(EcPrivateKey *key)
Release an EC private key.
Definition: ec.c:100
error_t pemImportEddsaPrivateKey(EddsaPrivateKey *privateKey, const char_t *input, size_t length, const char_t *password)
Decode a PEM file containing a EdDSA private key.
@ IKE_CERT_TYPE_DSA
Definition: ike.h:1418
@ IKE_CERT_TYPE_RSA
Definition: ike.h:1416
@ IKE_CERT_TYPE_ECDSA_BRAINPOOLP512R1
Definition: ike.h:1424
EdDSA private key.
Definition: eddsa.h:75
@ IKE_CERT_TYPE_ED25519
Definition: ike.h:1426
IKEv2 (Internet Key Exchange Protocol)
@ IKE_CERT_TYPE_ED448
Definition: ike.h:1427
IkeSignFormat
Signature format.
Definition: ike_sign_misc.h:48
#define SHA384_HASH_ALGO
Definition: sha384.h:45
#define IkeSaEntry
Definition: ike.h:836
uint8_t n
RSA private key.
Definition: rsa.h:68
@ IKE_SIGN_FORMAT_RAW
Definition: ike_sign_misc.h:49
error_t ikeFormatEcdsaSignature(const EcdsaSignature *signature, uint8_t *data, size_t *length, IkeSignFormat format)
ECDSA signature formatting.
error_t ed25519GenerateSignatureEx(const uint8_t *privateKey, const uint8_t *publicKey, const DataFrag *messageFrags, uint_t messageNumFrags, const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature)
EdDSA signature generation.
Definition: ed25519.c:288
uint8_t authData[]
Definition: ipv6.h:375
void dsaFreeSignature(DsaSignature *signature)
Release a DSA signature.
Definition: dsa.c:181
void ecInitPrivateKey(EcPrivateKey *key)
Initialize an EC private key.
Definition: ec.c:80
@ IKE_AUTH_METHOD_DSS
DSS Digital Signature.
Definition: ike.h:1166
error_t ikeGenerateEcdsaSignature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, const EcCurve *group, const HashAlgo *hashAlgo, uint8_t *signature, size_t *signatureLen, IkeSignFormat format)
ECDSA signature generation.
EddsaPublicKey q
Public key.
Definition: eddsa.h:79
@ IKE_AUTH_METHOD_ECDSA_P256_SHA256
ECDSA with SHA-256 on the P-256 curve.
Definition: ike.h:1167
Common interface for hash algorithms.
Definition: crypto.h:1243
error_t ikeGenerateEd25519Signature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *signature, size_t *signatureLen)
Ed25519 signature generation.
DSA signature.
Definition: dsa.h:85
#define EcCurve
Definition: ec.h:346
@ IKE_CERT_TYPE_ECDSA_P521
Definition: ike.h:1421
@ ERROR_UNSUPPORTED_SIGNATURE_ALGO
Definition: error.h:132
error_t pemImportDsaPrivateKey(DsaPrivateKey *privateKey, const char_t *input, size_t length, const char_t *password)
Decode a PEM file containing a DSA private key.
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.
X509RsaPssParameters rsaPssParams
Definition: x509_common.h:1166
@ IKE_AUTH_METHOD_ECDSA_P384_SHA384
ECDSA with SHA-384 on the P-384 curve.
Definition: ike.h:1168
@ IKE_HASH_ALGO_SHA384
Definition: ike.h:1401
IKEv2 algorithm negotiation.
Signature algorithm identifier.
Definition: x509_common.h:1163
Debugging facilities.
const HashAlgo * ikeSelectSignHashAlgo(IkeSaEntry *sa, uint16_t preferredHashAlgoId)
Select the hash algorithm to be used for signing.
error_t ikeFormatDsaSignature(const DsaSignature *signature, uint8_t *data, size_t *length, IkeSignFormat format)
DSA signature formatting.
Definition: ike_sign_misc.c:56
#define arraysize(a)
Definition: os_port.h:71
@ IKE_CERT_TYPE_ECDSA_P256
Definition: ike.h:1419