/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 aes_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, |
44 | | 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 aes_ssse3_cipher_setkey(void *_ctx, const void *userkey, |
64 | | 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 = vpaes_set_encrypt_key(userkey, keysize * 8, |
73 | 0 | ALIGN16(&ctx->expanded_key)); |
74 | 0 | else |
75 | 0 | ret = vpaes_set_decrypt_key(userkey, keysize * 8, |
76 | 0 | ALIGN16(&ctx->expanded_key)); |
77 | |
|
78 | 0 | if (ret != 0) |
79 | 0 | return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED); |
80 | | |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | | static int aes_ssse3_encrypt(void *_ctx, const void *src, size_t src_size, |
85 | | void *dst, size_t dst_size) |
86 | 0 | { |
87 | 0 | struct aes_ctx *ctx = _ctx; |
88 | |
|
89 | 0 | if (unlikely(dst_size < src_size)) |
90 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
91 | | |
92 | 0 | if (unlikely(src_size % 16 != 0)) |
93 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
94 | | |
95 | 0 | vpaes_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key), |
96 | 0 | ctx->iv, 1); |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | | static int aes_ssse3_decrypt(void *_ctx, const void *src, size_t src_size, |
101 | | void *dst, size_t dst_size) |
102 | 0 | { |
103 | 0 | struct aes_ctx *ctx = _ctx; |
104 | |
|
105 | 0 | if (unlikely(dst_size < src_size)) |
106 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
107 | | |
108 | 0 | if (unlikely(src_size % 16 != 0)) |
109 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
110 | | |
111 | 0 | vpaes_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key), |
112 | 0 | ctx->iv, 0); |
113 | |
|
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | | static int aes_setiv(void *_ctx, const void *iv, size_t iv_size) |
118 | 0 | { |
119 | 0 | struct aes_ctx *ctx = _ctx; |
120 | |
|
121 | 0 | if (iv_size != 16) |
122 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
123 | | |
124 | 0 | memcpy(ctx->iv, iv, 16); |
125 | 0 | return 0; |
126 | 0 | } |
127 | | |
128 | | static void aes_deinit(void *_ctx) |
129 | 0 | { |
130 | 0 | struct aes_ctx *ctx = _ctx; |
131 | |
|
132 | 0 | zeroize_temp_key(ctx, sizeof(*ctx)); |
133 | 0 | gnutls_free(ctx); |
134 | 0 | } |
135 | | |
136 | | const gnutls_crypto_cipher_st _gnutls_aes_ssse3 = { |
137 | | .init = aes_cipher_init, |
138 | | .setkey = aes_ssse3_cipher_setkey, |
139 | | .setiv = aes_setiv, |
140 | | .encrypt = aes_ssse3_encrypt, |
141 | | .decrypt = aes_ssse3_decrypt, |
142 | | .deinit = aes_deinit, |
143 | | }; |