Coverage Report

Created: 2025-03-18 06:55

/src/gnutls/lib/accelerated/x86/sha-padlock.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
 * Portions Copyright (C) 2001 Niels Moeller
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GNUTLS.
8
 *
9
 * The GNUTLS library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
21
 *
22
 */
23
24
#include "gnutls_int.h"
25
#include "hash_int.h"
26
#include "errors.h"
27
#include <nettle/sha.h>
28
#include <nettle/hmac.h>
29
#include <nettle/macros.h>
30
#include "aes-padlock.h"
31
#include <assert.h>
32
#include "sha-padlock.h"
33
#include "x86-common.h"
34
35
#ifdef HAVE_LIBNETTLE
36
37
typedef void (*update_func)(void *, size_t, const uint8_t *);
38
typedef void (*digest_func)(void *, size_t, uint8_t *);
39
typedef void (*set_key_func)(void *, size_t, const uint8_t *);
40
typedef void (*init_func)(void *);
41
42
struct padlock_hash_ctx {
43
  union {
44
    struct sha1_ctx sha1;
45
    struct sha224_ctx sha224;
46
    struct sha256_ctx sha256;
47
    struct sha384_ctx sha384;
48
    struct sha512_ctx sha512;
49
  } ctx;
50
  void *ctx_ptr;
51
  gnutls_digest_algorithm_t algo;
52
  size_t length;
53
  update_func update;
54
  digest_func digest;
55
  init_func init;
56
};
57
58
static int wrap_padlock_hash_update(void *_ctx, const void *text,
59
            size_t textsize)
60
0
{
61
0
  struct padlock_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_padlock_hash_deinit(void *hd)
69
0
{
70
0
  gnutls_free(hd);
71
0
}
72
73
#define MD1_INCR(c) (c->count++)
74
#define SHA1_COMPRESS(ctx, data) \
75
0
  (padlock_sha1_blocks((void *)(ctx)->state, data, 1))
76
#define SHA256_COMPRESS(ctx, data) \
77
0
  (padlock_sha256_blocks((void *)(ctx)->state, data, 1))
78
#define SHA512_COMPRESS(ctx, data) \
79
0
  (padlock_sha512_blocks((void *)(ctx)->state, data, 1))
80
81
void padlock_sha1_update(struct sha1_ctx *ctx, size_t length,
82
       const uint8_t *data)
83
0
{
84
0
  MD_UPDATE(ctx, length, data, SHA1_COMPRESS, MD1_INCR(ctx));
85
0
}
86
87
void padlock_sha256_update(struct sha256_ctx *ctx, size_t length,
88
         const uint8_t *data)
89
0
{
90
0
  MD_UPDATE(ctx, length, data, SHA256_COMPRESS, MD1_INCR(ctx));
91
0
}
92
93
void padlock_sha512_update(struct sha512_ctx *ctx, size_t length,
94
         const uint8_t *data)
95
0
{
96
0
  MD_UPDATE(ctx, length, data, SHA512_COMPRESS, MD_INCR(ctx));
97
0
}
98
99
static void _nettle_write_be32(unsigned length, uint8_t *dst, uint32_t *src)
100
0
{
101
0
  unsigned i;
102
0
  unsigned words;
103
0
  unsigned leftover;
104
105
0
  words = length / 4;
106
0
  leftover = length % 4;
107
108
0
  for (i = 0; i < words; i++, dst += 4)
109
0
    WRITE_UINT32(dst, src[i]);
110
111
0
  if (leftover) {
112
0
    uint32_t word;
113
0
    unsigned j = leftover;
114
115
0
    word = src[i];
116
117
0
    switch (leftover) {
118
0
    default:
119
0
      abort();
120
0
    case 3:
121
0
      dst[--j] = (word >> 8) & 0xff;
122
0
      FALLTHROUGH;
123
0
    case 2:
124
0
      dst[--j] = (word >> 16) & 0xff;
125
0
      FALLTHROUGH;
126
0
    case 1:
127
0
      dst[--j] = (word >> 24) & 0xff;
128
0
    }
129
0
  }
130
0
}
131
132
static void padlock_sha1_digest(struct sha1_ctx *ctx, size_t length,
133
        uint8_t *digest)
134
0
{
135
0
  uint64_t bit_count;
136
137
0
  assert(length <= SHA1_DIGEST_SIZE);
138
139
0
  MD_PAD(ctx, 8, SHA1_COMPRESS);
140
141
  /* There are 512 = 2^9 bits in one block */
142
0
  bit_count = (ctx->count << 9) | (ctx->index << 3);
143
144
  /* append the 64 bit count */
145
0
  WRITE_UINT64(ctx->block + (SHA1_BLOCK_SIZE - 8), bit_count);
146
0
  SHA1_COMPRESS(ctx, ctx->block);
147
148
0
  _nettle_write_be32(length, digest, ctx->state);
149
0
}
150
151
static void padlock_sha256_digest(struct sha256_ctx *ctx, size_t length,
152
          uint8_t *digest)
153
0
{
154
0
  uint64_t bit_count;
155
156
0
  assert(length <= SHA256_DIGEST_SIZE);
157
158
0
  MD_PAD(ctx, 8, SHA256_COMPRESS);
159
160
  /* There are 512 = 2^9 bits in one block */
161
0
  bit_count = (ctx->count << 9) | (ctx->index << 3);
162
163
  /* This is slightly inefficient, as the numbers are converted to
164
     big-endian format, and will be converted back by the compression
165
     function. It's probably not worth the effort to fix this. */
166
0
  WRITE_UINT64(ctx->block + (SHA256_BLOCK_SIZE - 8), bit_count);
167
0
  SHA256_COMPRESS(ctx, ctx->block);
168
169
0
  _nettle_write_be32(length, digest, ctx->state);
170
0
}
171
172
static void padlock_sha512_digest(struct sha512_ctx *ctx, size_t length,
173
          uint8_t *digest)
174
0
{
175
0
  uint64_t high, low;
176
177
0
  unsigned i;
178
0
  unsigned words;
179
0
  unsigned leftover;
180
181
0
  assert(length <= SHA512_DIGEST_SIZE);
182
183
0
  MD_PAD(ctx, 16, SHA512_COMPRESS);
184
185
  /* There are 1024 = 2^10 bits in one block */
186
0
  high = (ctx->count_high << 10) | (ctx->count_low >> 54);
187
0
  low = (ctx->count_low << 10) | (ctx->index << 3);
188
189
  /* This is slightly inefficient, as the numbers are converted to
190
     big-endian format, and will be converted back by the compression
191
     function. It's probably not worth the effort to fix this. */
192
0
  WRITE_UINT64(ctx->block + (SHA512_DATA_SIZE - 16), high);
193
0
  WRITE_UINT64(ctx->block + (SHA512_DATA_SIZE - 8), low);
194
0
  SHA512_COMPRESS(ctx, ctx->block);
195
196
0
  words = length / 8;
197
0
  leftover = length % 8;
198
199
0
  for (i = 0; i < words; i++, digest += 8)
200
0
    WRITE_UINT64(digest, ctx->state[i]);
201
202
0
  if (leftover) {
203
    /* Truncate to the right size */
204
0
    uint64_t word = ctx->state[i] >> (8 * (8 - leftover));
205
206
0
    do {
207
0
      digest[--leftover] = word & 0xff;
208
0
      word >>= 8;
209
0
    } while (leftover);
210
0
  }
211
0
}
212
213
static int _ctx_init(gnutls_digest_algorithm_t algo,
214
         struct padlock_hash_ctx *ctx)
215
0
{
216
0
  switch (algo) {
217
0
  case GNUTLS_DIG_SHA1:
218
0
    sha1_init(&ctx->ctx.sha1);
219
0
    ctx->update = (update_func)padlock_sha1_update;
220
0
    ctx->digest = (digest_func)padlock_sha1_digest;
221
0
    ctx->init = (init_func)sha1_init;
222
0
    ctx->ctx_ptr = &ctx->ctx.sha1;
223
0
    ctx->length = SHA1_DIGEST_SIZE;
224
0
    break;
225
0
  case GNUTLS_DIG_SHA224:
226
0
    sha224_init(&ctx->ctx.sha224);
227
0
    ctx->update = (update_func)padlock_sha256_update;
228
0
    ctx->digest = (digest_func)padlock_sha256_digest;
229
0
    ctx->init = (init_func)sha224_init;
230
0
    ctx->ctx_ptr = &ctx->ctx.sha224;
231
0
    ctx->length = SHA224_DIGEST_SIZE;
232
0
    break;
233
0
  case GNUTLS_DIG_SHA256:
234
0
    sha256_init(&ctx->ctx.sha256);
235
0
    ctx->update = (update_func)padlock_sha256_update;
236
0
    ctx->digest = (digest_func)padlock_sha256_digest;
237
0
    ctx->init = (init_func)sha256_init;
238
0
    ctx->ctx_ptr = &ctx->ctx.sha256;
239
0
    ctx->length = SHA256_DIGEST_SIZE;
240
0
    break;
241
0
  case GNUTLS_DIG_SHA384:
242
0
    sha384_init(&ctx->ctx.sha384);
243
0
    ctx->update = (update_func)padlock_sha512_update;
244
0
    ctx->digest = (digest_func)padlock_sha512_digest;
245
0
    ctx->init = (init_func)sha384_init;
246
0
    ctx->ctx_ptr = &ctx->ctx.sha384;
247
0
    ctx->length = SHA384_DIGEST_SIZE;
248
0
    break;
249
0
  case GNUTLS_DIG_SHA512:
250
0
    sha512_init(&ctx->ctx.sha512);
251
0
    ctx->update = (update_func)padlock_sha512_update;
252
0
    ctx->digest = (digest_func)padlock_sha512_digest;
253
0
    ctx->init = (init_func)sha512_init;
254
0
    ctx->ctx_ptr = &ctx->ctx.sha512;
255
0
    ctx->length = SHA512_DIGEST_SIZE;
256
0
    break;
257
0
  default:
258
0
    gnutls_assert();
259
0
    return GNUTLS_E_INVALID_REQUEST;
260
0
  }
261
262
0
  return 0;
263
0
}
264
265
static int wrap_padlock_hash_init(gnutls_digest_algorithm_t algo, void **_ctx)
266
0
{
267
0
  struct padlock_hash_ctx *ctx;
268
0
  int ret;
269
270
0
  ctx = gnutls_malloc(sizeof(struct padlock_hash_ctx));
271
0
  if (ctx == NULL) {
272
0
    gnutls_assert();
273
0
    return GNUTLS_E_MEMORY_ERROR;
274
0
  }
275
276
0
  ctx->algo = algo;
277
278
0
  if ((ret = _ctx_init(algo, ctx)) < 0) {
279
0
    gnutls_assert();
280
0
    return ret;
281
0
  }
282
283
0
  *_ctx = ctx;
284
285
0
  return 0;
286
0
}
287
288
static void *wrap_padlock_hash_copy(const void *_ctx)
289
0
{
290
0
  struct padlock_hash_ctx *new_ctx;
291
0
  const struct padlock_hash_ctx *ctx = _ctx;
292
0
  ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx);
293
294
0
  new_ctx = gnutls_malloc(sizeof(struct padlock_hash_ctx));
295
0
  if (new_ctx == NULL) {
296
0
    gnutls_assert();
297
0
    return NULL;
298
0
  }
299
300
0
  memcpy(new_ctx, ctx, sizeof(*new_ctx));
301
0
  new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off;
302
303
0
  return new_ctx;
304
0
}
305
306
static int wrap_padlock_hash_output(void *src_ctx, void *digest,
307
            size_t digestsize)
308
0
{
309
0
  struct padlock_hash_ctx *ctx;
310
0
  ctx = src_ctx;
311
312
0
  if (digestsize < ctx->length)
313
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
314
315
0
  ctx->digest(ctx->ctx_ptr, digestsize, digest);
316
317
0
  ctx->init(ctx->ctx_ptr);
318
319
0
  return 0;
320
0
}
321
322
int wrap_padlock_hash_fast(gnutls_digest_algorithm_t algo, const void *text,
323
         size_t text_size, void *digest)
324
0
{
325
0
  if (text_size == 0 && text == NULL)
326
0
    text = digest;
327
0
  if (algo == GNUTLS_DIG_SHA1) {
328
0
    uint32_t iv[5] = {
329
0
      0x67452301UL, 0xEFCDAB89UL, 0x98BADCFEUL,
330
0
      0x10325476UL, 0xC3D2E1F0UL,
331
0
    };
332
0
    padlock_sha1_oneshot(iv, text, text_size);
333
0
    _nettle_write_be32(20, digest, iv);
334
0
  } else if (algo == GNUTLS_DIG_SHA256) {
335
0
    uint32_t iv[8] = {
336
0
      0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
337
0
      0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL,
338
0
    };
339
0
    padlock_sha256_oneshot(iv, text, text_size);
340
0
    _nettle_write_be32(32, digest, iv);
341
0
  } else {
342
0
    struct padlock_hash_ctx ctx;
343
0
    int ret;
344
345
0
    ret = _ctx_init(algo, &ctx);
346
0
    if (ret < 0)
347
0
      return gnutls_assert_val(ret);
348
0
    ctx.algo = algo;
349
350
0
    wrap_padlock_hash_update(&ctx, text, text_size);
351
352
0
    wrap_padlock_hash_output(&ctx, digest, ctx.length);
353
0
  }
354
355
0
  return 0;
356
0
}
357
358
const struct nettle_hash padlock_sha1 =
359
  NN_HASH(sha1, padlock_sha1_update, padlock_sha1_digest, SHA1);
360
const struct nettle_hash padlock_sha224 =
361
  NN_HASH(sha224, padlock_sha256_update, padlock_sha256_digest, SHA224);
362
const struct nettle_hash padlock_sha256 =
363
  NN_HASH(sha256, padlock_sha256_update, padlock_sha256_digest, SHA256);
364
const struct nettle_hash padlock_sha384 =
365
  NN_HASH(sha384, padlock_sha512_update, padlock_sha512_digest, SHA384);
366
const struct nettle_hash padlock_sha512 =
367
  NN_HASH(sha512, padlock_sha512_update, padlock_sha512_digest, SHA512);
368
369
const gnutls_crypto_digest_st _gnutls_sha_padlock_oneshot = {
370
  .init = NULL,
371
  .hash = NULL,
372
  .output = NULL,
373
  .deinit = NULL,
374
  .fast = wrap_padlock_hash_fast
375
};
376
377
const gnutls_crypto_digest_st _gnutls_sha_padlock = {
378
  .init = wrap_padlock_hash_init,
379
  .hash = wrap_padlock_hash_update,
380
  .output = wrap_padlock_hash_output,
381
  .copy = wrap_padlock_hash_copy,
382
  .deinit = wrap_padlock_hash_deinit,
383
  .fast = wrap_padlock_hash_fast,
384
};
385
386
#endif /* HAVE_LIBNETTLE */