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>
34 typedef CLIB_PACKED (struct {
37 }) ip4_and_esp_header_t;
39 typedef CLIB_PACKED (struct {
42 }) ip6_and_esp_header_t;
45 const EVP_CIPHER * type;
55 esp_crypto_alg_t * esp_crypto_algs;
56 esp_integ_alg_t * esp_integ_algs;
57 EVP_CIPHER_CTX encrypt_ctx;
58 EVP_CIPHER_CTX decrypt_ctx;
60 ipsec_crypto_alg_t last_encrytp_alg;
61 ipsec_crypto_alg_t last_decrytp_alg;
62 ipsec_integ_alg_t last_integ_alg;
70 esp_main_t * em = &esp_main;
72 memset (em, 0, sizeof (em[0]));
74 vec_validate(em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
75 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc();
76 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc();
77 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc();
79 vec_validate(em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
82 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
86 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
90 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
94 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
98 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
102 EVP_CIPHER_CTX_init(&(em->encrypt_ctx));
103 EVP_CIPHER_CTX_init(&(em->decrypt_ctx));
104 HMAC_CTX_init(&(em->hmac_ctx));
107 always_inline unsigned int
108 hmac_calc(ipsec_integ_alg_t alg,
117 esp_main_t * em = &esp_main;
118 HMAC_CTX * ctx = &(em->hmac_ctx);
119 const EVP_MD * md = NULL;
122 ASSERT(alg < IPSEC_INTEG_N_ALG);
124 if (PREDICT_FALSE(em->esp_integ_algs[alg].md == 0))
127 if (PREDICT_FALSE(alg != em->last_integ_alg)) {
128 md = em->esp_integ_algs[alg].md;
129 em->last_integ_alg = alg;
132 HMAC_Init(ctx, key, key_len, md);
134 HMAC_Update(ctx, data, data_len);
136 if (PREDICT_TRUE(use_esn))
137 HMAC_Update(ctx, (u8 *) &seq_hi, sizeof(seq_hi));
138 HMAC_Final(ctx, signature, &len);
140 return em->esp_integ_algs[alg].trunc_size;