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