intel directory renamed to x86.
[gnutls:gnutls.git] / lib / accelerated / x86 / aes-x86.c
1 /*
2  * Copyright (C) 2011 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 3 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 {
37   AES_KEY expanded_key;
38   uint8_t iv[16];
39   int enc;
40 };
41
42 static int
43 aes_cipher_init (gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
44 {
45   /* we use key size to distinguish */
46   if (algorithm != GNUTLS_CIPHER_AES_128_CBC
47       && algorithm != GNUTLS_CIPHER_AES_192_CBC
48       && algorithm != GNUTLS_CIPHER_AES_256_CBC)
49     return GNUTLS_E_INVALID_REQUEST;
50
51   *_ctx = gnutls_calloc (1, sizeof (struct aes_ctx));
52   if (*_ctx == NULL)
53     {
54       gnutls_assert ();
55       return GNUTLS_E_MEMORY_ERROR;
56     }
57
58   ((struct aes_ctx*)(*_ctx))->enc = enc;
59
60   return 0;
61 }
62
63 static int
64 aes_cipher_setkey (void *_ctx, const void *userkey, size_t keysize)
65 {
66   struct aes_ctx *ctx = _ctx;
67   int ret;
68
69   if (ctx->enc)
70     ret = aesni_set_encrypt_key (userkey, keysize * 8, ALIGN16(&ctx->expanded_key));
71   else
72     ret = aesni_set_decrypt_key (userkey, keysize * 8, ALIGN16(&ctx->expanded_key));
73
74   if (ret != 0)
75     return gnutls_assert_val (GNUTLS_E_ENCRYPTION_FAILED);
76
77   return 0;
78 }
79
80 static int
81 aes_setiv (void *_ctx, const void *iv, size_t iv_size)
82 {
83   struct aes_ctx *ctx = _ctx;
84
85   memcpy (ctx->iv, iv, 16);
86   return 0;
87 }
88
89 static int
90 aes_encrypt (void *_ctx, const void *src, size_t src_size,
91              void *dst, size_t dst_size)
92 {
93   struct aes_ctx *ctx = _ctx;
94
95   aesni_cbc_encrypt (src, dst, src_size, ALIGN16(&ctx->expanded_key), ctx->iv, 1);
96   return 0;
97 }
98
99 static int
100 aes_decrypt (void *_ctx, const void *src, size_t src_size,
101              void *dst, size_t dst_size)
102 {
103   struct aes_ctx *ctx = _ctx;
104
105   aesni_cbc_encrypt (src, dst, src_size, ALIGN16(&ctx->expanded_key), ctx->iv, 0);
106
107   return 0;
108 }
109
110 static void
111 aes_deinit (void *_ctx)
112 {
113   gnutls_free (_ctx);
114 }
115
116 static const gnutls_crypto_cipher_st cipher_struct = {
117   .init = aes_cipher_init,
118   .setkey = aes_cipher_setkey,
119   .setiv = aes_setiv,
120   .encrypt = aes_encrypt,
121   .decrypt = aes_decrypt,
122   .deinit = aes_deinit,
123 };
124
125 static unsigned
126 check_optimized_aes (void)
127 {
128   unsigned int a, b, c, d;
129   _gnutls_cpuid (1, &a, &b, &c, &d);
130
131   return (c & 0x2000000);
132 }
133
134 #ifdef ASM_X86_64
135 static unsigned
136 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
146 check_intel_or_amd (void)
147 {
148   unsigned int a, b, c, d;
149   _gnutls_cpuid (0, &a, &b, &c, &d);
150
151   if ((memcmp (&b, "Genu", 4) == 0 &&
152        memcmp (&d, "ineI", 4) == 0 &&
153        memcmp (&c, "ntel", 4) == 0) ||
154       (memcmp (&b, "Auth", 4) == 0 &&
155        memcmp (&d, "enti", 4) == 0 && memcmp (&c, "cAMD", 4) == 0))
156     {
157       return 1;
158     }
159
160   return 0;
161 }
162
163 void
164 register_x86_crypto (void)
165 {
166   int ret;
167
168   if (check_intel_or_amd () == 0)
169     return;
170
171   if (check_optimized_aes ())
172     {
173       _gnutls_debug_log ("Intel AES accelerator was detected\n");
174       ret =
175         gnutls_crypto_single_cipher_register (GNUTLS_CIPHER_AES_128_CBC, 80,
176                                               &cipher_struct);
177       if (ret < 0)
178         {
179           gnutls_assert ();
180         }
181
182       ret =
183         gnutls_crypto_single_cipher_register (GNUTLS_CIPHER_AES_192_CBC, 80,
184                                               &cipher_struct);
185       if (ret < 0)
186         {
187           gnutls_assert ();
188         }
189
190       ret =
191         gnutls_crypto_single_cipher_register (GNUTLS_CIPHER_AES_256_CBC, 80,
192                                               &cipher_struct);
193       if (ret < 0)
194         {
195           gnutls_assert ();
196         }
197
198 #ifdef ASM_X86_64
199       if (check_pclmul ())
200         {
201           /* register GCM ciphers */
202           _gnutls_debug_log ("Intel GCM accelerator was detected\n");
203           ret =
204             gnutls_crypto_single_cipher_register (GNUTLS_CIPHER_AES_128_GCM,
205                                                   80, &aes_gcm_struct);
206           if (ret < 0)
207             {
208               gnutls_assert ();
209             }
210
211           ret =
212             gnutls_crypto_single_cipher_register (GNUTLS_CIPHER_AES_256_GCM,
213                                                   80, &aes_gcm_struct);
214           if (ret < 0)
215             {
216               gnutls_assert ();
217             }
218           
219           if (ret >= 0)
220             _gnutls_priority_prefer_aes_gcm();
221         }
222 #endif
223     }
224
225   return;
226 }