diff options
author | Alan T. DeKok <aland@freeradius.org> | 2024-06-11 14:15:45 -0400 |
---|---|---|
committer | Robert Gingras <rgingras@mieweb.com> | 2025-03-31 11:17:14 -0400 |
commit | 00f4bac96a2b2531907182da2ecb49f2d6c712f6 (patch) | |
tree | c32bf73a0ee939e387aefebba96ce16f87577eb7 /src | |
parent | c14d4c2359b6c507467051a67935603038528889 (diff) | |
download | libpam-radius-auth-00f4bac96a2b2531907182da2ecb49f2d6c712f6.tar.gz libpam-radius-auth-00f4bac96a2b2531907182da2ecb49f2d6c712f6.zip |
add HMAC-MD5 implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/md5.c | 73 | ||||
-rw-r--r-- | src/md5.h | 3 |
2 files changed, 76 insertions, 0 deletions
@@ -173,6 +173,79 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ } +/** Calculate HMAC using internal MD5 implementation + * + * @param digest Caller digest to be filled in. + * @param text Pointer to data stream. + * @param text_len length of data stream. + * @param key Pointer to authentication key. + * @param key_len Length of authentication key. + * + */ +void hmac_md5(uint8_t digest[16], uint8_t const *text, size_t text_len, + uint8_t const *key, size_t key_len) +{ + MD5_CTX context; + uint8_t k_ipad[65]; /* inner padding - key XORd with ipad */ + uint8_t k_opad[65]; /* outer padding - key XORd with opad */ + uint8_t tk[16]; + int i; + + /* if key is longer than 64 bytes reset it to key=MD5(key) */ + if (key_len > 64) { + MD5_CTX tctx; + + MD5Init(&tctx); + MD5Update(&tctx, key, key_len); + MD5Final(tk, &tctx); + + key = tk; + key_len = 16; + } + + /* + * the HMAC_MD5 transform looks like: + * + * MD5(K XOR opad, MD5(K XOR ipad, text)) + * + * where K is an n byte key + * ipad is the byte 0x36 repeated 64 times + + * opad is the byte 0x5c repeated 64 times + * and text is the data being protected + */ + + /* start out by storing key in pads */ + memset( k_ipad, 0, sizeof(k_ipad)); + memset( k_opad, 0, sizeof(k_opad)); + memcpy( k_ipad, key, key_len); + memcpy( k_opad, key, key_len); + + /* XOR key with ipad and opad values */ + for (i = 0; i < 64; i++) { + k_ipad[i] ^= 0x36; + k_opad[i] ^= 0x5c; + } + /* + * perform inner MD5 + */ + MD5Init(&context); /* init context for 1st + * pass */ + MD5Update(&context, k_ipad, 64); /* start with inner pad */ + MD5Update(&context, text, text_len); /* then text of datagram */ + MD5Final(digest, &context); /* finish up 1st pass */ + /* + * perform outer MD5 + */ + MD5Init(&context); /* init context for 2nd + * pass */ + MD5Update(&context, k_opad, 64); /* start with outer pad */ + MD5Update(&context, digest, 16); /* then results of 1st + * hash */ + MD5Final(digest, &context); /* finish up 2nd pass */ +} + + #ifndef ASM_MD5 /* The four core functions - F1 is optimized somewhat */ @@ -51,6 +51,9 @@ void MD5Update(struct MD5Context *, unsigned const char *, unsigned); void MD5Final(unsigned char digest[16], struct MD5Context *); void MD5Transform(uint32_t buf[4], uint32_t const in[16]); +void hmac_md5(uint8_t digest[16], uint8_t const *text, size_t text_len, + uint8_t const *key, size_t key_len); + /* * This is needed to make RSAREF happy on some MS-DOS compilers. */ |