ah.h
Go to the documentation of this file.
1 /**
2  * @file ah.h
3  * @brief AH (IP Authentication Header)
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 _AH_H
32 #define _AH_H
33 
34 //Dependencies
35 #include "ipsec/ipsec.h"
36 
37 //AH support
38 #ifndef AH_SUPPORT
39  #define AH_SUPPORT DISABLED
40 #elif (AH_SUPPORT != ENABLED && AH_SUPPORT != DISABLED)
41  #error AH_SUPPORT parameter is not valid
42 #endif
43 
44 //Extended Sequence Numbers support
45 #ifndef AH_ESN_SUPPORT
46  #define AH_ESN_SUPPORT ENABLED
47 #elif (AH_ESN_SUPPORT != ENABLED && AH_ESN_SUPPORT != DISABLED)
48  #error AH_ESN_SUPPORT parameter is not valid
49 #endif
50 
51 //CMAC integrity support
52 #ifndef AH_CMAC_SUPPORT
53  #define AH_CMAC_SUPPORT DISABLED
54 #elif (AH_CMAC_SUPPORT != ENABLED && AH_CMAC_SUPPORT != DISABLED)
55  #error AH_CMAC_SUPPORT parameter is not valid
56 #endif
57 
58 //HMAC integrity support
59 #ifndef AH_HMAC_SUPPORT
60  #define AH_HMAC_SUPPORT ENABLED
61 #elif (AH_HMAC_SUPPORT != ENABLED && AH_HMAC_SUPPORT != DISABLED)
62  #error AH_HMAC_SUPPORT parameter is not valid
63 #endif
64 
65 //KMAC128 integrity support (experimental)
66 #ifndef AH_KMAC128_SUPPORT
67  #define AH_KMAC128_SUPPORT DISABLED
68 #elif (AH_KMAC128_SUPPORT != ENABLED && AH_KMAC128_SUPPORT != DISABLED)
69  #error AH_KMAC128_SUPPORT parameter is not valid
70 #endif
71 
72 //KMAC256 integrity support (experimental)
73 #ifndef AH_KMAC256_SUPPORT
74  #define AH_KMAC256_SUPPORT DISABLED
75 #elif (AH_KMAC256_SUPPORT != ENABLED && AH_KMAC256_SUPPORT != DISABLED)
76  #error AH_KMAC256_SUPPORT parameter is not valid
77 #endif
78 
79 //AES 128-bit cipher support
80 #ifndef AH_AES_128_SUPPORT
81  #define AH_AES_128_SUPPORT DISABLED
82 #elif (AH_AES_128_SUPPORT != ENABLED && AH_AES_128_SUPPORT != DISABLED)
83  #error AH_AES_128_SUPPORT parameter is not valid
84 #endif
85 
86 //MD5 hash support (insecure)
87 #ifndef AH_MD5_SUPPORT
88  #define AH_MD5_SUPPORT DISABLED
89 #elif (AH_MD5_SUPPORT != ENABLED && AH_MD5_SUPPORT != DISABLED)
90  #error AH_MD5_SUPPORT parameter is not valid
91 #endif
92 
93 //SHA-1 hash support (weak)
94 #ifndef AH_SHA1_SUPPORT
95  #define AH_SHA1_SUPPORT ENABLED
96 #elif (AH_SHA1_SUPPORT != ENABLED && AH_SHA1_SUPPORT != DISABLED)
97  #error AH_SHA1_SUPPORT parameter is not valid
98 #endif
99 
100 //SHA-256 hash support
101 #ifndef AH_SHA256_SUPPORT
102  #define AH_SHA256_SUPPORT ENABLED
103 #elif (AH_SHA256_SUPPORT != ENABLED && AH_SHA256_SUPPORT != DISABLED)
104  #error AH_SHA256_SUPPORT parameter is not valid
105 #endif
106 
107 //SHA-384 hash support
108 #ifndef AH_SHA384_SUPPORT
109  #define AH_SHA384_SUPPORT ENABLED
110 #elif (AH_SHA384_SUPPORT != ENABLED && AH_SHA384_SUPPORT != DISABLED)
111  #error AH_SHA384_SUPPORT parameter is not valid
112 #endif
113 
114 //SHA-512 hash support
115 #ifndef AH_SHA512_SUPPORT
116  #define AH_SHA512_SUPPORT ENABLED
117 #elif (AH_SHA512_SUPPORT != ENABLED && AH_SHA512_SUPPORT != DISABLED)
118  #error AH_SHA512_SUPPORT parameter is not valid
119 #endif
120 
121 //SM3 hash support (experimental)
122 #ifndef AH_SM3_SUPPORT
123  #define AH_SM3_SUPPORT DISABLED
124 #elif (AH_SM3_SUPPORT != ENABLED && AH_SM3_SUPPORT != DISABLED)
125  #error AH_SM3_SUPPORT parameter is not valid
126 #endif
127 
128 //Maximum digest size
129 #if (AH_HMAC_SUPPORT == ENABLED && AH_SHA512_SUPPORT == ENABLED)
130  #define AH_MAX_DIGEST_SIZE 64
131 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA384_SUPPORT == ENABLED)
132  #define AH_MAX_DIGEST_SIZE 48
133 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA256_SUPPORT == ENABLED)
134  #define AH_MAX_DIGEST_SIZE 32
135 #else
136  #define AH_MAX_DIGEST_SIZE 12
137 #endif
138 
139 //Maximum size of the ICV field
140 #if (AH_HMAC_SUPPORT == ENABLED && AH_SHA512_SUPPORT == ENABLED)
141  #define AH_MAX_ICV_SIZE 32
142 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA384_SUPPORT == ENABLED)
143  #define AH_MAX_ICV_SIZE 24
144 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA256_SUPPORT == ENABLED)
145  #define AH_MAX_ICV_SIZE 16
146 #else
147  #define AH_MAX_ICV_SIZE 12
148 #endif
149 
150 //Maximum overhead caused by AH security protocol
151 #define AH_MAX_OVERHEAD (sizeof(AhHeader) + AH_MAX_ICV_SIZE)
152 
153 //C++ guard
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 //CC-RX, CodeWarrior or Win32 compiler?
159 #if defined(__CCRX__)
160  #pragma pack
161 #elif defined(__CWCC__) || defined(_WIN32)
162  #pragma pack(push, 1)
163 #endif
164 
165 
166 /**
167  * @brief AH header
168  **/
169 
171 {
172  uint8_t nextHeader; //0
173  uint8_t payloadLen; //1
174  uint16_t reserved; //2-3
175  uint32_t spi; //4-7
176  uint32_t seqNum; //8-11
177  uint8_t icv[]; //12
179 
180 
181 //CC-RX, CodeWarrior or Win32 compiler?
182 #if defined(__CCRX__)
183  #pragma unpack
184 #elif defined(__CWCC__) || defined(_WIN32)
185  #pragma pack(pop)
186 #endif
187 
188 //AH related functions
190  const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset,
191  NetRxAncillary *ancillary);
192 
194  const Ipv4Header *ipv4Header, AhHeader *ahHeader, const NetBuffer *buffer,
195  size_t offset);
196 
198  const Ipv4Header *ipv4Header, const AhHeader *ahHeader,
199  const NetBuffer *buffer, size_t offset);
200 
202 
203 void ahDumpHeader(const AhHeader *ahHeader);
204 
205 //C++ guard
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #endif
uint8_t icv[]
Definition: ah.h:177
#define Ipv4Header
Definition: ipv4.h:36
uint16_t reserved
Definition: ah.h:174
uint32_t spi
Definition: ah.h:175
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
typedef __packed_struct
AH header.
Definition: ah.h:171
uint32_t seqNum
Definition: ah.h:176
void ahProcessMutableIpv4Options(Ipv4Header *header)
Zeroize mutable IPv4 options.
Definition: ah.c:687
error_t
Error codes.
Definition: error.h:43
AhHeader
Definition: ah.h:178
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
uint8_t payloadLen
Definition: ah.h:173
uint8_t nextHeader
Definition: ipv6.h:302
#define IpsecSadEntry
Definition: ipsec.h:36
error_t ahVerifyIcv(IpsecContext *context, IpsecSadEntry *sa, const Ipv4Header *ipv4Header, const AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV verification.
Definition: ah.c:452
error_t ahGenerateIcv(IpsecContext *context, IpsecSadEntry *sa, const Ipv4Header *ipv4Header, AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV generation.
Definition: ah.c:283
IPsec (IP security)
void ahDumpHeader(const AhHeader *ahHeader)
Dump AH header for debugging purpose.
Definition: ah.c:753
#define IpsecContext
Definition: ipsec.h:40
error_t ipv4ProcessAhHeader(NetInterface *interface, const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset, NetRxAncillary *ancillary)
Process AH protected packet.
Definition: ah.c:60