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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip/ip.h>
19 #include <vnet/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
40 typedef CLIB_PACKED (struct {
43 }) ip4_and_esp_header_t;
47 typedef CLIB_PACKED (struct {
51 }) ip4_and_udp_and_esp_header_t;
55 typedef CLIB_PACKED (struct {
58 }) ip6_and_esp_header_t;
62 * AES GCM Additional Authentication data
64 typedef struct esp_aead_t_
67 * for GCM: when using ESN it's:
68 * SPI, seq-hi, seg-low
73 } __clib_packed esp_aead_t;
75 #define ESP_SEQ_MAX (4294967295UL)
76 #define ESP_MAX_BLOCK_SIZE (16)
77 #define ESP_MAX_IV_SIZE (16)
78 #define ESP_MAX_ICV_SIZE (32)
80 u8 *format_esp_header (u8 * s, va_list * args);
82 /* TODO seq increment should be atomic to be accessed by multiple workers */
84 esp_seq_advance (ipsec_sa_t * sa)
86 if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
88 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
90 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
91 sa->seq_hi == ESP_SEQ_MAX))
99 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
100 sa->seq == ESP_SEQ_MAX))
109 always_inline unsigned int
110 hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len,
113 vnet_crypto_op_t _op, *op = &_op;
115 if (PREDICT_FALSE (sa->integ_op_id == 0))
118 vnet_crypto_op_init (op, sa->integ_op_id);
119 op->key_index = sa->integ_key_index;
122 op->digest = signature;
123 op->digest_len = sa->integ_icv_size;
125 if (ipsec_sa_is_set_USE_ESN (sa))
127 u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
130 clib_memcpy (data + data_len, &seq_hi, 4);
133 vnet_crypto_process_ops (vm, op, 1);
134 return sa->integ_icv_size;
138 esp_aad_fill (vnet_crypto_op_t * op,
139 const esp_header_t * esp, const ipsec_sa_t * sa)
143 aad = (esp_aead_t *) op->aad;
144 aad->data[0] = esp->spi;
146 if (ipsec_sa_is_set_USE_ESN (sa))
148 /* SPI, seq-hi, seq-low */
149 aad->data[1] = clib_host_to_net_u32 (sa->seq_hi);
150 aad->data[2] = esp->seq;
156 aad->data[1] = esp->seq;
160 #endif /* __ESP_H__ */
163 * fd.io coding-style-patch-verification: ON
166 * eval: (c-set-style "gnu")