ipsec: add support for AES CTR
[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/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
21
22 typedef struct
23 {
24   union
25   {
26     u32 spi;
27     u8 spi_bytes[4];
28   };
29   u32 seq;
30   u8 data[0];
31 } esp_header_t;
32
33 typedef struct
34 {
35   u8 pad_length;
36   u8 next_header;
37 } esp_footer_t;
38
39 /* *INDENT-OFF* */
40 typedef CLIB_PACKED (struct {
41   ip4_header_t ip4;
42   esp_header_t esp;
43 }) ip4_and_esp_header_t;
44 /* *INDENT-ON* */
45
46 /* *INDENT-OFF* */
47 typedef CLIB_PACKED (struct {
48   ip4_header_t ip4;
49   udp_header_t udp;
50   esp_header_t esp;
51 }) ip4_and_udp_and_esp_header_t;
52 /* *INDENT-ON* */
53
54 /* *INDENT-OFF* */
55 typedef CLIB_PACKED (struct {
56   ip6_header_t ip6;
57   esp_header_t esp;
58 }) ip6_and_esp_header_t;
59 /* *INDENT-ON* */
60
61 /**
62  * AES counter mode nonce
63  */
64 typedef struct
65 {
66   u32 salt;
67   u64 iv;
68   u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */
69 } __clib_packed esp_ctr_nonce_t;
70
71 STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16);
72
73 /**
74  * AES GCM Additional Authentication data
75  */
76 typedef struct esp_aead_t_
77 {
78   /**
79    * for GCM: when using ESN it's:
80    *   SPI, seq-hi, seg-low
81    * else
82    *   SPI, seq-low
83    */
84   u32 data[3];
85 } __clib_packed esp_aead_t;
86
87 #define ESP_SEQ_MAX             (4294967295UL)
88 #define ESP_MAX_BLOCK_SIZE      (16)
89 #define ESP_MAX_IV_SIZE         (16)
90 #define ESP_MAX_ICV_SIZE        (32)
91
92 u8 *format_esp_header (u8 * s, va_list * args);
93
94 /* TODO seq increment should be atomic to be accessed by multiple workers */
95 always_inline int
96 esp_seq_advance (ipsec_sa_t * sa)
97 {
98   if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
99     {
100       if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
101         {
102           if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
103                              sa->seq_hi == ESP_SEQ_MAX))
104             return 1;
105           sa->seq_hi++;
106         }
107       sa->seq++;
108     }
109   else
110     {
111       if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
112                          sa->seq == ESP_SEQ_MAX))
113         return 1;
114       sa->seq++;
115     }
116
117   return 0;
118 }
119
120 always_inline u16
121 esp_aad_fill (u8 * data, const esp_header_t * esp, const ipsec_sa_t * sa)
122 {
123   esp_aead_t *aad;
124
125   aad = (esp_aead_t *) data;
126   aad->data[0] = esp->spi;
127
128   if (ipsec_sa_is_set_USE_ESN (sa))
129     {
130       /* SPI, seq-hi, seq-low */
131       aad->data[1] = (u32) clib_host_to_net_u32 (sa->seq_hi);
132       aad->data[2] = esp->seq;
133       return 12;
134     }
135   else
136     {
137       /* SPI, seq-low */
138       aad->data[1] = esp->seq;
139       return 8;
140     }
141 }
142
143 /* Special case to drop or hand off packets for sync/async modes.
144  *
145  * Different than sync mode, async mode only enqueue drop or hand-off packets
146  * to next nodes.
147  */
148 always_inline void
149 esp_set_next_index (int is_async, u32 * from, u16 * nexts, u32 bi,
150                     u16 * drop_index, u16 drop_next, u16 * next)
151 {
152   if (is_async)
153     {
154       from[*drop_index] = bi;
155       nexts[*drop_index] = drop_next;
156       *drop_index += 1;
157     }
158   else
159     next[0] = drop_next;
160 }
161
162 /* when submitting a frame is failed, drop all buffers in the frame */
163 always_inline void
164 esp_async_recycle_failed_submit (vnet_crypto_async_frame_t * f,
165                                  vlib_buffer_t ** b, u32 * from, u16 * nexts,
166                                  u16 * n_dropped, u16 drop_next_index,
167                                  vlib_error_t err)
168 {
169   u32 n_drop = f->n_elts;
170   u32 *bi = f->buffer_indices;
171   b -= n_drop;
172   while (n_drop--)
173     {
174       b[0]->error = err;
175       esp_set_next_index (1, from, nexts, bi[0], n_dropped, drop_next_index,
176                           NULL);
177       bi++;
178       b++;
179     }
180   vnet_crypto_async_reset_frame (f);
181 }
182
183 /**
184  * The post data structure to for esp_encrypt/decrypt_inline to write to
185  * vib_buffer_t opaque unused field, and for post nodes to pick up after
186  * dequeue.
187  **/
188 typedef struct
189 {
190   union
191   {
192     struct
193     {
194       u8 icv_sz;
195       u8 iv_sz;
196       ipsec_sa_flags_t flags;
197       u32 sa_index;
198     };
199     u64 sa_data;
200   };
201
202   u32 seq;
203   i16 current_data;
204   i16 current_length;
205   u16 hdr_sz;
206   u16 is_chain;
207   u32 protect_index;
208 } esp_decrypt_packet_data_t;
209
210 STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
211 STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64));
212
213 /* we are forced to store the decrypt post data into 2 separate places -
214    vlib_opaque and opaque2. */
215 typedef struct
216 {
217   vlib_buffer_t *lb;
218   u32 free_buffer_index;
219   u8 icv_removed;
220 } esp_decrypt_packet_data2_t;
221
222 typedef union
223 {
224   u16 next_index;
225   esp_decrypt_packet_data_t decrypt_data;
226 } esp_post_data_t;
227
228 STATIC_ASSERT (sizeof (esp_post_data_t) <=
229                STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
230                "Custom meta-data too large for vnet_buffer_opaque_t");
231
232 #define esp_post_data(b) \
233     ((esp_post_data_t *)((u8 *)((b)->opaque) \
234         + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))
235
236 STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <=
237                STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused),
238                "Custom meta-data too large for vnet_buffer_opaque2_t");
239
240 #define esp_post_data2(b) \
241     ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \
242         + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused)))
243
244 typedef struct
245 {
246   /* esp post node index for async crypto */
247   u32 esp4_post_next;
248   u32 esp6_post_next;
249   u32 esp4_tun_post_next;
250   u32 esp6_tun_post_next;
251   u32 esp_mpls_tun_post_next;
252 } esp_async_post_next_t;
253
254 extern esp_async_post_next_t esp_encrypt_async_next;
255 extern esp_async_post_next_t esp_decrypt_async_next;
256
257 #endif /* __ESP_H__ */
258
259 /*
260  * fd.io coding-style-patch-verification: ON
261  *
262  * Local Variables:
263  * eval: (c-set-style "gnu")
264  * End:
265  */