summaryrefslogtreecommitdiffstats
path: root/crypto/hmac.c
blob: c2195d972e23d5796661d8a9591671f68a1b27c8 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * (C) Copyright 2015 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * GPL v2 only
 */

#include <common.h>
#include <digest.h>
#include <malloc.h>

#include "internal.h"

struct digest_hmac {
	char *name;
	unsigned int pad_length;
	struct digest_algo algo;
};

struct digest_hmac_ctx {
	struct digest *d;

	unsigned char *ipad;
	unsigned char *opad;

	unsigned char *key;
	unsigned int keylen;
};

static inline struct digest_hmac *to_digest_hmac(struct digest_algo *algo)
{
	return container_of(algo, struct digest_hmac, algo);
}

static int digest_hmac_alloc(struct digest *d)
{
	struct digest_hmac_ctx *dh = d->ctx;
	struct digest_hmac *hmac = to_digest_hmac(d->algo);

	dh->d = digest_alloc(hmac->name);
	if (!dh->d)
		return -EINVAL;

	dh->ipad = xmalloc(hmac->pad_length);
	dh->opad = xmalloc(hmac->pad_length);

	return 0;
}

static void digest_hmac_free(struct digest *d)
{
	struct digest_hmac_ctx *dh = d->ctx;

	free(dh->ipad);
	free(dh->opad);
	free(dh->key);

	digest_free(dh->d);
}

static int digest_hmac_set_key(struct digest *d, const unsigned char *key,
				unsigned int len)
{
	struct digest_hmac_ctx *dh = d->ctx;
	struct digest_hmac *hmac = to_digest_hmac(d->algo);
	unsigned char *sum = NULL;
	int ret;

	free(dh->key);
	if (len > hmac->pad_length) {
		sum = xmalloc(digest_length(dh->d));
		ret = digest_digest(dh->d, dh->key, dh->keylen, sum);
		if (ret)
			goto err;
		dh->keylen = digest_length(dh->d);
		dh->key = sum;
	} else {
		dh->key = xmalloc(len);
		memcpy(dh->key, key, len);
		dh->keylen = len;
	}

	ret = 0;
err:
	free(sum);
	return ret;
}

static int digest_hmac_init(struct digest *d)
{
	struct digest_hmac_ctx *dh = d->ctx;
	struct digest_hmac *hmac = to_digest_hmac(d->algo);
	int i, ret;
	unsigned char *key = dh->key;
	unsigned int keylen = dh->keylen;

	memset(dh->ipad, 0x36, hmac->pad_length);
	memset(dh->opad, 0x5C, hmac->pad_length);

	for (i = 0; i < keylen; i++) {
		dh->ipad[i] = (unsigned char)(dh->ipad[i] ^ key[i]);
		dh->opad[i] = (unsigned char)(dh->opad[i] ^ key[i]);
	}

	ret = digest_init(dh->d);
	if (ret)
		return ret;
	return digest_update(dh->d, dh->ipad, hmac->pad_length);
}

static int digest_hmac_update(struct digest *d, const void *data,
			      unsigned long len)
{
	struct digest_hmac_ctx *dh = d->ctx;

	return digest_update(dh->d, data, len);
}

static int digest_hmac_final(struct digest *d, unsigned char *md)
{
	struct digest_hmac_ctx *dh = d->ctx;
	struct digest_hmac *hmac = to_digest_hmac(d->algo);
	unsigned char *tmp = NULL;
	int ret;

	tmp = xmalloc(digest_length(d));

	ret = digest_final(dh->d, tmp);
	if (ret)
		goto err;
	ret = digest_init(dh->d);
	if (ret)
		goto err;
	ret = digest_update(dh->d, dh->opad, hmac->pad_length);
	if (ret)
		goto err;
	ret = digest_update(dh->d, tmp, digest_length(d));
	if (ret)
		goto err;
	ret = digest_final(dh->d, md);

err:
	free(tmp);

	return ret;
}

struct digest_algo hmac_algo = {
	.alloc = digest_hmac_alloc,
	.init = digest_hmac_init,
	.update = digest_hmac_update,
	.final = digest_hmac_final,
	.digest = digest_generic_digest,
	.verify = digest_generic_verify,
	.set_key = digest_hmac_set_key,
	.free = digest_hmac_free,
	.ctx_length = sizeof(struct digest_hmac),
};

int digest_hmac_register(struct digest_algo *algo, unsigned int pad_length)
{
	struct digest_hmac *dh;

	if (!algo || !pad_length)
		return -EINVAL;

	dh = xzalloc(sizeof(*dh));
	dh->name = xstrdup(algo->name);
	dh->pad_length = pad_length;
	dh->algo = hmac_algo;
	dh->algo.length = algo->length;
	dh->algo.name = asprintf("hmac(%s)", algo->name);

	return digest_algo_register(&dh->algo);
}