blob: 50cac806d14e5d385ce42645653ac4048dbff209 [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
18#include <openssl/hmac.h>
19#include <openssl/rand.h>
20#include <openssl/evp.h>
21
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070022typedef struct
23{
Ed Warnickecb9cada2015-12-08 15:45:58 -070024 u32 spi;
25 u32 seq;
26 u8 data[0];
27} esp_header_t;
28
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070029typedef struct
30{
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 u8 pad_length;
32 u8 next_header;
33} esp_footer_t;
34
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070036typedef CLIB_PACKED (struct {
37 ip4_header_t ip4;
38 esp_header_t esp;
39}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070042/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043typedef CLIB_PACKED (struct {
44 ip6_header_t ip6;
45 esp_header_t esp;
46}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070047/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070049typedef struct
50{
51 const EVP_CIPHER *type;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052} esp_crypto_alg_t;
53
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054typedef struct
55{
56 const EVP_MD *md;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 u8 trunc_size;
58} esp_integ_alg_t;
59
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070060typedef struct
61{
62 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Matthew Smith29d85102016-05-01 14:52:08 -050063 EVP_CIPHER_CTX encrypt_ctx;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070064 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Matthew Smith29d85102016-05-01 14:52:08 -050065 EVP_CIPHER_CTX decrypt_ctx;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070066 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
Matthew Smith29d85102016-05-01 14:52:08 -050067 HMAC_CTX hmac_ctx;
68 ipsec_crypto_alg_t last_encrypt_alg;
69 ipsec_crypto_alg_t last_decrypt_alg;
70 ipsec_integ_alg_t last_integ_alg;
71} esp_main_per_thread_data_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073typedef struct
74{
75 esp_crypto_alg_t *esp_crypto_algs;
76 esp_integ_alg_t *esp_integ_algs;
77 esp_main_per_thread_data_t *per_thread_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078} esp_main_t;
79
80esp_main_t esp_main;
81
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000082#define ESP_WINDOW_SIZE (64)
83#define ESP_SEQ_MAX (4294967295UL)
84
85
86always_inline int
87esp_replay_check (ipsec_sa_t * sa, u32 seq)
88{
89 u32 diff;
90
91 if (PREDICT_TRUE (seq > sa->last_seq))
92 return 0;
93
94 diff = sa->last_seq - seq;
95
96 if (ESP_WINDOW_SIZE > diff)
97 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
98 else
99 return 1;
100
101 return 0;
102}
103
104always_inline int
105esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
106{
107 u32 tl = sa->last_seq;
108 u32 th = sa->last_seq_hi;
109 u32 diff = tl - seq;
110
111 if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
112 {
113 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
114 {
115 sa->seq_hi = th;
116 if (seq <= tl)
117 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
118 else
119 return 0;
120 }
121 else
122 {
123 sa->seq_hi = th + 1;
124 return 0;
125 }
126 }
127 else
128 {
129 if (seq >= (tl - ESP_WINDOW_SIZE + 1))
130 {
131 sa->seq_hi = th - 1;
132 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
133 }
134 else
135 {
136 sa->seq_hi = th;
137 if (seq <= tl)
138 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
139 else
140 return 0;
141 }
142 }
143
144 return 0;
145}
146
147/* TODO seq increment should be atomic to be accessed by multiple workers */
148always_inline void
149esp_replay_advance (ipsec_sa_t * sa, u32 seq)
150{
151 u32 pos;
152
153 if (seq > sa->last_seq)
154 {
155 pos = seq - sa->last_seq;
156 if (pos < ESP_WINDOW_SIZE)
157 sa->replay_window = ((sa->replay_window) << pos) | 1;
158 else
159 sa->replay_window = 1;
160 sa->last_seq = seq;
161 }
162 else
163 {
164 pos = sa->last_seq - seq;
165 sa->replay_window |= (1ULL << pos);
166 }
167}
168
169always_inline void
170esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
171{
172 int wrap = sa->seq_hi - sa->last_seq_hi;
173 u32 pos;
174
175 if (wrap == 0 && seq > sa->last_seq)
176 {
177 pos = seq - sa->last_seq;
178 if (pos < ESP_WINDOW_SIZE)
179 sa->replay_window = ((sa->replay_window) << pos) | 1;
180 else
181 sa->replay_window = 1;
182 sa->last_seq = seq;
183 }
184 else if (wrap > 0)
185 {
186 pos = ~seq + sa->last_seq + 1;
187 if (pos < ESP_WINDOW_SIZE)
188 sa->replay_window = ((sa->replay_window) << pos) | 1;
189 else
190 sa->replay_window = 1;
191 sa->last_seq = seq;
192 sa->last_seq_hi = sa->seq_hi;
193 }
194 else if (wrap < 0)
195 {
196 pos = ~seq + sa->last_seq + 1;
197 sa->replay_window |= (1ULL << pos);
198 }
199 else
200 {
201 pos = sa->last_seq - seq;
202 sa->replay_window |= (1ULL << pos);
203 }
204}
205
206always_inline int
207esp_seq_advance (ipsec_sa_t * sa)
208{
209 if (PREDICT_TRUE (sa->use_esn))
210 {
211 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
212 {
213 if (PREDICT_FALSE
214 (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
215 return 1;
216 sa->seq_hi++;
217 }
218 sa->seq++;
219 }
220 else
221 {
222 if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
223 return 1;
224 sa->seq++;
225 }
226
227 return 0;
228}
229
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230always_inline void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231esp_init ()
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 esp_main_t *em = &esp_main;
234 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
236 memset (em, 0, sizeof (em[0]));
237
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
239 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
240 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
241 em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
244 esp_integ_alg_t *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
246 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 i->md = EVP_sha1 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 i->trunc_size = 12;
249
250 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 i->md = EVP_sha256 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 i->trunc_size = 12;
253
254 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
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 = 16;
257
258 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 i->md = EVP_sha384 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 i->trunc_size = 24;
261
262 i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700263 i->md = EVP_sha512 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 i->trunc_size = 32;
265
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700266 vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
267 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500268 int thread_id;
269
270 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
271 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700272 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
273 EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
274 HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
Matthew Smith29d85102016-05-01 14:52:08 -0500275 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276}
277
278always_inline unsigned int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700279hmac_calc (ipsec_integ_alg_t alg,
280 u8 * key,
281 int key_len,
282 u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700284 esp_main_t *em = &esp_main;
285 u32 cpu_index = os_get_cpu_number ();
286 HMAC_CTX *ctx = &(em->per_thread_data[cpu_index].hmac_ctx);
287 const EVP_MD *md = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 unsigned int len;
289
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700290 ASSERT (alg < IPSEC_INTEG_N_ALG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292 if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 return 0;
294
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700295 if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_integ_alg))
296 {
297 md = em->esp_integ_algs[alg].md;
298 em->per_thread_data[cpu_index].last_integ_alg = alg;
299 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700301 HMAC_Init (ctx, key, key_len, md);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700303 HMAC_Update (ctx, data, data_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700305 if (PREDICT_TRUE (use_esn))
306 HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
307 HMAC_Final (ctx, signature, &len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
309 return em->esp_integ_algs[alg].trunc_size;
310}
311
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000312#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700313
314/*
315 * fd.io coding-style-patch-verification: ON
316 *
317 * Local Variables:
318 * eval: (c-set-style "gnu")
319 * End:
320 */