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-2025 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.5.0
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_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, saltLen, digest, signature, signatureLen);
436  }
437 
438  //Free previously allocated memory
439  rsaFreePrivateKey(&rsaPrivateKey);
440 
441  //Return status code
442  return error;
443 #else
444  //Not implemented
445  return ERROR_NOT_IMPLEMENTED;
446 #endif
447 }
448 
449 
450 /**
451  * @brief DSA signature generation
452  * @param[in] sa Pointer to the IKE SA
453  * @param[in] id Pointer to the identification data
454  * @param[in] idLen Length of the identification data, in bytes
455  * @param[in] hashAlgo Hash algorithm
456  * @param[out] signature Output stream where to write the signature
457  * @param[out] signatureLen Total number of bytes that have been written
458  * @param[in] format Signature format (raw or ASN.1)
459  * @return Error code
460  **/
461 
463  size_t idLen, const HashAlgo *hashAlgo, uint8_t *signature,
464  size_t *signatureLen, IkeSignFormat format)
465 {
466 #if (IKE_DSA_SIGN_SUPPORT == ENABLED)
467  error_t error;
468  IkeContext *context;
469  DsaPrivateKey dsaPrivateKey;
470  DsaSignature dsaSignature;
471  uint8_t digest[IKE_MAX_DIGEST_SIZE];
472 
473  //Point to the IKE context
474  context = sa->context;
475 
476  //Initialize DSA private key
477  dsaInitPrivateKey(&dsaPrivateKey);
478  //Initialize DSA signature
479  dsaInitSignature(&dsaSignature);
480 
481  //Digest signed octets
482  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
483  sa->originalInitiator);
484 
485  //Check status code
486  if(!error)
487  {
488  //Import DSA private key
489  error = pemImportDsaPrivateKey(&dsaPrivateKey, context->privateKey,
490  context->privateKeyLen, context->password);
491  }
492 
493  //Check status code
494  if(!error)
495  {
496  //Generate DSA signature
497  error = dsaGenerateSignature(context->prngAlgo, context->prngContext,
498  &dsaPrivateKey, digest, hashAlgo->digestSize, &dsaSignature);
499  }
500 
501  //Check status code
502  if(!error)
503  {
504  //Encode (R, S) integer pair
505  error = ikeFormatDsaSignature(&dsaSignature, signature, signatureLen,
506  format);
507  }
508 
509  //Free previously allocated memory
510  dsaFreePrivateKey(&dsaPrivateKey);
511  dsaFreeSignature(&dsaSignature);
512 
513  //Return status code
514  return error;
515 #else
516  //Not implemented
517  return ERROR_NOT_IMPLEMENTED;
518 #endif
519 }
520 
521 
522 /**
523  * @brief ECDSA signature generation
524  * @param[in] sa Pointer to the IKE SA
525  * @param[in] id Pointer to the identification data
526  * @param[in] idLen Length of the identification data, in bytes
527  * @param[in] group Elliptic curve group
528  * @param[in] hashAlgo Hash algorithm
529  * @param[out] signature Output stream where to write the signature
530  * @param[out] signatureLen Total number of bytes that have been written
531  * @param[in] format Signature format (raw or ASN.1)
532  * @return Error code
533  **/
534 
536  size_t idLen, const EcCurve *group, const HashAlgo *hashAlgo,
537  uint8_t *signature, size_t *signatureLen, IkeSignFormat format)
538 {
539 #if (IKE_ECDSA_SIGN_SUPPORT == ENABLED)
540  error_t error;
541  IkeContext *context;
542  EcPrivateKey ecPrivateKey;
543  EcdsaSignature ecdsaSignature;
544  uint8_t digest[IKE_MAX_DIGEST_SIZE];
545 
546  //Point to the IKE context
547  context = sa->context;
548 
549  //Initialize EC private key
550  ecInitPrivateKey(&ecPrivateKey);
551  //Initialize ECDSA signature
552  ecdsaInitSignature(&ecdsaSignature);
553 
554  //Digest signed octets
555  error = ikeDigestSignedOctets(sa, hashAlgo, id, idLen, digest,
556  sa->originalInitiator);
557 
558  //Check status code
559  if(!error)
560  {
561  //Import EC private key
562  error = pemImportEcPrivateKey(&ecPrivateKey, context->privateKey,
563  context->privateKeyLen, context->password);
564  }
565 
566  //Check status code
567  if(!error)
568  {
569  //Generate ECDSA signature
570  error = ecdsaGenerateSignature(context->prngAlgo, context->prngContext,
571  &ecPrivateKey, digest, hashAlgo->digestSize, &ecdsaSignature);
572  }
573 
574  //Check status code
575  if(!error)
576  {
577  //Encode (R, S) integer pair
578  error = ikeFormatEcdsaSignature(&ecdsaSignature, signature, signatureLen,
579  format);
580  }
581 
582  //Free previously allocated memory
583  ecFreePrivateKey(&ecPrivateKey);
584  ecdsaFreeSignature(&ecdsaSignature);
585 
586  //Return status code
587  return error;
588 #else
589  //Not implemented
590  return ERROR_NOT_IMPLEMENTED;
591 #endif
592 }
593 
594 
595 /**
596  * @brief Ed25519 signature generation
597  * @param[in] sa Pointer to the IKE SA
598  * @param[in] id Pointer to the identification data
599  * @param[in] idLen Length of the identification data, in bytes
600  * @param[out] signature Output stream where to write the signature
601  * @param[out] signatureLen Total number of bytes that have been written
602  * @return Error code
603  **/
604 
606  size_t idLen, uint8_t *signature, size_t *signatureLen)
607 {
608 #if (IKE_ED25519_SIGN_SUPPORT == ENABLED)
609  error_t error;
610  const uint8_t *q;
611  IkeContext *context;
612  EddsaPrivateKey ed25519PrivateKey;
613  DataChunk messageChunks[3];
614  uint8_t macId[IKE_MAX_DIGEST_SIZE];
615 
616  //Point to the IKE context
617  context = sa->context;
618 
619  //Initialize Ed25519 private key
620  eddsaInitPrivateKey(&ed25519PrivateKey);
621 
622  //Data to be signed is run through the EdDSA algorithm without pre-hashing
623  error = ikeGetSignedOctets(sa, id, idLen, macId, messageChunks,
624  sa->originalInitiator);
625 
626  //Check status code
627  if(!error)
628  {
629  //Import Ed25519 private key
630  error = pemImportEddsaPrivateKey(&ed25519PrivateKey, context->privateKey,
631  context->privateKeyLen, context->password);
632  }
633 
634  //Check status code
635  if(!error)
636  {
637  //The public key is optional
638  q = (ed25519PrivateKey.q.curve != NULL) ? ed25519PrivateKey.q.q : NULL;
639 
640  //Generate Ed25519 signature
641  error = ed25519GenerateSignatureEx(ed25519PrivateKey.d, q,
642  messageChunks, arraysize(messageChunks), NULL, 0, 0, signature);
643  }
644 
645  //Check status code
646  if(!error)
647  {
648  //The Ed25519 signature consists of 32 octets
649  *signatureLen = ED25519_SIGNATURE_LEN;
650  }
651 
652  //Free previously allocated memory
653  eddsaFreePrivateKey(&ed25519PrivateKey);
654 
655  //Return status code
656  return error;
657 #else
658  //Not implemented
659  return ERROR_NOT_IMPLEMENTED;
660 #endif
661 }
662 
663 
664 /**
665  * @brief Ed448 signature generation
666  * @param[in] sa Pointer to the IKE SA
667  * @param[in] id Pointer to the identification data
668  * @param[in] idLen Length of the identification data, in bytes
669  * @param[out] signature Output stream where to write the signature
670  * @param[out] signatureLen Total number of bytes that have been written
671  * @return Error code
672  **/
673 
675  size_t idLen, uint8_t *signature, size_t *signatureLen)
676 {
677 #if (IKE_ED448_SIGN_SUPPORT == ENABLED)
678  error_t error;
679  const uint8_t *q;
680  IkeContext *context;
681  EddsaPrivateKey ed448PrivateKey;
682  DataChunk messageChunks[3];
683  uint8_t macId[IKE_MAX_DIGEST_SIZE];
684 
685  //Point to the IKE context
686  context = sa->context;
687 
688  //Initialize Ed448 private key
689  eddsaInitPrivateKey(&ed448PrivateKey);
690 
691  //Data to be signed is run through the EdDSA algorithm without pre-hashing
692  error = ikeGetSignedOctets(sa, id, idLen, macId, messageChunks,
693  sa->originalInitiator);
694 
695  //Check status code
696  if(!error)
697  {
698  //Import Ed448 private key
699  error = pemImportEddsaPrivateKey(&ed448PrivateKey, context->privateKey,
700  context->privateKeyLen, context->password);
701  }
702 
703  //Check status code
704  if(!error)
705  {
706  //The public key is optional
707  q = (ed448PrivateKey.q.curve != NULL) ? ed448PrivateKey.q.q : NULL;
708 
709  //Generate Ed448 signature
710  error = ed448GenerateSignatureEx(ed448PrivateKey.d, q, messageChunks,
711  arraysize(messageChunks), NULL, 0, 0, signature);
712  }
713 
714  //Check status code
715  if(!error)
716  {
717  //The Ed448 signature consists of 32 octets
718  *signatureLen = ED448_SIGNATURE_LEN;
719  }
720 
721  //Free previously allocated memory
722  eddsaFreePrivateKey(&ed448PrivateKey);
723 
724  //Return status code
725  return error;
726 #else
727  //Not implemented
728  return ERROR_NOT_IMPLEMENTED;
729 #endif
730 }
731 
732 #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:509
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:1538
#define SHA256_HASH_ALGO
Definition: sha256.h:49
#define SHA1_HASH_ALGO
Definition: sha1.h:49
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.
Definition: pem_import.c:1228
@ IKE_CERT_TYPE_ECDSA_BRAINPOOLP384R1
Definition: ike.h:1354
#define SECP521R1_CURVE
Definition: ec_curves.h:53
@ IKE_HASH_ALGO_SHA256
Definition: ike.h:1331
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:1348
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:1113
#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 pemImportRsaPrivateKey(RsaPrivateKey *privateKey, const char_t *input, size_t length, const char_t *password)
Decode a PEM file containing an RSA private key.
Definition: pem_import.c:379
size_t digestSize
Definition: crypto.h:1088
@ IKE_AUTH_METHOD_RSA
RSA Digital Signature.
Definition: ike.h:1108
#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.
#define ED25519_SIGNATURE_LEN
Definition: ed25519.h:44
#define IKE_MAX_DIGEST_SIZE
Definition: ike.h:736
@ IKE_CERT_TYPE_ECDSA_P384
Definition: ike.h:1351
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:1353
error_t ikeGetSignedOctets(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *macId, DataChunk *messageChunks, bool_t initiator)
Retrieve the octets to be signed using EdDSA.
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.
Definition: pem_import.c:662
#define IkeContext
Definition: ike.h:796
void ecdsaFreeSignature(EcdsaSignature *signature)
Release an ECDSA signature.
Definition: ecdsa.c:86
uint8_t q[EDDSA_MAX_PUBLIC_KEY_LEN]
Public key.
Definition: eddsa.h:66
PEM file import functions.
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:1116
@ IKE_HASH_ALGO_SHA512
Definition: ike.h:1333
error_t rsassaPssSign(const PrngAlgo *prngAlgo, void *prngContext, const RsaPrivateKey *key, const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
RSASSA-PSS signature generation operation.
Definition: rsa.c:1189
void ecdsaInitSignature(EcdsaSignature *signature)
Initialize an ECDSA signature.
Definition: ecdsa.c:73
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:500
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
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.
Definition: pem_import.c:945
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:1524
DSA private key.
Definition: dsa.h:72
void ecFreePrivateKey(EcPrivateKey *key)
Release an EC private key.
Definition: ec.c:100
@ IKE_CERT_TYPE_DSA
Definition: ike.h:1349
@ IKE_CERT_TYPE_RSA
Definition: ike.h:1347
@ IKE_CERT_TYPE_ECDSA_BRAINPOOLP512R1
Definition: ike.h:1355
EdDSA private key.
Definition: eddsa.h:75
@ IKE_CERT_TYPE_ED25519
Definition: ike.h:1357
IKEv2 (Internet Key Exchange Protocol)
@ IKE_CERT_TYPE_ED448
Definition: ike.h:1358
IkeSignFormat
Signature format.
Definition: ike_sign_misc.h:48
Data chunk descriptor.
Definition: crypto.h:1017
#define SHA384_HASH_ALGO
Definition: sha384.h:45
#define IkeSaEntry
Definition: ike.h:800
error_t ed25519GenerateSignatureEx(const uint8_t *privateKey, const uint8_t *publicKey, const DataChunk *message, uint_t messageLen, const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature)
EdDSA signature generation.
Definition: ed25519.c:268
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.
uint8_t authData[]
Definition: ipv6.h:353
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:1110
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:1111
Common interface for hash algorithms.
Definition: crypto.h:1082
error_t ikeGenerateEd25519Signature(IkeSaEntry *sa, const uint8_t *id, size_t idLen, uint8_t *signature, size_t *signatureLen)
Ed25519 signature generation.
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:935
error_t ed448GenerateSignatureEx(const uint8_t *privateKey, const uint8_t *publicKey, const DataChunk *message, uint_t messageLen, const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature)
EdDSA signature generation.
Definition: ed448.c:257
DSA signature.
Definition: dsa.h:85
#define EcCurve
Definition: ec.h:346
@ IKE_CERT_TYPE_ECDSA_P521
Definition: ike.h:1352
@ ERROR_UNSUPPORTED_SIGNATURE_ALGO
Definition: error.h:132
X509RsaPssParameters rsaPssParams
Definition: x509_common.h:1091
@ IKE_AUTH_METHOD_ECDSA_P384_SHA384
ECDSA with SHA-384 on the P-384 curve.
Definition: ike.h:1112
@ IKE_HASH_ALGO_SHA384
Definition: ike.h:1332
IKEv2 algorithm negotiation.
Signature algorithm identifier.
Definition: x509_common.h:1088
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:55
#define arraysize(a)
Definition: os_port.h:71
@ IKE_CERT_TYPE_ECDSA_P256
Definition: ike.h:1350