reindented code
[gnutls:gnutls.git] / lib / accelerated / x86 / aes-x86.c
1 /*
2  * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS 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 <http://www.gnu.org/licenses/>
20  *
21  */
22
23 /*
24  * The following code is an implementation of the AES-128-CBC cipher
25  * using intel's AES instruction set. 
26  */
27
28 #include <gnutls_errors.h>
29 #include <gnutls_int.h>
30 #include <gnutls/crypto.h>
31 #include <gnutls_errors.h>
32 #include <aes-x86.h>
33 #include <x86.h>
34
35 struct aes_ctx {
36         AES_KEY expanded_key;
37         uint8_t iv[16];
38         int enc;
39 };
40
41 static int
42 aes_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
43 {
44         /* we use key size to distinguish */
45         if (algorithm != GNUTLS_CIPHER_AES_128_CBC
46             && algorithm != GNUTLS_CIPHER_AES_192_CBC
47             && algorithm != GNUTLS_CIPHER_AES_256_CBC)
48                 return GNUTLS_E_INVALID_REQUEST;
49
50         *_ctx = gnutls_calloc(1, sizeof(struct aes_ctx));
51         if (*_ctx == NULL) {
52                 gnutls_assert();
53                 return GNUTLS_E_MEMORY_ERROR;
54         }
55
56         ((struct aes_ctx *) (*_ctx))->enc = enc;
57
58         return 0;
59 }
60
61 static int
62 aes_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
63 {
64         struct aes_ctx *ctx = _ctx;
65         int ret;
66
67         if (ctx->enc)
68                 ret =
69                     aesni_set_encrypt_key(userkey, keysize * 8,
70                                           ALIGN16(&ctx->expanded_key));
71         else
72                 ret =
73                     aesni_set_decrypt_key(userkey, keysize * 8,
74                                           ALIGN16(&ctx->expanded_key));
75
76         if (ret != 0)
77                 return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
78
79         return 0;
80 }
81
82 static int aes_setiv(void *_ctx, const void *iv, size_t iv_size)
83 {
84         struct aes_ctx *ctx = _ctx;
85
86         memcpy(ctx->iv, iv, 16);
87         return 0;
88 }
89
90 static int
91 aes_encrypt(void *_ctx, const void *src, size_t src_size,
92             void *dst, size_t dst_size)
93 {
94         struct aes_ctx *ctx = _ctx;
95
96         aesni_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key),
97                           ctx->iv, 1);
98         return 0;
99 }
100
101 static int
102 aes_decrypt(void *_ctx, const void *src, size_t src_size,
103             void *dst, size_t dst_size)
104 {
105         struct aes_ctx *ctx = _ctx;
106
107         aesni_cbc_encrypt(src, dst, src_size, ALIGN16(&ctx->expanded_key),
108                           ctx->iv, 0);
109
110         return 0;
111 }
112
113 static void aes_deinit(void *_ctx)
114 {
115         gnutls_free(_ctx);
116 }
117
118 static const gnutls_crypto_cipher_st cipher_struct = {
119         .init = aes_cipher_init,
120         .setkey = aes_cipher_setkey,
121         .setiv = aes_setiv,
122         .encrypt = aes_encrypt,
123         .decrypt = aes_decrypt,
124         .deinit = aes_deinit,
125 };
126
127 static unsigned check_optimized_aes(void)
128 {
129         unsigned int a, b, c, d;
130         gnutls_cpuid(1, &a, &b, &c, &d);
131
132         return (c & 0x2000000);
133 }
134
135 #ifdef ASM_X86_64
136 static unsigned check_pclmul(void)
137 {
138         unsigned int a, b, c, d;
139         gnutls_cpuid(1, &a, &b, &c, &d);
140
141         return (c & 0x2);
142 }
143 #endif
144
145 static unsigned check_intel_or_amd(void)
146 {
147         unsigned int a, b, c, d;
148         gnutls_cpuid(0, &a, &b, &c, &d);
149
150         if ((memcmp(&b, "Genu", 4) == 0 &&
151              memcmp(&d, "ineI", 4) == 0 &&
152              memcmp(&c, "ntel", 4) == 0) ||
153             (memcmp(&b, "Auth", 4) == 0 &&
154              memcmp(&d, "enti", 4) == 0 && memcmp(&c, "cAMD", 4) == 0)) {
155                 return 1;
156         }
157
158         return 0;
159 }
160
161 void register_x86_crypto(void)
162 {
163         int ret;
164
165         if (check_intel_or_amd() == 0)
166                 return;
167
168         if (check_optimized_aes()) {
169                 _gnutls_debug_log("Intel AES accelerator was detected\n");
170                 ret =
171                     gnutls_crypto_single_cipher_register
172                     (GNUTLS_CIPHER_AES_128_CBC, 80, &cipher_struct);
173                 if (ret < 0) {
174                         gnutls_assert();
175                 }
176
177                 ret =
178                     gnutls_crypto_single_cipher_register
179                     (GNUTLS_CIPHER_AES_192_CBC, 80, &cipher_struct);
180                 if (ret < 0) {
181                         gnutls_assert();
182                 }
183
184                 ret =
185                     gnutls_crypto_single_cipher_register
186                     (GNUTLS_CIPHER_AES_256_CBC, 80, &cipher_struct);
187                 if (ret < 0) {
188                         gnutls_assert();
189                 }
190 #ifdef ASM_X86_64
191                 if (check_pclmul()) {
192                         /* register GCM ciphers */
193                         _gnutls_debug_log
194                             ("Intel GCM accelerator was detected\n");
195                         ret =
196                             gnutls_crypto_single_cipher_register
197                             (GNUTLS_CIPHER_AES_128_GCM, 80,
198                              &aes_gcm_struct);
199                         if (ret < 0) {
200                                 gnutls_assert();
201                         }
202
203                         ret =
204                             gnutls_crypto_single_cipher_register
205                             (GNUTLS_CIPHER_AES_256_GCM, 80,
206                              &aes_gcm_struct);
207                         if (ret < 0) {
208                                 gnutls_assert();
209                         }
210                 }
211 #endif
212         }
213
214         return;
215 }