2
* HMAC (RFC-2104) implementation.
4
* Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
5
* Copyright (c) 2011-2012 Florian Zeitz <florob@babelmonkeys.de>
7
* This software is released under the MIT license.
12
#include "safe-memset.h"
14
void hmac_init(struct hmac_context *_ctx, const unsigned char *key,
15
size_t key_len, const struct hash_method *meth)
17
struct hmac_context_priv *ctx = &_ctx->u.priv;
19
unsigned char k_ipad[64];
20
unsigned char k_opad[64];
21
unsigned char hashedkey[meth->digest_size];
23
i_assert(meth->context_size <= HMAC_MAX_CONTEXT_SIZE);
29
meth->loop(ctx->ctx, key, key_len);
30
meth->result(ctx->ctx, hashedkey);
32
key_len = meth->digest_size;
35
memcpy(k_ipad, key, key_len);
36
memset(k_ipad + key_len, 0, 64 - key_len);
37
memcpy(k_opad, k_ipad, 64);
39
for (i = 0; i < 64; i++) {
45
meth->loop(ctx->ctx, k_ipad, 64);
46
meth->init(ctx->ctxo);
47
meth->loop(ctx->ctxo, k_opad, 64);
49
safe_memset(k_ipad, 0, 64);
50
safe_memset(k_opad, 0, 64);
53
void hmac_final(struct hmac_context *_ctx, unsigned char *digest)
55
struct hmac_context_priv *ctx = &_ctx->u.priv;
57
ctx->hash->result(ctx->ctx, digest);
59
ctx->hash->loop(ctx->ctxo, digest, ctx->hash->digest_size);
60
ctx->hash->result(ctx->ctxo, digest);