ike_key_exchange.c
Go to the documentation of this file.
1 /**
2  * @file ike_key_exchange.c
3  * @brief Diffie-Hellman key exchange
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_key_exchange.h"
37 #include "ike/ike_algorithms.h"
38 #include "ike/ike_dh_groups.h"
39 #include "debug.h"
40 
41 //Check IKEv2 library configuration
42 #if (IKE_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief Initialize key exchange context
47  * @param[in] keContext Pointer to the key exchange context
48  **/
49 
51 {
52 #if (IKE_DH_KE_SUPPORT == ENABLED)
53  //Initialize Diffie-Hellman context
54  dhInit(&keContext->dhContext);
55 #endif
56 
57 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
58  //Initialize ECDH context
59  ecdhInit(&keContext->ecdhContext);
60 #endif
61 }
62 
63 
64 /**
65  * @brief Release key exchange context
66  * @param[in] keContext Pointer to the key exchange context
67  **/
68 
70 {
71 #if (IKE_DH_KE_SUPPORT == ENABLED)
72  //Release Diffie-Hellman context
73  dhFree(&keContext->dhContext);
74 #endif
75 
76 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
77  //Release ECDH context
78  ecdhFree(&keContext->ecdhContext);
79 #endif
80 }
81 
82 
83 /**
84  * @brief Key pair generation
85  * @param[in] keContext Pointer to the key exchange context
86  * @param[in] prngAlgo PRNG algorithm
87  * @param[in] prngContext Pointer to the PRNG context
88  * @return Error code
89  **/
90 
91 error_t ikeGenerateKeyPair(IkeKeContext *keContext, const PrngAlgo *prngAlgo,
92  void *prngContext)
93 {
94  error_t error;
95 
96  //Debug message
97  TRACE_INFO("Generating Diffie-Hellman key pair...\r\n");
98 
99 #if (IKE_DH_KE_SUPPORT == ENABLED)
100  //Diffie-Hellman key exchange algorithm?
101  if(ikeIsDhKeyExchangeAlgo(keContext->groupNum))
102  {
103  //Load Diffie-Hellman parameters
104  error = ikeLoadDhParams(&keContext->dhContext.params, keContext->groupNum);
105 
106  //Check status code
107  if(!error)
108  {
109  //Generate an ephemeral key pair
110  error = dhGenerateKeyPair(&keContext->dhContext, prngAlgo,
111  prngContext);
112  }
113  }
114  else
115 #endif
116 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
117  //ECDH key exchange algorithm?
118  if(ikeIsEcdhKeyExchangeAlgo(keContext->groupNum))
119  {
120  const EcCurve *curve;
121 
122  //Get the elliptic curve that matches the specified group number
123  curve = ikeGetEcdhCurve(keContext->groupNum);
124 
125  //Valid elliptic curve?
126  if(curve != NULL)
127  {
128  //Save elliptic curve parameters
129  error = ecdhSetCurve(&keContext->ecdhContext, curve);
130 
131  //Check status code
132  if(!error)
133  {
134  //Generate an ephemeral key pair
135  error = ecdhGenerateKeyPair(&keContext->ecdhContext, prngAlgo,
136  prngContext);
137  }
138  }
139  else
140  {
141  //Report an error
142  error = ERROR_UNSUPPORTED_TYPE;
143  }
144  }
145  else
146 #endif
147  //Unknown key exchange algorithm?
148  {
149  //Report an error
151  }
152 
153  //Return status code
154  return error;
155 }
156 
157 
158 /**
159  * @brief Compute shared secret
160  * @param[in] keContext Pointer to the key exchange context
161  * @param[out] output Buffer where to store the shared secret
162  * @param[out] outputLen Length of the resulting shared secret
163  * @return Error code
164  **/
165 
166 error_t ikeComputeSharedSecret(IkeKeContext *keContext, uint8_t *output,
167  size_t *outputLen)
168 {
169  error_t error;
170 
171  //Debug message
172  TRACE_INFO("Computing shared secret...\r\n");
173 
174 #if (IKE_DH_KE_SUPPORT == ENABLED)
175  //Diffie-Hellman key exchange algorithm?
176  if(ikeIsDhKeyExchangeAlgo(keContext->groupNum))
177  {
178  //Let g^ir be the shared secret from the ephemeral Diffie-Hellman
179  //exchange
180  error = dhComputeSharedSecret(&keContext->dhContext, output,
181  IKE_MAX_SHARED_SECRET_LEN, outputLen);
182  }
183  else
184 #endif
185 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
186  //ECDH key exchange algorithm?
187  if(ikeIsEcdhKeyExchangeAlgo(keContext->groupNum))
188  {
189  //The Diffie-Hellman shared secret value consists of the x value of the
190  //Diffie-Hellman common value (refer to RFC 5903, section 7)
191  error = ecdhComputeSharedSecret(&keContext->ecdhContext, output,
192  IKE_MAX_SHARED_SECRET_LEN, outputLen);
193  }
194  else
195 #endif
196  //Unknown key exchange algorithm?
197  {
198  //Report an error
200  }
201 
202  //Return status code
203  return error;
204 }
205 
206 
207 /**
208  * @brief Format public key
209  * @param[in] keContext Pointer to the key exchange context
210  * @param[out] p Buffer where to format the public key
211  * @param[out] written Total number of bytes that have been written
212  * @return Error code
213  **/
214 
216  size_t *written)
217 {
218  error_t error;
219 
220 #if (IKE_DH_KE_SUPPORT == ENABLED)
221  //Diffie-Hellman key exchange algorithm?
222  if(ikeIsDhKeyExchangeAlgo(keContext->groupNum))
223  {
224  //The length of the Diffie-Hellman public value for MODP groups must be
225  //equal to the length of the prime modulus over which the exponentiation
226  //was performed, prepending zero bits to the value if necessary (refer
227  //to RFC 7296, section 3.4)
228  error = dhExportPublicKey(&keContext->dhContext, p, written,
230  }
231  else
232 #endif
233 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
234  //ECDH key exchange algorithm?
235  if(ikeIsEcdhKeyExchangeAlgo(keContext->groupNum))
236  {
237  //The Diffie-Hellman public value is obtained by concatenating the x and
238  //y values (refer to RFC 5903, section 7)
239  error = ecdhExportPublicKey(&keContext->ecdhContext, p, written,
241  }
242  else
243 #endif
244  //Unknown key exchange algorithm?
245  {
246  //Report an error
248  }
249 
250  //Return status code
251  return error;
252 }
253 
254 
255 /**
256  * @brief Parse peer's public key
257  * @param[in] keContext Pointer to the key exchange context
258  * @param[out] p Pointer the public key
259  * @param[out] length Length of the public key, in bytes
260  * @return Error code
261  **/
262 
263 error_t ikeParsePublicKey(IkeKeContext *keContext, const uint8_t *p,
264  size_t length)
265 {
266  error_t error;
267 
268 #if (IKE_DH_KE_SUPPORT == ENABLED)
269  //Diffie-Hellman key exchange algorithm?
270  if(ikeIsDhKeyExchangeAlgo(keContext->groupNum))
271  {
272  const IkeDhGroup *dhGroup;
273 
274  //Get the Diffie-Hellman group that matches the specified group number
275  dhGroup = ikeGetDhGroup(keContext->groupNum);
276 
277  //Valid Diffie-Hellman group?
278  if(dhGroup != NULL)
279  {
280  //The length of the Diffie-Hellman public value for MODP groups must
281  //be equal to the length of the prime modulus over which the
282  //exponentiation was performed, prepending zero bits to the value if
283  //necessary (refer to RFC 7296, section 3.4)
284  if(length == dhGroup->pLen)
285  {
286  //Load Diffie-Hellman parameters
287  error = ikeLoadDhParams(&keContext->dhContext.params, keContext->groupNum);
288 
289  //Check status code
290  if(!error)
291  {
292  //Load peer's Diffie-Hellman public value
293  error = dhImportPeerPublicKey(&keContext->dhContext, p, length,
295  }
296  }
297  else
298  {
299  //Report an error
300  error = ERROR_INVALID_SYNTAX;
301  }
302  }
303  else
304  {
305  //Report an error
306  error = ERROR_INVALID_GROUP;
307  }
308  }
309  else
310 #endif
311 #if (IKE_ECDH_KE_SUPPORT == ENABLED)
312  //ECDH key exchange algorithm?
313  if(ikeIsEcdhKeyExchangeAlgo(keContext->groupNum))
314  {
315  const EcCurve *curve;
316 
317  //Get the elliptic curve that matches the specified group number
318  curve = ikeGetEcdhCurve(keContext->groupNum);
319 
320  //Valid elliptic curve?
321  if(curve != NULL)
322  {
323  //Save elliptic curve parameters
324  error = ecdhSetCurve(&keContext->ecdhContext, curve);
325 
326  //Check status code
327  if(!error)
328  {
329  //In an ECP key exchange, the Diffie-Hellman public value passed in
330  //a KE payload consists of two components, x and y, corresponding
331  //to the coordinates of an elliptic curve point
332  error = ecdhImportPeerPublicKey(&keContext->ecdhContext, p, length,
334  }
335  }
336  else
337  {
338  //Report an error
339  error = ERROR_INVALID_GROUP;
340  }
341  }
342  else
343 #endif
344  //Unknown key exchange algorithm?
345  {
346  //Report an error
347  error = ERROR_INVALID_GROUP;
348  }
349 
350  //Return status code
351  return error;
352 }
353 
354 #endif
void ikeFreeKeContext(IkeKeContext *keContext)
Release key exchange context.
@ ERROR_UNSUPPORTED_TYPE
Definition: error.h:125
Diffie-Hellman key exchange.
void ikeInitKeContext(IkeKeContext *keContext)
Initialize key exchange context.
error_t ikeComputeSharedSecret(IkeKeContext *keContext, uint8_t *output, size_t *outputLen)
Compute shared secret.
#define PrngAlgo
Definition: crypto.h:1140
uint8_t p
Definition: ndp.h:300
void ecdhFree(EcdhContext *context)
Release ECDH context.
Definition: ecdh.c:65
Diffie-Hellman group.
Definition: ike_dh_groups.h:49
size_t pLen
Length of the prime modulus, in bytes.
Definition: ike_dh_groups.h:52
#define IKE_MAX_SHARED_SECRET_LEN
Definition: ike.h:802
@ ERROR_INVALID_GROUP
Definition: error.h:276
error_t dhComputeSharedSecret(DhContext *context, uint8_t *output, size_t outputSize, size_t *outputLen)
Compute Diffie-Hellman shared secret.
Definition: dh.c:289
error_t ikeFormatPublicKey(IkeKeContext *keContext, uint8_t *p, size_t *written)
Format public key.
error_t
Error codes.
Definition: error.h:43
DhContext dhContext
Diffie-Hellman context.
Definition: ike.h:1818
DhParameters params
Definition: dh.h:61
void dhFree(DhContext *context)
Release Diffie-Hellman context.
Definition: dh.c:71
bool_t ikeIsDhKeyExchangeAlgo(uint16_t groupNum)
Test if the group number identifies a DH key exchange algorithm.
@ EC_PUBLIC_KEY_FORMAT_RAW
Definition: ec.h:387
error_t dhImportPeerPublicKey(DhContext *context, const uint8_t *input, size_t length, MpiFormat format)
Import peer's public key.
Definition: dh.c:218
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
error_t dhExportPublicKey(DhContext *context, uint8_t *output, size_t *written, MpiFormat format)
Export our own public key.
Definition: dh.c:185
error_t ecdhComputeSharedSecret(EcdhContext *context, uint8_t *output, size_t outputSize, size_t *outputLen)
Compute ECDH shared secret.
Definition: ecdh.c:415
@ ERROR_UNSUPPORTED_KEY_EXCH_ALGO
Definition: error.h:131
IKEv2 (Internet Key Exchange Protocol)
error_t ecdhImportPeerPublicKey(EcdhContext *context, const uint8_t *input, size_t length, EcPublicKeyFormat format)
Import peer's public key.
Definition: ecdh.c:274
void ecdhInit(EcdhContext *context)
Initialize ECDH context.
Definition: ecdh.c:49
error_t ikeLoadDhParams(DhParameters *params, uint16_t groupNum)
Load Diffie-Hellman parameters.
error_t ikeGenerateKeyPair(IkeKeContext *keContext, const PrngAlgo *prngAlgo, void *prngContext)
Key pair generation.
EcdhContext ecdhContext
ECDH context.
Definition: ike.h:1821
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:93
@ ERROR_INVALID_SYNTAX
Definition: error.h:68
const EcCurve * ikeGetEcdhCurve(uint16_t groupNum)
Get the elliptic curve that matches the specified group number.
error_t ecdhSetCurve(EcdhContext *context, const EcCurve *curve)
Specify the elliptic curve to use.
Definition: ecdh.c:83
#define EcCurve
Definition: ec.h:346
error_t ikeParsePublicKey(IkeKeContext *keContext, const uint8_t *p, size_t length)
Parse peer's public key.
uint16_t groupNum
Key exchange method.
Definition: ike.h:1816
error_t dhGenerateKeyPair(DhContext *context, const PrngAlgo *prngAlgo, void *prngContext)
Diffie-Hellman key pair generation.
Definition: dh.c:119
Key exchange context.
Definition: ike.h:1815
bool_t ikeIsEcdhKeyExchangeAlgo(uint16_t groupNum)
Test if the group number identifies an ECDH key exchange algorithm.
Diffie-Hellman groups.
const IkeDhGroup * ikeGetDhGroup(uint16_t groupNum)
Get the Diffie-Hellman group that matches the specified group number.
void dhInit(DhContext *context)
Initialize Diffie-Hellman context.
Definition: dh.c:54
error_t ecdhExportPublicKey(EcdhContext *context, uint8_t *output, size_t *written, EcPublicKeyFormat format)
Export our own public key.
Definition: ecdh.c:257
IKEv2 algorithm negotiation.
Debugging facilities.
error_t ecdhGenerateKeyPair(EcdhContext *context, const PrngAlgo *prngAlgo, void *prngContext)
ECDH key pair generation.
Definition: ecdh.c:115