Coverage Report

Created: 2026-10-28 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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/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
1.18k
{
61
1.18k
  struct x86_hash_ctx *ctx = _ctx;
62
63
1.18k
  ctx->update(ctx->ctx_ptr, textsize, text);
64
65
1.18k
  return GNUTLS_E_SUCCESS;
66
1.18k
}
67
68
static void wrap_x86_hash_deinit(void *hd)
69
594
{
70
594
  gnutls_free(hd);
71
594
}
72
73
void x86_sha1_update(struct sha1_ctx *ctx, size_t length, const uint8_t *data)
74
1.11k
{
75
1.11k
  struct {
76
1.11k
    uint32_t h0, h1, h2, h3, h4;
77
1.11k
    uint32_t Nl, Nh;
78
1.11k
    uint32_t data[16];
79
1.11k
    unsigned int num;
80
1.11k
  } octx;
81
1.11k
  size_t res;
82
1.11k
  unsigned t2, i;
83
84
1.11k
  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
1.11k
  octx.h0 = ctx->state[0];
94
1.11k
  octx.h1 = ctx->state[1];
95
1.11k
  octx.h2 = ctx->state[2];
96
1.11k
  octx.h3 = ctx->state[3];
97
1.11k
  octx.h4 = ctx->state[4];
98
99
1.11k
  memcpy(octx.data, ctx->block, SHA1_DATA_SIZE);
100
1.11k
  octx.num = ctx->index;
101
102
1.11k
  res = length % SHA1_DATA_SIZE;
103
1.11k
  length -= res;
104
105
1.11k
  if (length > 0) {
106
717
    t2 = length / SHA1_DATA_SIZE;
107
108
717
    sha1_block_data_order(&octx, data, t2);
109
110
19.2k
    for (i = 0; i < t2; i++)
111
18.4k
      ctx->count++;
112
717
    data += length;
113
717
  }
114
115
1.11k
  ctx->state[0] = octx.h0;
116
1.11k
  ctx->state[1] = octx.h1;
117
1.11k
  ctx->state[2] = octx.h2;
118
1.11k
  ctx->state[3] = octx.h3;
119
1.11k
  ctx->state[4] = octx.h4;
120
121
1.11k
  memcpy(ctx->block, octx.data, octx.num);
122
1.11k
  ctx->index = octx.num;
123
124
1.11k
  if (res > 0) {
125
831
    sha1_update(ctx, res, data);
126
831
  }
127
1.11k
}
128
129
void x86_sha256_update(struct sha256_ctx *ctx, size_t length,
130
           const uint8_t *data)
131
3.39k
{
132
3.39k
  struct {
133
3.39k
    uint32_t h[8];
134
3.39k
    uint32_t Nl, Nh;
135
3.39k
    uint32_t data[16];
136
3.39k
    unsigned int num;
137
3.39k
    unsigned md_len;
138
3.39k
  } octx;
139
3.39k
  size_t res;
140
3.39k
  unsigned t2, i;
141
142
3.39k
  if ((res = ctx->index)) {
143
594
    res = SHA256_DATA_SIZE - res;
144
594
    if (length < res)
145
274
      res = length;
146
594
    sha256_update(ctx, res, data);
147
594
    data += res;
148
594
    length -= res;
149
594
  }
150
151
3.39k
  memcpy(octx.h, ctx->state, sizeof(octx.h));
152
3.39k
  memcpy(octx.data, ctx->block, SHA256_DATA_SIZE);
153
3.39k
  octx.num = ctx->index;
154
155
3.39k
  res = length % SHA256_DATA_SIZE;
156
3.39k
  length -= res;
157
158
3.39k
  if (length > 0) {
159
1.18k
    t2 = length / SHA1_DATA_SIZE;
160
1.18k
    sha256_block_data_order(&octx, data, t2);
161
162
20.0k
    for (i = 0; i < t2; i++)
163
18.8k
      ctx->count++;
164
1.18k
    data += length;
165
1.18k
  }
166
167
3.39k
  memcpy(ctx->state, octx.h, sizeof(octx.h));
168
169
3.39k
  memcpy(ctx->block, octx.data, octx.num);
170
3.39k
  ctx->index = octx.num;
171
172
3.39k
  if (res > 0) {
173
1.49k
    sha256_update(ctx, res, data);
174
1.49k
  }
175
3.39k
}
176
177
void x86_sha512_update(struct sha512_ctx *ctx, size_t length,
178
           const uint8_t *data)
179
2.98k
{
180
2.98k
  struct {
181
2.98k
    uint64_t h[8];
182
2.98k
    uint64_t Nl, Nh;
183
2.98k
    union {
184
2.98k
      uint64_t d[16];
185
2.98k
      uint8_t p[16 * 8];
186
2.98k
    } u;
187
2.98k
    unsigned int num;
188
2.98k
    unsigned md_len;
189
2.98k
  } octx;
190
2.98k
  size_t res;
191
2.98k
  unsigned t2, i;
192
193
2.98k
  if ((res = ctx->index)) {
194
0
    res = SHA512_DATA_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.98k
  memcpy(octx.h, ctx->state, sizeof(octx.h));
203
2.98k
  memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE);
204
2.98k
  octx.num = ctx->index;
205
206
2.98k
  res = length % SHA512_DATA_SIZE;
207
2.98k
  length -= res;
208
209
2.98k
  if (length > 0) {
210
1.81k
    t2 = length / SHA512_DATA_SIZE;
211
1.81k
    sha512_block_data_order(&octx, data, t2);
212
213
3.79k
    for (i = 0; i < t2; i++)
214
1.97k
      MD_INCR(ctx);
215
1.81k
    data += length;
216
1.81k
  }
217
218
2.98k
  memcpy(ctx->state, octx.h, sizeof(octx.h));
219
220
2.98k
  memcpy(ctx->block, octx.u.p, octx.num);
221
2.98k
  ctx->index = octx.num;
222
223
2.98k
  if (res > 0) {
224
1.48k
    sha512_update(ctx, res, data);
225
1.48k
  }
226
2.98k
}
227
228
static int _ctx_init(gnutls_digest_algorithm_t algo, struct x86_hash_ctx *ctx)
229
2.19k
{
230
2.19k
  switch (algo) {
231
594
  case GNUTLS_DIG_SHA1:
232
594
    sha1_init(&ctx->ctx.sha1);
233
594
    ctx->update = (update_func)x86_sha1_update;
234
594
    ctx->digest = (digest_func)sha1_digest;
235
594
    ctx->init = (init_func)sha1_init;
236
594
    ctx->ctx_ptr = &ctx->ctx.sha1;
237
594
    ctx->length = SHA1_DIGEST_SIZE;
238
594
    break;
239
0
  case GNUTLS_DIG_SHA224:
240
0
    sha224_init(&ctx->ctx.sha224);
241
0
    ctx->update = (update_func)x86_sha256_update;
242
0
    ctx->digest = (digest_func)sha224_digest;
243
0
    ctx->init = (init_func)sha224_init;
244
0
    ctx->ctx_ptr = &ctx->ctx.sha224;
245
0
    ctx->length = SHA224_DIGEST_SIZE;
246
0
    break;
247
1.60k
  case GNUTLS_DIG_SHA256:
248
1.60k
    sha256_init(&ctx->ctx.sha256);
249
1.60k
    ctx->update = (update_func)x86_sha256_update;
250
1.60k
    ctx->digest = (digest_func)sha256_digest;
251
1.60k
    ctx->init = (init_func)sha256_init;
252
1.60k
    ctx->ctx_ptr = &ctx->ctx.sha256;
253
1.60k
    ctx->length = SHA256_DIGEST_SIZE;
254
1.60k
    break;
255
0
  case GNUTLS_DIG_SHA384:
256
0
    sha384_init(&ctx->ctx.sha384);
257
0
    ctx->update = (update_func)x86_sha512_update;
258
0
    ctx->digest = (digest_func)sha384_digest;
259
0
    ctx->init = (init_func)sha384_init;
260
0
    ctx->ctx_ptr = &ctx->ctx.sha384;
261
0
    ctx->length = SHA384_DIGEST_SIZE;
262
0
    break;
263
0
  case GNUTLS_DIG_SHA512:
264
0
    sha512_init(&ctx->ctx.sha512);
265
0
    ctx->update = (update_func)x86_sha512_update;
266
0
    ctx->digest = (digest_func)sha512_digest;
267
0
    ctx->init = (init_func)sha512_init;
268
0
    ctx->ctx_ptr = &ctx->ctx.sha512;
269
0
    ctx->length = SHA512_DIGEST_SIZE;
270
0
    break;
271
0
  default:
272
0
    gnutls_assert();
273
0
    return GNUTLS_E_INVALID_REQUEST;
274
2.19k
  }
275
276
2.19k
  return 0;
277
2.19k
}
278
279
static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx)
280
594
{
281
594
  struct x86_hash_ctx *ctx;
282
594
  int ret;
283
284
594
  ctx = gnutls_malloc(sizeof(struct x86_hash_ctx));
285
594
  if (ctx == NULL) {
286
0
    gnutls_assert();
287
0
    return GNUTLS_E_MEMORY_ERROR;
288
0
  }
289
290
594
  ctx->algo = algo;
291
292
594
  if ((ret = _ctx_init(algo, ctx)) < 0) {
293
0
    gnutls_assert();
294
0
    return ret;
295
0
  }
296
297
594
  *_ctx = ctx;
298
299
594
  return 0;
300
594
}
301
302
static void *wrap_x86_hash_copy(const void *_ctx)
303
0
{
304
0
  struct x86_hash_ctx *new_ctx;
305
0
  const struct x86_hash_ctx *ctx = _ctx;
306
0
  ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx);
307
308
0
  new_ctx = gnutls_malloc(sizeof(struct x86_hash_ctx));
309
0
  if (new_ctx == NULL) {
310
0
    gnutls_assert();
311
0
    return NULL;
312
0
  }
313
314
0
  memcpy(new_ctx, ctx, sizeof(*new_ctx));
315
0
  new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off;
316
317
0
  return new_ctx;
318
0
}
319
320
static int wrap_x86_hash_output(void *src_ctx, void *digest, size_t digestsize)
321
594
{
322
594
  struct x86_hash_ctx *ctx;
323
594
  ctx = src_ctx;
324
325
594
  if (digestsize < ctx->length)
326
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
327
328
594
  ctx->digest(ctx->ctx_ptr, digestsize, digest);
329
330
594
  return 0;
331
594
}
332
333
static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo, const void *text,
334
            size_t text_size, void *digest)
335
1.60k
{
336
1.60k
  struct x86_hash_ctx ctx;
337
1.60k
  int ret;
338
339
1.60k
  ret = _ctx_init(algo, &ctx);
340
1.60k
  if (ret < 0)
341
0
    return gnutls_assert_val(ret);
342
343
1.60k
  ctx.update(&ctx, text_size, text);
344
1.60k
  ctx.digest(&ctx, ctx.length, digest);
345
346
1.60k
  return 0;
347
1.60k
}
348
349
const struct nettle_hash x86_sha1 =
350
  NN_HASH(sha1, x86_sha1_update, sha1_digest, SHA1);
351
const struct nettle_hash x86_sha224 =
352
  NN_HASH(sha224, x86_sha256_update, sha224_digest, SHA224);
353
const struct nettle_hash x86_sha256 =
354
  NN_HASH(sha256, x86_sha256_update, sha256_digest, SHA256);
355
356
const struct nettle_hash x86_sha384 =
357
  NN_HASH(sha384, x86_sha512_update, sha384_digest, SHA384);
358
const struct nettle_hash x86_sha512 =
359
  NN_HASH(sha512, x86_sha512_update, sha512_digest, SHA512);
360
361
const gnutls_crypto_digest_st _gnutls_sha_x86_ssse3 = {
362
  .init = wrap_x86_hash_init,
363
  .hash = wrap_x86_hash_update,
364
  .output = wrap_x86_hash_output,
365
  .copy = wrap_x86_hash_copy,
366
  .deinit = wrap_x86_hash_deinit,
367
  .fast = wrap_x86_hash_fast,
368
};