crypto: fix init dependency
[vpp.git] / src / plugins / crypto_openssl / main.c
index 09d7267..850ec65 100644 (file)
@@ -102,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) \
@@ -149,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); \