crypto: fix init dependency
[vpp.git] / src / plugins / crypto_openssl / main.c
index 30c9c03..850ec65 100644 (file)
@@ -44,6 +44,7 @@ static openssl_per_thread_data_t *per_thread_data = 0;
   _(AES_256_CBC, EVP_aes_256_cbc)
 
 #define foreach_openssl_hmac_op \
+  _(MD5, EVP_md5) \
   _(SHA1, EVP_sha1) \
   _(SHA224, EVP_sha224) \
   _(SHA256, EVP_sha256) \
@@ -68,7 +69,8 @@ openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
 
       EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
       EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
-      EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
+      if (out_len < op->len)
+       EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     }
   return n_ops;
@@ -89,7 +91,8 @@ openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
 
       EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
       EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
-      EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
+      if (out_len < op->len)
+       EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     }
   return n_ops;
@@ -99,21 +102,35 @@ static_always_inline u32
 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
                  const EVP_MD * md)
 {
+  u8 buffer[64];
   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
                                                     vm->thread_index);
   HMAC_CTX *ctx = ptd->hmac_ctx;
-  u32 i;
+  u32 i, n_fail = 0;
   for (i = 0; i < n_ops; i++)
     {
       vnet_crypto_op_t *op = ops[i];
       unsigned int out_len;
+      size_t sz = op->hmac_trunc_len ? op->hmac_trunc_len : EVP_MD_size (md);
 
       HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
       HMAC_Update (ctx, op->src, op->len);
-      HMAC_Final (ctx, op->dst, &out_len);
+      HMAC_Final (ctx, buffer, &out_len);
+
+      if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
+       {
+         if ((memcmp (op->dst, buffer, sz)))
+           {
+             n_fail++;
+             op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
+             continue;
+           }
+       }
+      else
+       clib_memcpy_fast (op->dst, buffer, sz);
       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
     }
-  return n_ops;
+  return n_ops - n_fail;
 }
 
 #define _(a, b) \
@@ -146,12 +163,13 @@ crypto_openssl_init (vlib_main_t * vm)
   time_t t;
   pid_t pid;
 
-  u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
   clib_error_t *error;
 
   if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
     return error;
 
+  u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
+
 #define _(a, b) \
   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
                                    openssl_ops_enc_##a); \