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