Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 15 | #ifndef __ESP_H__ |
| 16 | #define __ESP_H__ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 18 | #include <vnet/ip/ip.h> |
| 19 | #include <vnet/ipsec/ipsec.h> |
| 20 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | #include <openssl/hmac.h> |
| 22 | #include <openssl/rand.h> |
| 23 | #include <openssl/evp.h> |
| 24 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 25 | typedef struct |
| 26 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | u32 spi; |
| 28 | u32 seq; |
| 29 | u8 data[0]; |
| 30 | } esp_header_t; |
| 31 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 32 | typedef struct |
| 33 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | u8 pad_length; |
| 35 | u8 next_header; |
| 36 | } esp_footer_t; |
| 37 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | typedef CLIB_PACKED (struct { |
| 40 | ip4_header_t ip4; |
| 41 | esp_header_t esp; |
| 42 | }) ip4_and_esp_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 43 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 45 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | typedef CLIB_PACKED (struct { |
| 47 | ip6_header_t ip6; |
| 48 | esp_header_t esp; |
| 49 | }) ip6_and_esp_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 50 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
| 54 | const EVP_CIPHER *type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } esp_crypto_alg_t; |
| 56 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
| 59 | const EVP_MD *md; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | u8 trunc_size; |
| 61 | } esp_integ_alg_t; |
| 62 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
| 65 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 66 | EVP_CIPHER_CTX encrypt_ctx; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 67 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 68 | EVP_CIPHER_CTX decrypt_ctx; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 69 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | } esp_main_t; |
| 82 | |
| 83 | esp_main_t esp_main; |
| 84 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 85 | #define ESP_WINDOW_SIZE (64) |
| 86 | #define ESP_SEQ_MAX (4294967295UL) |
| 87 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 88 | u8 *format_esp_header (u8 * s, va_list * args); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | always_inline void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 235 | esp_init () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 237 | esp_main_t *em = &esp_main; |
| 238 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
| 240 | memset (em, 0, sizeof (em[0])); |
| 241 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 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 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 247 | vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1); |
| 248 | esp_integ_alg_t *i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
| 250 | i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 251 | i->md = EVP_sha1 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | i->trunc_size = 12; |
| 253 | |
| 254 | i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 255 | i->md = EVP_sha256 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | i->trunc_size = 12; |
| 257 | |
| 258 | i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 259 | i->md = EVP_sha256 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | i->trunc_size = 16; |
| 261 | |
| 262 | i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 263 | i->md = EVP_sha384 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | i->trunc_size = 24; |
| 265 | |
| 266 | i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 267 | i->md = EVP_sha512 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | i->trunc_size = 32; |
| 269 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 270 | vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1, |
| 271 | CLIB_CACHE_LINE_BYTES); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 272 | int thread_id; |
| 273 | |
| 274 | for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++) |
| 275 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 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)); |
Matthew Smith | 29d8510 | 2016-05-01 14:52:08 -0500 | [diff] [blame] | 279 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | always_inline unsigned int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 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) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 288 | esp_main_t *em = &esp_main; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 289 | u32 thread_index = vlib_get_thread_index (); |
| 290 | HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 291 | const EVP_MD *md = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | unsigned int len; |
| 293 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 294 | ASSERT (alg < IPSEC_INTEG_N_ALG); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 296 | if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 299 | if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 300 | { |
| 301 | md = em->esp_integ_algs[alg].md; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 302 | em->per_thread_data[thread_index].last_integ_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 303 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 305 | HMAC_Init (ctx, key, key_len, md); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 307 | HMAC_Update (ctx, data, data_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 309 | if (PREDICT_TRUE (use_esn)) |
| 310 | HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi)); |
| 311 | HMAC_Final (ctx, signature, &len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | |
| 313 | return em->esp_integ_algs[alg].trunc_size; |
| 314 | } |
| 315 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 316 | #endif /* __ESP_H__ */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * fd.io coding-style-patch-verification: ON |
| 320 | * |
| 321 | * Local Variables: |
| 322 | * eval: (c-set-style "gnu") |
| 323 | * End: |
| 324 | */ |