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>
35 typedef CLIB_PACKED (struct {
38 }) ip4_and_esp_header_t;
42 typedef CLIB_PACKED (struct {
46 }) ip4_and_udp_and_esp_header_t;
50 typedef CLIB_PACKED (struct {
53 }) ip6_and_esp_header_t;
56 #define ESP_WINDOW_SIZE (64)
57 #define ESP_SEQ_MAX (4294967295UL)
59 u8 *format_esp_header (u8 * s, va_list * args);
62 esp_replay_check (ipsec_sa_t * sa, u32 seq)
66 if (PREDICT_TRUE (seq > sa->last_seq))
69 diff = sa->last_seq - seq;
71 if (ESP_WINDOW_SIZE > diff)
72 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
80 esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
82 u32 tl = sa->last_seq;
83 u32 th = sa->last_seq_hi;
86 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
88 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
92 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
104 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
107 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
113 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
122 /* TODO seq increment should be atomic to be accessed by multiple workers */
124 esp_replay_advance (ipsec_sa_t * sa, u32 seq)
128 if (seq > sa->last_seq)
130 pos = seq - sa->last_seq;
131 if (pos < ESP_WINDOW_SIZE)
132 sa->replay_window = ((sa->replay_window) << pos) | 1;
134 sa->replay_window = 1;
139 pos = sa->last_seq - seq;
140 sa->replay_window |= (1ULL << pos);
145 esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
147 int wrap = sa->seq_hi - sa->last_seq_hi;
150 if (wrap == 0 && seq > sa->last_seq)
152 pos = seq - sa->last_seq;
153 if (pos < ESP_WINDOW_SIZE)
154 sa->replay_window = ((sa->replay_window) << pos) | 1;
156 sa->replay_window = 1;
161 pos = ~seq + sa->last_seq + 1;
162 if (pos < ESP_WINDOW_SIZE)
163 sa->replay_window = ((sa->replay_window) << pos) | 1;
165 sa->replay_window = 1;
167 sa->last_seq_hi = sa->seq_hi;
171 pos = ~seq + sa->last_seq + 1;
172 sa->replay_window |= (1ULL << pos);
176 pos = sa->last_seq - seq;
177 sa->replay_window |= (1ULL << pos);
182 esp_seq_advance (ipsec_sa_t * sa)
184 if (PREDICT_TRUE (sa->use_esn))
186 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
189 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
197 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
208 ipsec_proto_main_t *em = &ipsec_proto_main;
209 vlib_thread_main_t *tm = vlib_get_thread_main ();
211 clib_memset (em, 0, sizeof (em[0]));
213 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 =
216 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
218 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
220 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 =
225 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
227 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
229 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
231 em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
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;
238 vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
239 ipsec_proto_main_integ_alg_t *i;
241 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
245 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
246 i->md = EVP_sha256 ();
249 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
250 i->md = EVP_sha256 ();
253 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
254 i->md = EVP_sha384 ();
257 i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
258 i->md = EVP_sha512 ();
261 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
262 CLIB_CACHE_LINE_BYTES);
265 for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
267 #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 ();
272 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));
279 always_inline unsigned int
280 hmac_calc (ipsec_integ_alg_t alg,
283 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
285 ipsec_proto_main_t *em = &ipsec_proto_main;
286 u32 thread_index = vlib_get_thread_index ();
287 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
288 HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
290 HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
292 const EVP_MD *md = NULL;
295 ASSERT (alg < IPSEC_INTEG_N_ALG);
297 if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
300 if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
302 md = em->ipsec_proto_main_integ_algs[alg].md;
303 em->per_thread_data[thread_index].last_integ_alg = alg;
306 HMAC_Init_ex (ctx, key, key_len, md, NULL);
308 HMAC_Update (ctx, data, data_len);
310 if (PREDICT_TRUE (use_esn))
311 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
312 HMAC_Final (ctx, signature, &len);
314 return em->ipsec_proto_main_integ_algs[alg].trunc_size;
317 #endif /* __ESP_H__ */
320 * fd.io coding-style-patch-verification: ON
323 * eval: (c-set-style "gnu")