aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/hmac.c
diff options
context:
space:
mode:
authorHans Nilsson <[email protected]>2019-06-05 10:09:37 +0200
committerHans Nilsson <[email protected]>2019-06-14 13:33:07 +0200
commitc8be59d5ec262084b335eceec2ab1db0a6dd133d (patch)
tree63ce47877042c8d2f90480b9a344e301c8462691 /lib/crypto/c_src/hmac.c
parent88772e99964342bac0442b01da93ba51e4b2f3f9 (diff)
downloadotp-c8be59d5ec262084b335eceec2ab1db0a6dd133d.tar.gz
otp-c8be59d5ec262084b335eceec2ab1db0a6dd133d.tar.bz2
otp-c8be59d5ec262084b335eceec2ab1db0a6dd133d.zip
crypto: Move mac compatibility functions to hmac.c and cmac.c
The ultimate goal is to get rid of compatibility with old cryptolib versions so we could remove those compatibility files permanently.
Diffstat (limited to 'lib/crypto/c_src/hmac.c')
-rw-r--r--lib/crypto/c_src/hmac.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/crypto/c_src/hmac.c b/lib/crypto/c_src/hmac.c
index 1a4f96e1d5..5e2c68bfee 100644
--- a/lib/crypto/c_src/hmac.c
+++ b/lib/crypto/c_src/hmac.c
@@ -18,6 +18,16 @@
* %CopyrightEnd%
*/
+
+/*****************************************************************
+ *
+ * This file has functions for compatibility with cryptolibs
+ * lacking the EVP_Digest API.
+ *
+ * See mac.c for the implementation using the EVP interface.
+ *
+ ****************************************************************/
+
#ifndef HAS_EVP_PKEY_CTX
#include "hmac.h"
@@ -215,4 +225,44 @@ ERL_NIF_TERM hmac_final_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return ret;
}
+
+
+int hmac_low_level(ErlNifEnv* env, const EVP_MD *md,
+ ErlNifBinary key_bin, ErlNifBinary text,
+ ErlNifBinary *ret_bin, int *ret_bin_alloc, ERL_NIF_TERM *return_term)
+{
+ unsigned int size_int;
+ size_t size;
+
+ /* Find the needed space */
+ if (HMAC(md,
+ key_bin.data, (int)key_bin.size,
+ text.data, text.size,
+ NULL, &size_int) == NULL)
+ {
+ *return_term = EXCP_ERROR(env, "Get HMAC size failed");
+ return 0;
+ }
+
+ size = (size_t)size_int; /* Otherwise "size" is unused in 0.9.8.... */
+ if (!enif_alloc_binary(size, ret_bin))
+ {
+ *return_term = EXCP_ERROR(env, "Alloc binary");
+ return 0;
+ }
+ *ret_bin_alloc = 1;
+
+ /* And do the real HMAC calc */
+ if (HMAC(md,
+ key_bin.data, (int)key_bin.size,
+ text.data, text.size,
+ ret_bin->data, &size_int) == NULL)
+ {
+ *return_term = EXCP_ERROR(env, "HMAC sign failed");
+ return 0;
+ }
+
+ return 1;
+}
+
#endif