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 {
49 }) ip6_and_esp_header_t;
54 const EVP_CIPHER *type;
57 } ipsec_proto_main_crypto_alg_t;
63 } ipsec_proto_main_integ_alg_t;
67 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
68 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
69 EVP_CIPHER_CTX *encrypt_ctx;
71 EVP_CIPHER_CTX encrypt_ctx;
73 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
74 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
75 EVP_CIPHER_CTX *decrypt_ctx;
77 EVP_CIPHER_CTX decrypt_ctx;
79 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
80 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
85 ipsec_crypto_alg_t last_encrypt_alg;
86 ipsec_crypto_alg_t last_decrypt_alg;
87 ipsec_integ_alg_t last_integ_alg;
88 } ipsec_proto_main_per_thread_data_t;
92 ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs;
93 ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs;
94 ipsec_proto_main_per_thread_data_t *per_thread_data;
97 extern ipsec_proto_main_t ipsec_proto_main;
99 #define ESP_WINDOW_SIZE (64)
100 #define ESP_SEQ_MAX (4294967295UL)
102 u8 *format_esp_header (u8 * s, va_list * args);
105 esp_replay_check (ipsec_sa_t * sa, u32 seq)
109 if (PREDICT_TRUE (seq > sa->last_seq))
112 diff = sa->last_seq - seq;
114 if (ESP_WINDOW_SIZE > diff)
115 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
123 esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
125 u32 tl = sa->last_seq;
126 u32 th = sa->last_seq_hi;
129 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
131 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
135 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
147 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
150 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
156 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
165 /* TODO seq increment should be atomic to be accessed by multiple workers */
167 esp_replay_advance (ipsec_sa_t * sa, u32 seq)
171 if (seq > sa->last_seq)
173 pos = seq - sa->last_seq;
174 if (pos < ESP_WINDOW_SIZE)
175 sa->replay_window = ((sa->replay_window) << pos) | 1;
177 sa->replay_window = 1;
182 pos = sa->last_seq - seq;
183 sa->replay_window |= (1ULL << pos);
188 esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
190 int wrap = sa->seq_hi - sa->last_seq_hi;
193 if (wrap == 0 && seq > sa->last_seq)
195 pos = seq - sa->last_seq;
196 if (pos < ESP_WINDOW_SIZE)
197 sa->replay_window = ((sa->replay_window) << pos) | 1;
199 sa->replay_window = 1;
204 pos = ~seq + sa->last_seq + 1;
205 if (pos < ESP_WINDOW_SIZE)
206 sa->replay_window = ((sa->replay_window) << pos) | 1;
208 sa->replay_window = 1;
210 sa->last_seq_hi = sa->seq_hi;
214 pos = ~seq + sa->last_seq + 1;
215 sa->replay_window |= (1ULL << pos);
219 pos = sa->last_seq - seq;
220 sa->replay_window |= (1ULL << pos);
225 esp_seq_advance (ipsec_sa_t * sa)
227 if (PREDICT_TRUE (sa->use_esn))
229 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
232 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
240 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
251 ipsec_proto_main_t *em = &ipsec_proto_main;
252 vlib_thread_main_t *tm = vlib_get_thread_main ();
254 memset (em, 0, sizeof (em[0]));
256 vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
257 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type =
259 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
261 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
263 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
264 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
265 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
266 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size =
268 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
270 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
272 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
274 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
276 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
277 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
278 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
279 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
281 vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
282 ipsec_proto_main_integ_alg_t *i;
284 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
288 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
289 i->md = EVP_sha256 ();
292 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
293 i->md = EVP_sha256 ();
296 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
297 i->md = EVP_sha384 ();
300 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
301 i->md = EVP_sha512 ();
304 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
305 CLIB_CACHE_LINE_BYTES);
308 for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
310 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
311 em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
312 em->per_thread_data[thread_id].decrypt_ctx = EVP_CIPHER_CTX_new ();
313 em->per_thread_data[thread_id].hmac_ctx = HMAC_CTX_new ();
315 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
316 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
317 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
322 always_inline unsigned int
323 hmac_calc (ipsec_integ_alg_t alg,
326 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
328 ipsec_proto_main_t *em = &ipsec_proto_main;
329 u32 thread_index = vlib_get_thread_index ();
330 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
331 HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
333 HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
335 const EVP_MD *md = NULL;
338 ASSERT (alg < IPSEC_INTEG_N_ALG);
340 if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
343 if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
345 md = em->ipsec_proto_main_integ_algs[alg].md;
346 em->per_thread_data[thread_index].last_integ_alg = alg;
349 HMAC_Init_ex (ctx, key, key_len, md, NULL);
351 HMAC_Update (ctx, data, data_len);
353 if (PREDICT_TRUE (use_esn))
354 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
355 HMAC_Final (ctx, signature, &len);
357 return em->ipsec_proto_main_integ_algs[alg].trunc_size;
360 #endif /* __ESP_H__ */
363 * fd.io coding-style-patch-verification: ON
366 * eval: (c-set-style "gnu")