1 /* $OpenBSD: eng_aesni.c,v 1.10 2017/01/29 17:49:23 beck Exp $ */
3 * Support for Intel AES-NI intruction set
4 * Author: Huang Ying <ying.huang@intel.com>
6 * Intel AES-NI is a new set of Single Instruction Multiple Data
7 * (SIMD) instructions that are going to be introduced in the next
8 * generation of Intel processor, as of 2009. These instructions
9 * enable fast and secure data encryption and decryption, using the
10 * Advanced Encryption Standard (AES), defined by FIPS Publication
11 * number 197. The architecture introduces six instructions that
12 * offer full hardware support for AES. Four of them support high
13 * performance data encryption and decryption, and the other two
14 * instructions support the AES key expansion procedure.
16 * The white paper can be downloaded from:
17 * http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
19 * This file is based on engines/e_padlock.c
22 /* ====================================================================
23 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in
34 * the documentation and/or other materials provided with the
37 * 3. All advertising materials mentioning features or use of this
38 * software must display the following acknowledgment:
39 * "This product includes software developed by the OpenSSL Project
40 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
42 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
43 * endorse or promote products derived from this software without
44 * prior written permission. For written permission, please contact
45 * licensing@OpenSSL.org.
47 * 5. Products derived from this software may not be called "OpenSSL"
48 * nor may "OpenSSL" appear in their names without prior written
49 * permission of the OpenSSL Project.
51 * 6. Redistributions of any form whatsoever must retain the following
53 * "This product includes software developed by the OpenSSL Project
54 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
56 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
57 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
60 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
65 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
67 * OF THE POSSIBILITY OF SUCH DAMAGE.
68 * ====================================================================
70 * This product includes cryptographic software written by Eric Young
71 * (eay@cryptsoft.com). This product includes software written by Tim
72 * Hudson (tjh@cryptsoft.com).
78 #include <openssl/opensslconf.h>
80 #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AES_NI) && !defined(OPENSSL_NO_AES)
82 #include <openssl/aes.h>
83 #include <openssl/dso.h>
84 #include <openssl/engine.h>
85 #include <openssl/err.h>
86 #include <openssl/evp.h>
88 /* AES-NI is available *ONLY* on some x86 CPUs. Not only that it
89 doesn't exist elsewhere, but it even can't be compiled on other
91 #undef COMPILE_HW_AESNI
92 #if (defined(__x86_64) || defined(__x86_64__) || \
93 defined(_M_AMD64) || defined(_M_X64) || \
94 defined(OPENSSL_IA32_SSE2)) && !defined(OPENSSL_NO_ASM) && !defined(__i386__)
95 #define COMPILE_HW_AESNI
98 static ENGINE
*ENGINE_aesni(void);
100 void ENGINE_load_aesni(void)
102 /* On non-x86 CPUs it just returns. */
103 #ifdef COMPILE_HW_AESNI
104 ENGINE
*toadd
= ENGINE_aesni();
108 ENGINE_register_complete (toadd
);
114 #ifdef COMPILE_HW_AESNI
115 int aesni_set_encrypt_key(const unsigned char *userKey
, int bits
,
117 int aesni_set_decrypt_key(const unsigned char *userKey
, int bits
,
120 void aesni_encrypt(const unsigned char *in
, unsigned char *out
,
122 void aesni_decrypt(const unsigned char *in
, unsigned char *out
,
125 void aesni_ecb_encrypt(const unsigned char *in
, unsigned char *out
,
126 size_t length
, const AES_KEY
*key
, int enc
);
127 void aesni_cbc_encrypt(const unsigned char *in
, unsigned char *out
,
128 size_t length
, const AES_KEY
*key
, unsigned char *ivec
, int enc
);
130 /* Function for ENGINE detection and control */
131 static int aesni_init(ENGINE
*e
);
134 static int aesni_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
135 const int **nids
, int nid
);
137 #define AESNI_MIN_ALIGN 16
138 #define AESNI_ALIGN(x) \
139 ((void *)(((unsigned long)(x)+AESNI_MIN_ALIGN-1)&~(AESNI_MIN_ALIGN-1)))
142 static const char aesni_id
[] = "aesni",
143 aesni_name
[] = "Intel AES-NI engine",
144 no_aesni_name
[] = "Intel AES-NI engine (no-aesni)";
147 /* The input and output encrypted as though 128bit cfb mode is being
148 * used. The extra state information to record how much of the
149 * 128bit block we have used is contained in *num;
152 aesni_cfb128_encrypt(const unsigned char *in
, unsigned char *out
,
153 unsigned int len
, const void *key
, unsigned char ivec
[16], int *num
,
162 #if !defined(OPENSSL_SMALL_FOOTPRINT)
163 if (16%sizeof(size_t) == 0) do { /* always true actually */
165 *(out
++) = ivec
[n
] ^= *(in
++);
170 aesni_encrypt(ivec
, ivec
, key
);
171 for (n
= 0; n
< 16; n
+= sizeof(size_t)) {
172 *(size_t*)(out
+ n
) =
173 *(size_t*)(ivec
+ n
) ^= *(size_t*)(in
+ n
);
181 aesni_encrypt(ivec
, ivec
, key
);
183 out
[n
] = ivec
[n
] ^= in
[n
];
190 /* the rest would be commonly eliminated by x86* compiler */
194 aesni_encrypt(ivec
, ivec
, key
);
196 out
[l
] = ivec
[n
] ^= in
[l
];
202 #if !defined(OPENSSL_SMALL_FOOTPRINT)
203 if (16%sizeof(size_t) == 0) do { /* always true actually */
206 *(out
++) = ivec
[n
] ^ (c
= *(in
++));
212 aesni_encrypt(ivec
, ivec
, key
);
213 for (n
= 0; n
< 16; n
+= sizeof(size_t)) {
214 size_t t
= *(size_t*)(in
+ n
);
215 *(size_t*)(out
+ n
) = *(size_t*)(ivec
+ n
) ^ t
;
216 *(size_t*)(ivec
+ n
) = t
;
224 aesni_encrypt(ivec
, ivec
, key
);
227 out
[n
] = ivec
[n
] ^ (c
= in
[n
]);
235 /* the rest would be commonly eliminated by x86* compiler */
240 aesni_encrypt(ivec
, ivec
, key
);
242 out
[l
] = ivec
[n
] ^ (c
= in
[l
]);
251 /* The input and output encrypted as though 128bit ofb mode is being
252 * used. The extra state information to record how much of the
253 * 128bit block we have used is contained in *num;
256 aesni_ofb128_encrypt(const unsigned char *in
, unsigned char *out
,
257 unsigned int len
, const void *key
, unsigned char ivec
[16], int *num
)
264 #if !defined(OPENSSL_SMALL_FOOTPRINT)
265 if (16%sizeof(size_t) == 0) do { /* always true actually */
267 *(out
++) = *(in
++) ^ ivec
[n
];
272 aesni_encrypt(ivec
, ivec
, key
);
273 for (n
= 0; n
< 16; n
+= sizeof(size_t))
274 *(size_t*)(out
+ n
) =
275 *(size_t*)(in
+ n
) ^ *(size_t*)(ivec
+ n
);
282 aesni_encrypt(ivec
, ivec
, key
);
284 out
[n
] = in
[n
] ^ ivec
[n
];
291 /* the rest would be commonly eliminated by x86* compiler */
295 aesni_encrypt(ivec
, ivec
, key
);
297 out
[l
] = in
[l
] ^ ivec
[n
];
304 /* ===== Engine "management" functions ===== */
306 /* Prepare the ENGINE structure for registration */
308 aesni_bind_helper(ENGINE
*e
)
312 engage
= (OPENSSL_cpu_caps() & CPUCAP_MASK_AESNI
) != 0;
314 /* Register everything or return with an error */
315 if (!ENGINE_set_id(e
, aesni_id
) ||
316 !ENGINE_set_name(e
, engage
? aesni_name
: no_aesni_name
) ||
317 !ENGINE_set_init_function(e
, aesni_init
) ||
318 (engage
&& !ENGINE_set_ciphers (e
, aesni_ciphers
)))
321 /* Everything looks good */
329 ENGINE
*eng
= ENGINE_new();
335 if (!aesni_bind_helper(eng
)) {
343 /* Check availability of the engine */
345 aesni_init(ENGINE
*e
)
350 #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
351 #define NID_aes_128_cfb NID_aes_128_cfb128
354 #if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
355 #define NID_aes_128_ofb NID_aes_128_ofb128
358 #if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
359 #define NID_aes_192_cfb NID_aes_192_cfb128
362 #if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
363 #define NID_aes_192_ofb NID_aes_192_ofb128
366 #if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
367 #define NID_aes_256_cfb NID_aes_256_cfb128
370 #if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
371 #define NID_aes_256_ofb NID_aes_256_ofb128
374 /* List of supported ciphers. */
375 static int aesni_cipher_nids
[] = {
391 static int aesni_cipher_nids_num
=
392 (sizeof(aesni_cipher_nids
) / sizeof(aesni_cipher_nids
[0]));
396 unsigned int _pad1
[3];
400 aesni_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *user_key
,
401 const unsigned char *iv
, int enc
)
404 AES_KEY
*key
= AESNI_ALIGN(ctx
->cipher_data
);
406 if ((ctx
->cipher
->flags
& EVP_CIPH_MODE
) == EVP_CIPH_CFB_MODE
||
407 (ctx
->cipher
->flags
& EVP_CIPH_MODE
) == EVP_CIPH_OFB_MODE
||
409 ret
= aesni_set_encrypt_key(user_key
, ctx
->key_len
* 8, key
);
411 ret
= aesni_set_decrypt_key(user_key
, ctx
->key_len
* 8, key
);
414 EVPerror(EVP_R_AES_KEY_SETUP_FAILED
);
422 aesni_cipher_ecb(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
423 const unsigned char *in
, size_t inl
)
425 AES_KEY
*key
= AESNI_ALIGN(ctx
->cipher_data
);
427 aesni_ecb_encrypt(in
, out
, inl
, key
, ctx
->encrypt
);
432 aesni_cipher_cbc(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
433 const unsigned char *in
, size_t inl
)
435 AES_KEY
*key
= AESNI_ALIGN(ctx
->cipher_data
);
437 aesni_cbc_encrypt(in
, out
, inl
, key
, ctx
->iv
, ctx
->encrypt
);
442 aesni_cipher_cfb(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
443 const unsigned char *in
, size_t inl
)
445 AES_KEY
*key
= AESNI_ALIGN(ctx
->cipher_data
);
447 aesni_cfb128_encrypt(in
, out
, inl
, key
, ctx
->iv
, &ctx
->num
,
453 aesni_cipher_ofb(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
454 const unsigned char *in
, size_t inl
)
456 AES_KEY
*key
= AESNI_ALIGN(ctx
->cipher_data
);
458 aesni_ofb128_encrypt(in
, out
, inl
, key
, ctx
->iv
, &ctx
->num
);
462 #define AES_BLOCK_SIZE 16
464 #define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
465 #define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
466 #define EVP_CIPHER_block_size_OFB 1
467 #define EVP_CIPHER_block_size_CFB 1
469 /* Declaring so many ciphers by hand would be a pain.
470 Instead introduce a bit of preprocessor magic :-) */
471 #define DECLARE_AES_EVP(ksize,lmode,umode) \
472 static const EVP_CIPHER aesni_##ksize##_##lmode = { \
473 NID_aes_##ksize##_##lmode, \
474 EVP_CIPHER_block_size_##umode, \
477 0 | EVP_CIPH_##umode##_MODE, \
479 aesni_cipher_##lmode, \
482 EVP_CIPHER_set_asn1_iv, \
483 EVP_CIPHER_get_asn1_iv, \
488 DECLARE_AES_EVP(128, ecb
, ECB
);
489 DECLARE_AES_EVP(128, cbc
, CBC
);
490 DECLARE_AES_EVP(128, cfb
, CFB
);
491 DECLARE_AES_EVP(128, ofb
, OFB
);
493 DECLARE_AES_EVP(192, ecb
, ECB
);
494 DECLARE_AES_EVP(192, cbc
, CBC
);
495 DECLARE_AES_EVP(192, cfb
, CFB
);
496 DECLARE_AES_EVP(192, ofb
, OFB
);
498 DECLARE_AES_EVP(256, ecb
, ECB
);
499 DECLARE_AES_EVP(256, cbc
, CBC
);
500 DECLARE_AES_EVP(256, cfb
, CFB
);
501 DECLARE_AES_EVP(256, ofb
, OFB
);
504 aesni_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
, const int **nids
, int nid
)
506 /* No specific cipher => return a list of supported nids ... */
508 *nids
= aesni_cipher_nids
;
509 return aesni_cipher_nids_num
;
512 /* ... or the requested "cipher" otherwise */
514 case NID_aes_128_ecb
:
515 *cipher
= &aesni_128_ecb
;
517 case NID_aes_128_cbc
:
518 *cipher
= &aesni_128_cbc
;
520 case NID_aes_128_cfb
:
521 *cipher
= &aesni_128_cfb
;
523 case NID_aes_128_ofb
:
524 *cipher
= &aesni_128_ofb
;
527 case NID_aes_192_ecb
:
528 *cipher
= &aesni_192_ecb
;
530 case NID_aes_192_cbc
:
531 *cipher
= &aesni_192_cbc
;
533 case NID_aes_192_cfb
:
534 *cipher
= &aesni_192_cfb
;
536 case NID_aes_192_ofb
:
537 *cipher
= &aesni_192_ofb
;
540 case NID_aes_256_ecb
:
541 *cipher
= &aesni_256_ecb
;
543 case NID_aes_256_cbc
:
544 *cipher
= &aesni_256_cbc
;
546 case NID_aes_256_cfb
:
547 *cipher
= &aesni_256_cfb
;
549 case NID_aes_256_ofb
:
550 *cipher
= &aesni_256_ofb
;
554 /* Sorry, we don't support this NID */
561 #endif /* COMPILE_HW_AESNI */
562 #endif /* !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AESNI) && !defined(OPENSSL_NO_AES) */