dpdk/ipsec: rework plus improved cli commands
[vpp.git] / src / 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 #ifndef __ESP_H__
16 #define __ESP_H__
17
18 #include <vnet/ip/ip.h>
19 #include <vnet/ipsec/ipsec.h>
20
21 #include <openssl/hmac.h>
22 #include <openssl/rand.h>
23 #include <openssl/evp.h>
24
25 typedef struct
26 {
27   u32 spi;
28   u32 seq;
29   u8 data[0];
30 } esp_header_t;
31
32 typedef struct
33 {
34   u8 pad_length;
35   u8 next_header;
36 } esp_footer_t;
37
38 /* *INDENT-OFF* */
39 typedef CLIB_PACKED (struct {
40   ip4_header_t ip4;
41   esp_header_t esp;
42 }) ip4_and_esp_header_t;
43 /* *INDENT-ON* */
44
45 /* *INDENT-OFF* */
46 typedef CLIB_PACKED (struct {
47   ip6_header_t ip6;
48   esp_header_t esp;
49 }) ip6_and_esp_header_t;
50 /* *INDENT-ON* */
51
52 typedef struct
53 {
54   const EVP_CIPHER *type;
55 } esp_crypto_alg_t;
56
57 typedef struct
58 {
59   const EVP_MD *md;
60   u8 trunc_size;
61 } esp_integ_alg_t;
62
63 typedef struct
64 {
65   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
66   EVP_CIPHER_CTX encrypt_ctx;
67     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
68   EVP_CIPHER_CTX decrypt_ctx;
69     CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
70   HMAC_CTX hmac_ctx;
71   ipsec_crypto_alg_t last_encrypt_alg;
72   ipsec_crypto_alg_t last_decrypt_alg;
73   ipsec_integ_alg_t last_integ_alg;
74 } esp_main_per_thread_data_t;
75
76 typedef struct
77 {
78   esp_crypto_alg_t *esp_crypto_algs;
79   esp_integ_alg_t *esp_integ_algs;
80   esp_main_per_thread_data_t *per_thread_data;
81 } esp_main_t;
82
83 esp_main_t esp_main;
84
85 #define ESP_WINDOW_SIZE         (64)
86 #define ESP_SEQ_MAX             (4294967295UL)
87
88 u8 *format_esp_header (u8 * s, va_list * args);
89
90 always_inline int
91 esp_replay_check (ipsec_sa_t * sa, u32 seq)
92 {
93   u32 diff;
94
95   if (PREDICT_TRUE (seq > sa->last_seq))
96     return 0;
97
98   diff = sa->last_seq - seq;
99
100   if (ESP_WINDOW_SIZE > diff)
101     return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
102   else
103     return 1;
104
105   return 0;
106 }
107
108 always_inline int
109 esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
110 {
111   u32 tl = sa->last_seq;
112   u32 th = sa->last_seq_hi;
113   u32 diff = tl - seq;
114
115   if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
116     {
117       if (seq >= (tl - ESP_WINDOW_SIZE + 1))
118         {
119           sa->seq_hi = th;
120           if (seq <= tl)
121             return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
122           else
123             return 0;
124         }
125       else
126         {
127           sa->seq_hi = th + 1;
128           return 0;
129         }
130     }
131   else
132     {
133       if (seq >= (tl - ESP_WINDOW_SIZE + 1))
134         {
135           sa->seq_hi = th - 1;
136           return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
137         }
138       else
139         {
140           sa->seq_hi = th;
141           if (seq <= tl)
142             return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
143           else
144             return 0;
145         }
146     }
147
148   return 0;
149 }
150
151 /* TODO seq increment should be atomic to be accessed by multiple workers */
152 always_inline void
153 esp_replay_advance (ipsec_sa_t * sa, u32 seq)
154 {
155   u32 pos;
156
157   if (seq > sa->last_seq)
158     {
159       pos = seq - sa->last_seq;
160       if (pos < ESP_WINDOW_SIZE)
161         sa->replay_window = ((sa->replay_window) << pos) | 1;
162       else
163         sa->replay_window = 1;
164       sa->last_seq = seq;
165     }
166   else
167     {
168       pos = sa->last_seq - seq;
169       sa->replay_window |= (1ULL << pos);
170     }
171 }
172
173 always_inline void
174 esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
175 {
176   int wrap = sa->seq_hi - sa->last_seq_hi;
177   u32 pos;
178
179   if (wrap == 0 && seq > sa->last_seq)
180     {
181       pos = seq - sa->last_seq;
182       if (pos < ESP_WINDOW_SIZE)
183         sa->replay_window = ((sa->replay_window) << pos) | 1;
184       else
185         sa->replay_window = 1;
186       sa->last_seq = seq;
187     }
188   else if (wrap > 0)
189     {
190       pos = ~seq + sa->last_seq + 1;
191       if (pos < ESP_WINDOW_SIZE)
192         sa->replay_window = ((sa->replay_window) << pos) | 1;
193       else
194         sa->replay_window = 1;
195       sa->last_seq = seq;
196       sa->last_seq_hi = sa->seq_hi;
197     }
198   else if (wrap < 0)
199     {
200       pos = ~seq + sa->last_seq + 1;
201       sa->replay_window |= (1ULL << pos);
202     }
203   else
204     {
205       pos = sa->last_seq - seq;
206       sa->replay_window |= (1ULL << pos);
207     }
208 }
209
210 always_inline int
211 esp_seq_advance (ipsec_sa_t * sa)
212 {
213   if (PREDICT_TRUE (sa->use_esn))
214     {
215       if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
216         {
217           if (PREDICT_FALSE
218               (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
219             return 1;
220           sa->seq_hi++;
221         }
222       sa->seq++;
223     }
224   else
225     {
226       if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
227         return 1;
228       sa->seq++;
229     }
230
231   return 0;
232 }
233
234 always_inline void
235 esp_init ()
236 {
237   esp_main_t *em = &esp_main;
238   vlib_thread_main_t *tm = vlib_get_thread_main ();
239
240   memset (em, 0, sizeof (em[0]));
241
242   vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
243   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
244   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
245   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
246
247   vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
248   esp_integ_alg_t *i;
249
250   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
251   i->md = EVP_sha1 ();
252   i->trunc_size = 12;
253
254   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
255   i->md = EVP_sha256 ();
256   i->trunc_size = 12;
257
258   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
259   i->md = EVP_sha256 ();
260   i->trunc_size = 16;
261
262   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
263   i->md = EVP_sha384 ();
264   i->trunc_size = 24;
265
266   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
267   i->md = EVP_sha512 ();
268   i->trunc_size = 32;
269
270   vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
271                         CLIB_CACHE_LINE_BYTES);
272   int thread_id;
273
274   for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
275     {
276       EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
277       EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
278       HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
279     }
280 }
281
282 always_inline unsigned int
283 hmac_calc (ipsec_integ_alg_t alg,
284            u8 * key,
285            int key_len,
286            u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
287 {
288   esp_main_t *em = &esp_main;
289   u32 thread_index = vlib_get_thread_index ();
290   HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
291   const EVP_MD *md = NULL;
292   unsigned int len;
293
294   ASSERT (alg < IPSEC_INTEG_N_ALG);
295
296   if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
297     return 0;
298
299   if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
300     {
301       md = em->esp_integ_algs[alg].md;
302       em->per_thread_data[thread_index].last_integ_alg = alg;
303     }
304
305   HMAC_Init (ctx, key, key_len, md);
306
307   HMAC_Update (ctx, data, data_len);
308
309   if (PREDICT_TRUE (use_esn))
310     HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
311   HMAC_Final (ctx, signature, &len);
312
313   return em->esp_integ_algs[alg].trunc_size;
314 }
315
316 #endif /* __ESP_H__ */
317
318 /*
319  * fd.io coding-style-patch-verification: ON
320  *
321  * Local Variables:
322  * eval: (c-set-style "gnu")
323  * End:
324  */