0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#ifndef HMAC_H
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#define HMAC_H
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#include "hash-method.h"
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#include "sha1.h"
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#define HMAC_MAX_CONTEXT_SIZE 256
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainenstruct hmac_context_priv {
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz char ctx[HMAC_MAX_CONTEXT_SIZE];
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz char ctxo[HMAC_MAX_CONTEXT_SIZE];
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz const struct hash_method *hash;
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz};
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainenstruct hmac_context {
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen union {
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen struct hmac_context_priv priv;
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen uint64_t padding_requirement;
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen } u;
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen};
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitzvoid hmac_init(struct hmac_context *ctx, const unsigned char *key,
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz size_t key_len, const struct hash_method *meth);
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitzvoid hmac_final(struct hmac_context *ctx, unsigned char *digest);
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitzstatic inline void
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainenhmac_update(struct hmac_context *_ctx, const void *data, size_t size)
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz{
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen struct hmac_context_priv *ctx = &_ctx->u.priv;
d8361cc8576d9ede93a037f9b96f2a3f9b7e9054Timo Sirainen
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz ctx->hash->loop(ctx->ctx, data, size);
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz}
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomibuffer_t *t_hmac_data(const struct hash_method *meth,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const unsigned char *key, size_t key_len,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const void *data, size_t data_len);
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomibuffer_t *t_hmac_buffer(const struct hash_method *meth,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const unsigned char *key, size_t key_len,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const buffer_t *data);
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomibuffer_t *t_hmac_str(const struct hash_method *meth,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const unsigned char *key, size_t key_len,
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi const char *data);
ffe51a1969f24d88bbfacb3ba2ec83d5b99ae774Aki Tuomi
0a3769a4ef3afbbbd05df38f43ec7382fd65a2b6Florian Zeitz#endif