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 <openssl/hmac.h>
19 #include <openssl/rand.h>
20 #include <openssl/evp.h>
36 typedef CLIB_PACKED (struct {
39 }) ip4_and_esp_header_t;
43 typedef CLIB_PACKED (struct {
46 }) ip6_and_esp_header_t;
51 const EVP_CIPHER *type;
62 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
63 EVP_CIPHER_CTX encrypt_ctx;
64 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
65 EVP_CIPHER_CTX decrypt_ctx;
66 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
68 ipsec_crypto_alg_t last_encrypt_alg;
69 ipsec_crypto_alg_t last_decrypt_alg;
70 ipsec_integ_alg_t last_integ_alg;
71 } esp_main_per_thread_data_t;
75 esp_crypto_alg_t *esp_crypto_algs;
76 esp_integ_alg_t *esp_integ_algs;
77 esp_main_per_thread_data_t *per_thread_data;
82 #define ESP_WINDOW_SIZE (64)
83 #define ESP_SEQ_MAX (4294967295UL)
87 esp_replay_check (ipsec_sa_t * sa, u32 seq)
91 if (PREDICT_TRUE (seq > sa->last_seq))
94 diff = sa->last_seq - seq;
96 if (ESP_WINDOW_SIZE > diff)
97 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
105 esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
107 u32 tl = sa->last_seq;
108 u32 th = sa->last_seq_hi;
111 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
113 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
117 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
129 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
132 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
138 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
147 /* TODO seq increment should be atomic to be accessed by multiple workers */
149 esp_replay_advance (ipsec_sa_t * sa, u32 seq)
153 if (seq > sa->last_seq)
155 pos = seq - sa->last_seq;
156 if (pos < ESP_WINDOW_SIZE)
157 sa->replay_window = ((sa->replay_window) << pos) | 1;
159 sa->replay_window = 1;
164 pos = sa->last_seq - seq;
165 sa->replay_window |= (1ULL << pos);
170 esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
172 int wrap = sa->seq_hi - sa->last_seq_hi;
175 if (wrap == 0 && seq > sa->last_seq)
177 pos = seq - sa->last_seq;
178 if (pos < ESP_WINDOW_SIZE)
179 sa->replay_window = ((sa->replay_window) << pos) | 1;
181 sa->replay_window = 1;
186 pos = ~seq + sa->last_seq + 1;
187 if (pos < ESP_WINDOW_SIZE)
188 sa->replay_window = ((sa->replay_window) << pos) | 1;
190 sa->replay_window = 1;
192 sa->last_seq_hi = sa->seq_hi;
196 pos = ~seq + sa->last_seq + 1;
197 sa->replay_window |= (1ULL << pos);
201 pos = sa->last_seq - seq;
202 sa->replay_window |= (1ULL << pos);
207 esp_seq_advance (ipsec_sa_t * sa)
209 if (PREDICT_TRUE (sa->use_esn))
211 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
214 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
222 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
233 esp_main_t *em = &esp_main;
234 vlib_thread_main_t *tm = vlib_get_thread_main ();
236 memset (em, 0, sizeof (em[0]));
238 vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
239 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
240 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
241 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
243 vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
246 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
250 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
251 i->md = EVP_sha256 ();
254 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
255 i->md = EVP_sha256 ();
258 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
259 i->md = EVP_sha384 ();
262 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
263 i->md = EVP_sha512 ();
266 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
267 CLIB_CACHE_LINE_BYTES);
270 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
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));
278 always_inline unsigned int
279 hmac_calc (ipsec_integ_alg_t alg,
282 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
284 esp_main_t *em = &esp_main;
285 u32 cpu_index = os_get_cpu_number ();
286 HMAC_CTX *ctx = &(em->per_thread_data[cpu_index].hmac_ctx);
287 const EVP_MD *md = NULL;
290 ASSERT (alg < IPSEC_INTEG_N_ALG);
292 if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
295 if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_integ_alg))
297 md = em->esp_integ_algs[alg].md;
298 em->per_thread_data[cpu_index].last_integ_alg = alg;
301 HMAC_Init (ctx, key, key_len, md);
303 HMAC_Update (ctx, data, data_len);
305 if (PREDICT_TRUE (use_esn))
306 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
307 HMAC_Final (ctx, signature, &len);
309 return em->esp_integ_algs[alg].trunc_size;
312 #endif /* __ESP_H__ */
315 * fd.io coding-style-patch-verification: ON
318 * eval: (c-set-style "gnu")