Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/accelerated/x86/aes-cbc-x86-ssse3.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
 * Copyright (C) 2018 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS 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
/*
25
 * The following code is an implementation of the AES-128-CBC cipher
26
 * using intel's AES instruction set. 
27
 */
28
29
#include "errors.h"
30
#include "gnutls_int.h"
31
#include <gnutls/crypto.h>
32
#include "errors.h"
33
#include <aes-x86.h>
34
#include <sha-x86.h>
35
#include <x86-common.h>
36
37
struct aes_ctx {
38
  AES_KEY expanded_key;
39
  uint8_t iv[16];
40
  int enc;
41
};
42
43
static int
44
aes_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
45
0
{
46
  /* we use key size to distinguish */
47
0
  if (algorithm != GNUTLS_CIPHER_AES_128_CBC
48
0
      && algorithm != GNUTLS_CIPHER_AES_192_CBC
49
0
      && algorithm != GNUTLS_CIPHER_AES_256_CBC)
50
0
    return GNUTLS_E_INVALID_REQUEST;
51
52
0
  *_ctx = gnutls_calloc(1, sizeof(struct aes_ctx));
53
0
  if (*_ctx == NULL) {
54
0
    gnutls_assert();
55
0
    return GNUTLS_E_MEMORY_ERROR;
56
0
  }
57
58
0
  ((struct aes_ctx *)(*_ctx))->enc = enc;
59
60
0
  return 0;
61
0
}
62
63
static int
64
aes_ssse3_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
65
0
{
66
0
  struct aes_ctx *ctx = _ctx;
67
0
  int ret;
68
69
0
  CHECK_AES_KEYSIZE(keysize);
70
71
0
  if (ctx->enc)
72
0
    ret =
73
0
        vpaes_set_encrypt_key(userkey, keysize * 8,
74
0
            ALIGN16(&ctx->expanded_key));
75
0
  else
76
0
    ret =
77
0
        vpaes_set_decrypt_key(userkey, keysize * 8,
78
0
            ALIGN16(&ctx->expanded_key));
79
80
0
  if (ret != 0)
81
0
    return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
82
83
0
  return 0;
84
0
}
85
86
static int
87
aes_ssse3_encrypt(void *_ctx, const void *src, size_t src_size,
88
      void *dst, size_t dst_size)
89
0
{
90
0
  struct aes_ctx *ctx = _ctx;
91
92
0
  if (unlikely(dst_size < src_size))
93
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
94
95
0
  if (unlikely(src_size % 16 != 0))
96
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
97
98
0
  vpaes_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key),
99
0
        ctx->iv, 1);
100
0
  return 0;
101
0
}
102
103
static int
104
aes_ssse3_decrypt(void *_ctx, const void *src, size_t src_size,
105
      void *dst, size_t dst_size)
106
0
{
107
0
  struct aes_ctx *ctx = _ctx;
108
109
0
  if (unlikely(dst_size < src_size))
110
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
111
112
0
  if (unlikely(src_size % 16 != 0))
113
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
114
115
0
  vpaes_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key),
116
0
        ctx->iv, 0);
117
118
0
  return 0;
119
0
}
120
121
static int aes_setiv(void *_ctx, const void *iv, size_t iv_size)
122
0
{
123
0
  struct aes_ctx *ctx = _ctx;
124
125
0
  if (iv_size != 16)
126
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
127
128
0
  memcpy(ctx->iv, iv, 16);
129
0
  return 0;
130
0
}
131
132
static void aes_deinit(void *_ctx)
133
0
{
134
0
  struct aes_ctx *ctx = _ctx;
135
136
0
  zeroize_temp_key(ctx, sizeof(*ctx));
137
0
  gnutls_free(ctx);
138
0
}
139
140
const gnutls_crypto_cipher_st _gnutls_aes_ssse3 = {
141
  .init = aes_cipher_init,
142
  .setkey = aes_ssse3_cipher_setkey,
143
  .setiv = aes_setiv,
144
  .encrypt = aes_ssse3_encrypt,
145
  .decrypt = aes_ssse3_decrypt,
146
  .deinit = aes_deinit,
147
};