Intel AES-NI engine.
[openssl.git] / crypto / engine / eng_aesni.c
1 /*
2  * Support for Intel AES-NI intruction set
3  *   Author: Huang Ying <ying.huang@intel.com>
4  *
5  * Intel AES-NI is a new set of Single Instruction Multiple Data
6  * (SIMD) instructions that are going to be introduced in the next
7  * generation of Intel processor, as of 2009. These instructions
8  * enable fast and secure data encryption and decryption, using the
9  * Advanced Encryption Standard (AES), defined by FIPS Publication
10  * number 197.  The architecture introduces six instructions that
11  * offer full hardware support for AES. Four of them support high
12  * performance data encryption and decryption, and the other two
13  * instructions support the AES key expansion procedure.
14  *
15  * The white paper can be downloaded from:
16  *   http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
17  *
18  * This file is based on engines/e_padlock.c
19  */
20
21 /* ====================================================================
22  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  *
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in
33  *    the documentation and/or other materials provided with the
34  *    distribution.
35  *
36  * 3. All advertising materials mentioning features or use of this
37  *    software must display the following acknowledgment:
38  *    "This product includes software developed by the OpenSSL Project
39  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
40  *
41  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
42  *    endorse or promote products derived from this software without
43  *    prior written permission. For written permission, please contact
44  *    licensing@OpenSSL.org.
45  *
46  * 5. Products derived from this software may not be called "OpenSSL"
47  *    nor may "OpenSSL" appear in their names without prior written
48  *    permission of the OpenSSL Project.
49  *
50  * 6. Redistributions of any form whatsoever must retain the following
51  *    acknowledgment:
52  *    "This product includes software developed by the OpenSSL Project
53  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
56  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
59  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
62  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
66  * OF THE POSSIBILITY OF SUCH DAMAGE.
67  * ====================================================================
68  *
69  * This product includes cryptographic software written by Eric Young
70  * (eay@cryptsoft.com).  This product includes software written by Tim
71  * Hudson (tjh@cryptsoft.com).
72  *
73  */
74
75
76 #include <openssl/opensslconf.h>
77
78 #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AES_NI) && !defined(OPENSSL_NO_AES)
79
80 #include <stdio.h>
81 #include <string.h>
82 #include <assert.h>
83 #include <openssl/crypto.h>
84 #include <openssl/dso.h>
85 #include <openssl/engine.h>
86 #include <openssl/evp.h>
87 #include <openssl/aes.h>
88 #include <openssl/err.h>
89 #include <cryptlib.h>
90 #include <openssl/modes.h>
91
92 /* AES-NI is available *ONLY* on some x86 CPUs.  Not only that it
93    doesn't exist elsewhere, but it even can't be compiled on other
94    platforms! */
95 #undef COMPILE_HW_AESNI
96 #if (defined(__x86_64) || defined(__x86_64__) || \
97      defined(_M_AMD64) || defined(_M_X64) || \
98      defined(OPENSSL_IA32_SSE2)) && !defined(OPENSSL_NO_ASM)
99 #define COMPILE_HW_AESNI
100 static ENGINE *ENGINE_aesni (void);
101 #endif
102
103 void ENGINE_load_aesni (void)
104 {
105 /* On non-x86 CPUs it just returns. */
106 #ifdef COMPILE_HW_AESNI
107         ENGINE *toadd = ENGINE_aesni();
108         if (!toadd)
109                 return;
110         ENGINE_add (toadd);
111         ENGINE_free (toadd);
112         ERR_clear_error ();
113 #endif
114 }
115
116 #ifdef COMPILE_HW_AESNI
117 int aesni_set_encrypt_key(const unsigned char *userKey, const int bits,
118                               AES_KEY *key);
119 int aesni_set_decrypt_key(const unsigned char *userKey, const int bits,
120                               AES_KEY *key);
121
122 void aesni_encrypt(const unsigned char *in, unsigned char *out,
123                        const AES_KEY *key);
124 void aesni_decrypt(const unsigned char *in, unsigned char *out,
125                        const AES_KEY *key);
126
127 void aesni_ecb_encrypt(const unsigned char *in,
128                            unsigned char *out,
129                            const unsigned long length,
130                            const AES_KEY *key,
131                            const int enc);
132 void aesni_cbc_encrypt(const unsigned char *in,
133                            unsigned char *out,
134                            const unsigned long length,
135                            const AES_KEY *key,
136                            unsigned char *ivec, const int enc);
137
138 /* Function for ENGINE detection and control */
139 static int aesni_init(ENGINE *e);
140
141 /* Cipher Stuff */
142 static int aesni_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
143                                 const int **nids, int nid);
144
145 #define AESNI_MIN_ALIGN 16
146 #define AESNI_ALIGN(x) \
147         ((void *)(((unsigned long)(x)+AESNI_MIN_ALIGN-1)&~(AESNI_MIN_ALIGN-1)))
148
149 /* Engine names */
150 static const char *aesni_id = "aesni";
151 static const char *aesni_name = "Intel AES-NI engine";
152
153 /* ===== Engine "management" functions ===== */
154
155 /* Prepare the ENGINE structure for registration */
156 static int
157 aesni_bind_helper(ENGINE *e)
158 {
159         if (!(OPENSSL_ia32cap_P[1] & (1UL << (57-32))))
160                 return 0;
161
162         /* Register everything or return with an error */
163         if (!ENGINE_set_id(e, aesni_id) ||
164             !ENGINE_set_name(e, aesni_name) ||
165
166             !ENGINE_set_init_function(e, aesni_init) ||
167             !ENGINE_set_ciphers (e, aesni_ciphers))
168                 return 0;
169
170         /* Everything looks good */
171         return 1;
172 }
173
174 /* Constructor */
175 static ENGINE *
176 ENGINE_aesni(void)
177 {
178         ENGINE *eng = ENGINE_new();
179
180         if (!eng) {
181                 return NULL;
182         }
183
184         if (!aesni_bind_helper(eng)) {
185                 ENGINE_free(eng);
186                 return NULL;
187         }
188
189         return eng;
190 }
191
192 /* Check availability of the engine */
193 static int
194 aesni_init(ENGINE *e)
195 {
196         return 1;
197 }
198
199 #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
200 #define NID_aes_128_cfb NID_aes_128_cfb128
201 #endif
202
203 #if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
204 #define NID_aes_128_ofb NID_aes_128_ofb128
205 #endif
206
207 #if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
208 #define NID_aes_192_cfb NID_aes_192_cfb128
209 #endif
210
211 #if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
212 #define NID_aes_192_ofb NID_aes_192_ofb128
213 #endif
214
215 #if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
216 #define NID_aes_256_cfb NID_aes_256_cfb128
217 #endif
218
219 #if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
220 #define NID_aes_256_ofb NID_aes_256_ofb128
221 #endif
222
223 /* List of supported ciphers. */
224 static int aesni_cipher_nids[] = {
225         NID_aes_128_ecb,
226         NID_aes_128_cbc,
227         NID_aes_128_cfb,
228         NID_aes_128_ofb,
229
230         NID_aes_192_ecb,
231         NID_aes_192_cbc,
232         NID_aes_192_cfb,
233         NID_aes_192_ofb,
234
235         NID_aes_256_ecb,
236         NID_aes_256_cbc,
237         NID_aes_256_cfb,
238         NID_aes_256_ofb,
239 };
240 static int aesni_cipher_nids_num =
241         (sizeof(aesni_cipher_nids)/sizeof(aesni_cipher_nids[0]));
242
243 typedef struct
244 {
245         AES_KEY ks;
246         unsigned int _pad1[3];
247 } AESNI_KEY;
248
249 static int
250 aesni_init_key (EVP_CIPHER_CTX *ctx, const unsigned char *user_key,
251                     const unsigned char *iv, int enc)
252 {
253         int ret;
254         AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
255
256         if ((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CFB_MODE
257             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_OFB_MODE
258             || enc)
259                 ret=aesni_set_encrypt_key(user_key, ctx->key_len * 8, key);
260         else
261                 ret=aesni_set_decrypt_key(user_key, ctx->key_len * 8, key);
262
263         if(ret < 0) {
264                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
265                 return 0;
266         }
267
268         return 1;
269 }
270
271 static int aesni_cipher_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
272                  const unsigned char *in, size_t inl)
273 {       AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
274         aesni_ecb_encrypt(in, out, inl, key, ctx->encrypt);
275         return 1;
276 }
277 static int aesni_cipher_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
278                  const unsigned char *in, size_t inl)
279 {       AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
280         aesni_cbc_encrypt(in, out, inl, key,
281                               ctx->iv, ctx->encrypt);
282         return 1;
283 }
284 static int aesni_cipher_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
285                  const unsigned char *in, size_t inl)
286 {       AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
287         CRYPTO_cfb128_encrypt(in, out, inl, key, ctx->iv,
288                                 &ctx->num, ctx->encrypt,
289                                 aesni_encrypt);
290         return 1;
291 }
292 static int aesni_cipher_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
293                  const unsigned char *in, size_t inl)
294 {       AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
295         CRYPTO_ofb128_encrypt(in, out, inl, key, ctx->iv,
296                                 &ctx->num, aesni_encrypt);
297         return 1;
298 }
299
300 #define AES_BLOCK_SIZE          16
301
302 #define EVP_CIPHER_block_size_ECB       AES_BLOCK_SIZE
303 #define EVP_CIPHER_block_size_CBC       AES_BLOCK_SIZE
304 #define EVP_CIPHER_block_size_OFB       1
305 #define EVP_CIPHER_block_size_CFB       1
306
307 /* Declaring so many ciphers by hand would be a pain.
308    Instead introduce a bit of preprocessor magic :-) */
309 #define DECLARE_AES_EVP(ksize,lmode,umode)      \
310 static const EVP_CIPHER aesni_##ksize##_##lmode = {     \
311         NID_aes_##ksize##_##lmode,                      \
312         EVP_CIPHER_block_size_##umode,                  \
313         ksize / 8,                                      \
314         AES_BLOCK_SIZE,                                 \
315         0 | EVP_CIPH_##umode##_MODE,                    \
316         aesni_init_key,                         \
317         aesni_cipher_##lmode,                           \
318         NULL,                                           \
319         sizeof(AESNI_KEY),                              \
320         EVP_CIPHER_set_asn1_iv,                         \
321         EVP_CIPHER_get_asn1_iv,                         \
322         NULL,                                           \
323         NULL                                            \
324 }
325
326 DECLARE_AES_EVP(128,ecb,ECB);
327 DECLARE_AES_EVP(128,cbc,CBC);
328 DECLARE_AES_EVP(128,cfb,CFB);
329 DECLARE_AES_EVP(128,ofb,OFB);
330
331 DECLARE_AES_EVP(192,ecb,ECB);
332 DECLARE_AES_EVP(192,cbc,CBC);
333 DECLARE_AES_EVP(192,cfb,CFB);
334 DECLARE_AES_EVP(192,ofb,OFB);
335
336 DECLARE_AES_EVP(256,ecb,ECB);
337 DECLARE_AES_EVP(256,cbc,CBC);
338 DECLARE_AES_EVP(256,cfb,CFB);
339 DECLARE_AES_EVP(256,ofb,OFB);
340
341 static int
342 aesni_ciphers (ENGINE *e, const EVP_CIPHER **cipher,
343                       const int **nids, int nid)
344 {
345         /* No specific cipher => return a list of supported nids ... */
346         if (!cipher) {
347                 *nids = aesni_cipher_nids;
348                 return aesni_cipher_nids_num;
349         }
350
351         /* ... or the requested "cipher" otherwise */
352         switch (nid) {
353         case NID_aes_128_ecb:
354                 *cipher = &aesni_128_ecb;
355                 break;
356         case NID_aes_128_cbc:
357                 *cipher = &aesni_128_cbc;
358                 break;
359         case NID_aes_128_cfb:
360                 *cipher = &aesni_128_cfb;
361                 break;
362         case NID_aes_128_ofb:
363                 *cipher = &aesni_128_ofb;
364                 break;
365
366         case NID_aes_192_ecb:
367                 *cipher = &aesni_192_ecb;
368                 break;
369         case NID_aes_192_cbc:
370                 *cipher = &aesni_192_cbc;
371                 break;
372         case NID_aes_192_cfb:
373                 *cipher = &aesni_192_cfb;
374                 break;
375         case NID_aes_192_ofb:
376                 *cipher = &aesni_192_ofb;
377                 break;
378
379         case NID_aes_256_ecb:
380                 *cipher = &aesni_256_ecb;
381                 break;
382         case NID_aes_256_cbc:
383                 *cipher = &aesni_256_cbc;
384                 break;
385         case NID_aes_256_cfb:
386                 *cipher = &aesni_256_cfb;
387                 break;
388         case NID_aes_256_ofb:
389                 *cipher = &aesni_256_ofb;
390                 break;
391
392         default:
393                 /* Sorry, we don't support this NID */
394                 *cipher = NULL;
395                 return 0;
396         }
397
398         return 1;
399 }
400
401 #endif /* COMPILE_HW_AESNI */
402 #endif /* !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AESNI) && !defined(OPENSSL_NO_AES) */