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-2024 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.4.0
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 //AES 128-bit cipher support
66 #ifndef AH_AES_128_SUPPORT
67  #define AH_AES_128_SUPPORT DISABLED
68 #elif (AH_AES_128_SUPPORT != ENABLED && AH_AES_128_SUPPORT != DISABLED)
69  #error AH_AES_128_SUPPORT parameter is not valid
70 #endif
71 
72 //MD5 hash support (insecure)
73 #ifndef AH_MD5_SUPPORT
74  #define AH_MD5_SUPPORT DISABLED
75 #elif (AH_MD5_SUPPORT != ENABLED && AH_MD5_SUPPORT != DISABLED)
76  #error AH_MD5_SUPPORT parameter is not valid
77 #endif
78 
79 //SHA-1 hash support (weak)
80 #ifndef AH_SHA1_SUPPORT
81  #define AH_SHA1_SUPPORT ENABLED
82 #elif (AH_SHA1_SUPPORT != ENABLED && AH_SHA1_SUPPORT != DISABLED)
83  #error AH_SHA1_SUPPORT parameter is not valid
84 #endif
85 
86 //SHA-256 hash support
87 #ifndef AH_SHA256_SUPPORT
88  #define AH_SHA256_SUPPORT ENABLED
89 #elif (AH_SHA256_SUPPORT != ENABLED && AH_SHA256_SUPPORT != DISABLED)
90  #error AH_SHA256_SUPPORT parameter is not valid
91 #endif
92 
93 //SHA-384 hash support
94 #ifndef AH_SHA384_SUPPORT
95  #define AH_SHA384_SUPPORT ENABLED
96 #elif (AH_SHA384_SUPPORT != ENABLED && AH_SHA384_SUPPORT != DISABLED)
97  #error AH_SHA384_SUPPORT parameter is not valid
98 #endif
99 
100 //SHA-512 hash support
101 #ifndef AH_SHA512_SUPPORT
102  #define AH_SHA512_SUPPORT ENABLED
103 #elif (AH_SHA512_SUPPORT != ENABLED && AH_SHA512_SUPPORT != DISABLED)
104  #error AH_SHA512_SUPPORT parameter is not valid
105 #endif
106 
107 //Maximum size of the ICV field
108 #if (AH_HMAC_SUPPORT == ENABLED && AH_SHA512_SUPPORT == ENABLED)
109  #define AH_MAX_ICV_SIZE 32
110 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA384_SUPPORT == ENABLED)
111  #define AH_MAX_ICV_SIZE 24
112 #elif (AH_HMAC_SUPPORT == ENABLED && AH_SHA256_SUPPORT == ENABLED)
113  #define AH_MAX_ICV_SIZE 16
114 #else
115  #define AH_MAX_ICV_SIZE 12
116 #endif
117 
118 //Maximum overhead caused by AH security protocol
119 #define AH_MAX_OVERHEAD (sizeof(AhHeader) + AH_MAX_ICV_SIZE)
120 
121 //C++ guard
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
126 //CC-RX, CodeWarrior or Win32 compiler?
127 #if defined(__CCRX__)
128  #pragma pack
129 #elif defined(__CWCC__) || defined(_WIN32)
130  #pragma pack(push, 1)
131 #endif
132 
133 
134 /**
135  * @brief AH header
136  **/
137 
139 {
140  uint8_t nextHeader; //0
141  uint8_t payloadLen; //1
142  uint16_t reserved; //2-3
143  uint32_t spi; //4-7
144  uint32_t seqNum; //8-11
145  uint8_t icv[]; //12
147 
148 
149 //CC-RX, CodeWarrior or Win32 compiler?
150 #if defined(__CCRX__)
151  #pragma unpack
152 #elif defined(__CWCC__) || defined(_WIN32)
153  #pragma pack(pop)
154 #endif
155 
156 //AH related functions
158  const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset,
159  NetRxAncillary *ancillary);
160 
161 error_t ahGenerateIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header,
162  AhHeader *ahHeader, const NetBuffer *buffer, size_t offset);
163 
164 error_t ahVerifyIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header,
165  const AhHeader *ahHeader, const NetBuffer *buffer, size_t offset);
166 
168 
169 void ahDumpHeader(const AhHeader *ahHeader);
170 
171 //C++ guard
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif
uint8_t icv[]
Definition: ah.h:145
error_t ipv4ProcessAhHeader(NetInterface *interface, const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset, NetRxAncillary *ancillary)
Process AH protected packet.
Definition: ah.c:60
void ahDumpHeader(const AhHeader *ahHeader)
Dump AH header for debugging purpose.
Definition: ah.c:760
AhHeader
Definition: ah.h:146
uint32_t spi
Definition: ah.h:143
uint16_t reserved
Definition: ah.h:142
uint32_t seqNum
Definition: ah.h:144
void ahProcessMutableIpv4Options(Ipv4Header *header)
Zeroize mutable IPv4 options.
Definition: ah.c:694
error_t ahVerifyIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header, const AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV verification.
Definition: ah.c:447
typedef __packed_struct
AH header.
Definition: ah.h:139
error_t ahGenerateIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header, AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV generation.
Definition: ah.c:275
uint8_t payloadLen
Definition: ah.h:141
error_t
Error codes.
Definition: error.h:43
IPsec (IP security)
#define IpsecSadEntry
Definition: ipsec.h:36
#define Ipv4Header
Definition: ipv4.h:36
uint8_t nextHeader
Definition: ipv6.h:273
#define NetInterface
Definition: net.h:36
#define NetRxAncillary
Definition: net_misc.h:40
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89