blob: 0047265455452810206da84ea9a5ab4aeaf30f1f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000015#ifndef __ESP_H__
16#define __ESP_H__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010018#include <vnet/ip/ip.h>
19#include <vnet/ipsec/ipsec.h>
20
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070021typedef struct
22{
Ed Warnickecb9cada2015-12-08 15:45:58 -070023 u32 spi;
24 u32 seq;
25 u8 data[0];
26} esp_header_t;
27
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070028typedef struct
29{
Ed Warnickecb9cada2015-12-08 15:45:58 -070030 u8 pad_length;
31 u8 next_header;
32} esp_footer_t;
33
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070034/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070035typedef CLIB_PACKED (struct {
36 ip4_header_t ip4;
37 esp_header_t esp;
38}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070042typedef CLIB_PACKED (struct {
Klement Sekera4b089f22018-04-17 18:04:57 +020043 ip4_header_t ip4;
44 udp_header_t udp;
45 esp_header_t esp;
46}) ip4_and_udp_and_esp_header_t;
47/* *INDENT-ON* */
48
49/* *INDENT-OFF* */
50typedef CLIB_PACKED (struct {
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 ip6_header_t ip6;
52 esp_header_t esp;
53}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000056#define ESP_WINDOW_SIZE (64)
57#define ESP_SEQ_MAX (4294967295UL)
58
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010059u8 *format_esp_header (u8 * s, va_list * args);
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000060
61always_inline int
62esp_replay_check (ipsec_sa_t * sa, u32 seq)
63{
64 u32 diff;
65
66 if (PREDICT_TRUE (seq > sa->last_seq))
67 return 0;
68
69 diff = sa->last_seq - seq;
70
71 if (ESP_WINDOW_SIZE > diff)
72 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
73 else
74 return 1;
75
76 return 0;
77}
78
79always_inline int
80esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
81{
82 u32 tl = sa->last_seq;
83 u32 th = sa->last_seq_hi;
84 u32 diff = tl - seq;
85
86 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
87 {
88 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
89 {
90 sa->seq_hi = th;
91 if (seq <= tl)
92 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
93 else
94 return 0;
95 }
96 else
97 {
98 sa->seq_hi = th + 1;
99 return 0;
100 }
101 }
102 else
103 {
104 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
105 {
106 sa->seq_hi = th - 1;
107 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
108 }
109 else
110 {
111 sa->seq_hi = th;
112 if (seq <= tl)
113 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
114 else
115 return 0;
116 }
117 }
118
119 return 0;
120}
121
122/* TODO seq increment should be atomic to be accessed by multiple workers */
123always_inline void
124esp_replay_advance (ipsec_sa_t * sa, u32 seq)
125{
126 u32 pos;
127
128 if (seq > sa->last_seq)
129 {
130 pos = seq - sa->last_seq;
131 if (pos < ESP_WINDOW_SIZE)
132 sa->replay_window = ((sa->replay_window) << pos) | 1;
133 else
134 sa->replay_window = 1;
135 sa->last_seq = seq;
136 }
137 else
138 {
139 pos = sa->last_seq - seq;
140 sa->replay_window |= (1ULL << pos);
141 }
142}
143
144always_inline void
145esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
146{
147 int wrap = sa->seq_hi - sa->last_seq_hi;
148 u32 pos;
149
150 if (wrap == 0 && seq > sa->last_seq)
151 {
152 pos = seq - sa->last_seq;
153 if (pos < ESP_WINDOW_SIZE)
154 sa->replay_window = ((sa->replay_window) << pos) | 1;
155 else
156 sa->replay_window = 1;
157 sa->last_seq = seq;
158 }
159 else if (wrap > 0)
160 {
161 pos = ~seq + sa->last_seq + 1;
162 if (pos < ESP_WINDOW_SIZE)
163 sa->replay_window = ((sa->replay_window) << pos) | 1;
164 else
165 sa->replay_window = 1;
166 sa->last_seq = seq;
167 sa->last_seq_hi = sa->seq_hi;
168 }
169 else if (wrap < 0)
170 {
171 pos = ~seq + sa->last_seq + 1;
172 sa->replay_window |= (1ULL << pos);
173 }
174 else
175 {
176 pos = sa->last_seq - seq;
177 sa->replay_window |= (1ULL << pos);
178 }
179}
180
181always_inline int
182esp_seq_advance (ipsec_sa_t * sa)
183{
184 if (PREDICT_TRUE (sa->use_esn))
185 {
186 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
187 {
188 if (PREDICT_FALSE
189 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
190 return 1;
191 sa->seq_hi++;
192 }
193 sa->seq++;
194 }
195 else
196 {
197 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
198 return 1;
199 sa->seq++;
200 }
201
202 return 0;
203}
204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205always_inline void
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800206ipsec_proto_init ()
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800208 ipsec_proto_main_t *em = &ipsec_proto_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Dave Barachb7b92992018-10-17 10:38:51 -0400211 clib_memset (em, 0, sizeof (em[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800213 vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
214 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type =
215 EVP_aes_128_cbc ();
216 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
217 EVP_aes_192_cbc ();
218 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
219 EVP_aes_256_cbc ();
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800220 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
221 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
222 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
223 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size =
224 16;
225 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
226 16;
227 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
228 16;
229 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
230 EVP_des_cbc ();
231 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
232 EVP_des_ede3_cbc ();
233 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
234 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
235 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
236 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800238 vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
239 ipsec_proto_main_integ_alg_t *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800241 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 i->md = EVP_sha1 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 i->trunc_size = 12;
244
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800245 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 i->md = EVP_sha256 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 i->trunc_size = 12;
248
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800249 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700250 i->md = EVP_sha256 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 i->trunc_size = 16;
252
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800253 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700254 i->md = EVP_sha384 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 i->trunc_size = 24;
256
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800257 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 i->md = EVP_sha512 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 i->trunc_size = 32;
260
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700261 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
262 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500263 int thread_id;
264
Marco Varlese46316102018-03-23 13:32:50 +0100265 for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
Matthew Smith29d85102016-05-01 14:52:08 -0500266 {
Marco Varlesef616d102017-11-09 15:16:20 +0100267#if OPENSSL_VERSION_NUMBER >= 0x10100000L
268 em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
269 em->per_thread_data[thread_id].decrypt_ctx = EVP_CIPHER_CTX_new ();
270 em->per_thread_data[thread_id].hmac_ctx = HMAC_CTX_new ();
271#else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700272 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
273 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
274 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
Marco Varlesef616d102017-11-09 15:16:20 +0100275#endif
Matthew Smith29d85102016-05-01 14:52:08 -0500276 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277}
278
279always_inline unsigned int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700280hmac_calc (ipsec_integ_alg_t alg,
281 u8 * key,
282 int key_len,
283 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284{
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800285 ipsec_proto_main_t *em = &ipsec_proto_main;
Damjan Marion586afd72017-04-05 19:18:20 +0200286 u32 thread_index = vlib_get_thread_index ();
Marco Varlesef616d102017-11-09 15:16:20 +0100287#if OPENSSL_VERSION_NUMBER >= 0x10100000L
288 HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
289#else
Damjan Marion586afd72017-04-05 19:18:20 +0200290 HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
Marco Varlesef616d102017-11-09 15:16:20 +0100291#endif
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292 const EVP_MD *md = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 unsigned int len;
294
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700295 ASSERT (alg < IPSEC_INTEG_N_ALG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800297 if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 return 0;
299
Damjan Marion586afd72017-04-05 19:18:20 +0200300 if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700301 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800302 md = em->ipsec_proto_main_integ_algs[alg].md;
Damjan Marion586afd72017-04-05 19:18:20 +0200303 em->per_thread_data[thread_index].last_integ_alg = alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700304 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Marco Varlesef616d102017-11-09 15:16:20 +0100306 HMAC_Init_ex (ctx, key, key_len, md, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700308 HMAC_Update (ctx, data, data_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700310 if (PREDICT_TRUE (use_esn))
311 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
312 HMAC_Final (ctx, signature, &len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800314 return em->ipsec_proto_main_integ_algs[alg].trunc_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315}
316
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000317#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700318
319/*
320 * fd.io coding-style-patch-verification: ON
321 *
322 * Local Variables:
323 * eval: (c-set-style "gnu")
324 * End:
325 */