crypto-openssl: fix iv size for AES CTR
[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_aes_evp_op                                            \
40   _ (cbc, DES_CBC, EVP_des_cbc, 8)                                            \
41   _ (cbc, 3DES_CBC, EVP_des_ede3_cbc, 8)                                      \
42   _ (cbc, AES_128_CBC, EVP_aes_128_cbc, 16)                                   \
43   _ (cbc, AES_192_CBC, EVP_aes_192_cbc, 16)                                   \
44   _ (cbc, AES_256_CBC, EVP_aes_256_cbc, 16)                                   \
45   _ (gcm, AES_128_GCM, EVP_aes_128_gcm, 8)                                    \
46   _ (gcm, AES_192_GCM, EVP_aes_192_gcm, 8)                                    \
47   _ (gcm, AES_256_GCM, EVP_aes_256_gcm, 8)                                    \
48   _ (cbc, AES_128_CTR, EVP_aes_128_ctr, 8)                                    \
49   _ (cbc, AES_192_CTR, EVP_aes_192_ctr, 8)                                    \
50   _ (cbc, AES_256_CTR, EVP_aes_256_ctr, 8)
51
52 #define foreach_openssl_chacha20_evp_op                                       \
53   _ (chacha20_poly1305, CHACHA20_POLY1305, EVP_chacha20_poly1305, 8)
54
55 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
56 #define foreach_openssl_evp_op foreach_openssl_aes_evp_op \
57                                foreach_openssl_chacha20_evp_op
58 #else
59 #define foreach_openssl_evp_op foreach_openssl_aes_evp_op
60 #endif
61
62 #ifndef EVP_CTRL_AEAD_GET_TAG
63 #define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
64 #endif
65
66 #ifndef EVP_CTRL_AEAD_SET_TAG
67 #define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
68 #endif
69
70 #define foreach_openssl_hmac_op \
71   _(MD5, EVP_md5) \
72   _(SHA1, EVP_sha1) \
73   _(SHA224, EVP_sha224) \
74   _(SHA256, EVP_sha256) \
75   _(SHA384, EVP_sha384) \
76   _(SHA512, EVP_sha512)
77
78 static_always_inline u32
79 openssl_ops_enc_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
80                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
81                      const EVP_CIPHER *cipher, const int iv_len)
82 {
83   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
84                                                      vm->thread_index);
85   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
86   vnet_crypto_op_chunk_t *chp;
87   u32 i, j, curr_len = 0;
88   u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
89
90   for (i = 0; i < n_ops; i++)
91     {
92       vnet_crypto_op_t *op = ops[i];
93       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
94       int out_len = 0;
95
96       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
97         RAND_bytes (op->iv, iv_len);
98
99       EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
100
101       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
102         EVP_CIPHER_CTX_set_padding (ctx, 0);
103
104       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
105         {
106           chp = chunks + op->chunk_index;
107           u32 offset = 0;
108           for (j = 0; j < op->n_chunks; j++)
109             {
110               EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
111                                  chp->len);
112               curr_len = chp->len;
113               offset += out_len;
114               chp += 1;
115             }
116           if (out_len < curr_len)
117             EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
118
119           offset = 0;
120           chp = chunks + op->chunk_index;
121           for (j = 0; j < op->n_chunks; j++)
122             {
123               clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
124               offset += chp->len;
125               chp += 1;
126             }
127         }
128       else
129         {
130           EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
131           if (out_len < op->len)
132             EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
133         }
134       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
135     }
136   return n_ops;
137 }
138
139 static_always_inline u32
140 openssl_ops_dec_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
141                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
142                      const EVP_CIPHER *cipher, const int iv_len)
143 {
144   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
145                                                      vm->thread_index);
146   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
147   vnet_crypto_op_chunk_t *chp;
148   u32 i, j, curr_len = 0;
149   u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
150
151   for (i = 0; i < n_ops; i++)
152     {
153       vnet_crypto_op_t *op = ops[i];
154       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
155       int out_len = 0;
156
157       EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
158
159       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
160         EVP_CIPHER_CTX_set_padding (ctx, 0);
161
162       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
163         {
164           chp = chunks + op->chunk_index;
165           u32 offset = 0;
166           for (j = 0; j < op->n_chunks; j++)
167             {
168               EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
169                                  chp->len);
170               curr_len = chp->len;
171               offset += out_len;
172               chp += 1;
173             }
174           if (out_len < curr_len)
175             EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
176
177           offset = 0;
178           chp = chunks + op->chunk_index;
179           for (j = 0; j < op->n_chunks; j++)
180             {
181               clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
182               offset += chp->len;
183               chp += 1;
184             }
185         }
186       else
187         {
188           EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
189           if (out_len < op->len)
190             EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
191         }
192       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
193     }
194   return n_ops;
195 }
196
197 static_always_inline u32
198 openssl_ops_enc_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
199                       vnet_crypto_op_chunk_t *chunks, u32 n_ops,
200                       const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
201 {
202   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
203                                                      vm->thread_index);
204   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
205   vnet_crypto_op_chunk_t *chp;
206   u32 i, j;
207   for (i = 0; i < n_ops; i++)
208     {
209       vnet_crypto_op_t *op = ops[i];
210       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
211       int len = 0;
212
213       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
214         RAND_bytes (op->iv, 8);
215
216       EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
217       if (is_gcm)
218         EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
219       EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
220       if (op->aad_len)
221         EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
222       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
223         {
224           chp = chunks + op->chunk_index;
225           for (j = 0; j < op->n_chunks; j++)
226             {
227               EVP_EncryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
228               chp += 1;
229             }
230         }
231       else
232         EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
233       EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
234       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_GET_TAG, op->tag_len, op->tag);
235       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
236     }
237   return n_ops;
238 }
239
240 static_always_inline u32
241 openssl_ops_enc_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
242                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
243                      const EVP_CIPHER *cipher, const int iv_len)
244 {
245   return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
246                                /* is_gcm */ 1, iv_len);
247 }
248
249 static_always_inline __clib_unused u32
250 openssl_ops_enc_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
251                                    vnet_crypto_op_chunk_t *chunks, u32 n_ops,
252                                    const EVP_CIPHER *cipher, const int iv_len)
253 {
254   return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
255                                /* is_gcm */ 0, iv_len);
256 }
257
258 static_always_inline u32
259 openssl_ops_dec_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
260                       vnet_crypto_op_chunk_t *chunks, u32 n_ops,
261                       const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
262 {
263   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
264                                                      vm->thread_index);
265   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
266   vnet_crypto_op_chunk_t *chp;
267   u32 i, j, n_fail = 0;
268   for (i = 0; i < n_ops; i++)
269     {
270       vnet_crypto_op_t *op = ops[i];
271       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
272       int len = 0;
273
274       EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
275       if (is_gcm)
276         EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
277       EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
278       if (op->aad_len)
279         EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
280       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
281         {
282           chp = chunks + op->chunk_index;
283           for (j = 0; j < op->n_chunks; j++)
284             {
285               EVP_DecryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
286               chp += 1;
287             }
288         }
289       else
290         EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
291       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_SET_TAG, op->tag_len, op->tag);
292
293       if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
294         op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
295       else
296         {
297           n_fail++;
298           op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
299         }
300     }
301   return n_ops - n_fail;
302 }
303
304 static_always_inline u32
305 openssl_ops_dec_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
306                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
307                      const EVP_CIPHER *cipher, const int iv_len)
308 {
309   return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
310                                /* is_gcm */ 1, iv_len);
311 }
312
313 static_always_inline __clib_unused u32
314 openssl_ops_dec_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
315                                    vnet_crypto_op_chunk_t *chunks, u32 n_ops,
316                                    const EVP_CIPHER *cipher, const int iv_len)
317 {
318   return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
319                                /* is_gcm */ 0, iv_len);
320 }
321
322 static_always_inline u32
323 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
324                   vnet_crypto_op_chunk_t * chunks, u32 n_ops,
325                   const EVP_MD * md)
326 {
327   u8 buffer[64];
328   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
329                                                      vm->thread_index);
330   HMAC_CTX *ctx = ptd->hmac_ctx;
331   vnet_crypto_op_chunk_t *chp;
332   u32 i, j, n_fail = 0;
333   for (i = 0; i < n_ops; i++)
334     {
335       vnet_crypto_op_t *op = ops[i];
336       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
337       unsigned int out_len = 0;
338       size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
339
340       HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
341       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
342         {
343           chp = chunks + op->chunk_index;
344           for (j = 0; j < op->n_chunks; j++)
345             {
346               HMAC_Update (ctx, chp->src, chp->len);
347               chp += 1;
348             }
349         }
350       else
351         HMAC_Update (ctx, op->src, op->len);
352       HMAC_Final (ctx, buffer, &out_len);
353
354       if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
355         {
356           if ((memcmp (op->digest, buffer, sz)))
357             {
358               n_fail++;
359               op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
360               continue;
361             }
362         }
363       else
364         clib_memcpy_fast (op->digest, buffer, sz);
365       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
366     }
367   return n_ops - n_fail;
368 }
369
370 #define _(m, a, b, iv)                                                        \
371   static u32 openssl_ops_enc_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[],   \
372                                   u32 n_ops)                                  \
373   {                                                                           \
374     return openssl_ops_enc_##m (vm, ops, 0, n_ops, b (), iv);                 \
375   }                                                                           \
376                                                                               \
377   u32 openssl_ops_dec_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[],          \
378                            u32 n_ops)                                         \
379   {                                                                           \
380     return openssl_ops_dec_##m (vm, ops, 0, n_ops, b (), iv);                 \
381   }                                                                           \
382                                                                               \
383   static u32 openssl_ops_enc_chained_##a (                                    \
384     vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
385     u32 n_ops)                                                                \
386   {                                                                           \
387     return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b (), iv);            \
388   }                                                                           \
389                                                                               \
390   static u32 openssl_ops_dec_chained_##a (                                    \
391     vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
392     u32 n_ops)                                                                \
393   {                                                                           \
394     return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b (), iv);            \
395   }
396
397 foreach_openssl_evp_op;
398 #undef _
399
400 #define _(a, b) \
401 static u32 \
402 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
403 { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \
404 static u32 \
405 openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
406     vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
407 { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \
408
409 foreach_openssl_hmac_op;
410 #undef _
411
412
413 clib_error_t *
414 crypto_openssl_init (vlib_main_t * vm)
415 {
416   vlib_thread_main_t *tm = vlib_get_thread_main ();
417   openssl_per_thread_data_t *ptd;
418   u8 *seed_data = 0;
419   time_t t;
420   pid_t pid;
421
422   u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
423
424 #define _(m, a, b, iv)                                                        \
425   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC,      \
426                                      openssl_ops_enc_##a,                     \
427                                      openssl_ops_enc_chained_##a);            \
428   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC,      \
429                                      openssl_ops_dec_##a,                     \
430                                      openssl_ops_dec_chained_##a);
431
432   foreach_openssl_evp_op;
433 #undef _
434
435 #define _(a, b) \
436   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
437                                     openssl_ops_hmac_##a, \
438                                     openssl_ops_hmac_chained_##a); \
439
440   foreach_openssl_hmac_op;
441 #undef _
442
443   vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
444                         CLIB_CACHE_LINE_BYTES);
445
446   vec_foreach (ptd, per_thread_data)
447   {
448     ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
449 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
450     ptd->hmac_ctx = HMAC_CTX_new ();
451 #else
452     HMAC_CTX_init (&(ptd->_hmac_ctx));
453     ptd->hmac_ctx = &ptd->_hmac_ctx;
454 #endif
455   }
456
457   t = time (NULL);
458   pid = getpid ();
459   vec_add (seed_data, &t, sizeof (t));
460   vec_add (seed_data, &pid, sizeof (pid));
461   vec_add (seed_data, seed_data, sizeof (seed_data));
462
463   RAND_seed ((const void *) seed_data, vec_len (seed_data));
464
465   vec_free (seed_data);
466
467   return 0;
468 }
469
470 /* *INDENT-OFF* */
471 VLIB_INIT_FUNCTION (crypto_openssl_init) =
472 {
473   .runs_after = VLIB_INITS ("vnet_crypto_init"),
474 };
475 /* *INDENT-ON* */
476
477
478 /* *INDENT-OFF* */
479 VLIB_PLUGIN_REGISTER () = {
480   .version = VPP_BUILD_VER,
481   .description = "OpenSSL Crypto Engine",
482 };
483 /* *INDENT-ON* */
484
485 /*
486  * fd.io coding-style-patch-verification: ON
487  *
488  * Local Variables:
489  * eval: (c-set-style "gnu")
490  * End:
491  */