2334dc48bc58a29540494fe83a7767ac02e8746b
[vpp.git] / vnet / vnet / ipsec / esp.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #if DPDK==1
16 #include <vnet/devices/dpdk/dpdk.h>
17 #endif
18
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21 #include <openssl/evp.h>
22
23 typedef struct {
24   u32 spi;
25   u32 seq;
26   u8 data[0];
27 } esp_header_t;
28
29 typedef struct {
30   u8 pad_length;
31   u8 next_header;
32 } esp_footer_t;
33
34 typedef CLIB_PACKED (struct {
35   ip4_header_t ip4;
36   esp_header_t esp;
37 }) ip4_and_esp_header_t;
38
39 typedef CLIB_PACKED (struct {
40   ip6_header_t ip6;
41   esp_header_t esp;
42 }) ip6_and_esp_header_t;
43
44 typedef struct {
45   const EVP_CIPHER * type;
46 } esp_crypto_alg_t;
47
48 typedef struct {
49   const EVP_MD * md;
50   u8 trunc_size;
51 } esp_integ_alg_t;
52
53 typedef struct {
54   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
55   EVP_CIPHER_CTX encrypt_ctx;
56   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
57   EVP_CIPHER_CTX decrypt_ctx;
58   CLIB_CACHE_LINE_ALIGN_MARK(cacheline2);
59   HMAC_CTX hmac_ctx;
60   ipsec_crypto_alg_t last_encrypt_alg;
61   ipsec_crypto_alg_t last_decrypt_alg;
62   ipsec_integ_alg_t last_integ_alg;
63 } esp_main_per_thread_data_t;
64
65 typedef struct {
66   esp_crypto_alg_t * esp_crypto_algs;
67   esp_integ_alg_t * esp_integ_algs;
68   esp_main_per_thread_data_t * per_thread_data;
69 } esp_main_t;
70
71 esp_main_t esp_main;
72
73 always_inline void
74 esp_init()
75 {
76   esp_main_t * em = &esp_main;
77   vlib_thread_main_t * tm = vlib_get_thread_main();
78
79   memset (em, 0, sizeof (em[0]));
80
81   vec_validate(em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
82   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc();
83   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc();
84   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc();
85
86   vec_validate(em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
87   esp_integ_alg_t * i;
88
89   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
90   i->md = EVP_sha1();
91   i->trunc_size = 12;
92
93   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
94   i->md = EVP_sha256();
95   i->trunc_size = 12;
96
97   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
98   i->md = EVP_sha256();
99   i->trunc_size = 16;
100
101   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
102   i->md = EVP_sha384();
103   i->trunc_size = 24;
104
105   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
106   i->md = EVP_sha512();
107   i->trunc_size = 32;
108
109   vec_validate_aligned(em->per_thread_data, tm->n_vlib_mains-1, CLIB_CACHE_LINE_BYTES);
110   int thread_id;
111
112   for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
113     {
114       EVP_CIPHER_CTX_init(&(em->per_thread_data[thread_id].encrypt_ctx));
115       EVP_CIPHER_CTX_init(&(em->per_thread_data[thread_id].decrypt_ctx));
116       HMAC_CTX_init(&(em->per_thread_data[thread_id].hmac_ctx));
117     }
118 }
119
120 always_inline unsigned int
121 hmac_calc(ipsec_integ_alg_t alg,
122           u8 * key,
123           int key_len,
124           u8 * data,
125           int data_len,
126           u8 * signature,
127           u8 use_esn,
128           u32 seq_hi)
129 {
130   esp_main_t * em = &esp_main;
131   u32 cpu_index = os_get_cpu_number();
132   HMAC_CTX * ctx = &(em->per_thread_data[cpu_index].hmac_ctx);
133   const EVP_MD * md = NULL;
134   unsigned int len;
135
136   ASSERT(alg < IPSEC_INTEG_N_ALG);
137
138   if (PREDICT_FALSE(em->esp_integ_algs[alg].md == 0))
139     return 0;
140
141   if (PREDICT_FALSE(alg != em->per_thread_data[cpu_index].last_integ_alg)) {
142     md = em->esp_integ_algs[alg].md;
143     em->per_thread_data[cpu_index].last_integ_alg = alg;
144   }
145
146   HMAC_Init(ctx, key, key_len, md);
147
148   HMAC_Update(ctx, data, data_len);
149
150   if (PREDICT_TRUE(use_esn))
151     HMAC_Update(ctx, (u8 *) &seq_hi, sizeof(seq_hi));
152   HMAC_Final(ctx, signature, &len);
153
154   return em->esp_integ_algs[alg].trunc_size;
155 }
156