IPSEC TEST: various hash alogrithms
[vpp.git] / src / vnet / ipsec / esp.h
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  */
15 #ifndef __ESP_H__
16 #define __ESP_H__
17
18 #include <vnet/ip/ip.h>
19 #include <vnet/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
21
22 typedef struct
23 {
24   u32 spi;
25   u32 seq;
26   u8 data[0];
27 } esp_header_t;
28
29 typedef struct
30 {
31   u8 pad_length;
32   u8 next_header;
33 } esp_footer_t;
34
35 /* *INDENT-OFF* */
36 typedef CLIB_PACKED (struct {
37   ip4_header_t ip4;
38   esp_header_t esp;
39 }) ip4_and_esp_header_t;
40 /* *INDENT-ON* */
41
42 /* *INDENT-OFF* */
43 typedef CLIB_PACKED (struct {
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 {
52   ip6_header_t ip6;
53   esp_header_t esp;
54 }) ip6_and_esp_header_t;
55 /* *INDENT-ON* */
56
57 #define ESP_SEQ_MAX             (4294967295UL)
58 #define ESP_MAX_BLOCK_SIZE      (16)
59 #define ESP_MAX_IV_SIZE         (16)
60 #define ESP_MAX_ICV_SIZE        (32)
61
62 u8 *format_esp_header (u8 * s, va_list * args);
63
64 /* TODO seq increment should be atomic to be accessed by multiple workers */
65 always_inline int
66 esp_seq_advance (ipsec_sa_t * sa)
67 {
68   if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
69     {
70       if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
71         {
72           if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
73                              sa->seq_hi == ESP_SEQ_MAX))
74             return 1;
75           sa->seq_hi++;
76         }
77       sa->seq++;
78     }
79   else
80     {
81       if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
82                          sa->seq == ESP_SEQ_MAX))
83         return 1;
84       sa->seq++;
85     }
86
87   return 0;
88 }
89
90
91 always_inline unsigned int
92 hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len,
93            u8 * signature)
94 {
95   vnet_crypto_op_t _op, *op = &_op;
96
97   if (PREDICT_FALSE (sa->integ_op_id == 0))
98     return 0;
99
100   vnet_crypto_op_init (op, sa->integ_op_id);
101   op->key = sa->integ_key.data;
102   op->key_len = sa->integ_key.len;
103   op->src = data;
104   op->len = data_len;
105   op->digest = signature;
106   op->digest_len = sa->integ_icv_size;
107
108   if (ipsec_sa_is_set_USE_ESN (sa))
109     {
110       u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
111
112       op->len += 4;
113       clib_memcpy (data + data_len, &seq_hi, 4);
114     }
115
116   vnet_crypto_process_ops (vm, op, 1);
117   return sa->integ_icv_size;
118 }
119
120 #endif /* __ESP_H__ */
121
122 /*
123  * fd.io coding-style-patch-verification: ON
124  *
125  * Local Variables:
126  * eval: (c-set-style "gnu")
127  * End:
128  */