Add support for native vpp_lite (non-dpdk) platform
[vpp.git] / vnet / 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 #if DPDK==1
16 #include <vnet/devices/dpdk/dpdk.h>
17 #endif
18
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21 #include <openssl/evp.h>
22
23 typedef struct {
24   u32 spi;
25   u32 seq;
26   u8 data[0];
27 } esp_header_t;
28
29 typedef struct {
30   u8 pad_length;
31   u8 next_header;
32 } esp_footer_t;
33
34 typedef CLIB_PACKED (struct {
35   ip4_header_t ip4;
36   esp_header_t esp;
37 }) ip4_and_esp_header_t;
38
39 typedef CLIB_PACKED (struct {
40   ip6_header_t ip6;
41   esp_header_t esp;
42 }) ip6_and_esp_header_t;
43
44 typedef struct {
45   const EVP_CIPHER * type;
46 } esp_crypto_alg_t;
47
48 typedef struct {
49   const EVP_MD * md;
50   u8 trunc_size;
51 } esp_integ_alg_t;
52
53
54 typedef struct {
55   esp_crypto_alg_t * esp_crypto_algs;
56   esp_integ_alg_t * esp_integ_algs;
57   EVP_CIPHER_CTX encrypt_ctx;
58   EVP_CIPHER_CTX decrypt_ctx;
59   HMAC_CTX hmac_ctx;
60   ipsec_crypto_alg_t last_encrytp_alg;
61   ipsec_crypto_alg_t last_decrytp_alg;
62   ipsec_integ_alg_t last_integ_alg;
63 } esp_main_t;
64
65 esp_main_t esp_main;
66
67 always_inline void
68 esp_init()
69 {
70   esp_main_t * em = &esp_main;
71
72   memset (em, 0, sizeof (em[0]));
73
74   vec_validate(em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
75   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc();
76   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc();
77   em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc();
78
79   vec_validate(em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
80   esp_integ_alg_t * i;
81
82   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
83   i->md = EVP_sha1();
84   i->trunc_size = 12;
85
86   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
87   i->md = EVP_sha256();
88   i->trunc_size = 12;
89
90   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
91   i->md = EVP_sha256();
92   i->trunc_size = 16;
93
94   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
95   i->md = EVP_sha384();
96   i->trunc_size = 24;
97
98   i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
99   i->md = EVP_sha512();
100   i->trunc_size = 32;
101
102   EVP_CIPHER_CTX_init(&(em->encrypt_ctx));
103   EVP_CIPHER_CTX_init(&(em->decrypt_ctx));
104   HMAC_CTX_init(&(em->hmac_ctx));
105 }
106
107 always_inline unsigned int
108 hmac_calc(ipsec_integ_alg_t alg,
109           u8 * key,
110           int key_len,
111           u8 * data,
112           int data_len,
113           u8 * signature,
114           u8 use_esn,
115           u32 seq_hi)
116 {
117   esp_main_t * em = &esp_main;
118   HMAC_CTX * ctx = &(em->hmac_ctx);
119   const EVP_MD * md = NULL;
120   unsigned int len;
121
122   ASSERT(alg < IPSEC_INTEG_N_ALG);
123
124   if (PREDICT_FALSE(em->esp_integ_algs[alg].md == 0))
125     return 0;
126
127   if (PREDICT_FALSE(alg != em->last_integ_alg)) {
128     md = em->esp_integ_algs[alg].md;
129     em->last_integ_alg = alg;
130   }
131
132   HMAC_Init(ctx, key, key_len, md);
133
134   HMAC_Update(ctx, data, data_len);
135
136   if (PREDICT_TRUE(use_esn))
137     HMAC_Update(ctx, (u8 *) &seq_hi, sizeof(seq_hi));
138   HMAC_Final(ctx, signature, &len);
139
140   return em->esp_integ_algs[alg].trunc_size;
141 }
142