esp_packet_encrypt.c
Go to the documentation of this file.
1 /**
2  * @file esp_packet_encrypt.c
3  * @brief ESP packet encryption
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 ESP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ipsec/ipsec.h"
36 #include "esp/esp.h"
37 #include "esp/esp_packet_encrypt.h"
40 #include "aead/aead_algorithms.h"
41 #include "debug.h"
42 
43 //Check IPsec library configuration
44 #if (ESP_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Encrypt an outgoing ESP packet
49  * @param[in] context Pointer to the IPsec context
50  * @param[in] sa Pointer to the SAD entry
51  * @param[in] espHeader Pointer to the ESP header
52  * @param[in,out] payload Payload data to be encrypted
53  * @param[in,out] payloadLen Actual length of the payload data
54  * @param[in] nextHeader Value of the next header field
55  * @return Error code
56  **/
57 
59  const EspHeader *espHeader, uint8_t *payload, size_t *payloadLen,
60  uint8_t nextHeader)
61 {
62  error_t error;
63  size_t length;
64  uint8_t *data;
65  uint8_t *icv;
66  const CipherAlgo *cipherAlgo;
67 
68  //Get the length of the payload data, in bytes
69  length = *payloadLen;
70 
71  //Point to the plaintext
72  data = payload + sa->ivLen;
73 
74  //The transmitted ESP trailer consists of the Padding, Pad Length, and
75  //Next Header fields (refer to RFC 4303, section 2)
77 
78  //Point to the Integrity Checksum Value (ICV)
79  icv = payload + sa->ivLen + length;
80 
81  //The SAD entry specifies the algorithms and keys to be employed for
82  //encryption (refer to RFC 4303, section 3.4.2)
83  cipherAlgo = sa->cipherAlgo;
84 
85 #if (ESP_NULL_SUPPORT == ENABLED)
86  //Integrity-only ESP?
87  if(sa->cipherMode == CIPHER_MODE_NULL)
88  {
89  //No confidentiality is offered by using the NULL encryption algorithm
90  error = espComputeChecksum(context, sa, espHeader, payload, length, icv);
91  //Any error to report?
92  if(error)
93  return error;
94  }
95  else
96 #endif
97 #if (ESP_CBC_SUPPORT == ENABLED)
98  //CBC cipher mode?
99  if(sa->cipherMode == CIPHER_MODE_CBC)
100  {
101  uint8_t iv[MAX_CIPHER_BLOCK_SIZE];
102 
103  //The IV must be chosen at random, and must be unpredictable
104  error = context->prngAlgo->generate(context->prngContext, iv, sa->ivLen);
105  //Any error to report?
106  if(error)
107  return error;
108 
109  //The IV field must be the same size as the block size of the cipher
110  //algorithm being used
111  osMemcpy(payload, iv, sa->ivLen);
112 
113  //Initialize cipher context
114  error = cipherAlgo->init(&sa->cipherContext, sa->encKey, sa->encKeyLen);
115  //Any error to report?
116  if(error)
117  return error;
118 
119  //Perform CBC encryption
120  error = cbcEncrypt(cipherAlgo, &sa->cipherContext, iv, data, data,
121  length);
122  //Any error to report?
123  if(error)
124  return error;
125 
126  //The ICV is a variable-length field computed over the ESP header,
127  //Payload, and ESP trailer fields (refer to RFC 4303, section 2.8)
128  error = espComputeChecksum(context, sa, espHeader, payload,
129  sa->ivLen + length, icv);
130  //Any error to report?
131  if(error)
132  return error;
133  }
134  else
135 #endif
136 #if (ESP_CTR_SUPPORT == ENABLED)
137  //CTR cipher mode?
138  if(sa->cipherMode == CIPHER_MODE_CTR)
139  {
140  uint8_t counter[16];
141 
142  //The IV must be chosen by the encryptor in a manner that ensures that
143  //the same IV value is used only once for a given key (refer to RFC 3686,
144  //section 3.1)
145  espGenerateIv(sa->iv);
146 
147  //The IV field must be 8 octets when the AES-CTR algorithm is used for
148  //IKEv2 encryption
149  osMemcpy(payload, sa->iv, 8);
150 
151  //The counter block is 128 bits, including a 4-octet nonce, 8-octet IV,
152  //and 4-octet block counter, in that order (refer to RFC 3686, section 4)
153  osMemcpy(counter, sa->encKey + sa->encKeyLen, 4);
154  osMemcpy(counter + 4, sa->iv, 8);
155 
156  //The block counter begins with the value of one and increments by one
157  //to generate the next portion of the key stream
158  STORE32BE(1, counter + 12);
159 
160  //Initialize cipher context
161  error = cipherAlgo->init(&sa->cipherContext, sa->encKey, sa->encKeyLen);
162  //Any error to report?
163  if(error)
164  return error;
165 
166  //Perform CTR encryption
167  error = ctrEncrypt(cipherAlgo, &sa->cipherContext,
168  cipherAlgo->blockSize * 8, counter, data, data, length);
169  //Any error to report?
170  if(error)
171  return error;
172 
173  //The ICV is a variable-length field computed over the ESP header,
174  //Payload, and ESP trailer fields (refer to RFC 4303, section 2.8)
175  error = espComputeChecksum(context, sa, espHeader, payload,
176  sa->ivLen + length, icv);
177  //Any error to report?
178  if(error)
179  return error;
180  }
181  else
182 #endif
183 #if (ESP_CCM_8_SUPPORT == ENABLED || ESP_CCM_12_SUPPORT == ENABLED || \
184  ESP_CCM_16_SUPPORT == ENABLED)
185  //CCM AEAD cipher?
186  if(sa->cipherMode == CIPHER_MODE_CCM)
187  {
188  size_t aadLen;
189  uint8_t aad[12];
190  uint8_t nonce[11];
191 
192  //The IV must be chosen by the encryptor in a manner that ensures that
193  //the same IV value is used only once for a given key (refer to RFC 4309,
194  //section 3.1)
195  espGenerateIv(sa->iv);
196 
197  //The Initialization Vector (IV) must be eight octets
198  osMemcpy(payload, sa->iv, 8);
199 
200  //The components of the nonce are the salt with the IV (refer to RFC 4309,
201  //section 4)
202  osMemcpy(nonce, sa->encKey + sa->encKeyLen, 3);
203  osMemcpy(nonce + 3, sa->iv, 8);
204 
205  //Two formats of the AAD are defined (refer to RFC 4309, section 5)
206  if(sa->esn)
207  {
208  //AAD Format with 64-bit sequence number
209  osMemcpy(aad, (uint8_t *) &espHeader->spi, 4);
210  STORE64BE(sa->seqNum, aad + 4);
211  aadLen = 12;
212  }
213  else
214  {
215  //AAD Format with 32-bit sequence number
216  osMemcpy(aad, espHeader, 8);
217  aadLen = 8;
218  }
219 
220  //Initialize cipher context
221  error = cipherAlgo->init(&sa->cipherContext, sa->encKey, sa->encKeyLen);
222  //Any error to report?
223  if(error)
224  return error;
225 
226  //Authenticated encryption using CCM
227  error = ccmEncrypt(sa->cipherAlgo, &sa->cipherContext, nonce, 11, aad,
228  aadLen, data, data, length, icv, sa->icvLen);
229  //Any error to report?
230  if(error)
231  return error;
232  }
233  else
234 #endif
235 #if (ESP_GCM_8_SUPPORT == ENABLED || ESP_GCM_12_SUPPORT == ENABLED || \
236  ESP_GCM_16_SUPPORT == ENABLED)
237  //GCM AEAD cipher?
238  if(sa->cipherMode == CIPHER_MODE_GCM)
239  {
240  size_t aadLen;
241  uint8_t aad[12];
242  uint8_t nonce[12];
243  GcmContext gcmContext;
244 
245  //For a given key, the IV must not repeat. The encrypter can use any IV
246  //generation method that meets the uniqueness requirement, without
247  //coordinating with the decrypter (refer to RFC 4106, section 3.1)
248  espGenerateIv(sa->iv);
249 
250  //The Initialization Vector (IV) must be eight octets
251  osMemcpy(payload, sa->iv, 8);
252 
253  //The components of the nonce are the salt with the IV (refer to RFC 4106,
254  //section 4)
255  osMemcpy(nonce, sa->encKey + sa->encKeyLen, 4);
256  osMemcpy(nonce + 4, sa->iv, 8);
257 
258  //Two formats of the AAD are defined (refer to RFC 4106, section 5)
259  if(sa->esn)
260  {
261  //AAD Format with 64-bit sequence number
262  osMemcpy(aad, (uint8_t *) &espHeader->spi, 4);
263  STORE64BE(sa->seqNum, aad + 4);
264  aadLen = 12;
265  }
266  else
267  {
268  //AAD Format with 32-bit sequence number
269  osMemcpy(aad, espHeader, 8);
270  aadLen = 8;
271  }
272 
273  //Initialize cipher context
274  error = cipherAlgo->init(&sa->cipherContext, sa->encKey, sa->encKeyLen);
275  //Any error to report?
276  if(error)
277  return error;
278 
279  //Initialize GCM context
280  error = gcmInit(&gcmContext, sa->cipherAlgo, &sa->cipherContext);
281  //Any error to report?
282  if(error)
283  return error;
284 
285  //Authenticated encryption using GCM
286  error = gcmEncrypt(&gcmContext, nonce, 12, aad, aadLen, data, data,
287  length, icv, sa->icvLen);
288  //Any error to report?
289  if(error)
290  return error;
291  }
292  else
293 #endif
294 #if (ESP_CHACHA20_POLY1305_SUPPORT == ENABLED)
295  //ChaCha20Poly1305 AEAD cipher?
296  if(sa->cipherMode == CIPHER_MODE_CHACHA20_POLY1305)
297  {
298  size_t aadLen;
299  uint8_t aad[12];
300  uint8_t nonce[12];
301 
302  //The IV must be unique for each invocation for a particular security
303  //association (SA) but does not need to be unpredictable (refer to
304  //RFC 7634, section 2)
305  espGenerateIv(sa->iv);
306 
307  //The IV is 64 bits, and is included explicitly in the Encrypted payload
308  osMemcpy(payload, sa->iv, 8);
309 
310  //The 96-bit nonce is formed from a concatenation of the 32-bit salt and
311  //the 64-bit IV (refer to RFC 7634, section 2)
312  osMemcpy(nonce, sa->encKey + sa->encKeyLen, 4);
313  osMemcpy(nonce + 4, sa->iv, 8);
314 
315  //Extended sequence numbers?
316  if(sa->esn)
317  {
318  //For SAs with ESN, the AAD is 12 octets: a 4-octet SPI followed by an
319  //8-octet sequence number as a 64-bit integer in big-endian byte order
320  //(refer to RFC 7634, section 2.1)
321  osMemcpy(aad, (uint8_t *) &espHeader->spi, 4);
322  STORE64BE(sa->seqNum, aad + 4);
323  aadLen = 12;
324  }
325  else
326  {
327  //For SAs with 32-bit sequence numbers, the AAD is 8 octets: a 4-octet
328  //SPI followed by a 4-octet sequence number ordered exactly as it is in
329  //the packet
330  osMemcpy(aad, espHeader, 8);
331  aadLen = 8;
332  }
333 
334  //Authenticated encryption using ChaCha20Poly1305
335  error = chacha20Poly1305Encrypt(sa->encKey, sa->encKeyLen, nonce, 12,
336  aad, aadLen, data, data, length, icv, sa->icvLen);
337  //Any error to report?
338  if(error)
339  return error;
340  }
341  else
342 #endif
343 #if (ESP_NULL_SUPPORT == ENABLED && ESP_GMAC_SUPPORT == ENABLED)
344  //GMAC authentication algorithm?
345  if(sa->cipherMode == CIPHER_MODE_GMAC)
346  {
347  uint8_t temp[8];
348  uint8_t nonce[12];
349  GmacContext gmacContext;
350 
351  //For a given key, the IV must not repeat. The encrypter can use any IV
352  //generation method that meets the uniqueness requirement, without
353  //coordinating with the receiver (refer to RFC 4543, section 3.1)
354  espGenerateIv(sa->iv);
355 
356  //The Initialization Vector (IV) must be eight octets
357  osMemcpy(payload, sa->iv, 8);
358 
359  //The components of the nonce are the salt with the IV (refer to RFC 4543,
360  //section 3.2)
361  osMemcpy(nonce, sa->encKey + sa->encKeyLen, 4);
362  osMemcpy(nonce + 4, sa->iv, 8);
363 
364  //Initialize GMAC context
365  error = gmacInit(&gmacContext, sa->cipherAlgo, sa->encKey, sa->encKeyLen);
366  //Any error to report?
367  if(error)
368  return error;
369 
370  //The nonce is passed to the AES-GMAC authentication algorithm
371  error = gmacReset(&gmacContext, nonce, 12);
372  //Any error to report?
373  if(error)
374  return error;
375 
376  //Two formats of the AAD are defined (refer to RFC 4543, section 3.3)
377  if(sa->esn)
378  {
379  //Convert the 64-bit sequence number to network byte order
380  STORE64BE(sa->seqNum, temp);
381 
382  //The AAD consists of the SPI, 64-bit sequence number, and ESP payload
383  gmacUpdate(&gmacContext, (uint8_t *) &espHeader->spi, 4);
384  gmacUpdate(&gmacContext, temp, 8);
385  gmacUpdate(&gmacContext, sa->iv, 8);
386  gmacUpdate(&gmacContext, data, length);
387  }
388  else
389  {
390  //The AAD consists of the SPI, 32-bit sequence number, and ESP payload
391  gmacUpdate(&gmacContext, espHeader, sizeof(EspHeader));
392  gmacUpdate(&gmacContext, sa->iv, 8);
393  gmacUpdate(&gmacContext, data, length);
394  }
395 
396  //The authentication tag must not be truncated, so the length of the ICV
397  //is 16 octets (refer to RFC 4543, section 3.4)
398  error = gmacFinal(&gmacContext, icv, sa->icvLen);
399  //Any error to report?
400  if(error)
401  return error;
402 
403  }
404  else
405 #endif
406  //Invalid cipher mode?
407  {
408  //The specified cipher mode is not supported
410  }
411 
412  //Total length of the encrypted packet
413  *payloadLen = sa->ivLen + length + sa->icvLen;
414 
415  //Successful processing
416  return NO_ERROR;
417 }
418 
419 
420 /**
421  * @brief Compute ICV checksum
422  * @param[in] context Pointer to the IPsec context
423  * @param[in] sa Pointer to the SAD entry
424  * @param[in] espHeader Pointer to the ESP header
425  * @param[in] payload Pointer to the payload data
426  * @param[in] length Length of the packet, in bytes
427  * @param[out] icv Integrity Checksum Value (ICV)
428  * @return Error code
429  **/
430 
432  const EspHeader *espHeader, const uint8_t *payload, size_t length,
433  uint8_t *icv)
434 {
435  error_t error;
436 
437 #if (ESP_CMAC_SUPPORT == ENABLED)
438  //CMAC integrity algorithm?
439  if(sa->authMacAlgo == MAC_ALGO_CMAC)
440  {
441  CmacContext *cmacContext;
442 
443  //Point to the CMAC context
444  cmacContext = &context->macContext.cmacContext;
445 
446  //The SAD entry specifies the algorithm employed for ICV computation
447  error = cmacInit(cmacContext, sa->authCipherAlgo, sa->authKey,
448  sa->authKeyLen);
449 
450  //Check status code
451  if(!error)
452  {
453  //The checksum must be computed over the encrypted message. Its length
454  //is determined by the integrity algorithm negotiated
455  cmacUpdate(cmacContext, espHeader, sizeof(EspHeader));
456  cmacUpdate(cmacContext, payload, length);
457 
458  //Extended sequence number?
459  if(sa->esn)
460  {
461  //The high-order 32 bits are maintained as part of the sequence
462  //number counter by both transmitter and receiver and are included
463  //in the computation of the ICV (refer to RFC 4303, section 2.2.1)
464  uint32_t h = htonl(sa->seqNum >> 32);
465  cmacUpdate(cmacContext, (uint8_t *) &h, 4);
466  }
467 
468  //Finalize CMAC computation
469  cmacFinal(cmacContext, icv, sa->icvLen);
470  }
471  }
472  else
473 #endif
474 #if (ESP_HMAC_SUPPORT == ENABLED)
475  //HMAC integrity algorithm?
476  if(sa->authMacAlgo == MAC_ALGO_HMAC)
477  {
478  HmacContext *hmacContext;
479 
480  //Point to the HMAC context
481  hmacContext = &context->macContext.hmacContext;
482 
483  //The SAD entry specifies the algorithm employed for ICV computation
484  error = hmacInit(hmacContext, sa->authHashAlgo, sa->authKey,
485  sa->authKeyLen);
486 
487  //Check status code
488  if(!error)
489  {
490  //The checksum must be computed over the encrypted message. Its length
491  //is determined by the integrity algorithm negotiated
492  hmacUpdate(hmacContext, espHeader, sizeof(EspHeader));
493  hmacUpdate(hmacContext, payload, length);
494 
495  //Extended sequence number?
496  if(sa->esn)
497  {
498  //The high-order 32 bits are maintained as part of the sequence
499  //number counter by both transmitter and receiver and are included
500  //in the computation of the ICV (refer to RFC 4303, section 2.2.1)
501  uint32_t h = htonl(sa->seqNum >> 32);
502  hmacUpdate(hmacContext, (uint8_t *) &h, 4);
503  }
504 
505  //Finalize HMAC computation
506  hmacFinal(hmacContext, NULL);
507 
508  //Copy the resulting checksum value
509  osMemcpy(icv, hmacContext->digest, sa->icvLen);
510  }
511  }
512  else
513 #endif
514  //Unknown integrity algorithm?
515  {
516  //Report an error
517  error = ERROR_FAILURE;
518  }
519 
520  //Return status code
521  return error;
522 }
523 
524 
525 /**
526  * @brief Compute the number of padding bytes
527  * @param[in] sa Pointer to the SAD entry
528  * @param[in] length Length of the payload data, in bytes
529  * @return Number of padding bytes
530  **/
531 
533 {
534  size_t n;
535 
536 #if (ESP_CBC_SUPPORT == ENABLED)
537  //CBC cipher mode?
538  if(sa->cipherMode == CIPHER_MODE_CBC)
539  {
540  //For the purpose of ensuring that data to be encrypted are a multiple
541  //of the algorithm's block size, the padding computation applies to the
542  //Payload Data exclusive of any IV, but including the ESP trailer fields
543  //(refer to RFC 4303, section 2.4)
544  n = (length + sizeof(EspTrailer)) % sa->cipherAlgo->blockSize;
545 
546  //Check whether any padding is required
547  if(n != 0)
548  {
549  //Calculate the length of the Padding field
550  n = sa->cipherAlgo->blockSize - n;
551  }
552  }
553  else
554 #endif
555  //AEAD cipher mode?
556  {
557  //Implementations that do not seek to hide the length of the plaintext
558  //should use the minimum amount of padding required, which will be less
559  //than four octets (refer to RFC 4106, section 3.2)
560  n = (length + sizeof(EspTrailer)) % 4;
561 
562  //Check whether any padding is required
563  if(n != 0)
564  {
565  //Calculate the length of the Padding field
566  n = 4 - n;
567  }
568  }
569 
570  //Return the number of padding bytes
571  return n;
572 }
573 
574 
575 /**
576  * @brief Append ESP trailer
577  * @param[in] sa Pointer to the SAD entry
578  * @param[in] data Pointer to the payload data
579  * @param[in] length Length of the payload data, in bytes
580  * @param[in] nextHeader Value of the next header field
581  * @return Length of the resulting payload data
582  **/
583 
584 size_t espAddTrailer(IpsecSadEntry *sa, uint8_t *data, size_t length,
585  uint8_t nextHeader)
586 {
587  size_t i;
588  size_t n;
589  EspTrailer *espTrailer;
590 
591  //The sender may add 0 to 255 bytes of padding
593 
594  //Padding bytes make up a monotonically increasing sequence
595  for(i = 1; i <= n; i++)
596  {
597  data[length++] = (uint8_t) i;
598  }
599 
600  //Point to the ESP trailer
601  espTrailer = (EspTrailer *) (data + length);
602 
603  //The Pad Length field indicates the number of pad bytes immediately
604  //preceding it in the Padding field (refer to RFC 4303, section 2.5)
605  espTrailer->padLength = n;
606 
607  //The Next Header field identifies the type of data contained in the
608  //payload data (refer to RFC 4303, section 2.6)
609  espTrailer->nextHeader = nextHeader;
610 
611  //Return the length of the resulting payload data
612  return length + sizeof(EspTrailer);
613 }
614 
615 
616 /**
617  * @brief IV generation
618  * @param[in,out] iv Pointer to the 8-octet initialization vector
619  **/
620 
621 void espGenerateIv(uint8_t *iv)
622 {
623  uint16_t temp;
624 
625  //The encryptor may generate the IV in any manner that ensures uniqueness.
626  //Common approaches to IV generation include incrementing a counter for each
627  //packet and linear feedback shift registers (refer to RFC 5282, section 3.1)
628  temp = iv[7] + 1;
629  iv[7] = temp & 0xFF;
630  temp = (temp >> 8) + iv[6];
631  iv[6] = temp & 0xFF;
632  temp = (temp >> 8) + iv[5];
633  iv[5] = temp & 0xFF;
634  temp = (temp >> 8) + iv[4];
635  iv[4] = temp & 0xFF;
636  temp = (temp >> 8) + iv[3];
637  iv[3] = temp & 0xFF;
638  temp = (temp >> 8) + iv[2];
639  iv[2] = temp & 0xFF;
640  temp = (temp >> 8) + iv[1];
641  iv[1] = temp & 0xFF;
642  temp = (temp >> 8) + iv[0];
643  iv[0] = temp & 0xFF;
644 }
645 
646 #endif
error_t espEncryptPacket(IpsecContext *context, IpsecSadEntry *sa, const EspHeader *espHeader, uint8_t *payload, size_t *payloadLen, uint8_t nextHeader)
Encrypt an outgoing ESP packet.
@ MAC_ALGO_CMAC
@ CIPHER_MODE_CBC
Definition: cipher_modes.h:82
uint8_t icv[]
Definition: ah.h:177
HMAC algorithm context.
Definition: hmac.h:59
__weak_func error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
Definition: cbc.c:61
Collection of cipher algorithms.
uint8_t data[]
Definition: ethernet.h:224
@ CIPHER_MODE_CTR
Definition: cipher_modes.h:85
__weak_func error_t ccmEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *n, size_t nLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using CCM.
Definition: ccm.c:67
size_t blockSize
Definition: crypto.h:1289
@ CIPHER_MODE_NULL
Definition: cipher_modes.h:79
__weak_func error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
Definition: gcm.c:99
size_t espAddTrailer(IpsecSadEntry *sa, uint8_t *data, size_t length, uint8_t nextHeader)
Append ESP trailer.
error_t gmacInit(GmacContext *context, const CipherAlgo *cipher, const void *key, size_t keyLen)
Initialize GMAC calculation.
Definition: gmac.c:160
#define MAX_CIPHER_BLOCK_SIZE
CipherAlgoInit init
Definition: crypto.h:1290
ESP (IP Encapsulating Security Payload)
@ ERROR_UNSUPPORTED_CIPHER_MODE
Definition: error.h:128
@ CIPHER_MODE_GMAC
Definition: cipher_modes.h:88
uint8_t h
Definition: ndp.h:302
@ CIPHER_MODE_CHACHA20_POLY1305
Definition: cipher_modes.h:91
#define htonl(value)
Definition: cpu_endian.h:414
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
error_t
Error codes.
Definition: error.h:43
error_t chacha20Poly1305Encrypt(const uint8_t *k, size_t kLen, const uint8_t *n, size_t nLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using ChaCha20Poly1305.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ CIPHER_MODE_GCM
Definition: cipher_modes.h:87
size_t espComputePadLength(IpsecSadEntry *sa, size_t length)
Compute the number of padding bytes.
error_t espComputeChecksum(IpsecContext *context, IpsecSadEntry *sa, const EspHeader *espHeader, const uint8_t *payload, size_t length, uint8_t *icv)
Compute ICV checksum.
__weak_func error_t ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m, uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
CTR encryption.
Definition: ctr.c:62
EspTrailer
Definition: esp.h:327
uint8_t nextHeader
Definition: ipv6.h:304
ESP packet encryption.
uint8_t iv[]
Definition: ike.h:1695
Block cipher modes of operation.
const CipherAlgo * cipherAlgo
Cipher algorithm.
Definition: gcm.h:65
uint8_t length
Definition: tcp.h:375
CMAC algorithm context.
Definition: cmac.h:54
#define IpsecSadEntry
Definition: ipsec.h:36
EspHeader
Definition: esp.h:315
__weak_func void hmacUpdate(HmacContext *context, const void *data, size_t length)
Update the HMAC context with a portion of the message being hashed.
Definition: hmac.c:201
Collection of AEAD algorithms.
GCM context.
Definition: gcm.h:64
#define STORE64BE(a, p)
Definition: cpu_endian.h:322
uint8_t digest[MAX_HASH_DIGEST_SIZE]
Definition: hmac.h:63
@ CIPHER_MODE_CCM
Definition: cipher_modes.h:86
IPsec (IP security)
uint8_t n
uint8_t payload[]
Definition: ipv6.h:308
__weak_func void hmacFinal(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation.
Definition: hmac.c:218
void gmacUpdate(GmacContext *context, const void *data, size_t dataLen)
Update the GMAC context with a portion of the message being hashed.
Definition: gmac.c:361
#define IpsecContext
Definition: ipsec.h:40
Common interface for encryption algorithms.
Definition: crypto.h:1285
@ MAC_ALGO_HMAC
GMAC algorithm context.
Definition: gmac.h:70
error_t cmacInit(CmacContext *context, const CipherAlgo *cipher, const void *key, size_t keyLen)
Initialize CMAC calculation.
Definition: cmac.c:107
void cmacUpdate(CmacContext *context, const void *data, size_t dataLen)
Update the CMAC context with a portion of the message being hashed.
Definition: cmac.c:191
error_t gmacReset(GmacContext *context, const uint8_t *iv, size_t ivLen)
Reset GMAC context.
Definition: gmac.c:284
uint16_t payloadLen
Definition: ipv6.h:303
error_t gmacFinal(GmacContext *context, uint8_t *mac, size_t macLen)
Finish the GMAC calculation.
Definition: gmac.c:404
__weak_func error_t hmacInit(HmacContext *context, const HashAlgo *hash, const void *key, size_t keyLen)
Initialize HMAC calculation.
Definition: hmac.c:140
error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen)
Finish the CMAC calculation.
Definition: cmac.c:237
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
__weak_func error_t gcmEncrypt(GcmContext *context, const uint8_t *iv, size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using GCM.
Definition: gcm.c:209
uint8_t nonce[]
Definition: ntp_common.h:239
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void espGenerateIv(uint8_t *iv)
IV generation.