2 * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* This file provides the backend hash/mac implementation for
24 * VIA Padlock hardware acceleration.
27 #include <gnutls_int.h>
28 #include <gnutls_hash_int.h>
29 #include <gnutls_errors.h>
30 #include <nettle/sha.h>
31 #include <nettle/hmac.h>
32 #include <nettle/macros.h>
35 #include <algorithms.h>
39 typedef void (*update_func) (void *, unsigned, const uint8_t *);
40 typedef void (*digest_func) (void *, unsigned, uint8_t *);
41 typedef void (*set_key_func) (void *, unsigned, const uint8_t *);
45 struct hmac_sha1_ctx sha1;
46 struct hmac_sha224_ctx sha224;
47 struct hmac_sha256_ctx sha256;
49 struct hmac_sha384_ctx sha384;
50 struct hmac_sha512_ctx sha512;
55 gnutls_mac_algorithm_t algo;
63 x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
64 unsigned key_length, const uint8_t * key)
66 HMAC_SET_KEY(ctx, &x86_sha1, key_length, key);
70 x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx,
71 unsigned length, const uint8_t * data)
73 x86_sha1_update(&ctx->state, length, data);
77 x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
78 unsigned length, uint8_t * digest)
80 HMAC_DIGEST(ctx, &x86_sha1, length, digest);
84 x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
85 unsigned key_length, const uint8_t * key)
87 HMAC_SET_KEY(ctx, &x86_sha256, key_length, key);
91 x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx,
92 unsigned length, const uint8_t * data)
94 x86_sha256_update(&ctx->state, length, data);
98 x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
99 unsigned length, uint8_t * digest)
101 HMAC_DIGEST(ctx, &x86_sha256, length, digest);
105 x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
106 unsigned key_length, const uint8_t * key)
108 HMAC_SET_KEY(ctx, &x86_sha224, key_length, key);
112 x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
113 unsigned length, uint8_t * digest)
115 HMAC_DIGEST(ctx, &x86_sha224, length, digest);
120 x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
121 unsigned key_length, const uint8_t * key)
123 HMAC_SET_KEY(ctx, &x86_sha384, key_length, key);
127 x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx,
128 unsigned length, uint8_t * digest)
130 HMAC_DIGEST(ctx, &x86_sha384, length, digest);
134 x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
135 unsigned key_length, const uint8_t * key)
137 HMAC_SET_KEY(ctx, &x86_sha512, key_length, key);
141 x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx,
142 unsigned length, const uint8_t * data)
144 x86_sha512_update(&ctx->state, length, data);
148 x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
149 unsigned length, uint8_t * digest)
151 HMAC_DIGEST(ctx, &x86_sha512, length, digest);
156 _hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx)
159 case GNUTLS_MAC_SHA1:
160 ctx->update = (update_func) x86_hmac_sha1_update;
161 ctx->digest = (digest_func) x86_hmac_sha1_digest;
162 ctx->setkey = (set_key_func) x86_hmac_sha1_set_key;
163 ctx->ctx_ptr = &ctx->ctx.sha1;
164 ctx->length = SHA1_DIGEST_SIZE;
166 case GNUTLS_MAC_SHA224:
167 ctx->update = (update_func) x86_hmac_sha256_update;
168 ctx->digest = (digest_func) x86_hmac_sha224_digest;
169 ctx->setkey = (set_key_func) x86_hmac_sha224_set_key;
170 ctx->ctx_ptr = &ctx->ctx.sha224;
171 ctx->length = SHA224_DIGEST_SIZE;
173 case GNUTLS_MAC_SHA256:
174 ctx->update = (update_func) x86_hmac_sha256_update;
175 ctx->digest = (digest_func) x86_hmac_sha256_digest;
176 ctx->setkey = (set_key_func) x86_hmac_sha256_set_key;
177 ctx->ctx_ptr = &ctx->ctx.sha256;
178 ctx->length = SHA256_DIGEST_SIZE;
181 case GNUTLS_MAC_SHA384:
182 ctx->update = (update_func) x86_hmac_sha512_update;
183 ctx->digest = (digest_func) x86_hmac_sha384_digest;
184 ctx->setkey = (set_key_func) x86_hmac_sha384_set_key;
185 ctx->ctx_ptr = &ctx->ctx.sha384;
186 ctx->length = SHA384_DIGEST_SIZE;
188 case GNUTLS_MAC_SHA512:
189 ctx->update = (update_func) x86_hmac_sha512_update;
190 ctx->digest = (digest_func) x86_hmac_sha512_digest;
191 ctx->setkey = (set_key_func) x86_hmac_sha512_set_key;
192 ctx->ctx_ptr = &ctx->ctx.sha512;
193 ctx->length = SHA512_DIGEST_SIZE;
198 return GNUTLS_E_INVALID_REQUEST;
205 static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
207 struct x86_hmac_ctx *ctx;
210 ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx));
213 return GNUTLS_E_MEMORY_ERROR;
218 ret = _hmac_ctx_init(algo, ctx);
220 return gnutls_assert_val(ret);
228 wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen)
230 struct x86_hmac_ctx *ctx = _ctx;
232 ctx->setkey(ctx->ctx_ptr, keylen, key);
234 return GNUTLS_E_SUCCESS;
238 wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize)
240 struct x86_hmac_ctx *ctx = _ctx;
242 ctx->update(ctx->ctx_ptr, textsize, text);
244 return GNUTLS_E_SUCCESS;
248 wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize)
250 struct x86_hmac_ctx *ctx;
253 if (digestsize < ctx->length) {
255 return GNUTLS_E_SHORT_MEMORY_BUFFER;
258 ctx->digest(ctx->ctx_ptr, digestsize, digest);
263 static void wrap_x86_hmac_deinit(void *hd)
268 static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo,
269 const void *nonce, size_t nonce_size,
270 const void *key, size_t key_size,
271 const void *text, size_t text_size,
274 struct x86_hmac_ctx ctx;
277 ret = _hmac_ctx_init(algo, &ctx);
279 return gnutls_assert_val(ret);
281 ctx.setkey(&ctx, key_size, key);
282 ctx.update(&ctx, text_size, text);
283 ctx.digest(&ctx, ctx.length, digest);
285 zeroize_temp_key(&ctx, sizeof(ctx));
290 const gnutls_crypto_mac_st hmac_sha_x86_struct = {
291 .init = wrap_x86_hmac_init,
292 .setkey = wrap_x86_hmac_setkey,
294 .hash = wrap_x86_hmac_update,
295 .output = wrap_x86_hmac_output,
296 .deinit = wrap_x86_hmac_deinit,
297 .fast = wrap_x86_hmac_fast,
300 #endif /* HAVE_LIBNETTLE */