diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-08-26 09:04:11 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-08-26 09:04:11 +0200 |
commit | 3621a04cf020cba747ba75136aec7a575890cceb (patch) | |
tree | 1cb7e00db51863beb015f349ac8990db9261a873 /src/hmac.c | |
parent | 94ea8d1ac53b3aada2c616f84b8e4686ae51054c (diff) |
Huge overhall; fix code; remove external deps
Diffstat (limited to 'src/hmac.c')
-rw-r--r-- | src/hmac.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/hmac.c b/src/hmac.c new file mode 100644 index 0000000..5175ee2 --- /dev/null +++ b/src/hmac.c @@ -0,0 +1,41 @@ +#include <string.h> + +#include "sha1.h" + +#define IPAD (0x36) +#define OPAD (0x5C) + +void +hmac_sha1(uint8_t *restrict out, + const uint8_t *restrict key, size_t keysz, + const uint8_t *restrict msg, size_t msgsz) +{ + uint8_t keyext[SHA1BLKSZ] = {0}, + keyipad[SHA1BLKSZ], + keyopad[SHA1BLKSZ]; + + if (keysz > SHA1BLKSZ) { + sha1_t sha; + sha1init(&sha); + sha1hash(&sha, key, keysz); + sha1end(&sha, keyext); + } else + memcpy(keyext, key, keysz); + + for (size_t i = 0; i < sizeof(keyext); i++) { + keyipad[i] = keyext[i] ^ IPAD; + keyopad[i] = keyext[i] ^ OPAD; + } + + sha1_t sha; + uint8_t dgst[SHA1DGSTSZ]; + sha1init(&sha); + sha1hash(&sha, keyipad, sizeof(keyipad)); + sha1hash(&sha, msg, msgsz); + sha1end(&sha, dgst); + + sha1init(&sha); + sha1hash(&sha, keyopad, sizeof(keyopad)); + sha1hash(&sha, dgst, sizeof(dgst)); + sha1end(&sha, out); +} |