dpdk: remove rte_mbuf modifications at many places in the code
[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
16 #include <openssl/hmac.h>
17 #include <openssl/rand.h>
18 #include <openssl/evp.h>
19
20 typedef struct
21 {
22   u32 spi;
23   u32 seq;
24   u8 data[0];
25 } esp_header_t;
26
27 typedef struct
28 {
29   u8 pad_length;
30   u8 next_header;
31 } esp_footer_t;
32
33 /* *INDENT-OFF* */
34 typedef CLIB_PACKED (struct {
35   ip4_header_t ip4;
36   esp_header_t esp;
37 }) ip4_and_esp_header_t;
38 /* *INDENT-ON* */
39
40 /* *INDENT-OFF* */
41 typedef CLIB_PACKED (struct {
42   ip6_header_t ip6;
43   esp_header_t esp;
44 }) ip6_and_esp_header_t;
45 /* *INDENT-ON* */
46
47 typedef struct
48 {
49   const EVP_CIPHER *type;
50 } esp_crypto_alg_t;
51
52 typedef struct
53 {
54   const EVP_MD *md;
55   u8 trunc_size;
56 } esp_integ_alg_t;
57
58 typedef struct
59 {
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);
65   HMAC_CTX hmac_ctx;
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;
70
71 typedef struct
72 {
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;
76 } esp_main_t;
77
78 esp_main_t esp_main;
79
80 always_inline void
81 esp_init ()
82 {
83   esp_main_t *em = &esp_main;
84   vlib_thread_main_t *tm = vlib_get_thread_main ();
85
86   memset (em, 0, sizeof (em[0]));
87
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 ();
92
93   vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
94   esp_integ_alg_t *i;
95
96   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
97   i->md = EVP_sha1 ();
98   i->trunc_size = 12;
99
100   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
101   i->md = EVP_sha256 ();
102   i->trunc_size = 12;
103
104   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
105   i->md = EVP_sha256 ();
106   i->trunc_size = 16;
107
108   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
109   i->md = EVP_sha384 ();
110   i->trunc_size = 24;
111
112   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
113   i->md = EVP_sha512 ();
114   i->trunc_size = 32;
115
116   vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
117                         CLIB_CACHE_LINE_BYTES);
118   int thread_id;
119
120   for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
121     {
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));
125     }
126 }
127
128 always_inline unsigned int
129 hmac_calc (ipsec_integ_alg_t alg,
130            u8 * key,
131            int key_len,
132            u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
133 {
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;
138   unsigned int len;
139
140   ASSERT (alg < IPSEC_INTEG_N_ALG);
141
142   if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
143     return 0;
144
145   if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_integ_alg))
146     {
147       md = em->esp_integ_algs[alg].md;
148       em->per_thread_data[cpu_index].last_integ_alg = alg;
149     }
150
151   HMAC_Init (ctx, key, key_len, md);
152
153   HMAC_Update (ctx, data, data_len);
154
155   if (PREDICT_TRUE (use_esn))
156     HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
157   HMAC_Final (ctx, signature, &len);
158
159   return em->esp_integ_algs[alg].trunc_size;
160 }
161
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */