blob: bc67f9d078386ddaed096ffb7a62390f8a1075b4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000015#ifndef __ESP_H__
16#define __ESP_H__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010018#include <vnet/ip/ip.h>
19#include <vnet/ipsec/ipsec.h>
20
Ed Warnickecb9cada2015-12-08 15:45:58 -070021#include <openssl/hmac.h>
22#include <openssl/rand.h>
23#include <openssl/evp.h>
24
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070025typedef struct
26{
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 u32 spi;
28 u32 seq;
29 u8 data[0];
30} esp_header_t;
31
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070032typedef struct
33{
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 u8 pad_length;
35 u8 next_header;
36} esp_footer_t;
37
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070039typedef CLIB_PACKED (struct {
40 ip4_header_t ip4;
41 esp_header_t esp;
42}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070045/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046typedef CLIB_PACKED (struct {
47 ip6_header_t ip6;
48 esp_header_t esp;
49}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070050/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070052typedef struct
53{
54 const EVP_CIPHER *type;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} esp_crypto_alg_t;
56
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070057typedef struct
58{
59 const EVP_MD *md;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 u8 trunc_size;
61} esp_integ_alg_t;
62
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063typedef struct
64{
65 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Matthew Smith29d85102016-05-01 14:52:08 -050066 EVP_CIPHER_CTX encrypt_ctx;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Matthew Smith29d85102016-05-01 14:52:08 -050068 EVP_CIPHER_CTX decrypt_ctx;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070069 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
Matthew Smith29d85102016-05-01 14:52:08 -050070 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076typedef 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081} esp_main_t;
82
Dave Wallace71612d62017-10-24 01:32:41 -040083extern esp_main_t esp_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000085#define ESP_WINDOW_SIZE (64)
86#define ESP_SEQ_MAX (4294967295UL)
87
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010088u8 *format_esp_header (u8 * s, va_list * args);
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000089
90always_inline int
91esp_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
108always_inline int
109esp_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 */
152always_inline void
153esp_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
173always_inline void
174esp_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
210always_inline int
211esp_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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234always_inline void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235esp_init ()
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700237 esp_main_t *em = &esp_main;
238 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
240 memset (em, 0, sizeof (em[0]));
241
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 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 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
248 esp_integ_alg_t *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 i->md = EVP_sha1 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 i->trunc_size = 12;
253
254 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 i->md = EVP_sha256 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 i->trunc_size = 12;
257
258 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 i->md = EVP_sha256 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 i->trunc_size = 16;
261
262 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700263 i->md = EVP_sha384 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 i->trunc_size = 24;
265
266 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700267 i->md = EVP_sha512 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 i->trunc_size = 32;
269
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700270 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
271 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500272 int thread_id;
273
274 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
275 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700276 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));
Matthew Smith29d85102016-05-01 14:52:08 -0500279 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280}
281
282always_inline unsigned int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283hmac_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)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288 esp_main_t *em = &esp_main;
Damjan Marion586afd72017-04-05 19:18:20 +0200289 u32 thread_index = vlib_get_thread_index ();
290 HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700291 const EVP_MD *md = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 unsigned int len;
293
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700294 ASSERT (alg < IPSEC_INTEG_N_ALG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700296 if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 return 0;
298
Damjan Marion586afd72017-04-05 19:18:20 +0200299 if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700300 {
301 md = em->esp_integ_algs[alg].md;
Damjan Marion586afd72017-04-05 19:18:20 +0200302 em->per_thread_data[thread_index].last_integ_alg = alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700303 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700305 HMAC_Init (ctx, key, key_len, md);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700307 HMAC_Update (ctx, data, data_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700309 if (PREDICT_TRUE (use_esn))
310 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
311 HMAC_Final (ctx, signature, &len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 return em->esp_integ_algs[alg].trunc_size;
314}
315
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000316#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700317
318/*
319 * fd.io coding-style-patch-verification: ON
320 *
321 * Local Variables:
322 * eval: (c-set-style "gnu")
323 * End:
324 */