crypto: add support for AEAD and AES-GCM
[vpp.git] / src / plugins / crypto_openssl / main.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2019 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <openssl/evp.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21
22 #include <vlib/vlib.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vnet/crypto/crypto.h>
25 #include <vpp/app/version.h>
26
27 typedef struct
28 {
29   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
30   EVP_CIPHER_CTX *evp_cipher_ctx;
31   HMAC_CTX *hmac_ctx;
32 #if OPENSSL_VERSION_NUMBER < 0x10100000L
33   HMAC_CTX _hmac_ctx;
34 #endif
35 } openssl_per_thread_data_t;
36
37 static openssl_per_thread_data_t *per_thread_data = 0;
38
39 #define foreach_openssl_evp_op \
40   _(cbc, DES_CBC, EVP_des_cbc) \
41   _(cbc, 3DES_CBC, EVP_des_ede3_cbc) \
42   _(cbc, AES_128_CBC, EVP_aes_128_cbc) \
43   _(cbc, AES_192_CBC, EVP_aes_192_cbc) \
44   _(cbc, AES_256_CBC, EVP_aes_256_cbc) \
45   _(gcm, AES_128_GCM, EVP_aes_128_gcm) \
46   _(gcm, AES_192_GCM, EVP_aes_192_gcm) \
47   _(gcm, AES_256_GCM, EVP_aes_256_gcm)
48
49 #define foreach_openssl_hmac_op \
50   _(MD5, EVP_md5) \
51   _(SHA1, EVP_sha1) \
52   _(SHA224, EVP_sha224) \
53   _(SHA256, EVP_sha256) \
54   _(SHA384, EVP_sha384) \
55   _(SHA512, EVP_sha512)
56
57 static_always_inline u32
58 openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
59                      const EVP_CIPHER * cipher)
60 {
61   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
62                                                      vm->thread_index);
63   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
64   u32 i;
65   for (i = 0; i < n_ops; i++)
66     {
67       vnet_crypto_op_t *op = ops[i];
68       int out_len;
69
70       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
71         RAND_bytes (op->iv, 16);
72
73       EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
74       EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
75       if (out_len < op->len)
76         EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
77       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
78     }
79   return n_ops;
80 }
81
82 static_always_inline u32
83 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
84                      const EVP_CIPHER * cipher)
85 {
86   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
87                                                      vm->thread_index);
88   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
89   u32 i;
90   for (i = 0; i < n_ops; i++)
91     {
92       vnet_crypto_op_t *op = ops[i];
93       int out_len;
94
95       EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
96       EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
97       if (out_len < op->len)
98         EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
99       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
100     }
101   return n_ops;
102 }
103
104 static_always_inline u32
105 openssl_ops_enc_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
106                      const EVP_CIPHER * cipher)
107 {
108   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
109                                                      vm->thread_index);
110   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
111   u32 i;
112   for (i = 0; i < n_ops; i++)
113     {
114       vnet_crypto_op_t *op = ops[i];
115       int len;
116
117       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
118         RAND_bytes (op->iv, op->iv_len);
119
120       EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
121       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, op->iv_len, NULL);
122       EVP_EncryptInit_ex (ctx, 0, 0, op->key, op->iv);
123       EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
124       EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
125       EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
126       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
127       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
128     }
129   return n_ops;
130 }
131
132 static_always_inline u32
133 openssl_ops_dec_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
134                      const EVP_CIPHER * cipher)
135 {
136   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
137                                                      vm->thread_index);
138   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
139   u32 i;
140   for (i = 0; i < n_ops; i++)
141     {
142       vnet_crypto_op_t *op = ops[i];
143       int len;
144
145       EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
146       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, op->iv_len, 0);
147       EVP_DecryptInit_ex (ctx, 0, 0, op->key, op->iv);
148       EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
149       EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
150       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
151
152       if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
153         op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
154       else
155         op->status = VNET_CRYPTO_OP_STATUS_FAIL_DECRYPT;
156     }
157   return n_ops;
158 }
159
160 static_always_inline u32
161 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
162                   const EVP_MD * md)
163 {
164   u8 buffer[64];
165   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
166                                                      vm->thread_index);
167   HMAC_CTX *ctx = ptd->hmac_ctx;
168   u32 i, n_fail = 0;
169   for (i = 0; i < n_ops; i++)
170     {
171       vnet_crypto_op_t *op = ops[i];
172       unsigned int out_len;
173       size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
174
175       HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
176       HMAC_Update (ctx, op->src, op->len);
177       HMAC_Final (ctx, buffer, &out_len);
178
179       if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
180         {
181           if ((memcmp (op->digest, buffer, sz)))
182             {
183               n_fail++;
184               op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
185               continue;
186             }
187         }
188       else
189         clib_memcpy_fast (op->digest, buffer, sz);
190       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
191     }
192   return n_ops - n_fail;
193 }
194
195 #define _(m, a, b) \
196 static u32 \
197 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
198 { return openssl_ops_enc_##m (vm, ops, n_ops, b ()); } \
199 \
200 u32 \
201 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
202 { return openssl_ops_dec_##m (vm, ops, n_ops, b ()); }
203
204 foreach_openssl_evp_op;
205 #undef _
206
207 #define _(a, b) \
208 static u32 \
209 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
210 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
211
212 foreach_openssl_hmac_op;
213 #undef _
214
215
216 clib_error_t *
217 crypto_openssl_init (vlib_main_t * vm)
218 {
219   vlib_thread_main_t *tm = vlib_get_thread_main ();
220   openssl_per_thread_data_t *ptd;
221   u8 *seed_data = 0;
222   clib_error_t *error;
223   time_t t;
224   pid_t pid;
225
226   if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
227     return error;
228
229   u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
230
231 #define _(m, a, b) \
232   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
233                                     openssl_ops_enc_##a); \
234   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
235                                     openssl_ops_dec_##a);
236
237   foreach_openssl_evp_op;
238 #undef _
239
240 #define _(a, b) \
241   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
242                                     openssl_ops_hmac_##a); \
243
244   foreach_openssl_hmac_op;
245 #undef _
246
247   vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
248                         CLIB_CACHE_LINE_BYTES);
249
250   vec_foreach (ptd, per_thread_data)
251   {
252     ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
253 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
254     ptd->hmac_ctx = HMAC_CTX_new ();
255 #else
256     HMAC_CTX_init (&(ptd->_hmac_ctx));
257     ptd->hmac_ctx = &ptd->_hmac_ctx;
258 #endif
259   }
260
261   t = time (NULL);
262   pid = getpid ();
263   vec_add (seed_data, &t, sizeof (t));
264   vec_add (seed_data, &pid, sizeof (pid));
265   vec_add (seed_data, seed_data, sizeof (seed_data));
266
267   RAND_seed ((const void *) seed_data, vec_len (seed_data));
268
269   vec_free (seed_data);
270
271   return 0;
272 }
273
274 VLIB_INIT_FUNCTION (crypto_openssl_init);
275
276 /* *INDENT-OFF* */
277 VLIB_PLUGIN_REGISTER () = {
278   .version = VPP_BUILD_VER,
279   .description = "OpenSSL Crypto Engine Plugin",
280 };
281 /* *INDENT-ON* */
282
283 /*
284  * fd.io coding-style-patch-verification: ON
285  *
286  * Local Variables:
287  * eval: (c-set-style "gnu")
288  * End:
289  */