32 RCSID(
"$Id: 4a3248a5337b73485d00af6dd21370e3dd0fe5f7 $")
34 #include <freeradius-devel/util/sha1.h>
35 #include <freeradius-devel/util/atexit.h>
36 #include <freeradius-devel/util/strerror.h>
38 #ifdef HMAC_SHA1_DATA_PROBLEMS
39 unsigned int sha1_data_problems = 0;
42 #ifdef HAVE_OPENSSL_EVP_H
43 # include <freeradius-devel/tls/openssl_user_macros.h>
44 # include <openssl/hmac.h>
46 static _Thread_local EVP_MD_CTX *sha1_hmac_ctx;
48 static int _hmac_sha1_ctx_free_on_exit(
void *arg)
66 uint8_t const *key,
size_t key_len)
72 ctx = EVP_MD_CTX_new();
77 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
83 pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, key_len);
89 if (
unlikely(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey) != 1)) {
108 EVP_MD_CTX_reset(ctx);
125 uint8_t const *key,
size_t key_len)
145 #ifdef HMAC_SHA1_DATA_PROBLEMS
146 if(sha1_data_problems)
150 printf(
"\nhmac-sha1 key(%d): ", key_len);
152 for (i = 0; i < key_len; i++) {
159 printf(
"%02x", key[i]);
161 printf(
"\nDATA: (%d) ",
inlen);
164 for (i = 0; i <
inlen; i++) {
177 printf(
"%02x",
in[i]);
197 memset(k_ipad, 0,
sizeof(k_ipad));
198 memset(k_opad, 0,
sizeof(k_opad));
199 memcpy(k_ipad, key, key_len);
200 memcpy(k_opad, key, key_len);
203 for (i = 0; i < 64; i++) {
222 #ifdef HMAC_SHA1_DATA_PROBLEMS
223 if (sha1_data_problems) {
226 printf(
"\nhmac-sha1 mac(20): ");
228 for (i = 0; i < 20; i++) {
235 printf(
"%02x", digest[i]);
#define fr_atexit_thread_local(_name, _free, _uctx)
int fr_hmac_sha1(uint8_t digest[static SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal SHA1 implementation.
void fr_sha1_init(fr_sha1_ctx *context)
void fr_sha1_final(uint8_t digest[static SHA1_DIGEST_LENGTH], fr_sha1_ctx *context)
void fr_sha1_update(fr_sha1_ctx *context, uint8_t const *in, size_t len)
#define SHA1_DIGEST_LENGTH
#define fr_strerror_const(_msg)
static size_t char fr_sbuff_t size_t inlen