aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/plugins/hmac/hmac_hmac.c
blob: 871a294d9a78d1ac9ec38d8b84bccb3b625bef55 (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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2012 Tobias Brunner
 * Copyright (C) 2005-2006 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "hmac_hmac.h"

#include <crypto/hmacs/hmac.h>
#include <crypto/hmacs/hmac_prf.h>
#include <crypto/hmacs/hmac_signer.h>

typedef struct private_hmac_t private_hmac_t;

/**
 * Private data of a hmac_hmac_t object.
 *
 * The variable names are the same as in the RFC.
 */
struct private_hmac_t {

	/**
	 * Implements hmac_t interface
	 */
	hmac_t public;

	/**
	 * Block size, as in RFC.
	 */
	u_int8_t b;

	/**
	 * Hash function.
	 */
	hasher_t *h;

	/**
	 * Previously xor'ed key using opad.
	 */
	chunk_t opaded_key;

	/**
	 * Previously xor'ed key using ipad.
	 */
	chunk_t ipaded_key;
};

METHOD(hmac_t, get_mac, void,
	private_hmac_t *this, chunk_t data, u_int8_t *out)
{
	/* H(K XOR opad, H(K XOR ipad, text))
	 *
	 * if out is NULL, we append text to the inner hash.
	 * else, we complete the inner and do the outer.
	 *
	 */

	u_int8_t buffer[this->h->get_hash_size(this->h)];
	chunk_t inner;

	if (out == NULL)
	{
		/* append data to inner */
		this->h->get_hash(this->h, data, NULL);
	}
	else
	{
		/* append and do outer hash */
		inner.ptr = buffer;
		inner.len = this->h->get_hash_size(this->h);

		/* complete inner */
		this->h->get_hash(this->h, data, buffer);

		/* do outer */
		this->h->get_hash(this->h, this->opaded_key, NULL);
		this->h->get_hash(this->h, inner, out);

		/* reinit for next call */
		this->h->get_hash(this->h, this->ipaded_key, NULL);
	}
}

METHOD(hmac_t, get_mac_size, size_t,
	private_hmac_t *this)
{
	return this->h->get_hash_size(this->h);
}

METHOD(hmac_t, set_key, void,
	private_hmac_t *this, chunk_t key)
{
	int i;
	u_int8_t buffer[this->b];

	memset(buffer, 0, this->b);

	if (key.len > this->b)
	{
		/* if key is too long, it will be hashed */
		this->h->get_hash(this->h, key, buffer);
	}
	else
	{
		/* if not, just copy it in our pre-padded k */
		memcpy(buffer, key.ptr, key.len);
	}

	/* apply ipad and opad to key */
	for (i = 0; i < this->b; i++)
	{
		this->ipaded_key.ptr[i] = buffer[i] ^ 0x36;
		this->opaded_key.ptr[i] = buffer[i] ^ 0x5C;
	}

	/* begin hashing of inner pad */
	this->h->reset(this->h);
	this->h->get_hash(this->h, this->ipaded_key, NULL);
}

METHOD(hmac_t, destroy, void,
	private_hmac_t *this)
{
	this->h->destroy(this->h);
	chunk_clear(&this->opaded_key);
	chunk_clear(&this->ipaded_key);
	free(this);
}

/*
 * Creates an hmac_t object
 */
static hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
{
	private_hmac_t *this;

	INIT(this,
		.public = {
			.get_mac = _get_mac,
			.get_mac_size = _get_mac_size,
			.set_key = _set_key,
			.destroy = _destroy,
		},
	);

	/* set b, according to hasher */
	switch (hash_algorithm)
	{
		case HASH_SHA1:
		case HASH_MD5:
		case HASH_SHA256:
			this->b = 64;
			break;
		case HASH_SHA384:
		case HASH_SHA512:
			this->b = 128;
			break;
		default:
			free(this);
			return NULL;
	}

	this->h = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
	if (this->h == NULL)
	{
		free(this);
		return NULL;
	}

	/* build ipad and opad */
	this->opaded_key.ptr = malloc(this->b);
	this->opaded_key.len = this->b;

	this->ipaded_key.ptr = malloc(this->b);
	this->ipaded_key.len = this->b;

	return &this->public;
}

/*
 * Described in header
 */
prf_t *hmac_hmac_prf_create(pseudo_random_function_t algo)
{
	hmac_t *hmac = NULL;

	switch (algo)
	{
		case PRF_HMAC_SHA1:
			hmac = hmac_create(HASH_SHA1);
			break;
		case PRF_HMAC_MD5:
			hmac = hmac_create(HASH_MD5);
			break;
		case PRF_HMAC_SHA2_256:
			hmac = hmac_create(HASH_SHA256);
			break;
		case PRF_HMAC_SHA2_384:
			hmac = hmac_create(HASH_SHA384);
			break;
		case PRF_HMAC_SHA2_512:
			hmac = hmac_create(HASH_SHA512);
			break;
		default:
			break;
	}
	if (hmac)
	{
		return hmac_prf_create(hmac);
	}
	return NULL;
}

/*
 * Described in header
 */
signer_t *hmac_hmac_signer_create(integrity_algorithm_t algo)
{
	hmac_t *hmac = NULL;
	size_t trunc = 0;

	switch (algo)
	{
		case AUTH_HMAC_MD5_96:
			hmac = hmac_create(HASH_MD5);
			trunc = 12;
			break;
		case AUTH_HMAC_MD5_128:
			hmac = hmac_create(HASH_MD5);
			trunc = 16;
			break;
		case AUTH_HMAC_SHA1_96:
			hmac = hmac_create(HASH_SHA1);
			trunc = 12;
			break;
		case AUTH_HMAC_SHA1_128:
			hmac = hmac_create(HASH_SHA1);
			trunc = 16;
			break;
		case AUTH_HMAC_SHA1_160:
			hmac = hmac_create(HASH_SHA1);
			trunc = 20;
			break;
		case AUTH_HMAC_SHA2_256_128:
			hmac = hmac_create(HASH_SHA256);
			trunc = 16;
			break;
		case AUTH_HMAC_SHA2_256_256:
			hmac = hmac_create(HASH_SHA256);
			trunc = 32;
			break;
		case AUTH_HMAC_SHA2_384_192:
			hmac = hmac_create(HASH_SHA384);
			trunc = 24;
			break;
		case AUTH_HMAC_SHA2_384_384:
			hmac = hmac_create(HASH_SHA384);
			trunc = 48;
			break;
		case AUTH_HMAC_SHA2_512_256:
			hmac = hmac_create(HASH_SHA512);
			trunc = 32;
			break;
		default:
			break;
	}
	if (hmac)
	{
		return hmac_signer_create(hmac, trunc);
	}
	return NULL;
}