esp.h
Go to the documentation of this file.
1 /**
2  * @file esp.h
3  * @brief ESP (IP Encapsulating Security Payload)
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 #ifndef _ESP_H
32 #define _ESP_H
33 
34 //Dependencies
35 #include "ipsec/ipsec.h"
36 
37 //ESP support
38 #ifndef ESP_SUPPORT
39  #define ESP_SUPPORT ENABLED
40 #elif (ESP_SUPPORT != ENABLED && ESP_SUPPORT != DISABLED)
41  #error ESP_SUPPORT parameter is not valid
42 #endif
43 
44 //UDP encapsulation of IPsec ESP packets
45 #ifndef ESP_UDP_ENCAPS_SUPPORT
46  #define ESP_UDP_ENCAPS_SUPPORT DISABLED
47 #elif (ESP_UDP_ENCAPS_SUPPORT != ENABLED && ESP_UDP_ENCAPS_SUPPORT != DISABLED)
48  #error ESP_UDP_ENCAPS_SUPPORT parameter is not valid
49 #endif
50 
51 //Extended Sequence Numbers support
52 #ifndef ESP_ESN_SUPPORT
53  #define ESP_ESN_SUPPORT ENABLED
54 #elif (ESP_ESN_SUPPORT != ENABLED && ESP_ESN_SUPPORT != DISABLED)
55  #error ESP_ESN_SUPPORT parameter is not valid
56 #endif
57 
58 //NULL cipher support (insecure)
59 #ifndef ESP_NULL_SUPPORT
60  #define ESP_NULL_SUPPORT DISABLED
61 #elif (ESP_NULL_SUPPORT != ENABLED && ESP_NULL_SUPPORT != DISABLED)
62  #error ESP_NULL_SUPPORT parameter is not valid
63 #endif
64 
65 //CBC cipher mode support
66 #ifndef ESP_CBC_SUPPORT
67  #define ESP_CBC_SUPPORT ENABLED
68 #elif (ESP_CBC_SUPPORT != ENABLED && ESP_CBC_SUPPORT != DISABLED)
69  #error ESP_CBC_SUPPORT parameter is not valid
70 #endif
71 
72 //CTR cipher mode support
73 #ifndef ESP_CTR_SUPPORT
74  #define ESP_CTR_SUPPORT DISABLED
75 #elif (ESP_CTR_SUPPORT != ENABLED && ESP_CTR_SUPPORT != DISABLED)
76  #error ESP_CTR_SUPPORT parameter is not valid
77 #endif
78 
79 //CCM_8 AEAD support
80 #ifndef ESP_CCM_8_SUPPORT
81  #define ESP_CCM_8_SUPPORT DISABLED
82 #elif (ESP_CCM_8_SUPPORT != ENABLED && ESP_CCM_8_SUPPORT != DISABLED)
83  #error ESP_CCM_8_SUPPORT parameter is not valid
84 #endif
85 
86 //CCM_12 AEAD support
87 #ifndef ESP_CCM_12_SUPPORT
88  #define ESP_CCM_12_SUPPORT DISABLED
89 #elif (ESP_CCM_12_SUPPORT != ENABLED && ESP_CCM_12_SUPPORT != DISABLED)
90  #error ESP_CCM_12_SUPPORT parameter is not valid
91 #endif
92 
93 //CCM_16 AEAD support
94 #ifndef ESP_CCM_16_SUPPORT
95  #define ESP_CCM_16_SUPPORT DISABLED
96 #elif (ESP_CCM_16_SUPPORT != ENABLED && ESP_CCM_16_SUPPORT != DISABLED)
97  #error ESP_CCM_16_SUPPORT parameter is not valid
98 #endif
99 
100 //GCM_8 AEAD support
101 #ifndef ESP_GCM_8_SUPPORT
102  #define ESP_GCM_8_SUPPORT DISABLED
103 #elif (ESP_GCM_8_SUPPORT != ENABLED && ESP_GCM_8_SUPPORT != DISABLED)
104  #error ESP_GCM_8_SUPPORT parameter is not valid
105 #endif
106 
107 //GCM_12 AEAD support
108 #ifndef ESP_GCM_12_SUPPORT
109  #define ESP_GCM_12_SUPPORT DISABLED
110 #elif (ESP_GCM_12_SUPPORT != ENABLED && ESP_GCM_12_SUPPORT != DISABLED)
111  #error ESP_GCM_12_SUPPORT parameter is not valid
112 #endif
113 
114 //GCM_16 AEAD support
115 #ifndef ESP_GCM_16_SUPPORT
116  #define ESP_GCM_16_SUPPORT ENABLED
117 #elif (ESP_GCM_16_SUPPORT != ENABLED && ESP_GCM_16_SUPPORT != DISABLED)
118  #error ESP_GCM_16_SUPPORT parameter is not valid
119 #endif
120 
121 //ChaCha20Poly1305 AEAD support
122 #ifndef ESP_CHACHA20_POLY1305_SUPPORT
123  #define ESP_CHACHA20_POLY1305_SUPPORT ENABLED
124 #elif (ESP_CHACHA20_POLY1305_SUPPORT != ENABLED && ESP_CHACHA20_POLY1305_SUPPORT != DISABLED)
125  #error ESP_CHACHA20_POLY1305_SUPPORT parameter is not valid
126 #endif
127 
128 //CMAC integrity support
129 #ifndef ESP_CMAC_SUPPORT
130  #define ESP_CMAC_SUPPORT DISABLED
131 #elif (ESP_CMAC_SUPPORT != ENABLED && ESP_CMAC_SUPPORT != DISABLED)
132  #error ESP_CMAC_SUPPORT parameter is not valid
133 #endif
134 
135 //GMAC integrity support
136 #ifndef ESP_GMAC_SUPPORT
137  #define ESP_GMAC_SUPPORT DISABLED
138 #elif (ESP_GMAC_SUPPORT != ENABLED && ESP_GMAC_SUPPORT != DISABLED)
139  #error ESP_GMAC_SUPPORT parameter is not valid
140 #endif
141 
142 //HMAC integrity support
143 #ifndef ESP_HMAC_SUPPORT
144  #define ESP_HMAC_SUPPORT ENABLED
145 #elif (ESP_HMAC_SUPPORT != ENABLED && ESP_HMAC_SUPPORT != DISABLED)
146  #error ESP_HMAC_SUPPORT parameter is not valid
147 #endif
148 
149 //KMAC128 integrity support (experimental)
150 #ifndef ESP_KMAC128_SUPPORT
151  #define ESP_KMAC128_SUPPORT DISABLED
152 #elif (ESP_KMAC128_SUPPORT != ENABLED && ESP_KMAC128_SUPPORT != DISABLED)
153  #error ESP_KMAC128_SUPPORT parameter is not valid
154 #endif
155 
156 //KMAC256 integrity support (experimental)
157 #ifndef ESP_KMAC256_SUPPORT
158  #define ESP_KMAC256_SUPPORT DISABLED
159 #elif (ESP_KMAC256_SUPPORT != ENABLED && ESP_KMAC256_SUPPORT != DISABLED)
160  #error ESP_KMAC256_SUPPORT parameter is not valid
161 #endif
162 
163 //IDEA cipher support (insecure)
164 #ifndef ESP_IDEA_SUPPORT
165  #define ESP_IDEA_SUPPORT DISABLED
166 #elif (ESP_IDEA_SUPPORT != ENABLED && ESP_IDEA_SUPPORT != DISABLED)
167  #error ESP_IDEA_SUPPORT parameter is not valid
168 #endif
169 
170 //DES cipher support (insecure)
171 #ifndef ESP_DES_SUPPORT
172  #define ESP_DES_SUPPORT DISABLED
173 #elif (ESP_DES_SUPPORT != ENABLED && ESP_DES_SUPPORT != DISABLED)
174  #error ESP_DES_SUPPORT parameter is not valid
175 #endif
176 
177 //Triple DES cipher support (weak)
178 #ifndef ESP_3DES_SUPPORT
179  #define ESP_3DES_SUPPORT DISABLED
180 #elif (ESP_3DES_SUPPORT != ENABLED && ESP_3DES_SUPPORT != DISABLED)
181  #error ESP_3DES_SUPPORT parameter is not valid
182 #endif
183 
184 //AES 128-bit cipher support
185 #ifndef ESP_AES_128_SUPPORT
186  #define ESP_AES_128_SUPPORT ENABLED
187 #elif (ESP_AES_128_SUPPORT != ENABLED && ESP_AES_128_SUPPORT != DISABLED)
188  #error ESP_AES_128_SUPPORT parameter is not valid
189 #endif
190 
191 //AES 192-bit cipher support
192 #ifndef ESP_AES_192_SUPPORT
193  #define ESP_AES_192_SUPPORT ENABLED
194 #elif (ESP_AES_192_SUPPORT != ENABLED && ESP_AES_192_SUPPORT != DISABLED)
195  #error ESP_AES_192_SUPPORT parameter is not valid
196 #endif
197 
198 //AES 256-bit cipher support
199 #ifndef ESP_AES_256_SUPPORT
200  #define ESP_AES_256_SUPPORT ENABLED
201 #elif (ESP_AES_256_SUPPORT != ENABLED && ESP_AES_256_SUPPORT != DISABLED)
202  #error ESP_AES_256_SUPPORT parameter is not valid
203 #endif
204 
205 //Camellia 128-bit cipher support
206 #ifndef ESP_CAMELLIA_128_SUPPORT
207  #define ESP_CAMELLIA_128_SUPPORT DISABLED
208 #elif (ESP_CAMELLIA_128_SUPPORT != ENABLED && ESP_CAMELLIA_128_SUPPORT != DISABLED)
209  #error ESP_CAMELLIA_128_SUPPORT parameter is not valid
210 #endif
211 
212 //Camellia 192-bit cipher support
213 #ifndef ESP_CAMELLIA_192_SUPPORT
214  #define ESP_CAMELLIA_192_SUPPORT DISABLED
215 #elif (ESP_CAMELLIA_192_SUPPORT != ENABLED && ESP_CAMELLIA_192_SUPPORT != DISABLED)
216  #error ESP_CAMELLIA_192_SUPPORT parameter is not valid
217 #endif
218 
219 //Camellia 256-bit cipher support
220 #ifndef ESP_CAMELLIA_256_SUPPORT
221  #define ESP_CAMELLIA_256_SUPPORT DISABLED
222 #elif (ESP_CAMELLIA_256_SUPPORT != ENABLED && ESP_CAMELLIA_256_SUPPORT != DISABLED)
223  #error ESP_CAMELLIA_256_SUPPORT parameter is not valid
224 #endif
225 
226 //SM4 cipher support (experimental)
227 #ifndef ESP_SM4_SUPPORT
228  #define ESP_SM4_SUPPORT DISABLED
229 #elif (ESP_SM4_SUPPORT != ENABLED && ESP_SM4_SUPPORT != DISABLED)
230  #error ESP_SM4_SUPPORT parameter is not valid
231 #endif
232 
233 //MD5 hash support (insecure)
234 #ifndef ESP_MD5_SUPPORT
235  #define ESP_MD5_SUPPORT DISABLED
236 #elif (ESP_MD5_SUPPORT != ENABLED && ESP_MD5_SUPPORT != DISABLED)
237  #error ESP_MD5_SUPPORT parameter is not valid
238 #endif
239 
240 //SHA-1 hash support (weak)
241 #ifndef ESP_SHA1_SUPPORT
242  #define ESP_SHA1_SUPPORT ENABLED
243 #elif (ESP_SHA1_SUPPORT != ENABLED && ESP_SHA1_SUPPORT != DISABLED)
244  #error ESP_SHA1_SUPPORT parameter is not valid
245 #endif
246 
247 //SHA-256 hash support
248 #ifndef ESP_SHA256_SUPPORT
249  #define ESP_SHA256_SUPPORT ENABLED
250 #elif (ESP_SHA256_SUPPORT != ENABLED && ESP_SHA256_SUPPORT != DISABLED)
251  #error ESP_SHA256_SUPPORT parameter is not valid
252 #endif
253 
254 //SHA-384 hash support
255 #ifndef ESP_SHA384_SUPPORT
256  #define ESP_SHA384_SUPPORT ENABLED
257 #elif (ESP_SHA384_SUPPORT != ENABLED && ESP_SHA384_SUPPORT != DISABLED)
258  #error ESP_SHA384_SUPPORT parameter is not valid
259 #endif
260 
261 //SHA-512 hash support
262 #ifndef ESP_SHA512_SUPPORT
263  #define ESP_SHA512_SUPPORT ENABLED
264 #elif (ESP_SHA512_SUPPORT != ENABLED && ESP_SHA512_SUPPORT != DISABLED)
265  #error ESP_SHA512_SUPPORT parameter is not valid
266 #endif
267 
268 //SM3 hash support (experimental)
269 #ifndef ESP_SM3_SUPPORT
270  #define ESP_SM3_SUPPORT DISABLED
271 #elif (ESP_SM3_SUPPORT != ENABLED && ESP_SM3_SUPPORT != DISABLED)
272  #error ESP_SM3_SUPPORT parameter is not valid
273 #endif
274 
275 //Size of the buffer for input/output operations
276 #ifndef ESP_BUFFER_SIZE
277  #define ESP_BUFFER_SIZE 2048
278 #elif (ESP_BUFFER_SIZE < 256)
279  #error ESP_BUFFER_SIZE parameter is not valid
280 #endif
281 
282 //Maximum digest size
283 #if (ESP_HMAC_SUPPORT == ENABLED && ESP_SHA512_SUPPORT == ENABLED)
284  #define ESP_MAX_DIGEST_SIZE 64
285 #elif (ESP_HMAC_SUPPORT == ENABLED && ESP_SHA384_SUPPORT == ENABLED)
286  #define ESP_MAX_DIGEST_SIZE 48
287 #elif (ESP_HMAC_SUPPORT == ENABLED && ESP_SHA256_SUPPORT == ENABLED)
288  #define ESP_MAX_DIGEST_SIZE 32
289 #else
290  #define ESP_MAX_DIGEST_SIZE 12
291 #endif
292 
293 //C++ guard
294 #ifdef __cplusplus
295 extern "C" {
296 #endif
297 
298 //CC-RX, CodeWarrior or Win32 compiler?
299 #if defined(__CCRX__)
300  #pragma pack
301 #elif defined(__CWCC__) || defined(_WIN32)
302  #pragma pack(push, 1)
303 #endif
304 
305 
306 /**
307  * @brief ESP header
308  **/
309 
311 {
312  uint32_t spi; //0-3
313  uint32_t seqNum; //4-7
314  uint8_t payloadData[]; //8
316 
317 
318 /**
319  * @brief ESP trailer
320  **/
321 
322 typedef __packed_struct
323 {
324  uint8_t padLength; //0
325  uint8_t nextHeader; //1
326  uint8_t icv[]; //2
328 
329 
330 //CC-RX, CodeWarrior or Win32 compiler?
331 #if defined(__CCRX__)
332  #pragma unpack
333 #elif defined(__CWCC__) || defined(_WIN32)
334  #pragma pack(pop)
335 #endif
336 
337 //ESP related functions
339  NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader,
340  uint16_t fragId, NetBuffer *buffer, size_t offset,
341  NetTxAncillary *ancillary);
342 
344  const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset,
345  NetRxAncillary *ancillary, bool_t udpEncapsulation);
346 
347 bool_t espIsUdpEncapsulatedPacket(const NetBuffer *buffer, size_t offset);
348 
349 void espDumpHeader(const EspHeader *espHeader);
350 
351 //C++ guard
352 #ifdef __cplusplus
353 }
354 #endif
355 
356 #endif
uint32_t seqNum
Definition: esp.h:313
bool_t espIsUdpEncapsulatedPacket(const NetBuffer *buffer, size_t offset)
Determine if a packet uses UDP-encapsulated ESP format.
Definition: esp.c:453
int bool_t
Definition: compiler_port.h:63
#define Ipv4Header
Definition: ipv4.h:36
uint32_t spi
Definition: ah.h:175
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
void espDumpHeader(const EspHeader *espHeader)
Dump ESP header for debugging purpose.
Definition: esp.c:496
typedef __packed_struct
ESP header.
Definition: esp.h:311
uint8_t icv[]
Definition: esp.h:326
error_t
Error codes.
Definition: error.h:43
error_t espProcessInboundIpv4Packet(NetInterface *interface, const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset, NetRxAncillary *ancillary, bool_t udpEncapsulation)
Process ESP protected packet.
Definition: esp.c:228
uint8_t nextHeader
Definition: esp.h:325
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
EspTrailer
Definition: esp.h:327
error_t espProtectOutboundIpv4Packet(IpsecContext *context, IpsecSadEntry *sa, NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, uint16_t fragId, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Protect an outbound IPv4 packet using ESP.
Definition: esp.c:66
uint8_t payloadData[]
Definition: esp.h:314
#define NetTxAncillary
Definition: net_misc.h:36
#define Ipv4PseudoHeader
Definition: ipv4.h:40
#define IpsecSadEntry
Definition: ipsec.h:36
EspHeader
Definition: esp.h:315
IPsec (IP security)
#define IpsecContext
Definition: ipsec.h:40