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/hmac-x86-ssse3.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2008, 2010-2012 Free Software Foundation, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GNUTLS.
7
 *
8
 * The GNUTLS library 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
/* This file provides the backend hash/mac implementation for
24
 * VIA Padlock hardware acceleration.
25
 */
26
27
#include "gnutls_int.h"
28
#include "hash_int.h"
29
#include "errors.h"
30
#include <nettle/sha.h>
31
#include <nettle/hmac.h>
32
#include <nettle/macros.h>
33
#include "aes-x86.h"
34
#include "sha-x86.h"
35
#include "algorithms.h"
36
37
#ifdef HAVE_LIBNETTLE
38
39
typedef void (*update_func)(void *, size_t, const uint8_t *);
40
typedef void (*digest_func)(void *, size_t, uint8_t *);
41
typedef void (*set_key_func)(void *, size_t, const uint8_t *);
42
43
struct x86_hmac_ctx {
44
  union {
45
    struct hmac_sha1_ctx sha1;
46
    struct hmac_sha224_ctx sha224;
47
    struct hmac_sha256_ctx sha256;
48
    struct hmac_sha384_ctx sha384;
49
    struct hmac_sha512_ctx sha512;
50
  } ctx;
51
52
  void *ctx_ptr;
53
  gnutls_mac_algorithm_t algo;
54
  size_t length;
55
  update_func update;
56
  digest_func digest;
57
  set_key_func setkey;
58
};
59
60
static void x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx, size_t key_length,
61
          const uint8_t *key)
62
129
{
63
129
  HMAC_SET_KEY(ctx, &x86_sha1, key_length, key);
64
129
}
65
66
static void x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx, size_t length,
67
         const uint8_t *data)
68
129
{
69
129
  x86_sha1_update(&ctx->state, length, data);
70
129
}
71
72
static void x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx, size_t length,
73
         uint8_t *digest)
74
129
{
75
129
  HMAC_DIGEST(ctx, &x86_sha1, length, digest);
76
129
}
77
78
static void x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
79
            size_t key_length, const uint8_t *key)
80
134
{
81
134
  HMAC_SET_KEY(ctx, &x86_sha256, key_length, key);
82
134
}
83
84
static void x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx, size_t length,
85
           const uint8_t *data)
86
300
{
87
300
  x86_sha256_update(&ctx->state, length, data);
88
300
}
89
90
static void x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx, size_t length,
91
           uint8_t *digest)
92
134
{
93
134
  HMAC_DIGEST(ctx, &x86_sha256, length, digest);
94
134
}
95
96
static void x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
97
            size_t key_length, const uint8_t *key)
98
166
{
99
166
  HMAC_SET_KEY(ctx, &x86_sha224, key_length, key);
100
166
}
101
102
static void x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx, size_t length,
103
           uint8_t *digest)
104
166
{
105
166
  HMAC_DIGEST(ctx, &x86_sha224, length, digest);
106
166
}
107
108
static void x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
109
            size_t key_length, const uint8_t *key)
110
185
{
111
185
  HMAC_SET_KEY(ctx, &x86_sha384, key_length, key);
112
185
}
113
114
static void x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx, size_t length,
115
           uint8_t *digest)
116
185
{
117
185
  HMAC_DIGEST(ctx, &x86_sha384, length, digest);
118
185
}
119
120
static void x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
121
            size_t key_length, const uint8_t *key)
122
562
{
123
562
  HMAC_SET_KEY(ctx, &x86_sha512, key_length, key);
124
562
}
125
126
static void x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx, size_t length,
127
           const uint8_t *data)
128
747
{
129
747
  x86_sha512_update(&ctx->state, length, data);
130
747
}
131
132
static void x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx, size_t length,
133
           uint8_t *digest)
134
562
{
135
562
  HMAC_DIGEST(ctx, &x86_sha512, length, digest);
136
562
}
137
138
static int _hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx)
139
1.17k
{
140
1.17k
  switch (algo) {
141
129
  case GNUTLS_MAC_SHA1:
142
129
    ctx->update = (update_func)x86_hmac_sha1_update;
143
129
    ctx->digest = (digest_func)x86_hmac_sha1_digest;
144
129
    ctx->setkey = (set_key_func)x86_hmac_sha1_set_key;
145
129
    ctx->ctx_ptr = &ctx->ctx.sha1;
146
129
    ctx->length = SHA1_DIGEST_SIZE;
147
129
    break;
148
166
  case GNUTLS_MAC_SHA224:
149
166
    ctx->update = (update_func)x86_hmac_sha256_update;
150
166
    ctx->digest = (digest_func)x86_hmac_sha224_digest;
151
166
    ctx->setkey = (set_key_func)x86_hmac_sha224_set_key;
152
166
    ctx->ctx_ptr = &ctx->ctx.sha224;
153
166
    ctx->length = SHA224_DIGEST_SIZE;
154
166
    break;
155
134
  case GNUTLS_MAC_SHA256:
156
134
    ctx->update = (update_func)x86_hmac_sha256_update;
157
134
    ctx->digest = (digest_func)x86_hmac_sha256_digest;
158
134
    ctx->setkey = (set_key_func)x86_hmac_sha256_set_key;
159
134
    ctx->ctx_ptr = &ctx->ctx.sha256;
160
134
    ctx->length = SHA256_DIGEST_SIZE;
161
134
    break;
162
185
  case GNUTLS_MAC_SHA384:
163
185
    ctx->update = (update_func)x86_hmac_sha512_update;
164
185
    ctx->digest = (digest_func)x86_hmac_sha384_digest;
165
185
    ctx->setkey = (set_key_func)x86_hmac_sha384_set_key;
166
185
    ctx->ctx_ptr = &ctx->ctx.sha384;
167
185
    ctx->length = SHA384_DIGEST_SIZE;
168
185
    break;
169
562
  case GNUTLS_MAC_SHA512:
170
562
    ctx->update = (update_func)x86_hmac_sha512_update;
171
562
    ctx->digest = (digest_func)x86_hmac_sha512_digest;
172
562
    ctx->setkey = (set_key_func)x86_hmac_sha512_set_key;
173
562
    ctx->ctx_ptr = &ctx->ctx.sha512;
174
562
    ctx->length = SHA512_DIGEST_SIZE;
175
562
    break;
176
0
  default:
177
0
    gnutls_assert();
178
0
    return GNUTLS_E_INVALID_REQUEST;
179
1.17k
  }
180
181
1.17k
  return 0;
182
1.17k
}
183
184
static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
185
1.17k
{
186
1.17k
  struct x86_hmac_ctx *ctx;
187
1.17k
  int ret;
188
189
1.17k
  ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx));
190
1.17k
  if (ctx == NULL) {
191
0
    gnutls_assert();
192
0
    return GNUTLS_E_MEMORY_ERROR;
193
0
  }
194
195
1.17k
  ctx->algo = algo;
196
197
1.17k
  ret = _hmac_ctx_init(algo, ctx);
198
1.17k
  if (ret < 0)
199
0
    return gnutls_assert_val(ret);
200
201
1.17k
  *_ctx = ctx;
202
203
1.17k
  return 0;
204
1.17k
}
205
206
static void *wrap_x86_hmac_copy(const void *_ctx)
207
0
{
208
0
  struct x86_hmac_ctx *new_ctx;
209
0
  const struct x86_hmac_ctx *ctx = _ctx;
210
0
  ptrdiff_t off = (uint8_t *)ctx->ctx_ptr - (uint8_t *)(&ctx->ctx);
211
212
0
  new_ctx = gnutls_malloc(sizeof(struct x86_hmac_ctx));
213
0
  if (new_ctx == NULL) {
214
0
    gnutls_assert();
215
0
    return NULL;
216
0
  }
217
218
0
  memcpy(new_ctx, ctx, sizeof(*new_ctx));
219
0
  new_ctx->ctx_ptr = (uint8_t *)&new_ctx->ctx + off;
220
221
0
  return new_ctx;
222
0
}
223
224
static int wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen)
225
1.17k
{
226
1.17k
  struct x86_hmac_ctx *ctx = _ctx;
227
228
1.17k
  ctx->setkey(ctx->ctx_ptr, keylen, key);
229
230
1.17k
  return GNUTLS_E_SUCCESS;
231
1.17k
}
232
233
static int wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize)
234
1.17k
{
235
1.17k
  struct x86_hmac_ctx *ctx = _ctx;
236
237
1.17k
  ctx->update(ctx->ctx_ptr, textsize, text);
238
239
1.17k
  return GNUTLS_E_SUCCESS;
240
1.17k
}
241
242
static int wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize)
243
1.17k
{
244
1.17k
  struct x86_hmac_ctx *ctx;
245
1.17k
  ctx = src_ctx;
246
247
1.17k
  if (digestsize < ctx->length) {
248
0
    gnutls_assert();
249
0
    return GNUTLS_E_SHORT_MEMORY_BUFFER;
250
0
  }
251
252
1.17k
  ctx->digest(ctx->ctx_ptr, digestsize, digest);
253
254
1.17k
  return 0;
255
1.17k
}
256
257
static void wrap_x86_hmac_deinit(void *hd)
258
1.17k
{
259
1.17k
  struct x86_hmac_ctx *ctx = hd;
260
261
1.17k
  zeroize_temp_key(ctx, sizeof(*ctx));
262
1.17k
  gnutls_free(ctx);
263
1.17k
}
264
265
static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo, const void *nonce,
266
            size_t nonce_size, const void *key,
267
            size_t key_size, const void *text,
268
            size_t text_size, void *digest)
269
0
{
270
0
  struct x86_hmac_ctx ctx;
271
0
  int ret;
272
273
0
  ret = _hmac_ctx_init(algo, &ctx);
274
0
  if (ret < 0)
275
0
    return gnutls_assert_val(ret);
276
277
0
  ctx.setkey(&ctx, key_size, key);
278
0
  ctx.update(&ctx, text_size, text);
279
0
  ctx.digest(&ctx, ctx.length, digest);
280
281
0
  zeroize_temp_key(&ctx, sizeof(ctx));
282
283
0
  return 0;
284
0
}
285
286
const gnutls_crypto_mac_st _gnutls_hmac_sha_x86_ssse3 = {
287
  .init = wrap_x86_hmac_init,
288
  .setkey = wrap_x86_hmac_setkey,
289
  .setnonce = NULL,
290
  .hash = wrap_x86_hmac_update,
291
  .output = wrap_x86_hmac_output,
292
  .copy = wrap_x86_hmac_copy,
293
  .deinit = wrap_x86_hmac_deinit,
294
  .fast = wrap_x86_hmac_fast,
295
};
296
297
#endif /* HAVE_LIBNETTLE */