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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip/ip.h>
19 #include <vnet/ipsec/ipsec.h>
21 #include <openssl/hmac.h>
22 #include <openssl/rand.h>
23 #include <openssl/evp.h>
39 typedef CLIB_PACKED (struct {
42 }) ip4_and_esp_header_t;
46 typedef CLIB_PACKED (struct {
50 }) ip4_and_udp_and_esp_header_t;
54 typedef CLIB_PACKED (struct {
57 }) ip6_and_esp_header_t;
62 const EVP_CIPHER *type;
65 } ipsec_proto_main_crypto_alg_t;
71 } ipsec_proto_main_integ_alg_t;
75 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
76 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
77 EVP_CIPHER_CTX *encrypt_ctx;
79 EVP_CIPHER_CTX encrypt_ctx;
81 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
82 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
83 EVP_CIPHER_CTX *decrypt_ctx;
85 EVP_CIPHER_CTX decrypt_ctx;
87 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
88 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
93 ipsec_crypto_alg_t last_encrypt_alg;
94 ipsec_crypto_alg_t last_decrypt_alg;
95 ipsec_integ_alg_t last_integ_alg;
96 } ipsec_proto_main_per_thread_data_t;
100 ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs;
101 ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs;
102 ipsec_proto_main_per_thread_data_t *per_thread_data;
103 } ipsec_proto_main_t;
105 extern ipsec_proto_main_t ipsec_proto_main;
107 #define ESP_WINDOW_SIZE (64)
108 #define ESP_SEQ_MAX (4294967295UL)
110 u8 *format_esp_header (u8 * s, va_list * args);
113 esp_replay_check (ipsec_sa_t * sa, u32 seq)
117 if (PREDICT_TRUE (seq > sa->last_seq))
120 diff = sa->last_seq - seq;
122 if (ESP_WINDOW_SIZE > diff)
123 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
131 esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
133 u32 tl = sa->last_seq;
134 u32 th = sa->last_seq_hi;
137 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
139 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
143 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
155 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
158 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
164 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
173 /* TODO seq increment should be atomic to be accessed by multiple workers */
175 esp_replay_advance (ipsec_sa_t * sa, u32 seq)
179 if (seq > sa->last_seq)
181 pos = seq - sa->last_seq;
182 if (pos < ESP_WINDOW_SIZE)
183 sa->replay_window = ((sa->replay_window) << pos) | 1;
185 sa->replay_window = 1;
190 pos = sa->last_seq - seq;
191 sa->replay_window |= (1ULL << pos);
196 esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
198 int wrap = sa->seq_hi - sa->last_seq_hi;
201 if (wrap == 0 && seq > sa->last_seq)
203 pos = seq - sa->last_seq;
204 if (pos < ESP_WINDOW_SIZE)
205 sa->replay_window = ((sa->replay_window) << pos) | 1;
207 sa->replay_window = 1;
212 pos = ~seq + sa->last_seq + 1;
213 if (pos < ESP_WINDOW_SIZE)
214 sa->replay_window = ((sa->replay_window) << pos) | 1;
216 sa->replay_window = 1;
218 sa->last_seq_hi = sa->seq_hi;
222 pos = ~seq + sa->last_seq + 1;
223 sa->replay_window |= (1ULL << pos);
227 pos = sa->last_seq - seq;
228 sa->replay_window |= (1ULL << pos);
233 esp_seq_advance (ipsec_sa_t * sa)
235 if (PREDICT_TRUE (sa->use_esn))
237 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
240 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
248 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
259 ipsec_proto_main_t *em = &ipsec_proto_main;
260 vlib_thread_main_t *tm = vlib_get_thread_main ();
262 clib_memset (em, 0, sizeof (em[0]));
264 vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
265 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type =
267 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
269 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
271 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
272 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
273 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
274 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size =
276 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
278 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
280 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
282 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
284 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
285 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
286 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
287 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
289 vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
290 ipsec_proto_main_integ_alg_t *i;
292 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
296 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
297 i->md = EVP_sha256 ();
300 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
301 i->md = EVP_sha256 ();
304 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
305 i->md = EVP_sha384 ();
308 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
309 i->md = EVP_sha512 ();
312 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
313 CLIB_CACHE_LINE_BYTES);
316 for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
318 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
319 em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
320 em->per_thread_data[thread_id].decrypt_ctx = EVP_CIPHER_CTX_new ();
321 em->per_thread_data[thread_id].hmac_ctx = HMAC_CTX_new ();
323 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
324 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
325 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
330 always_inline unsigned int
331 hmac_calc (ipsec_integ_alg_t alg,
334 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
336 ipsec_proto_main_t *em = &ipsec_proto_main;
337 u32 thread_index = vlib_get_thread_index ();
338 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
339 HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
341 HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
343 const EVP_MD *md = NULL;
346 ASSERT (alg < IPSEC_INTEG_N_ALG);
348 if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
351 if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
353 md = em->ipsec_proto_main_integ_algs[alg].md;
354 em->per_thread_data[thread_index].last_integ_alg = alg;
357 HMAC_Init_ex (ctx, key, key_len, md, NULL);
359 HMAC_Update (ctx, data, data_len);
361 if (PREDICT_TRUE (use_esn))
362 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
363 HMAC_Final (ctx, signature, &len);
365 return em->ipsec_proto_main_integ_algs[alg].trunc_size;
368 #endif /* __ESP_H__ */
371 * fd.io coding-style-patch-verification: ON
374 * eval: (c-set-style "gnu")