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 <openssl/hmac.h>
17 #include <openssl/rand.h>
18 #include <openssl/evp.h>
34 typedef CLIB_PACKED (struct {
37 }) ip4_and_esp_header_t;
41 typedef CLIB_PACKED (struct {
44 }) ip6_and_esp_header_t;
49 const EVP_CIPHER *type;
60 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
61 EVP_CIPHER_CTX encrypt_ctx;
62 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
63 EVP_CIPHER_CTX decrypt_ctx;
64 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
66 ipsec_crypto_alg_t last_encrypt_alg;
67 ipsec_crypto_alg_t last_decrypt_alg;
68 ipsec_integ_alg_t last_integ_alg;
69 } esp_main_per_thread_data_t;
73 esp_crypto_alg_t *esp_crypto_algs;
74 esp_integ_alg_t *esp_integ_algs;
75 esp_main_per_thread_data_t *per_thread_data;
83 esp_main_t *em = &esp_main;
84 vlib_thread_main_t *tm = vlib_get_thread_main ();
86 memset (em, 0, sizeof (em[0]));
88 vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
89 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
90 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
91 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
93 vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
96 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
100 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
101 i->md = EVP_sha256 ();
104 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
105 i->md = EVP_sha256 ();
108 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
109 i->md = EVP_sha384 ();
112 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
113 i->md = EVP_sha512 ();
116 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
117 CLIB_CACHE_LINE_BYTES);
120 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
122 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
123 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
124 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
128 always_inline unsigned int
129 hmac_calc (ipsec_integ_alg_t alg,
132 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
134 esp_main_t *em = &esp_main;
135 u32 cpu_index = os_get_cpu_number ();
136 HMAC_CTX *ctx = &(em->per_thread_data[cpu_index].hmac_ctx);
137 const EVP_MD *md = NULL;
140 ASSERT (alg < IPSEC_INTEG_N_ALG);
142 if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
145 if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_integ_alg))
147 md = em->esp_integ_algs[alg].md;
148 em->per_thread_data[cpu_index].last_integ_alg = alg;
151 HMAC_Init (ctx, key, key_len, md);
153 HMAC_Update (ctx, data, data_len);
155 if (PREDICT_TRUE (use_esn))
156 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
157 HMAC_Final (ctx, signature, &len);
159 return em->esp_integ_algs[alg].trunc_size;
164 * fd.io coding-style-patch-verification: ON
167 * eval: (c-set-style "gnu")