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