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