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> |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 19 | #include <vnet/crypto/crypto.h> |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 20 | #include <vnet/ipsec/ipsec.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 { |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 44 | ip4_header_t ip4; |
| 45 | udp_header_t udp; |
| 46 | esp_header_t esp; |
| 47 | }) ip4_and_udp_and_esp_header_t; |
| 48 | /* *INDENT-ON* */ |
| 49 | |
| 50 | /* *INDENT-OFF* */ |
| 51 | typedef CLIB_PACKED (struct { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | ip6_header_t ip6; |
| 53 | esp_header_t esp; |
| 54 | }) ip6_and_esp_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 55 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 57 | /** |
| 58 | * AES GCM Additional Authentication data |
| 59 | */ |
| 60 | typedef struct esp_aead_t_ |
| 61 | { |
| 62 | /** |
| 63 | * for GCM: when using ESN it's: |
| 64 | * SPI, seq-hi, seg-low |
| 65 | * else |
| 66 | * SPI, seq-low |
| 67 | */ |
| 68 | u32 data[3]; |
| 69 | } __clib_packed esp_aead_t; |
| 70 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 71 | #define ESP_SEQ_MAX (4294967295UL) |
| 72 | #define ESP_MAX_BLOCK_SIZE (16) |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 73 | #define ESP_MAX_IV_SIZE (16) |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 74 | #define ESP_MAX_ICV_SIZE (32) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 75 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 76 | u8 *format_esp_header (u8 * s, va_list * args); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 77 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 78 | /* TODO seq increment should be atomic to be accessed by multiple workers */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 79 | always_inline int |
| 80 | esp_seq_advance (ipsec_sa_t * sa) |
| 81 | { |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 82 | if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa))) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 83 | { |
| 84 | if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX)) |
| 85 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 86 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 87 | sa->seq_hi == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 88 | return 1; |
| 89 | sa->seq_hi++; |
| 90 | } |
| 91 | sa->seq++; |
| 92 | } |
| 93 | else |
| 94 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 95 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 96 | sa->seq == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 97 | return 1; |
| 98 | sa->seq++; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | always_inline unsigned int |
Damjan Marion | b966e8b | 2019-03-20 16:07:09 +0100 | [diff] [blame] | 106 | hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len, |
| 107 | u8 * signature) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 109 | vnet_crypto_op_t _op, *op = &_op; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 111 | if (PREDICT_FALSE (sa->integ_op_id == 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 114 | vnet_crypto_op_init (op, sa->integ_op_id); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 115 | op->key_index = sa->integ_key_index; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 116 | op->src = data; |
| 117 | op->len = data_len; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 118 | op->digest = signature; |
| 119 | op->digest_len = sa->integ_icv_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 121 | if (ipsec_sa_is_set_USE_ESN (sa)) |
Damjan Marion | af73eda | 2019-03-20 16:30:54 +0100 | [diff] [blame] | 122 | { |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 123 | u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi); |
| 124 | |
Damjan Marion | af73eda | 2019-03-20 16:30:54 +0100 | [diff] [blame] | 125 | op->len += 4; |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 126 | clib_memcpy (data + data_len, &seq_hi, 4); |
Damjan Marion | af73eda | 2019-03-20 16:30:54 +0100 | [diff] [blame] | 127 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 129 | vnet_crypto_process_ops (vm, op, 1); |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 130 | return sa->integ_icv_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 133 | always_inline void |
| 134 | esp_aad_fill (vnet_crypto_op_t * op, |
| 135 | const esp_header_t * esp, const ipsec_sa_t * sa) |
| 136 | { |
| 137 | esp_aead_t *aad; |
| 138 | |
| 139 | aad = (esp_aead_t *) op->aad; |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 140 | aad->data[0] = esp->spi; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 141 | |
| 142 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 143 | { |
| 144 | /* SPI, seq-hi, seq-low */ |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 145 | aad->data[1] = clib_host_to_net_u32 (sa->seq_hi); |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 146 | aad->data[2] = esp->seq; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 147 | op->aad_len = 12; |
| 148 | } |
| 149 | else |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 150 | { |
| 151 | /* SPI, seq-low */ |
| 152 | aad->data[1] = esp->seq; |
| 153 | op->aad_len = 8; |
| 154 | } |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 155 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 156 | #endif /* __ESP_H__ */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * fd.io coding-style-patch-verification: ON |
| 160 | * |
| 161 | * Local Variables: |
| 162 | * eval: (c-set-style "gnu") |
| 163 | * End: |
| 164 | */ |