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.
15 #include <vnet/devices/dpdk/dpdk.h>
17 #include <openssl/hmac.h>
18 #include <openssl/rand.h>
19 #include <openssl/evp.h>
32 typedef CLIB_PACKED (struct {
35 }) ip4_and_esp_header_t;
37 typedef CLIB_PACKED (struct {
40 }) ip6_and_esp_header_t;
43 const EVP_CIPHER * type;
53 esp_crypto_alg_t * esp_crypto_algs;
54 esp_integ_alg_t * esp_integ_algs;
55 EVP_CIPHER_CTX encrypt_ctx;
56 EVP_CIPHER_CTX decrypt_ctx;
58 ipsec_crypto_alg_t last_encrytp_alg;
59 ipsec_crypto_alg_t last_decrytp_alg;
60 ipsec_integ_alg_t last_integ_alg;
68 esp_main_t * em = &esp_main;
70 memset (em, 0, sizeof (em[0]));
72 vec_validate(em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
73 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc();
74 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc();
75 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc();
77 vec_validate(em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
80 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
84 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
88 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
92 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
96 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
100 EVP_CIPHER_CTX_init(&(em->encrypt_ctx));
101 EVP_CIPHER_CTX_init(&(em->decrypt_ctx));
102 HMAC_CTX_init(&(em->hmac_ctx));
105 always_inline unsigned int
106 hmac_calc(ipsec_integ_alg_t alg,
115 esp_main_t * em = &esp_main;
116 HMAC_CTX * ctx = &(em->hmac_ctx);
117 const EVP_MD * md = NULL;
120 ASSERT(alg < IPSEC_INTEG_N_ALG);
122 if (PREDICT_FALSE(em->esp_integ_algs[alg].md == 0))
125 if (PREDICT_FALSE(alg != em->last_integ_alg)) {
126 md = em->esp_integ_algs[alg].md;
127 em->last_integ_alg = alg;
130 HMAC_Init(ctx, key, key_len, md);
132 HMAC_Update(ctx, data, data_len);
134 if (PREDICT_TRUE(use_esn))
135 HMAC_Update(ctx, (u8 *) &seq_hi, sizeof(seq_hi));
136 HMAC_Final(ctx, signature, &len);
138 return em->esp_integ_algs[alg].trunc_size;