Coverage Report

Created: 2025-03-18 06:55

/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
0
    t2 = length / SHA1_DATA_SIZE;
107
108
0
    sha1_block_data_order(&octx, data, t2);
109
110
0
    for (i = 0; i < t2; i++)
111
0
      ctx->count++;
112
0
    data += length;
113
0
  }
114
115
0
  ctx->state[0] = octx.h0;
116
0
  ctx->state[1] = octx.h1;
117
0
  ctx->state[2] = octx.h2;
118
0
  ctx->state[3] = octx.h3;
119
0
  ctx->state[4] = octx.h4;
120
121
0
  memcpy(ctx->block, octx.data, octx.num);
122
0
  ctx->index = octx.num;
123
124
0
  if (res > 0) {
125
0
    sha1_update(ctx, res, data);
126
0
  }
127
0
}
128
129
void x86_sha256_update(struct sha256_ctx *ctx, size_t length,
130
           const uint8_t *data)
131
0
{
132
0
  struct {
133
0
    uint32_t h[8];
134
0
    uint32_t Nl, Nh;
135
0
    uint32_t data[16];
136
0
    unsigned int num;
137
0
    unsigned md_len;
138
0
  } octx;
139
0
  size_t res;
140
0
  unsigned t2, i;
141
142
0
  if ((res = ctx->index)) {
143
0
    res = SHA256_DATA_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
0
  memcpy(octx.h, ctx->state, sizeof(octx.h));
152
0
  memcpy(octx.data, ctx->block, SHA256_DATA_SIZE);
153
0
  octx.num = ctx->index;
154
155
0
  res = length % SHA256_DATA_SIZE;
156
0
  length -= res;
157
158
0
  if (length > 0) {
159
0
    t2 = length / SHA1_DATA_SIZE;
160
0
    sha256_block_data_order(&octx, data, t2);
161
162
0
    for (i = 0; i < t2; i++)
163
0
      ctx->count++;
164
0
    data += length;
165
0
  }
166
167
0
  memcpy(ctx->state, octx.h, sizeof(octx.h));
168
169
0
  memcpy(ctx->block, octx.data, octx.num);
170
0
  ctx->index = octx.num;
171
172
0
  if (res > 0) {
173
0
    sha256_update(ctx, res, data);
174
0
  }
175
0
}
176
177
void x86_sha512_update(struct sha512_ctx *ctx, size_t length,
178
           const uint8_t *data)
179
0
{
180
0
  struct {
181
0
    uint64_t h[8];
182
0
    uint64_t Nl, Nh;
183
0
    union {
184
0
      uint64_t d[16];
185
0
      uint8_t p[16 * 8];
186
0
    } u;
187
0
    unsigned int num;
188
0
    unsigned md_len;
189
0
  } octx;
190
0
  size_t res;
191
0
  unsigned t2, i;
192
193
0
  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
0
  memcpy(octx.h, ctx->state, sizeof(octx.h));
203
0
  memcpy(octx.u.p, ctx->block, SHA512_DATA_SIZE);
204
0
  octx.num = ctx->index;
205
206
0
  res = length % SHA512_DATA_SIZE;
207
0
  length -= res;
208
209
0
  if (length > 0) {
210
0
    t2 = length / SHA512_DATA_SIZE;
211
0
    sha512_block_data_order(&octx, data, t2);
212
213
0
    for (i = 0; i < t2; i++)
214
0
      MD_INCR(ctx);
215
0
    data += length;
216
0
  }
217
218
0
  memcpy(ctx->state, octx.h, sizeof(octx.h));
219
220
0
  memcpy(ctx->block, octx.u.p, octx.num);
221
0
  ctx->index = octx.num;
222
223
0
  if (res > 0) {
224
0
    sha512_update(ctx, res, data);
225
0
  }
226
0
}
227
228
static int _ctx_init(gnutls_digest_algorithm_t algo, struct x86_hash_ctx *ctx)
229
0
{
230
0
  switch (algo) {
231
0
  case GNUTLS_DIG_SHA1:
232
0
    sha1_init(&ctx->ctx.sha1);
233
0
    ctx->update = (update_func)x86_sha1_update;
234
0
    ctx->digest = (digest_func)sha1_digest;
235
0
    ctx->init = (init_func)sha1_init;
236
0
    ctx->ctx_ptr = &ctx->ctx.sha1;
237
0
    ctx->length = SHA1_DIGEST_SIZE;
238
0
    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
0
  case GNUTLS_DIG_SHA256:
248
0
    sha256_init(&ctx->ctx.sha256);
249
0
    ctx->update = (update_func)x86_sha256_update;
250
0
    ctx->digest = (digest_func)sha256_digest;
251
0
    ctx->init = (init_func)sha256_init;
252
0
    ctx->ctx_ptr = &ctx->ctx.sha256;
253
0
    ctx->length = SHA256_DIGEST_SIZE;
254
0
    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
0
  }
275
276
0
  return 0;
277
0
}
278
279
static int wrap_x86_hash_init(gnutls_digest_algorithm_t algo, void **_ctx)
280
0
{
281
0
  struct x86_hash_ctx *ctx;
282
0
  int ret;
283
284
0
  ctx = gnutls_malloc(sizeof(struct x86_hash_ctx));
285
0
  if (ctx == NULL) {
286
0
    gnutls_assert();
287
0
    return GNUTLS_E_MEMORY_ERROR;
288
0
  }
289
290
0
  ctx->algo = algo;
291
292
0
  if ((ret = _ctx_init(algo, ctx)) < 0) {
293
0
    gnutls_assert();
294
0
    return ret;
295
0
  }
296
297
0
  *_ctx = ctx;
298
299
0
  return 0;
300
0
}
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
0
{
322
0
  struct x86_hash_ctx *ctx;
323
0
  ctx = src_ctx;
324
325
0
  if (digestsize < ctx->length)
326
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
327
328
0
  ctx->digest(ctx->ctx_ptr, digestsize, digest);
329
330
0
  return 0;
331
0
}
332
333
static int wrap_x86_hash_fast(gnutls_digest_algorithm_t algo, const void *text,
334
            size_t text_size, void *digest)
335
0
{
336
0
  struct x86_hash_ctx ctx;
337
0
  int ret;
338
339
0
  ret = _ctx_init(algo, &ctx);
340
0
  if (ret < 0)
341
0
    return gnutls_assert_val(ret);
342
343
0
  ctx.update(&ctx, text_size, text);
344
0
  ctx.digest(&ctx, ctx.length, digest);
345
346
0
  return 0;
347
0
}
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
};