/src/gnutls/lib/accelerated/x86/hmac-x86-ssse3.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc. |
3 | | * |
4 | | * Author: Nikos Mavrogiannopoulos |
5 | | * |
6 | | * This file is part of GNUTLS. |
7 | | * |
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. |
12 | | * |
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. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
20 | | * |
21 | | */ |
22 | | |
23 | | /* This file provides the backend hash/mac implementation for |
24 | | * VIA Padlock hardware acceleration. |
25 | | */ |
26 | | |
27 | | #include "gnutls_int.h" |
28 | | #include "hash_int.h" |
29 | | #include "errors.h" |
30 | | #include <nettle/sha1.h> |
31 | | #include <nettle/sha2.h> |
32 | | #include <nettle/hmac.h> |
33 | | #include <nettle/macros.h> |
34 | | #include "aes-x86.h" |
35 | | #include "sha-x86.h" |
36 | | #include "algorithms.h" |
37 | | |
38 | | #if defined(HAVE_LIBNETTLE) && defined(HMAC_SET_KEY) |
39 | | |
40 | | typedef void (*update_func)(void *, size_t, const uint8_t *); |
41 | | typedef void (*digest_func)(void *, size_t, uint8_t *); |
42 | | typedef void (*set_key_func)(void *, size_t, const uint8_t *); |
43 | | |
44 | | struct x86_hmac_ctx { |
45 | | union { |
46 | | struct hmac_sha1_ctx sha1; |
47 | | struct hmac_sha224_ctx sha224; |
48 | | struct hmac_sha256_ctx sha256; |
49 | | struct hmac_sha384_ctx sha384; |
50 | | struct hmac_sha512_ctx sha512; |
51 | | } ctx; |
52 | | |
53 | | void *ctx_ptr; |
54 | | gnutls_mac_algorithm_t algo; |
55 | | size_t length; |
56 | | update_func update; |
57 | | digest_func digest; |
58 | | set_key_func setkey; |
59 | | }; |
60 | | |
61 | | static void x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx, size_t key_length, |
62 | | const uint8_t *key) |
63 | 0 | { |
64 | 0 | HMAC_SET_KEY(ctx, &x86_sha1, key_length, key); |
65 | 0 | } |
66 | | |
67 | | static void x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx, size_t length, |
68 | | const uint8_t *data) |
69 | 0 | { |
70 | 0 | x86_sha1_update(&ctx->state, length, data); |
71 | 0 | } |
72 | | |
73 | | static void x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx, size_t length, |
74 | | uint8_t *digest) |
75 | 0 | { |
76 | 0 | HMAC_DIGEST(ctx, &x86_sha1, length, digest); |
77 | 0 | } |
78 | | |
79 | | static void x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx, |
80 | | size_t key_length, const uint8_t *key) |
81 | 0 | { |
82 | 0 | HMAC_SET_KEY(ctx, &x86_sha256, key_length, key); |
83 | 0 | } |
84 | | |
85 | | static void x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx, size_t length, |
86 | | const uint8_t *data) |
87 | 0 | { |
88 | 0 | x86_sha256_update(&ctx->state, length, data); |
89 | 0 | } |
90 | | |
91 | | static void x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx, size_t length, |
92 | | uint8_t *digest) |
93 | 0 | { |
94 | 0 | HMAC_DIGEST(ctx, &x86_sha256, length, digest); |
95 | 0 | } |
96 | | |
97 | | static void x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx, |
98 | | size_t key_length, const uint8_t *key) |
99 | 0 | { |
100 | 0 | HMAC_SET_KEY(ctx, &x86_sha224, key_length, key); |
101 | 0 | } |
102 | | |
103 | | static void x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx, size_t length, |
104 | | uint8_t *digest) |
105 | 0 | { |
106 | 0 | HMAC_DIGEST(ctx, &x86_sha224, length, digest); |
107 | 0 | } |
108 | | |
109 | | static void x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx, |
110 | | size_t key_length, const uint8_t *key) |
111 | 0 | { |
112 | 0 | HMAC_SET_KEY(ctx, &x86_sha384, key_length, key); |
113 | 0 | } |
114 | | |
115 | | static void x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx, size_t length, |
116 | | uint8_t *digest) |
117 | 0 | { |
118 | 0 | HMAC_DIGEST(ctx, &x86_sha384, length, digest); |
119 | 0 | } |
120 | | |
121 | | static void x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx, |
122 | | size_t key_length, const uint8_t *key) |
123 | 0 | { |
124 | 0 | HMAC_SET_KEY(ctx, &x86_sha512, key_length, key); |
125 | 0 | } |
126 | | |
127 | | static void x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx, size_t length, |
128 | | const uint8_t *data) |
129 | 0 | { |
130 | 0 | x86_sha512_update(&ctx->state, length, data); |
131 | 0 | } |
132 | | |
133 | | static void x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx, size_t length, |
134 | | uint8_t *digest) |
135 | 0 | { |
136 | 0 | HMAC_DIGEST(ctx, &x86_sha512, length, digest); |
137 | 0 | } |
138 | | |
139 | | static int _hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx) |
140 | 0 | { |
141 | 0 | switch (algo) { |
142 | 0 | case GNUTLS_MAC_SHA1: |
143 | 0 | ctx->update = (update_func)x86_hmac_sha1_update; |
144 | 0 | ctx->digest = (digest_func)x86_hmac_sha1_digest; |
145 | 0 | ctx->setkey = (set_key_func)x86_hmac_sha1_set_key; |
146 | 0 | ctx->ctx_ptr = &ctx->ctx.sha1; |
147 | 0 | ctx->length = SHA1_DIGEST_SIZE; |
148 | 0 | break; |
149 | 0 | case GNUTLS_MAC_SHA224: |
150 | 0 | ctx->update = (update_func)x86_hmac_sha256_update; |
151 | 0 | ctx->digest = (digest_func)x86_hmac_sha224_digest; |
152 | 0 | ctx->setkey = (set_key_func)x86_hmac_sha224_set_key; |
153 | 0 | ctx->ctx_ptr = &ctx->ctx.sha224; |
154 | 0 | ctx->length = SHA224_DIGEST_SIZE; |
155 | 0 | break; |
156 | 0 | case GNUTLS_MAC_SHA256: |
157 | 0 | ctx->update = (update_func)x86_hmac_sha256_update; |
158 | 0 | ctx->digest = (digest_func)x86_hmac_sha256_digest; |
159 | 0 | ctx->setkey = (set_key_func)x86_hmac_sha256_set_key; |
160 | 0 | ctx->ctx_ptr = &ctx->ctx.sha256; |
161 | 0 | ctx->length = SHA256_DIGEST_SIZE; |
162 | 0 | break; |
163 | 0 | case GNUTLS_MAC_SHA384: |
164 | 0 | ctx->update = (update_func)x86_hmac_sha512_update; |
165 | 0 | ctx->digest = (digest_func)x86_hmac_sha384_digest; |
166 | 0 | ctx->setkey = (set_key_func)x86_hmac_sha384_set_key; |
167 | 0 | ctx->ctx_ptr = &ctx->ctx.sha384; |
168 | 0 | ctx->length = SHA384_DIGEST_SIZE; |
169 | 0 | break; |
170 | 0 | case GNUTLS_MAC_SHA512: |
171 | 0 | ctx->update = (update_func)x86_hmac_sha512_update; |
172 | 0 | ctx->digest = (digest_func)x86_hmac_sha512_digest; |
173 | 0 | ctx->setkey = (set_key_func)x86_hmac_sha512_set_key; |
174 | 0 | ctx->ctx_ptr = &ctx->ctx.sha512; |
175 | 0 | ctx->length = SHA512_DIGEST_SIZE; |
176 | 0 | break; |
177 | 0 | default: |
178 | 0 | gnutls_assert(); |
179 | 0 | return GNUTLS_E_INVALID_REQUEST; |
180 | 0 | } |
181 | | |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | | static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx) |
186 | 0 | { |
187 | 0 | struct x86_hmac_ctx *ctx; |
188 | 0 | int ret; |
189 | |
|
190 | 0 | ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx)); |
191 | 0 | if (ctx == NULL) { |
192 | 0 | gnutls_assert(); |
193 | 0 | return GNUTLS_E_MEMORY_ERROR; |
194 | 0 | } |
195 | | |
196 | 0 | ctx->algo = algo; |
197 | |
|
198 | 0 | ret = _hmac_ctx_init(algo, ctx); |
199 | 0 | if (ret < 0) { |
200 | 0 | gnutls_free(ctx); |
201 | 0 | return gnutls_assert_val(ret); |
202 | 0 | } |
203 | | |
204 | 0 | *_ctx = ctx; |
205 | |
|
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | | static void *wrap_x86_hmac_copy(const void *_ctx) |
210 | 0 | { |
211 | 0 | struct x86_hmac_ctx *new_ctx; |
212 | 0 | const struct x86_hmac_ctx *ctx = _ctx; |
213 | 0 | ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx); |
214 | |
|
215 | 0 | new_ctx = gnutls_malloc(sizeof(struct x86_hmac_ctx)); |
216 | 0 | if (new_ctx == NULL) { |
217 | 0 | gnutls_assert(); |
218 | 0 | return NULL; |
219 | 0 | } |
220 | | |
221 | 0 | memcpy(new_ctx, ctx, sizeof(*new_ctx)); |
222 | 0 | new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off; |
223 | |
|
224 | 0 | return new_ctx; |
225 | 0 | } |
226 | | |
227 | | static int wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen) |
228 | 0 | { |
229 | 0 | struct x86_hmac_ctx *ctx = _ctx; |
230 | |
|
231 | 0 | ctx->setkey(ctx->ctx_ptr, keylen, key); |
232 | |
|
233 | 0 | return GNUTLS_E_SUCCESS; |
234 | 0 | } |
235 | | |
236 | | static int wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize) |
237 | 0 | { |
238 | 0 | struct x86_hmac_ctx *ctx = _ctx; |
239 | |
|
240 | 0 | ctx->update(ctx->ctx_ptr, textsize, text); |
241 | |
|
242 | 0 | return GNUTLS_E_SUCCESS; |
243 | 0 | } |
244 | | |
245 | | static int wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize) |
246 | 0 | { |
247 | 0 | struct x86_hmac_ctx *ctx; |
248 | 0 | ctx = src_ctx; |
249 | |
|
250 | 0 | if (digestsize < ctx->length) { |
251 | 0 | gnutls_assert(); |
252 | 0 | return GNUTLS_E_SHORT_MEMORY_BUFFER; |
253 | 0 | } |
254 | | |
255 | 0 | ctx->digest(ctx->ctx_ptr, digestsize, digest); |
256 | |
|
257 | 0 | return 0; |
258 | 0 | } |
259 | | |
260 | | static void wrap_x86_hmac_deinit(void *hd) |
261 | 0 | { |
262 | 0 | struct x86_hmac_ctx *ctx = hd; |
263 | |
|
264 | 0 | zeroize_key(ctx, sizeof(*ctx)); |
265 | 0 | gnutls_free(ctx); |
266 | 0 | } |
267 | | |
268 | | static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo, const void *nonce, |
269 | | size_t nonce_size, const void *key, |
270 | | size_t key_size, const void *text, |
271 | | size_t text_size, void *digest) |
272 | 0 | { |
273 | 0 | struct x86_hmac_ctx ctx; |
274 | 0 | int ret; |
275 | |
|
276 | 0 | ret = _hmac_ctx_init(algo, &ctx); |
277 | 0 | if (ret < 0) |
278 | 0 | return gnutls_assert_val(ret); |
279 | | |
280 | 0 | ctx.setkey(&ctx, key_size, key); |
281 | 0 | ctx.update(&ctx, text_size, text); |
282 | 0 | ctx.digest(&ctx, ctx.length, digest); |
283 | |
|
284 | 0 | zeroize_key(&ctx, sizeof(ctx)); |
285 | |
|
286 | 0 | return 0; |
287 | 0 | } |
288 | | |
289 | | const gnutls_crypto_mac_st _gnutls_hmac_sha_x86_ssse3 = { |
290 | | .init = wrap_x86_hmac_init, |
291 | | .setkey = wrap_x86_hmac_setkey, |
292 | | .setnonce = NULL, |
293 | | .hash = wrap_x86_hmac_update, |
294 | | .output = wrap_x86_hmac_output, |
295 | | .copy = wrap_x86_hmac_copy, |
296 | | .deinit = wrap_x86_hmac_deinit, |
297 | | .fast = wrap_x86_hmac_fast, |
298 | | }; |
299 | | |
300 | | #endif /* HAVE_LIBNETTLE && HMAC_SET_KEY */ |