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.
16 #include <vnet/devices/dpdk/dpdk.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21 #include <openssl/evp.h>
37 typedef CLIB_PACKED (struct {
40 }) ip4_and_esp_header_t;
44 typedef CLIB_PACKED (struct {
47 }) ip6_and_esp_header_t;
52 const EVP_CIPHER *type;
63 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
64 EVP_CIPHER_CTX encrypt_ctx;
65 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
66 EVP_CIPHER_CTX decrypt_ctx;
67 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
69 ipsec_crypto_alg_t last_encrypt_alg;
70 ipsec_crypto_alg_t last_decrypt_alg;
71 ipsec_integ_alg_t last_integ_alg;
72 } esp_main_per_thread_data_t;
76 esp_crypto_alg_t *esp_crypto_algs;
77 esp_integ_alg_t *esp_integ_algs;
78 esp_main_per_thread_data_t *per_thread_data;
86 esp_main_t *em = &esp_main;
87 vlib_thread_main_t *tm = vlib_get_thread_main ();
89 memset (em, 0, sizeof (em[0]));
91 vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
92 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
93 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
94 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
96 vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
99 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
103 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
104 i->md = EVP_sha256 ();
107 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
108 i->md = EVP_sha256 ();
111 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
112 i->md = EVP_sha384 ();
115 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
116 i->md = EVP_sha512 ();
119 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
120 CLIB_CACHE_LINE_BYTES);
123 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
125 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
126 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
127 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
131 always_inline unsigned int
132 hmac_calc (ipsec_integ_alg_t alg,
135 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
137 esp_main_t *em = &esp_main;
138 u32 cpu_index = os_get_cpu_number ();
139 HMAC_CTX *ctx = &(em->per_thread_data[cpu_index].hmac_ctx);
140 const EVP_MD *md = NULL;
143 ASSERT (alg < IPSEC_INTEG_N_ALG);
145 if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
148 if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_integ_alg))
150 md = em->esp_integ_algs[alg].md;
151 em->per_thread_data[cpu_index].last_integ_alg = alg;
154 HMAC_Init (ctx, key, key_len, md);
156 HMAC_Update (ctx, data, data_len);
158 if (PREDICT_TRUE (use_esn))
159 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
160 HMAC_Final (ctx, signature, &len);
162 return em->esp_integ_algs[alg].trunc_size;
167 * fd.io coding-style-patch-verification: ON
170 * eval: (c-set-style "gnu")