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