/** MD5 HMAC not dependent on OpenSSL
*
- * @file src/lib/util/hmacmd5.c
+ * @file src/lib/util/hmac_md5.c
*
* @note New code that needs fast or incremental HMACs should use the OpenSSL EVP_* HMAC
* interface instead, as that can take advantage of acceleration instructions provided
*/
RCSID("$Id$")
-#ifdef HAVE_OPENSSL_EVP_H
-#include <freeradius-devel/tls/base.h>
-#endif
-
#include <freeradius-devel/util/md5.h>
#ifdef HAVE_OPENSSL_EVP_H
+# include <freeradius-devel/tls/base.h>
+# include <openssl/hmac.h>
+
+fr_thread_local_setup(HMAC_CTX *, md5_hmac_ctx)
+
+static void _hmac_md5_ctx_free_on_exit(void *arg)
+{
+ HMAC_CTX_free(arg);
+}
+
/** Calculate HMAC using OpenSSL's MD5 implementation
*
* @param digest Caller digest to be filled in.
void fr_hmac_md5(uint8_t digest[MD5_DIGEST_LENGTH], uint8_t const *text, size_t text_len,
uint8_t const *key, size_t key_len)
{
- HMAC_CTX *ctx = HMAC_CTX_new();
+ HMAC_CTX *ctx;
+
+ if (unlikely(!md5_hmac_ctx)) {
+ ctx = HMAC_CTX_new();
+ if (unlikely(!ctx)) return;
+ fr_thread_local_set_destructor(md5_hmac_ctx, _hmac_md5_ctx_free_on_exit, ctx);
+ } else {
+ ctx = md5_hmac_ctx;
+ }
#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
/* Since MD5 is not allowed by FIPS, explicitly allow it. */
HMAC_Init_ex(ctx, key, key_len, EVP_md5(), NULL);
HMAC_Update(ctx, text, text_len);
HMAC_Final(ctx, digest, NULL);
+ HMAC_CTX_cleanup(ctx);
}
-
#else
-
/** Calculate HMAC using internal MD5 implementation
*
* @param digest Caller digest to be filled in.
*/
int main(int argc, char **argv)
{
- uint8_t digest[16];
- char *key;
- int key_len;
- char *text;
- int text_len;
- int i;
+ uint8_t digest[16];
+ char *key;
+ int key_len;
+ char *text;
+ int text_len;
+ int i;
- key = argv[1];
- key_len = strlen(key);
+ key = argv[1];
+ key_len = strlen(key);
- text = argv[2];
- text_len = strlen(text);
+ text = argv[2];
+ text_len = strlen(text);
- fr_hmac_md5(digest, text, text_len, key, key_len);
+ fr_hmac_md5(digest, text, text_len, key, key_len);
- for (i = 0; i < 16; i++) {
- printf("%02x", digest[i]);
- }
- printf("\n");
+ for (i = 0; i < 16; i++) {
+ printf("%02x", digest[i]);
+ }
+ printf("\n");
- exit(0);
- return 0;
+ exit(0);
+ return 0;
}
#endif
*
* Adapted from hmacmd5.c (HMAC-MD5). Test cases from RFC2202.
*
- * @file src/lib/util/hmacsha1.c
+ * @file src/lib/util/hmac_sha1.c
*
* @author Michael Richardson <mcr@sandelman.ottawa.on.ca>
*
*/
RCSID("$Id$")
-#ifdef HAVE_OPENSSL_EVP_H
-#include <freeradius-devel/tls/base.h>
-#endif
-
#include <freeradius-devel/util/sha1.h>
#ifdef HMAC_SHA1_DATA_PROBLEMS
#endif
#ifdef HAVE_OPENSSL_EVP_H
+# include <freeradius-devel/tls/base.h>
+# include <openssl/hmac.h>
+
+fr_thread_local_setup(HMAC_CTX *, sha1_hmac_ctx)
+
+static void _hmac_sha1_ctx_free_on_exit(void *arg)
+{
+ HMAC_CTX_free(arg);
+}
+
/** Calculate HMAC using OpenSSL's SHA1 implementation
*
* @param digest Caller digest to be filled in.
void fr_hmac_sha1(uint8_t digest[SHA1_DIGEST_LENGTH], uint8_t const *text, size_t text_len,
uint8_t const *key, size_t key_len)
{
- HMAC_CTX *ctx = HMAC_CTX_new();
+ HMAC_CTX *ctx;
+
+ if (unlikely(!sha1_hmac_ctx)) {
+ ctx = HMAC_CTX_new();
+ if (unlikely(!ctx)) return;
+ fr_thread_local_set_destructor(sha1_hmac_ctx, _hmac_sha1_ctx_free_on_exit, ctx);
+ } else {
+ ctx = sha1_hmac_ctx;
+ }
+
HMAC_Init_ex(ctx, key, key_len, EVP_sha1(), NULL);
HMAC_Update(ctx, text, text_len);
HMAC_Final(ctx, digest, NULL);
+ HMAC_CTX_cleanup(ctx);
}
#else