Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/accelerated/x86/hmac-x86-ssse3.c
Line
Count
Source (jump to first uncovered line)
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
61
x86_hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
62
          size_t key_length, const uint8_t * key)
63
0
{
64
0
  HMAC_SET_KEY(ctx, &x86_sha1, key_length, key);
65
0
}
66
67
static void
68
x86_hmac_sha1_update(struct hmac_sha1_ctx *ctx,
69
         size_t length, const uint8_t * data)
70
0
{
71
0
  x86_sha1_update(&ctx->state, length, data);
72
0
}
73
74
static void
75
x86_hmac_sha1_digest(struct hmac_sha1_ctx *ctx, size_t length, uint8_t * digest)
76
0
{
77
0
  HMAC_DIGEST(ctx, &x86_sha1, length, digest);
78
0
}
79
80
static void
81
x86_hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
82
      size_t key_length, const uint8_t * key)
83
0
{
84
0
  HMAC_SET_KEY(ctx, &x86_sha256, key_length, key);
85
0
}
86
87
static void
88
x86_hmac_sha256_update(struct hmac_sha256_ctx *ctx,
89
           size_t length, const uint8_t * data)
90
0
{
91
0
  x86_sha256_update(&ctx->state, length, data);
92
0
}
93
94
static void
95
x86_hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
96
           size_t length, uint8_t * digest)
97
0
{
98
0
  HMAC_DIGEST(ctx, &x86_sha256, length, digest);
99
0
}
100
101
static void
102
x86_hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
103
      size_t key_length, const uint8_t * key)
104
0
{
105
0
  HMAC_SET_KEY(ctx, &x86_sha224, key_length, key);
106
0
}
107
108
static void
109
x86_hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
110
           size_t length, uint8_t * digest)
111
0
{
112
0
  HMAC_DIGEST(ctx, &x86_sha224, length, digest);
113
0
}
114
115
static void
116
x86_hmac_sha384_set_key(struct hmac_sha384_ctx *ctx,
117
      size_t key_length, const uint8_t * key)
118
0
{
119
0
  HMAC_SET_KEY(ctx, &x86_sha384, key_length, key);
120
0
}
121
122
static void
123
x86_hmac_sha384_digest(struct hmac_sha384_ctx *ctx,
124
           size_t length, uint8_t * digest)
125
0
{
126
0
  HMAC_DIGEST(ctx, &x86_sha384, length, digest);
127
0
}
128
129
static void
130
x86_hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
131
      size_t key_length, const uint8_t * key)
132
0
{
133
0
  HMAC_SET_KEY(ctx, &x86_sha512, key_length, key);
134
0
}
135
136
static void
137
x86_hmac_sha512_update(struct hmac_sha512_ctx *ctx,
138
           size_t length, const uint8_t * data)
139
0
{
140
0
  x86_sha512_update(&ctx->state, length, data);
141
0
}
142
143
static void
144
x86_hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
145
           size_t length, uint8_t * digest)
146
0
{
147
0
  HMAC_DIGEST(ctx, &x86_sha512, length, digest);
148
0
}
149
150
static int _hmac_ctx_init(gnutls_mac_algorithm_t algo, struct x86_hmac_ctx *ctx)
151
0
{
152
0
  switch (algo) {
153
0
  case GNUTLS_MAC_SHA1:
154
0
    ctx->update = (update_func) x86_hmac_sha1_update;
155
0
    ctx->digest = (digest_func) x86_hmac_sha1_digest;
156
0
    ctx->setkey = (set_key_func) x86_hmac_sha1_set_key;
157
0
    ctx->ctx_ptr = &ctx->ctx.sha1;
158
0
    ctx->length = SHA1_DIGEST_SIZE;
159
0
    break;
160
0
  case GNUTLS_MAC_SHA224:
161
0
    ctx->update = (update_func) x86_hmac_sha256_update;
162
0
    ctx->digest = (digest_func) x86_hmac_sha224_digest;
163
0
    ctx->setkey = (set_key_func) x86_hmac_sha224_set_key;
164
0
    ctx->ctx_ptr = &ctx->ctx.sha224;
165
0
    ctx->length = SHA224_DIGEST_SIZE;
166
0
    break;
167
0
  case GNUTLS_MAC_SHA256:
168
0
    ctx->update = (update_func) x86_hmac_sha256_update;
169
0
    ctx->digest = (digest_func) x86_hmac_sha256_digest;
170
0
    ctx->setkey = (set_key_func) x86_hmac_sha256_set_key;
171
0
    ctx->ctx_ptr = &ctx->ctx.sha256;
172
0
    ctx->length = SHA256_DIGEST_SIZE;
173
0
    break;
174
0
  case GNUTLS_MAC_SHA384:
175
0
    ctx->update = (update_func) x86_hmac_sha512_update;
176
0
    ctx->digest = (digest_func) x86_hmac_sha384_digest;
177
0
    ctx->setkey = (set_key_func) x86_hmac_sha384_set_key;
178
0
    ctx->ctx_ptr = &ctx->ctx.sha384;
179
0
    ctx->length = SHA384_DIGEST_SIZE;
180
0
    break;
181
0
  case GNUTLS_MAC_SHA512:
182
0
    ctx->update = (update_func) x86_hmac_sha512_update;
183
0
    ctx->digest = (digest_func) x86_hmac_sha512_digest;
184
0
    ctx->setkey = (set_key_func) x86_hmac_sha512_set_key;
185
0
    ctx->ctx_ptr = &ctx->ctx.sha512;
186
0
    ctx->length = SHA512_DIGEST_SIZE;
187
0
    break;
188
0
  default:
189
0
    gnutls_assert();
190
0
    return GNUTLS_E_INVALID_REQUEST;
191
0
  }
192
193
0
  return 0;
194
0
}
195
196
static int wrap_x86_hmac_init(gnutls_mac_algorithm_t algo, void **_ctx)
197
0
{
198
0
  struct x86_hmac_ctx *ctx;
199
0
  int ret;
200
201
0
  ctx = gnutls_calloc(1, sizeof(struct x86_hmac_ctx));
202
0
  if (ctx == NULL) {
203
0
    gnutls_assert();
204
0
    return GNUTLS_E_MEMORY_ERROR;
205
0
  }
206
207
0
  ctx->algo = algo;
208
209
0
  ret = _hmac_ctx_init(algo, ctx);
210
0
  if (ret < 0)
211
0
    return gnutls_assert_val(ret);
212
213
0
  *_ctx = ctx;
214
215
0
  return 0;
216
0
}
217
218
static void *wrap_x86_hmac_copy(const void *_ctx)
219
0
{
220
0
  struct x86_hmac_ctx *new_ctx;
221
0
  const struct x86_hmac_ctx *ctx = _ctx;
222
0
  ptrdiff_t off = (uint8_t *) ctx->ctx_ptr - (uint8_t *) (&ctx->ctx);
223
224
0
  new_ctx = gnutls_malloc(sizeof(struct x86_hmac_ctx));
225
0
  if (new_ctx == NULL) {
226
0
    gnutls_assert();
227
0
    return NULL;
228
0
  }
229
230
0
  memcpy(new_ctx, ctx, sizeof(*new_ctx));
231
0
  new_ctx->ctx_ptr = (uint8_t *) & new_ctx->ctx + off;
232
233
0
  return new_ctx;
234
0
}
235
236
static int wrap_x86_hmac_setkey(void *_ctx, const void *key, size_t keylen)
237
0
{
238
0
  struct x86_hmac_ctx *ctx = _ctx;
239
240
0
  ctx->setkey(ctx->ctx_ptr, keylen, key);
241
242
0
  return GNUTLS_E_SUCCESS;
243
0
}
244
245
static int wrap_x86_hmac_update(void *_ctx, const void *text, size_t textsize)
246
0
{
247
0
  struct x86_hmac_ctx *ctx = _ctx;
248
249
0
  ctx->update(ctx->ctx_ptr, textsize, text);
250
251
0
  return GNUTLS_E_SUCCESS;
252
0
}
253
254
static int wrap_x86_hmac_output(void *src_ctx, void *digest, size_t digestsize)
255
0
{
256
0
  struct x86_hmac_ctx *ctx;
257
0
  ctx = src_ctx;
258
259
0
  if (digestsize < ctx->length) {
260
0
    gnutls_assert();
261
0
    return GNUTLS_E_SHORT_MEMORY_BUFFER;
262
0
  }
263
264
0
  ctx->digest(ctx->ctx_ptr, digestsize, digest);
265
266
0
  return 0;
267
0
}
268
269
static void wrap_x86_hmac_deinit(void *hd)
270
0
{
271
0
  struct x86_hmac_ctx *ctx = hd;
272
273
0
  zeroize_temp_key(ctx, sizeof(*ctx));
274
0
  gnutls_free(ctx);
275
0
}
276
277
static int wrap_x86_hmac_fast(gnutls_mac_algorithm_t algo,
278
            const void *nonce, size_t nonce_size,
279
            const void *key, size_t key_size,
280
            const void *text, size_t text_size, void *digest)
281
0
{
282
0
  struct x86_hmac_ctx ctx;
283
0
  int ret;
284
285
0
  ret = _hmac_ctx_init(algo, &ctx);
286
0
  if (ret < 0)
287
0
    return gnutls_assert_val(ret);
288
289
0
  ctx.setkey(&ctx, key_size, key);
290
0
  ctx.update(&ctx, text_size, text);
291
0
  ctx.digest(&ctx, ctx.length, digest);
292
293
0
  zeroize_temp_key(&ctx, sizeof(ctx));
294
295
0
  return 0;
296
0
}
297
298
const gnutls_crypto_mac_st _gnutls_hmac_sha_x86_ssse3 = {
299
  .init = wrap_x86_hmac_init,
300
  .setkey = wrap_x86_hmac_setkey,
301
  .setnonce = NULL,
302
  .hash = wrap_x86_hmac_update,
303
  .output = wrap_x86_hmac_output,
304
  .copy = wrap_x86_hmac_copy,
305
  .deinit = wrap_x86_hmac_deinit,
306
  .fast = wrap_x86_hmac_fast,
307
};
308
309
#endif        /* HAVE_LIBNETTLE */