diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-08 19:27:58 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-08 19:27:58 +0100 |
commit | 8860e991f6650e47cfe6c1af595fe4fe92a4edfd (patch) | |
tree | 18f49bb3a21739a1a596b54d9f65e82cff4fc09f /lib/md5.c | |
parent | cc5b93f72db80abd1262a0a5e1d8400ceef54385 (diff) | |
parent | c8cafc8ebb5320ac7c6117c17e6460036f0fdf62 (diff) |
Merge branch 'master' into int-new
Diffstat (limited to 'lib/md5.c')
-rw-r--r-- | lib/md5.c | 81 |
1 files changed, 7 insertions, 74 deletions
@@ -39,8 +39,10 @@ static void md5_transform(u32 buf[4], u32 const in[16]); * initialization constants. */ void -md5_init(struct md5_context *ctx) +md5_init(struct hash_context *CTX) { + struct md5_context *ctx = (void *) CTX; + ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; ctx->buf[2] = 0x98badcfe; @@ -55,8 +57,9 @@ md5_init(struct md5_context *ctx) * of bytes. */ void -md5_update(struct md5_context *ctx, const byte *buf, uint len) +md5_update(struct hash_context *CTX, const byte *buf, uint len) { + struct md5_context *ctx = (void *) CTX; u32 t; /* Update bitcount */ @@ -105,8 +108,9 @@ md5_update(struct md5_context *ctx, const byte *buf, uint len) * 1 0* (64-bit count of bits processed, MSB-first) */ byte * -md5_final(struct md5_context *ctx) +md5_final(struct hash_context *CTX) { + struct md5_context *ctx = (void *) CTX; uint count; byte *p; @@ -149,13 +153,6 @@ md5_final(struct md5_context *ctx) return (byte*) ctx->buf; } -/* I am a hard paranoid */ -void -md5_erase_ctx(struct md5_context *ctx) -{ - memset((char *) ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ -} - /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ @@ -256,67 +253,3 @@ md5_transform(u32 buf[4], u32 const in[16]) buf[2] += c; buf[3] += d; } - - -/* - * MD5-HMAC - */ - -static void -md5_hash_buffer(byte *outbuf, const byte *buffer, size_t length) -{ - struct md5_context hd_tmp; - - md5_init(&hd_tmp); - md5_update(&hd_tmp, buffer, length); - memcpy(outbuf, md5_final(&hd_tmp), MD5_SIZE); -} - -void -md5_hmac_init(struct md5_hmac_context *ctx, const byte *key, size_t keylen) -{ - byte keybuf[MD5_BLOCK_SIZE], buf[MD5_BLOCK_SIZE]; - - /* Hash the key if necessary */ - if (keylen <= MD5_BLOCK_SIZE) - { - memcpy(keybuf, key, keylen); - bzero(keybuf + keylen, MD5_BLOCK_SIZE - keylen); - } - else - { - md5_hash_buffer(keybuf, key, keylen); - bzero(keybuf + MD5_SIZE, MD5_BLOCK_SIZE - MD5_SIZE); - } - - /* Initialize the inner digest */ - md5_init(&ctx->ictx); - int i; - for (i = 0; i < MD5_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x36; - md5_update(&ctx->ictx, buf, MD5_BLOCK_SIZE); - - /* Initialize the outer digest */ - md5_init(&ctx->octx); - for (i = 0; i < MD5_BLOCK_SIZE; i++) - buf[i] = keybuf[i] ^ 0x5c; - md5_update(&ctx->octx, buf, MD5_BLOCK_SIZE); -} - -void -md5_hmac_update(struct md5_hmac_context *ctx, const byte *buf, size_t buflen) -{ - /* Just update the inner digest */ - md5_update(&ctx->ictx, buf, buflen); -} - -byte * -md5_hmac_final(struct md5_hmac_context *ctx) -{ - /* Finish the inner digest */ - byte *isha = md5_final(&ctx->ictx); - - /* Finish the outer digest */ - md5_update(&ctx->octx, isha, MD5_SIZE); - return md5_final(&ctx->octx); -} |