summaryrefslogtreecommitdiffstats
path: root/src/lib/hmac.h
blob: b32c0394bab9d3a76a2031ce92296d23ac5d783c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef HMAC_H
#define HMAC_H

#include "hash-method.h"
#include "sha1.h"
#include "sha2.h"

#define HMAC_MAX_CONTEXT_SIZE sizeof(struct sha512_ctx)

struct hmac_context_priv {
	char ctx[HMAC_MAX_CONTEXT_SIZE];
	char ctxo[HMAC_MAX_CONTEXT_SIZE];
	const struct hash_method *hash;
};

struct hmac_context {
	union {
		struct hmac_context_priv priv;
		uint64_t padding_requirement;
	} u;
};

void hmac_init(struct hmac_context *ctx, const unsigned char *key,
		size_t key_len, const struct hash_method *meth);
void hmac_final(struct hmac_context *ctx, unsigned char *digest);


static inline void
hmac_update(struct hmac_context *_ctx, const void *data, size_t size)
{
	struct hmac_context_priv *ctx = &_ctx->u.priv;

	ctx->hash->loop(ctx->ctx, data, size);
}

buffer_t *t_hmac_data(const struct hash_method *meth,
		      const unsigned char *key, size_t key_len,
		      const void *data, size_t data_len);
buffer_t *t_hmac_buffer(const struct hash_method *meth,
			const unsigned char *key, size_t key_len,
			const buffer_t *data);
buffer_t *t_hmac_str(const struct hash_method *meth,
		     const unsigned char *key, size_t key_len,
		     const char *data);

void hmac_hkdf(const struct hash_method *method,
	       const unsigned char *salt, size_t salt_len,
	       const unsigned char *ikm, size_t ikm_len,
	       const unsigned char *info, size_t info_len,
	       buffer_t *okm_r, size_t okm_len);

static inline buffer_t *
t_hmac_hkdf(const struct hash_method *method,
	    const unsigned char *salt, size_t salt_len,
	    const unsigned char *ikm, size_t ikm_len,
	    const unsigned char *info, size_t info_len,
	    size_t okm_len)
{
	buffer_t *okm_buffer = t_buffer_create(okm_len);
	hmac_hkdf(method, salt, salt_len, ikm, ikm_len, info, info_len,
		  okm_buffer, okm_len);
	return okm_buffer;
}

#endif