summaryrefslogtreecommitdiff
path: root/lib/accelerated/x86/hmac-padlock.c
blob: 1d1a1bd1e608f2661f249beeede0068b095fe330 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

/* This file provides the backend hash/mac implementation for
 * VIA Padlock hardware acceleration.
 */

#include "gnutls_int.h"
#include <hash_int.h>
#include "errors.h"
#include <nettle/sha.h>
#include <nettle/hmac.h>
#include <nettle/macros.h>
#include <nettle/memxor.h>
#include <aes-padlock.h>
#include <sha-padlock.h>
#include <algorithms.h>

#ifdef HAVE_LIBNETTLE

#define IPAD 0x36
#define OPAD 0x5c
#define MAX_SHA_DIGEST_SIZE (512/8)

typedef void (*update_func) (void *, unsigned, const uint8_t *);
typedef void (*digest_func) (void *, unsigned, uint8_t *);
typedef void (*set_key_func) (void *, unsigned, const uint8_t *);

struct padlock_hmac_ctx {
	union {
		struct hmac_sha224_ctx sha224;
		struct hmac_sha256_ctx sha256;
		struct hmac_sha384_ctx sha384;
		struct hmac_sha512_ctx sha512;
		struct hmac_sha1_ctx sha1;
	} ctx;

	void *ctx_ptr;
	gnutls_mac_algorithm_t algo;
	size_t length;
	update_func update;
	digest_func digest;
	set_key_func setkey;
};

static void
padlock_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
			  unsigned key_length, const uint8_t * key)
{
	HMAC_SET_KEY(ctx, &padlock_sha1, key_length, key);
}

static void
padlock_hmac_sha1_update(struct hmac_sha1_ctx *ctx,
			 unsigned length, const uint8_t * data)
{
	padlock_sha1_update(&ctx->state, length, data);
}

static void
padlock_hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
			 unsigned length, uint8_t * digest)
{
	HMAC_DIGEST(ctx, &padlock_sha1, length, digest);
}

static void
padlock_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
			    unsigned key_length, const uint8_t * key)
{
	HMAC_SET_KEY(ctx, &padlock_sha256, key_length, key);
}

static void
padlock_hmac_sha256_update(struct hmac_sha256_ctx *ctx,
			   unsigned length, const uint8_t * data)
{
	padlock_sha256_update(&ctx->state, length, data);
}

static void
padlock_hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
			   unsigned length, uint8_t * digest)
{
	HMAC_DIGEST(ctx, &padlock_sha256, length, digest);
}

static void
padlock_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
			    unsigned key_length, const uint8_t * key)
{
	HMAC_SET_KEY(ctx, &padlock_sha224, key_length, key);
}

static void
padlock_hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
			   unsigned length, uint8_t * digest)
{
	HMAC_DIGEST(ctx, &padlock_sha224, length, digest);
}

static void
padlock_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
			    unsigned key_length, const uint8_t * key)
{
	HMAC_SET_KEY(ctx, &padlock_sha384, key_length, key);
}

static void
padlock_hmac_sha384_digest(struct hmac_sha384_ctx *ctx,
			   unsigned length, uint8_t * digest)
{
	HMAC_DIGEST(ctx, &padlock_sha384, length, digest);
}

static void
padlock_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
			    unsigned key_length, const uint8_t * key)
{
	HMAC_SET_KEY(ctx, &padlock_sha512, key_length, key);
}

static void
padlock_hmac_sha512_update(struct hmac_sha512_ctx *ctx,
			   unsigned length, const uint8_t * data)
{
	padlock_sha512_update(&ctx->state, length, data);
}

static void
padlock_hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
			   unsigned length, uint8_t * digest)
{
	HMAC_DIGEST(ctx, &padlock_sha512, length, digest);
}

static int
_hmac_ctx_init(gnutls_mac_algorithm_t algo, struct padlock_hmac_ctx *ctx)
{
	switch (algo) {
	case GNUTLS_MAC_SHA1:
		ctx->update = (update_func) padlock_hmac_sha1_update;
		ctx->digest = (digest_func) padlock_hmac_sha1_digest;
		ctx->setkey = (set_key_func) padlock_hmac_sha1_set_key;
		ctx->ctx_ptr = &ctx->ctx.sha1;
		ctx->length = SHA1_DIGEST_SIZE;
		break;
	case GNUTLS_MAC_SHA224:
		ctx->update = (update_func) padlock_hmac_sha256_update;
		ctx->digest = (digest_func) padlock_hmac_sha224_digest;
		ctx->setkey = (set_key_func) padlock_hmac_sha224_set_key;
		ctx->ctx_ptr = &ctx->ctx.sha224;
		ctx->length = SHA224_DIGEST_SIZE;
		break;
	case GNUTLS_MAC_SHA256:
		ctx->update = (update_func) padlock_hmac_sha256_update;
		ctx->digest = (digest_func) padlock_hmac_sha256_digest;
		ctx->setkey = (set_key_func) padlock_hmac_sha256_set_key;
		ctx->ctx_ptr = &ctx->ctx.sha256;
		ctx->length = SHA256_DIGEST_SIZE;
		break;
	case GNUTLS_MAC_SHA384:
		ctx->update = (update_func) padlock_hmac_sha512_update;
		ctx->digest = (digest_func) padlock_hmac_sha384_digest;
		ctx->setkey = (set_key_func) padlock_hmac_sha384_set_key;
		ctx->ctx_ptr = &ctx->ctx.sha384;
		ctx->length = SHA384_DIGEST_SIZE;
		break;
	case GNUTLS_MAC_SHA512:
		ctx->update = (update_func) padlock_hmac_sha512_update;
		ctx->digest = (digest_func) padlock_hmac_sha512_digest;
		ctx->setkey = (set_key_func) padlock_hmac_sha512_set_key;
		ctx->ctx_ptr = &ctx->ctx.sha512;
		ctx->length = SHA512_DIGEST_SIZE;
		break;
	default:
		gnutls_assert();
		return GNUTLS_E_INVALID_REQUEST;
	}

	return 0;
}


static int wrap_padlock_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
{
	struct padlock_hmac_ctx *ctx;
	int ret;

	ctx = gnutls_calloc(1, sizeof(struct padlock_hmac_ctx));
	if (ctx == NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	ctx->algo = algo;

	ret = _hmac_ctx_init(algo, ctx);
	if (ret < 0)
		return gnutls_assert_val(ret);

	*_ctx = ctx;

	return 0;
}

static int
wrap_padlock_hmac_setkey(void *_ctx, const void *key, size_t keylen)
{
	struct padlock_hmac_ctx *ctx = _ctx;

	ctx->setkey(ctx->ctx_ptr, keylen, key);

	return GNUTLS_E_SUCCESS;
}

static int
wrap_padlock_hmac_update(void *_ctx, const void *text, size_t textsize)
{
	struct padlock_hmac_ctx *ctx = _ctx;

	ctx->update(ctx->ctx_ptr, textsize, text);

	return GNUTLS_E_SUCCESS;
}

static int
wrap_padlock_hmac_output(void *src_ctx, void *digest, size_t digestsize)
{
	struct padlock_hmac_ctx *ctx;
	ctx = src_ctx;

	if (digestsize < ctx->length) {
		gnutls_assert();
		return GNUTLS_E_SHORT_MEMORY_BUFFER;
	}

	ctx->digest(ctx->ctx_ptr, digestsize, digest);

	return 0;
}

static void wrap_padlock_hmac_deinit(void *hd)
{
	gnutls_free(hd);
}

static int
wrap_padlock_hmac_fast(gnutls_mac_algorithm_t algo,
		       const void *nonce, size_t nonce_size,
		       const void *key, size_t key_size, const void *text,
		       size_t text_size, void *digest)
{
	if (algo == GNUTLS_MAC_SHA1 || algo == GNUTLS_MAC_SHA256) {
		unsigned char *pad;
		unsigned char pad2[SHA1_DATA_SIZE + MAX_SHA_DIGEST_SIZE];
		unsigned char hkey[MAX_SHA_DIGEST_SIZE];
		unsigned int digest_size =
		    _gnutls_mac_get_algo_len(mac_to_entry(algo));

		if (key_size > SHA1_DATA_SIZE) {
			wrap_padlock_hash_fast((gnutls_digest_algorithm_t)
					       algo, key, key_size, hkey);
			key = hkey;
			key_size = digest_size;
		}

		pad = gnutls_malloc(text_size + SHA1_DATA_SIZE);
		if (pad == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		memset(pad, IPAD, SHA1_DATA_SIZE);
		memxor(pad, key, key_size);

		memcpy(&pad[SHA1_DATA_SIZE], text, text_size);

		wrap_padlock_hash_fast((gnutls_digest_algorithm_t) algo,
				       pad, text_size + SHA1_DATA_SIZE,
				       &pad2[SHA1_DATA_SIZE]);

		gnutls_free(pad);

		memset(pad2, OPAD, SHA1_DATA_SIZE);
		memxor(pad2, key, key_size);

		wrap_padlock_hash_fast((gnutls_digest_algorithm_t) algo,
				       pad2, digest_size + SHA1_DATA_SIZE,
				       digest);

	} else {
		struct padlock_hmac_ctx ctx;
		int ret;

		ret = _hmac_ctx_init(algo, &ctx);
		if (ret < 0)
			return gnutls_assert_val(ret);
		ctx.algo = algo;

		wrap_padlock_hmac_setkey(&ctx, key, key_size);

		wrap_padlock_hmac_update(&ctx, text, text_size);

		wrap_padlock_hmac_output(&ctx, digest, ctx.length);
		wrap_padlock_hmac_deinit(&ctx);

		zeroize_temp_key(&ctx, sizeof(ctx));
	}

	return 0;
}

const gnutls_crypto_mac_st _gnutls_hmac_sha_padlock = {
	.init = NULL,
	.setkey = NULL,
	.setnonce = NULL,
	.hash = NULL,
	.output = NULL,
	.deinit = NULL,
	.fast = wrap_padlock_hmac_fast
};

const gnutls_crypto_mac_st _gnutls_hmac_sha_padlock_nano = {
	.init = wrap_padlock_hmac_init,
	.setkey = wrap_padlock_hmac_setkey,
	.setnonce = NULL,
	.hash = wrap_padlock_hmac_update,
	.output = wrap_padlock_hmac_output,
	.deinit = wrap_padlock_hmac_deinit,
	.fast = wrap_padlock_hmac_fast,
};

#endif				/* HAVE_LIBNETTLE */