Add MD5_Hmac

This commit is contained in:
Samuel Thibault 2024-10-19 21:42:32 +02:00
parent e856f0a920
commit 42ef80e0b4
2 changed files with 48 additions and 0 deletions

47
md5.c
View file

@ -270,3 +270,50 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
memset(ctx, 0, sizeof(*ctx));
}
void MD5_Hmac(unsigned char *result, const void *data, unsigned long size, const void *key, unsigned long key_size)
{
MD5_CTX ctx;
unsigned char block[MD5_BLOCK_SZ];
unsigned char short_key[MD5_DIGEST_SZ];
unsigned char temp[MD5_DIGEST_SZ];
const unsigned char *used_key;
int i;
if (key_size > MD5_BLOCK_SZ)
{
MD5_Init(&ctx);
MD5_Update(&ctx, key, key_size);
MD5_Final(short_key, &ctx);
used_key = short_key;
key_size = MD5_DIGEST_SZ;
}
else
{
used_key = key;
}
MD5_Init(&ctx);
memset(block, 0x36, MD5_BLOCK_SZ);
for (i = 0; i < key_size; i++)
block[i] ^= used_key[i];
MD5_Update(&ctx, block, MD5_BLOCK_SZ);
MD5_Update(&ctx, data, size);
MD5_Final(temp, &ctx);
MD5_Init(&ctx);
memset(block, 0x5c, MD5_BLOCK_SZ);
for (i = 0; i < key_size; i++)
block[i] ^= used_key[i];
MD5_Update(&ctx, block, MD5_BLOCK_SZ);
MD5_Update(&ctx, temp, sizeof(temp));
MD5_Final(result, &ctx);
}