/src/gnutls/lib/accelerated/x86/sha-x86-ssse3.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011-2012 Free Software Foundation, Inc. |
3 | | * |
4 | | * Author: Nikos Mavrogiannopoulos |
5 | | * |
6 | | * This file is part of GnuTLS. |
7 | | * |
8 | | * The GnuTLS 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 | | #include "errors.h" |
24 | | #include "gnutls_int.h" |
25 | | #include <gnutls/crypto.h> |
26 | | #include "errors.h" |
27 | | #include <aes-x86.h> |
28 | | #include <nettle/sha.h> |
29 | | #include <nettle/macros.h> |
30 | | #include <nettle/nettle-meta.h> |
31 | | #include <sha-x86.h> |
32 | | #include <x86-common.h> |
33 | | |
34 | | void sha1_block_data_order(void *c, const void *p, size_t len); |
35 | | void sha256_block_data_order(void *c, const void *p, size_t len); |
36 | | void sha512_block_data_order(void *c, const void *p, size_t len); |
37 | | |
38 | | typedef void (*update_func)(void *, size_t, const uint8_t *); |
39 | | typedef void (*digest_func)(void *, size_t, uint8_t *); |
40 | | typedef void (*set_key_func)(void *, size_t, const uint8_t *); |
41 | | typedef void (*init_func)(void *); |
42 | | |
43 | | struct x86_hash_ctx { |
44 | | union { |
45 | | struct sha1_ctx sha1; |
46 | | struct sha224_ctx sha224; |
47 | | struct sha256_ctx sha256; |
48 | | struct sha384_ctx sha384; |
49 | | struct sha512_ctx sha512; |
50 | | } ctx; |
51 | | void *ctx_ptr; |
52 | | gnutls_digest_algorithm_t algo; |
53 | | size_t length; |
54 | | update_func update; |
55 | | digest_func digest; |
56 | | init_func init; |
57 | | }; |
58 | | |
59 | | static int wrap_x86_hash_update(void *_ctx, const void *text, size_t textsize) |
60 | 0 | { |
61 | 0 | struct x86_hash_ctx *ctx = _ctx; |
62 | |
|
63 | 0 | ctx->update(ctx->ctx_ptr, textsize, text); |
64 | |
|
65 | 0 | return GNUTLS_E_SUCCESS; |
66 | 0 | } |
67 | | |
68 | | static void wrap_x86_hash_deinit(void *hd) |
69 | 0 | { |
70 | 0 | gnutls_free(hd); |
71 | 0 | } |
72 | | |
73 | | void x86_sha1_update(struct sha1_ctx *ctx, size_t length, const uint8_t * data) |
74 | 0 | { |
75 | 0 | struct { |
76 | 0 | uint32_t h0, h1, h2, h3, h4; |
77 | 0 | uint32_t Nl, Nh; |
78 | 0 | uint32_t data[16]; |
79 | 0 | unsigned int num; |
80 | 0 | } octx; |
81 | 0 | size_t res; |
82 | 0 | unsigned t2, i; |
83 | |
|
84 | 0 | if ((res = ctx->index)) { |
85 | 0 | res = SHA1_DATA_SIZE - res; |
86 | 0 | if (length < res) |
87 | 0 | res = length; |
88 | 0 | sha1_update(ctx, res, data); |
89 | 0 | data += res; |
90 | 0 | length -= res; |
91 | 0 | } |
92 | |
|
93 | 0 | octx.h0 = ctx->state[0]; |
94 | 0 | octx.h1 = ctx->state[1]; |
95 | 0 | octx.h2 = ctx->state[2]; |
96 | 0 | octx.h3 = ctx->state[3]; |
97 | 0 | octx.h4 = ctx->state[4]; |
98 | |
|
99 | 0 | memcpy(octx.data, ctx->block, SHA1_DATA_SIZE); |
100 | 0 | octx.num = ctx->index; |
101 | |
|
102 | 0 | res = length % SHA1_DATA_SIZE; |
103 | 0 | length -= res; |
104 | |
|
105 | 0 | if (length > 0) { |
106 | |
|
107 | 0 | t2 = length / SHA1_DATA_SIZE; |
108 | |
|
109 | 0 | sha1_block_data_order(&octx, data, t2); |
110 | |
|
111 | 0 | for (i = 0; i < t2; i++) |
112 | 0 | ctx->count++; |
113 | 0 | data += length; |
114 | 0 | } |
115 | |
|
116 | 0 | ctx->state[0] = octx.h0; |
117 | 0 | ctx->state[1] = octx.h1; |
118 | 0 | ctx->state[2] = octx.h2; |
119 | 0 | ctx->state[3] = octx.h3; |
120 | 0 | ctx->state[4] = octx.h4; |
121 | |
|
122 | 0 | memcpy(ctx->block, octx.data, octx.num); |
123 | 0 | ctx->index = octx.num; |
124 | |
|
125 | 0 | if (res > 0) { |
126 | 0 | sha1_update(ctx, res, data); |
127 | 0 | } |
128 | |
|
129 | 0 | } |
130 | | |
131 | | void x86_sha256_update(struct sha256_ctx *ctx, size_t length, |
132 | | const uint8_t * data) |
133 | 0 | { |
134 | 0 | struct { |
135 | 0 | uint32_t h[8]; |
136 | 0 | uint32_t Nl, Nh; |
137 | 0 | uint32_t data[16]; |
138 | 0 | unsigned int num; |
139 | 0 | unsigned md_len; |
140 | 0 | } octx; |
141 | 0 | size_t res; |
142 | 0 | unsigned t2, i; |
143 | |
|
144 | 0 | if ((res = ctx->index)) { |
145 | 0 | res = SHA256_DATA_SIZE - res; |
146 | 0 | if (length < res) |
147 | 0 | res = length; |
148 | 0 | sha256_update(ctx, res, data); |
149 | 0 | data += res; |
150 | 0 | length -= res; |
151 | 0 | } |
152 | |
|
153 | 0 | memcpy(octx.h, ctx->state, sizeof(octx.h)); |
154 | 0 | memcpy(octx.data, ctx->block, SHA256_DATA_SIZE); |
155 | 0 | octx.num = ctx->index; |
156 | |
|
157 | 0 | res = length % SHA256_DATA_SIZE; |
158 | 0 | length -= res; |
159 | |
|
160 | 0 | if (length > 0) { |
161 | 0 | t2 = length / SHA1_DATA_SIZE; |
162 | 0 | sha256_block_data_order(&octx, data, t2); |
163 | |
|
164 | 0 | for (i = 0; i < t2; i++) |
165 | 0 | ctx->count++; |
166 | 0 | data += length; |
167 | 0 | } |
168 | |
|
169 | 0 | memcpy(ctx->state, octx.h, sizeof(octx.h)); |
170 | |
|
171 | 0 | memcpy(ctx->block, octx.data, octx.num); |
172 | 0 | ctx->index = octx.num; |
173 | |
|
174 | 0 | if (res > 0) { |
175 | 0 | sha256_update(ctx, res, data); |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | | void x86_sha512_update(struct sha512_ctx *ctx, size_t length, |
180 | | const uint8_t * data) |
181 | 0 | { |
182 | 0 | struct { |
183 | 0 | uint64_t h[8]; |
184 | 0 | uint64_t Nl, Nh; |
185 | 0 | union { |
186 | 0 | uint64_t d[16]; |
187 | 0 | uint8_t p[16 * 8]; |
188 | 0 | } u; |
189 | 0 | unsigned int num; |
190 | 0 | unsigned md_len; |
191 | 0 | } octx; |
192 | 0 | size_t res; |
193 | 0 | unsigned t2, i; |
194 | |
|
195 | 0 | if ((res = ctx->index)) { |
196 | 0 | res = SHA512_DATA_SIZE - res; |
197 | 0 | if (length < res) |
198 | 0 | res = length; |
199 | 0 | sha512_update(ctx, res, data); |
200 | 0 | data += res; |
201 | 0 | length -= res; |
202 | 0 | } |
203 | |
|
204 | 0 | memcpy(octx.h, ctx->state, sizeof(octx.h)); |
205 | 0 | memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE); |
206 | 0 | octx.num = ctx->index; |
207 | |
|
208 | 0 | res = length % SHA512_DATA_SIZE; |
209 | 0 | length -= res; |
210 | |
|
211 | 0 | if (length > 0) { |
212 | 0 | t2 = length / SHA512_DATA_SIZE; |
213 | 0 | sha512_block_data_order(&octx, data, t2); |
214 | |
|
215 | 0 | for (i = 0; i < t2; i++) |
216 | 0 | MD_INCR(ctx); |
217 | 0 | data += length; |
218 | 0 | } |
219 | |
|
220 | 0 | memcpy(ctx->state, octx.h, sizeof(octx.h)); |
221 | |
|
222 | 0 | memcpy(ctx->block, octx.u.p, octx.num); |
223 | 0 | ctx->index = octx.num; |
224 | |
|
225 | 0 | if (res > 0) { |
226 | 0 | sha512_update(ctx, res, data); |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | | static int _ctx_init(gnutls_digest_algorithm_t algo, struct x86_hash_ctx *ctx) |
231 | 0 | { |
232 | 0 | switch (algo) { |
233 | 0 | case GNUTLS_DIG_SHA1: |
234 | 0 | sha1_init(&ctx->ctx.sha1); |
235 | 0 | ctx->update = (update_func) x86_sha1_update; |
236 | 0 | ctx->digest = (digest_func) sha1_digest; |
237 | 0 | ctx->init = (init_func) sha1_init; |
238 | 0 | ctx->ctx_ptr = &ctx->ctx.sha1; |
239 | 0 | ctx->length = SHA1_DIGEST_SIZE; |
240 | 0 | break; |
241 | 0 | case GNUTLS_DIG_SHA224: |
242 | 0 | sha224_init(&ctx->ctx.sha224); |
243 | 0 | ctx->update = (update_func) x86_sha256_update; |
244 | 0 | ctx->digest = (digest_func) sha224_digest; |
245 | 0 | ctx->init = (init_func) sha224_init; |
246 | 0 | ctx->ctx_ptr = &ctx->ctx.sha224; |
247 | 0 | ctx->length = SHA224_DIGEST_SIZE; |
248 | 0 | break; |
249 | 0 | case GNUTLS_DIG_SHA256: |
250 | 0 | sha256_init(&ctx->ctx.sha256); |
251 | 0 | ctx->update = (update_func) x86_sha256_update; |
252 | 0 | ctx->digest = (digest_func) sha256_digest; |
253 | 0 | ctx->init = (init_func) sha256_init; |
254 | 0 | ctx->ctx_ptr = &ctx->ctx.sha256; |
255 | 0 | ctx->length = SHA256_DIGEST_SIZE; |
256 | 0 | break; |
257 | 0 | case GNUTLS_DIG_SHA384: |
258 | 0 | sha384_init(&ctx->ctx.sha384); |
259 | 0 | ctx->update = (update_func) x86_sha512_update; |
260 | 0 | ctx->digest = (digest_func) sha384_digest; |
261 | 0 | ctx->init = (init_func) sha384_init; |
262 | 0 | ctx->ctx_ptr = &ctx->ctx.sha384; |
263 | 0 | ctx->length = SHA384_DIGEST_SIZE; |
264 | 0 | break; |
265 | 0 | case GNUTLS_DIG_SHA512: |
266 | 0 | sha512_init(&ctx->ctx.sha512); |
267 | 0 | ctx->update = (update_func) x86_sha512_update; |
268 | 0 | ctx->digest = (digest_func) sha512_digest; |
269 | 0 | ctx->init = (init_func) sha512_init; |
270 | 0 | ctx->ctx_ptr = &ctx->ctx.sha512; |
271 | 0 | ctx->length = SHA512_DIGEST_SIZE; |
272 | 0 | break; |
273 | 0 | default: |
274 | 0 | gnutls_assert(); |
275 | 0 | return GNUTLS_E_INVALID_REQUEST; |
276 | 0 | } |
277 | | |
278 | 0 | return 0; |
279 | 0 | } |
280 | | |
281 | | static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx) |
282 | 0 | { |
283 | 0 | struct x86_hash_ctx *ctx; |
284 | 0 | int ret; |
285 | |
|
286 | 0 | ctx = gnutls_malloc(sizeof(struct x86_hash_ctx)); |
287 | 0 | if (ctx == NULL) { |
288 | 0 | gnutls_assert(); |
289 | 0 | return GNUTLS_E_MEMORY_ERROR; |
290 | 0 | } |
291 | | |
292 | 0 | ctx->algo = algo; |
293 | |
|
294 | 0 | if ((ret = _ctx_init(algo, ctx)) < 0) { |
295 | 0 | gnutls_assert(); |
296 | 0 | return ret; |
297 | 0 | } |
298 | | |
299 | 0 | *_ctx = ctx; |
300 | |
|
301 | 0 | return 0; |
302 | 0 | } |
303 | | |
304 | | static void *wrap_x86_hash_copy(const void *_ctx) |
305 | 0 | { |
306 | 0 | struct x86_hash_ctx *new_ctx; |
307 | 0 | const struct x86_hash_ctx *ctx = _ctx; |
308 | 0 | ptrdiff_t off = (uint8_t *) ctx->ctx_ptr - (uint8_t *) (&ctx->ctx); |
309 | |
|
310 | 0 | new_ctx = gnutls_malloc(sizeof(struct x86_hash_ctx)); |
311 | 0 | if (new_ctx == NULL) { |
312 | 0 | gnutls_assert(); |
313 | 0 | return NULL; |
314 | 0 | } |
315 | | |
316 | 0 | memcpy(new_ctx, ctx, sizeof(*new_ctx)); |
317 | 0 | new_ctx->ctx_ptr = (uint8_t *) & new_ctx->ctx + off; |
318 | |
|
319 | 0 | return new_ctx; |
320 | 0 | } |
321 | | |
322 | | static int wrap_x86_hash_output(void *src_ctx, void *digest, size_t digestsize) |
323 | 0 | { |
324 | 0 | struct x86_hash_ctx *ctx; |
325 | 0 | ctx = src_ctx; |
326 | |
|
327 | 0 | if (digestsize < ctx->length) |
328 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
329 | | |
330 | 0 | ctx->digest(ctx->ctx_ptr, digestsize, digest); |
331 | |
|
332 | 0 | return 0; |
333 | 0 | } |
334 | | |
335 | | static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo, |
336 | | const void *text, size_t text_size, void *digest) |
337 | 0 | { |
338 | 0 | struct x86_hash_ctx ctx; |
339 | 0 | int ret; |
340 | |
|
341 | 0 | ret = _ctx_init(algo, &ctx); |
342 | 0 | if (ret < 0) |
343 | 0 | return gnutls_assert_val(ret); |
344 | | |
345 | 0 | ctx.update(&ctx, text_size, text); |
346 | 0 | ctx.digest(&ctx, ctx.length, digest); |
347 | |
|
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | | const struct nettle_hash x86_sha1 = |
352 | | NN_HASH(sha1, x86_sha1_update, sha1_digest, SHA1); |
353 | | const struct nettle_hash x86_sha224 = |
354 | | NN_HASH(sha224, x86_sha256_update, sha224_digest, SHA224); |
355 | | const struct nettle_hash x86_sha256 = |
356 | | NN_HASH(sha256, x86_sha256_update, sha256_digest, SHA256); |
357 | | |
358 | | const struct nettle_hash x86_sha384 = |
359 | | NN_HASH(sha384, x86_sha512_update, sha384_digest, SHA384); |
360 | | const struct nettle_hash x86_sha512 = |
361 | | NN_HASH(sha512, x86_sha512_update, sha512_digest, SHA512); |
362 | | |
363 | | const gnutls_crypto_digest_st _gnutls_sha_x86_ssse3 = { |
364 | | .init = wrap_x86_hash_init, |
365 | | .hash = wrap_x86_hash_update, |
366 | | .output = wrap_x86_hash_output, |
367 | | .copy = wrap_x86_hash_copy, |
368 | | .deinit = wrap_x86_hash_deinit, |
369 | | .fast = wrap_x86_hash_fast, |
370 | | }; |