blob: 2ab7299d7c8f32b1166d16e35d84fbcb01aac621 [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
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010018#include <vnet/ip/ip.h>
Damjan Marion91f17dc2019-03-18 18:59:25 +010019#include <vnet/crypto/crypto.h>
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010020#include <vnet/ipsec/ipsec.h>
21
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070022typedef struct
23{
Neale Ranns41afb332019-07-16 06:19:35 -070024 union
25 {
26 u32 spi;
27 u8 spi_bytes[4];
28 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 u32 seq;
30 u8 data[0];
31} esp_header_t;
32
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033typedef struct
34{
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 u8 pad_length;
36 u8 next_header;
37} esp_footer_t;
38
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040typedef CLIB_PACKED (struct {
41 ip4_header_t ip4;
42 esp_header_t esp;
43}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070046/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047typedef CLIB_PACKED (struct {
Klement Sekera4b089f22018-04-17 18:04:57 +020048 ip4_header_t ip4;
49 udp_header_t udp;
50 esp_header_t esp;
51}) ip4_and_udp_and_esp_header_t;
52/* *INDENT-ON* */
53
54/* *INDENT-OFF* */
55typedef CLIB_PACKED (struct {
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 ip6_header_t ip6;
57 esp_header_t esp;
58}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070059/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Neale Ranns47feb112019-04-11 15:14:07 +000061/**
62 * AES GCM Additional Authentication data
63 */
64typedef struct esp_aead_t_
65{
66 /**
67 * for GCM: when using ESN it's:
68 * SPI, seq-hi, seg-low
69 * else
70 * SPI, seq-low
71 */
72 u32 data[3];
73} __clib_packed esp_aead_t;
74
Damjan Marionc59b9a22019-03-19 15:38:40 +010075#define ESP_SEQ_MAX (4294967295UL)
76#define ESP_MAX_BLOCK_SIZE (16)
Damjan Marionb4fff3a2019-03-25 15:54:40 +010077#define ESP_MAX_IV_SIZE (16)
Neale Ranns1091c4a2019-04-08 14:48:23 +000078#define ESP_MAX_ICV_SIZE (32)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000079
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010080u8 *format_esp_header (u8 * s, va_list * args);
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000081
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000082/* TODO seq increment should be atomic to be accessed by multiple workers */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000083always_inline int
84esp_seq_advance (ipsec_sa_t * sa)
85{
Damjan Marion1e3aa5e2019-03-28 10:58:59 +010086 if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000087 {
88 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
89 {
Damjan Mariond709cbc2019-03-26 13:16:42 +010090 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
91 sa->seq_hi == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000092 return 1;
93 sa->seq_hi++;
94 }
95 sa->seq++;
96 }
97 else
98 {
Damjan Mariond709cbc2019-03-26 13:16:42 +010099 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
100 sa->seq == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000101 return 1;
102 sa->seq++;
103 }
104
105 return 0;
106}
107
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109always_inline unsigned int
Damjan Marionb966e8b2019-03-20 16:07:09 +0100110hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len,
111 u8 * signature)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100113 vnet_crypto_op_t _op, *op = &_op;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
Damjan Marion060bfb92019-03-29 13:47:54 +0100115 if (PREDICT_FALSE (sa->integ_op_id == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 return 0;
117
Damjan Marion060bfb92019-03-29 13:47:54 +0100118 vnet_crypto_op_init (op, sa->integ_op_id);
Damjan Mariond1bed682019-04-24 15:20:35 +0200119 op->key_index = sa->integ_key_index;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100120 op->src = data;
121 op->len = data_len;
Damjan Marion060bfb92019-03-29 13:47:54 +0100122 op->digest = signature;
123 op->digest_len = sa->integ_icv_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100125 if (ipsec_sa_is_set_USE_ESN (sa))
Damjan Marionaf73eda2019-03-20 16:30:54 +0100126 {
Neale Ranns3833ffd2019-03-21 14:34:09 +0000127 u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
128
Damjan Marionaf73eda2019-03-20 16:30:54 +0100129 op->len += 4;
Neale Ranns3833ffd2019-03-21 14:34:09 +0000130 clib_memcpy (data + data_len, &seq_hi, 4);
Damjan Marionaf73eda2019-03-20 16:30:54 +0100131 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Damjan Marion91f17dc2019-03-18 18:59:25 +0100133 vnet_crypto_process_ops (vm, op, 1);
Damjan Marion7c22ff72019-04-04 12:25:44 +0200134 return sa->integ_icv_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135}
136
Neale Ranns47feb112019-04-11 15:14:07 +0000137always_inline void
138esp_aad_fill (vnet_crypto_op_t * op,
139 const esp_header_t * esp, const ipsec_sa_t * sa)
140{
141 esp_aead_t *aad;
142
143 aad = (esp_aead_t *) op->aad;
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200144 aad->data[0] = esp->spi;
Neale Ranns47feb112019-04-11 15:14:07 +0000145
146 if (ipsec_sa_is_set_USE_ESN (sa))
147 {
148 /* SPI, seq-hi, seq-low */
Neale Ranns47feb112019-04-11 15:14:07 +0000149 aad->data[1] = clib_host_to_net_u32 (sa->seq_hi);
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200150 aad->data[2] = esp->seq;
Neale Ranns47feb112019-04-11 15:14:07 +0000151 op->aad_len = 12;
152 }
153 else
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200154 {
155 /* SPI, seq-low */
156 aad->data[1] = esp->seq;
157 op->aad_len = 8;
158 }
Neale Ranns47feb112019-04-11 15:14:07 +0000159}
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000160#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700161
162/*
163 * fd.io coding-style-patch-verification: ON
164 *
165 * Local Variables:
166 * eval: (c-set-style "gnu")
167 * End:
168 */