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