LCOV - code coverage report
Current view: top level - plugins/crypto_openssl - main.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 171 182 94.0 %
Date: 2023-07-05 22:20:52 Functions: 64 83 77.1 %

          Line data    Source code
       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 <sys/syscall.h>
      19             : 
      20             : #include <openssl/evp.h>
      21             : #include <openssl/hmac.h>
      22             : #include <openssl/rand.h>
      23             : #include <openssl/sha.h>
      24             : 
      25             : #include <vlib/vlib.h>
      26             : #include <vnet/plugin/plugin.h>
      27             : #include <vnet/crypto/crypto.h>
      28             : #include <vpp/app/version.h>
      29             : 
      30             : typedef struct
      31             : {
      32             :   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
      33             :   EVP_CIPHER_CTX *evp_cipher_ctx;
      34             :   HMAC_CTX *hmac_ctx;
      35             :   EVP_MD_CTX *hash_ctx;
      36             : #if OPENSSL_VERSION_NUMBER < 0x10100000L
      37             :   HMAC_CTX _hmac_ctx;
      38             : #endif
      39             : } openssl_per_thread_data_t;
      40             : 
      41             : static openssl_per_thread_data_t *per_thread_data = 0;
      42             : 
      43             : #define foreach_openssl_aes_evp_op                                            \
      44             :   _ (cbc, DES_CBC, EVP_des_cbc, 8)                                            \
      45             :   _ (cbc, 3DES_CBC, EVP_des_ede3_cbc, 8)                                      \
      46             :   _ (cbc, AES_128_CBC, EVP_aes_128_cbc, 16)                                   \
      47             :   _ (cbc, AES_192_CBC, EVP_aes_192_cbc, 16)                                   \
      48             :   _ (cbc, AES_256_CBC, EVP_aes_256_cbc, 16)                                   \
      49             :   _ (gcm, AES_128_GCM, EVP_aes_128_gcm, 8)                                    \
      50             :   _ (gcm, AES_192_GCM, EVP_aes_192_gcm, 8)                                    \
      51             :   _ (gcm, AES_256_GCM, EVP_aes_256_gcm, 8)                                    \
      52             :   _ (cbc, AES_128_CTR, EVP_aes_128_ctr, 8)                                    \
      53             :   _ (cbc, AES_192_CTR, EVP_aes_192_ctr, 8)                                    \
      54             :   _ (cbc, AES_256_CTR, EVP_aes_256_ctr, 8)
      55             : 
      56             : #define foreach_openssl_chacha20_evp_op                                       \
      57             :   _ (chacha20_poly1305, CHACHA20_POLY1305, EVP_chacha20_poly1305, 8)
      58             : 
      59             : #if OPENSSL_VERSION_NUMBER >= 0x10100000L
      60             : #define foreach_openssl_evp_op foreach_openssl_aes_evp_op \
      61             :                                foreach_openssl_chacha20_evp_op
      62             : #else
      63             : #define foreach_openssl_evp_op foreach_openssl_aes_evp_op
      64             : #endif
      65             : 
      66             : #ifndef EVP_CTRL_AEAD_GET_TAG
      67             : #define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
      68             : #endif
      69             : 
      70             : #ifndef EVP_CTRL_AEAD_SET_TAG
      71             : #define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
      72             : #endif
      73             : 
      74             : #define foreach_openssl_hash_op                                               \
      75             :   _ (SHA1, EVP_sha1)                                                          \
      76             :   _ (SHA224, EVP_sha224)                                                      \
      77             :   _ (SHA256, EVP_sha256)                                                      \
      78             :   _ (SHA384, EVP_sha384)                                                      \
      79             :   _ (SHA512, EVP_sha512)
      80             : 
      81             : #define foreach_openssl_hmac_op \
      82             :   _(MD5, EVP_md5) \
      83             :   _(SHA1, EVP_sha1) \
      84             :   _(SHA224, EVP_sha224) \
      85             :   _(SHA256, EVP_sha256) \
      86             :   _(SHA384, EVP_sha384) \
      87             :   _(SHA512, EVP_sha512)
      88             : 
      89             : static_always_inline u32
      90        1591 : openssl_ops_enc_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
      91             :                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
      92             :                      const EVP_CIPHER *cipher, const int iv_len)
      93             : {
      94        1591 :   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
      95             :                                                      vm->thread_index);
      96        1591 :   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
      97             :   vnet_crypto_op_chunk_t *chp;
      98        1591 :   u32 i, j, curr_len = 0;
      99             :   u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
     100             : 
     101       71331 :   for (i = 0; i < n_ops; i++)
     102             :     {
     103       69740 :       vnet_crypto_op_t *op = ops[i];
     104       69740 :       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
     105       69740 :       int out_len = 0;
     106             : 
     107       69740 :       EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
     108             : 
     109       69740 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     110             :         {
     111       34305 :           chp = chunks + op->chunk_index;
     112       34305 :           u32 offset = 0;
     113      112831 :           for (j = 0; j < op->n_chunks; j++)
     114             :             {
     115       78526 :               EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
     116       78526 :                                  chp->len);
     117       78526 :               curr_len = chp->len;
     118       78526 :               offset += out_len;
     119       78526 :               chp += 1;
     120             :             }
     121       34305 :           if (out_len < curr_len)
     122           0 :             EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
     123             : 
     124       34305 :           offset = 0;
     125       34305 :           chp = chunks + op->chunk_index;
     126      112831 :           for (j = 0; j < op->n_chunks; j++)
     127             :             {
     128       78526 :               clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
     129       78526 :               offset += chp->len;
     130       78526 :               chp += 1;
     131             :             }
     132             :         }
     133             :       else
     134             :         {
     135       35435 :           EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
     136       35435 :           if (out_len < op->len)
     137           0 :             EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
     138             :         }
     139       69740 :       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     140             :     }
     141        1591 :   return n_ops;
     142             : }
     143             : 
     144             : static_always_inline u32
     145        1681 : openssl_ops_dec_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     146             :                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     147             :                      const EVP_CIPHER *cipher, const int iv_len)
     148             : {
     149        1681 :   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
     150             :                                                      vm->thread_index);
     151        1681 :   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
     152             :   vnet_crypto_op_chunk_t *chp;
     153        1681 :   u32 i, j, curr_len = 0;
     154             :   u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
     155             : 
     156       72276 :   for (i = 0; i < n_ops; i++)
     157             :     {
     158       70595 :       vnet_crypto_op_t *op = ops[i];
     159       70595 :       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
     160       70595 :       int out_len = 0;
     161             : 
     162       70595 :       EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
     163             : 
     164       70595 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     165             :         {
     166       36181 :           chp = chunks + op->chunk_index;
     167       36181 :           u32 offset = 0;
     168      120871 :           for (j = 0; j < op->n_chunks; j++)
     169             :             {
     170       84690 :               EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
     171       84690 :                                  chp->len);
     172       84690 :               curr_len = chp->len;
     173       84690 :               offset += out_len;
     174       84690 :               chp += 1;
     175             :             }
     176       36181 :           if (out_len < curr_len)
     177       24120 :             EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
     178             : 
     179       36181 :           offset = 0;
     180       36181 :           chp = chunks + op->chunk_index;
     181      120871 :           for (j = 0; j < op->n_chunks; j++)
     182             :             {
     183       84690 :               clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
     184       84690 :               offset += chp->len;
     185       84690 :               chp += 1;
     186             :             }
     187             :         }
     188             :       else
     189             :         {
     190       34414 :           EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
     191       34414 :           if (out_len < op->len)
     192       22891 :             EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
     193             :         }
     194       70595 :       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     195             :     }
     196        1681 :   return n_ops;
     197             : }
     198             : 
     199             : static_always_inline u32
     200         516 : openssl_ops_enc_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     201             :                       vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     202             :                       const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
     203             : {
     204         516 :   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
     205             :                                                      vm->thread_index);
     206         516 :   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
     207             :   vnet_crypto_op_chunk_t *chp;
     208             :   u32 i, j;
     209       24999 :   for (i = 0; i < n_ops; i++)
     210             :     {
     211       24483 :       vnet_crypto_op_t *op = ops[i];
     212       24483 :       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
     213       24483 :       int len = 0;
     214             : 
     215       24483 :       EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
     216       24483 :       if (is_gcm)
     217       24483 :         EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
     218       24483 :       EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
     219       24483 :       if (op->aad_len)
     220       24483 :         EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
     221       24483 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     222             :         {
     223       11256 :           chp = chunks + op->chunk_index;
     224       36984 :           for (j = 0; j < op->n_chunks; j++)
     225             :             {
     226       25728 :               EVP_EncryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
     227       25728 :               chp += 1;
     228             :             }
     229             :         }
     230             :       else
     231       13227 :         EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
     232       24483 :       EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
     233       24483 :       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_GET_TAG, op->tag_len, op->tag);
     234       24483 :       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     235             :     }
     236         516 :   return n_ops;
     237             : }
     238             : 
     239             : static_always_inline u32
     240         516 : openssl_ops_enc_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     241             :                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     242             :                      const EVP_CIPHER *cipher, const int iv_len)
     243             : {
     244         516 :   return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
     245             :                                /* is_gcm */ 1, iv_len);
     246             : }
     247             : 
     248             : static_always_inline __clib_unused u32
     249           0 : openssl_ops_enc_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     250             :                                    vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     251             :                                    const EVP_CIPHER *cipher, const int iv_len)
     252             : {
     253           0 :   return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
     254             :                                /* is_gcm */ 0, iv_len);
     255             : }
     256             : 
     257             : static_always_inline u32
     258         546 : openssl_ops_dec_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     259             :                       vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     260             :                       const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
     261             : {
     262         546 :   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
     263             :                                                      vm->thread_index);
     264         546 :   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
     265             :   vnet_crypto_op_chunk_t *chp;
     266         546 :   u32 i, j, n_fail = 0;
     267       25314 :   for (i = 0; i < n_ops; i++)
     268             :     {
     269       24768 :       vnet_crypto_op_t *op = ops[i];
     270       24768 :       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
     271       24768 :       int len = 0;
     272             : 
     273       24768 :       EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
     274       24768 :       if (is_gcm)
     275       24768 :         EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
     276       24768 :       EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
     277       24768 :       if (op->aad_len)
     278       24768 :         EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
     279       24768 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     280             :         {
     281       12060 :           chp = chunks + op->chunk_index;
     282       39396 :           for (j = 0; j < op->n_chunks; j++)
     283             :             {
     284       27336 :               EVP_DecryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
     285       27336 :               chp += 1;
     286             :             }
     287             :         }
     288             :       else
     289       12708 :         EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
     290       24768 :       EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_SET_TAG, op->tag_len, op->tag);
     291             : 
     292       24768 :       if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
     293       24594 :         op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     294             :       else
     295             :         {
     296         174 :           n_fail++;
     297         174 :           op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
     298             :         }
     299             :     }
     300         546 :   return n_ops - n_fail;
     301             : }
     302             : 
     303             : static_always_inline u32
     304         546 : openssl_ops_dec_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     305             :                      vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     306             :                      const EVP_CIPHER *cipher, const int iv_len)
     307             : {
     308         546 :   return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
     309             :                                /* is_gcm */ 1, iv_len);
     310             : }
     311             : 
     312             : static_always_inline __clib_unused u32
     313           0 : openssl_ops_dec_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     314             :                                    vnet_crypto_op_chunk_t *chunks, u32 n_ops,
     315             :                                    const EVP_CIPHER *cipher, const int iv_len)
     316             : {
     317           0 :   return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
     318             :                                /* is_gcm */ 0, iv_len);
     319             : }
     320             : 
     321             : static_always_inline u32
     322         304 : openssl_ops_hash (vlib_main_t *vm, vnet_crypto_op_t *ops[],
     323             :                   vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_MD *md)
     324             : {
     325         304 :   openssl_per_thread_data_t *ptd =
     326         304 :     vec_elt_at_index (per_thread_data, vm->thread_index);
     327         304 :   EVP_MD_CTX *ctx = ptd->hash_ctx;
     328             :   vnet_crypto_op_chunk_t *chp;
     329         304 :   u32 md_len, i, j, n_fail = 0;
     330             : 
     331         608 :   for (i = 0; i < n_ops; i++)
     332             :     {
     333         304 :       vnet_crypto_op_t *op = ops[i];
     334             : 
     335         304 :       EVP_DigestInit_ex (ctx, md, NULL);
     336         304 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     337             :         {
     338           0 :           chp = chunks + op->chunk_index;
     339           0 :           for (j = 0; j < op->n_chunks; j++)
     340             :             {
     341           0 :               EVP_DigestUpdate (ctx, chp->src, chp->len);
     342           0 :               chp += 1;
     343             :             }
     344             :         }
     345             :       else
     346         304 :         EVP_DigestUpdate (ctx, op->src, op->len);
     347             : 
     348         304 :       EVP_DigestFinal_ex (ctx, op->digest, &md_len);
     349         304 :       op->digest_len = md_len;
     350         304 :       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     351             :     }
     352         304 :   return n_ops - n_fail;
     353             : }
     354             : 
     355             : static_always_inline u32
     356        3677 : openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
     357             :                   vnet_crypto_op_chunk_t * chunks, u32 n_ops,
     358             :                   const EVP_MD * md)
     359             : {
     360             :   u8 buffer[64];
     361        3677 :   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
     362             :                                                      vm->thread_index);
     363        3677 :   HMAC_CTX *ctx = ptd->hmac_ctx;
     364             :   vnet_crypto_op_chunk_t *chp;
     365        3677 :   u32 i, j, n_fail = 0;
     366      160990 :   for (i = 0; i < n_ops; i++)
     367             :     {
     368      157313 :       vnet_crypto_op_t *op = ops[i];
     369      157313 :       vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
     370      157313 :       unsigned int out_len = 0;
     371      157313 :       size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
     372             : 
     373      157313 :       HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
     374      157313 :       if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
     375             :         {
     376       77455 :           chp = chunks + op->chunk_index;
     377      256889 :           for (j = 0; j < op->n_chunks; j++)
     378             :             {
     379      179434 :               HMAC_Update (ctx, chp->src, chp->len);
     380      179434 :               chp += 1;
     381             :             }
     382             :         }
     383             :       else
     384       79858 :         HMAC_Update (ctx, op->src, op->len);
     385      157313 :       HMAC_Final (ctx, buffer, &out_len);
     386             : 
     387      157313 :       if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
     388             :         {
     389       79126 :           if ((memcmp (op->digest, buffer, sz)))
     390             :             {
     391         580 :               n_fail++;
     392         580 :               op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
     393         580 :               continue;
     394             :             }
     395             :         }
     396             :       else
     397       78187 :         clib_memcpy_fast (op->digest, buffer, sz);
     398      156733 :       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     399             :     }
     400        3677 :   return n_ops - n_fail;
     401             : }
     402             : 
     403             : #define _(m, a, b, iv)                                                        \
     404             :   static u32 openssl_ops_enc_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[],   \
     405             :                                   u32 n_ops)                                  \
     406             :   {                                                                           \
     407             :     return openssl_ops_enc_##m (vm, ops, 0, n_ops, b (), iv);                 \
     408             :   }                                                                           \
     409             :                                                                               \
     410             :   u32 openssl_ops_dec_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[],          \
     411             :                            u32 n_ops)                                         \
     412             :   {                                                                           \
     413             :     return openssl_ops_dec_##m (vm, ops, 0, n_ops, b (), iv);                 \
     414             :   }                                                                           \
     415             :                                                                               \
     416             :   static u32 openssl_ops_enc_chained_##a (                                    \
     417             :     vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
     418             :     u32 n_ops)                                                                \
     419             :   {                                                                           \
     420             :     return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b (), iv);            \
     421             :   }                                                                           \
     422             :                                                                               \
     423             :   static u32 openssl_ops_dec_chained_##a (                                    \
     424             :     vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
     425             :     u32 n_ops)                                                                \
     426             :   {                                                                           \
     427             :     return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b (), iv);            \
     428             :   }
     429             : 
     430        4334 : foreach_openssl_evp_op;
     431             : #undef _
     432             : 
     433             : #define _(a, b)                                                               \
     434             :   static u32 openssl_ops_hash_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[],  \
     435             :                                    u32 n_ops)                                 \
     436             :   {                                                                           \
     437             :     return openssl_ops_hash (vm, ops, 0, n_ops, b ());                        \
     438             :   }                                                                           \
     439             :   static u32 openssl_ops_hash_chained_##a (                                   \
     440             :     vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
     441             :     u32 n_ops)                                                                \
     442             :   {                                                                           \
     443             :     return openssl_ops_hash (vm, ops, chunks, n_ops, b ());                   \
     444             :   }
     445             : 
     446         304 : foreach_openssl_hash_op;
     447             : #undef _
     448             : 
     449             : #define _(a, b) \
     450             : static u32 \
     451             : openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
     452             : { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \
     453             : static u32 \
     454             : openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
     455             :     vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
     456             : { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \
     457             : 
     458        3677 : foreach_openssl_hmac_op;
     459             : #undef _
     460             : 
     461             : 
     462             : clib_error_t *
     463         559 : crypto_openssl_init (vlib_main_t * vm)
     464             : {
     465         559 :   vlib_thread_main_t *tm = vlib_get_thread_main ();
     466             :   openssl_per_thread_data_t *ptd;
     467             :   u8 seed[32];
     468             : 
     469         559 :   if (syscall (SYS_getrandom, &seed, sizeof (seed), 0) != sizeof (seed))
     470           0 :     return clib_error_return_unix (0, "getrandom() failed");
     471             : 
     472         559 :   RAND_seed (seed, sizeof (seed));
     473             : 
     474         559 :   u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
     475             : 
     476             : #define _(m, a, b, iv)                                                        \
     477             :   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC,      \
     478             :                                      openssl_ops_enc_##a,                     \
     479             :                                      openssl_ops_enc_chained_##a);            \
     480             :   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC,      \
     481             :                                      openssl_ops_dec_##a,                     \
     482             :                                      openssl_ops_dec_chained_##a);
     483             : 
     484         559 :   foreach_openssl_evp_op;
     485             : #undef _
     486             : 
     487             : #define _(a, b) \
     488             :   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
     489             :                                     openssl_ops_hmac_##a, \
     490             :                                     openssl_ops_hmac_chained_##a); \
     491             : 
     492         559 :   foreach_openssl_hmac_op;
     493             : #undef _
     494             : 
     495             : #define _(a, b)                                                               \
     496             :   vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HASH,     \
     497             :                                      openssl_ops_hash_##a,                    \
     498             :                                      openssl_ops_hash_chained_##a);
     499             : 
     500         559 :   foreach_openssl_hash_op;
     501             : #undef _
     502             : 
     503         559 :   vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
     504             :                         CLIB_CACHE_LINE_BYTES);
     505             : 
     506        1172 :   vec_foreach (ptd, per_thread_data)
     507             :   {
     508         613 :     ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
     509         613 :     EVP_CIPHER_CTX_set_padding (ptd->evp_cipher_ctx, 0);
     510             : #if OPENSSL_VERSION_NUMBER >= 0x10100000L
     511         613 :     ptd->hmac_ctx = HMAC_CTX_new ();
     512         613 :     ptd->hash_ctx = EVP_MD_CTX_create ();
     513             : #else
     514             :     HMAC_CTX_init (&(ptd->_hmac_ctx));
     515             :     ptd->hmac_ctx = &ptd->_hmac_ctx;
     516             : #endif
     517             :   }
     518             : 
     519         559 :   return 0;
     520             : }
     521             : 
     522             : /* *INDENT-OFF* */
     523        1119 : VLIB_INIT_FUNCTION (crypto_openssl_init) =
     524             : {
     525             :   .runs_after = VLIB_INITS ("vnet_crypto_init"),
     526             : };
     527             : /* *INDENT-ON* */
     528             : 
     529             : 
     530             : /* *INDENT-OFF* */
     531             : VLIB_PLUGIN_REGISTER () = {
     532             :   .version = VPP_BUILD_VER,
     533             :   .description = "OpenSSL Crypto Engine",
     534             : };
     535             : /* *INDENT-ON* */
     536             : 
     537             : /*
     538             :  * fd.io coding-style-patch-verification: ON
     539             :  *
     540             :  * Local Variables:
     541             :  * eval: (c-set-style "gnu")
     542             :  * End:
     543             :  */

Generated by: LCOV version 1.14