summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/engine/eng_aesni.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/engine/eng_aesni.c')
-rw-r--r--src/lib/libcrypto/engine/eng_aesni.c568
1 files changed, 0 insertions, 568 deletions
diff --git a/src/lib/libcrypto/engine/eng_aesni.c b/src/lib/libcrypto/engine/eng_aesni.c
deleted file mode 100644
index 5f9a36236a..0000000000
--- a/src/lib/libcrypto/engine/eng_aesni.c
+++ /dev/null
@@ -1,568 +0,0 @@
1/* $OpenBSD: eng_aesni.c,v 1.8 2015/02/10 09:46:30 miod Exp $ */
2/*
3 * Support for Intel AES-NI intruction set
4 * Author: Huang Ying <ying.huang@intel.com>
5 *
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.
15 *
16 * The white paper can be downloaded from:
17 * http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
18 *
19 * This file is based on engines/e_padlock.c
20 */
21
22/* ====================================================================
23 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 *
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 *
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
35 * distribution.
36 *
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/)"
41 *
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.
46 *
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.
50 *
51 * 6. Redistributions of any form whatsoever must retain the following
52 * acknowledgment:
53 * "This product includes software developed by the OpenSSL Project
54 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
55 *
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 * ====================================================================
69 *
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).
73 *
74 */
75
76#include <stdio.h>
77
78#include <openssl/opensslconf.h>
79
80#if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AES_NI) && !defined(OPENSSL_NO_AES)
81
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>
87
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
90 platforms! */
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
96#endif
97static ENGINE *ENGINE_aesni (void);
98
99void ENGINE_load_aesni (void)
100{
101/* On non-x86 CPUs it just returns. */
102#ifdef COMPILE_HW_AESNI
103 ENGINE *toadd = ENGINE_aesni();
104 if (!toadd)
105 return;
106 ENGINE_add (toadd);
107 ENGINE_register_complete (toadd);
108 ENGINE_free (toadd);
109 ERR_clear_error ();
110#endif
111}
112
113#ifdef COMPILE_HW_AESNI
114int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
115 AES_KEY *key);
116int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
117 AES_KEY *key);
118
119void aesni_encrypt(const unsigned char *in, unsigned char *out,
120 const AES_KEY *key);
121void aesni_decrypt(const unsigned char *in, unsigned char *out,
122 const AES_KEY *key);
123
124void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out,
125 size_t length, const AES_KEY *key, int enc);
126void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out,
127 size_t length, const AES_KEY *key, unsigned char *ivec, int enc);
128
129/* Function for ENGINE detection and control */
130static int aesni_init(ENGINE *e);
131
132/* Cipher Stuff */
133static int aesni_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
134 const int **nids, int nid);
135
136#define AESNI_MIN_ALIGN 16
137#define AESNI_ALIGN(x) \
138 ((void *)(((unsigned long)(x)+AESNI_MIN_ALIGN-1)&~(AESNI_MIN_ALIGN-1)))
139
140/* Engine names */
141static const char aesni_id[] = "aesni",
142 aesni_name[] = "Intel AES-NI engine",
143 no_aesni_name[] = "Intel AES-NI engine (no-aesni)";
144
145
146/* The input and output encrypted as though 128bit cfb mode is being
147 * used. The extra state information to record how much of the
148 * 128bit block we have used is contained in *num;
149 */
150static void
151aesni_cfb128_encrypt(const unsigned char *in, unsigned char *out,
152 unsigned int len, const void *key, unsigned char ivec[16], int *num,
153 int enc)
154{
155 unsigned int n;
156 size_t l = 0;
157
158 n = *num;
159
160 if (enc) {
161#if !defined(OPENSSL_SMALL_FOOTPRINT)
162 if (16%sizeof(size_t) == 0) do { /* always true actually */
163 while (n && len) {
164 *(out++) = ivec[n] ^= *(in++);
165 --len;
166 n = (n + 1) % 16;
167 }
168 while (len >= 16) {
169 aesni_encrypt(ivec, ivec, key);
170 for (n = 0; n < 16; n += sizeof(size_t)) {
171 *(size_t*)(out + n) =
172 *(size_t*)(ivec + n) ^= *(size_t*)(in + n);
173 }
174 len -= 16;
175 out += 16;
176 in += 16;
177 }
178 n = 0;
179 if (len) {
180 aesni_encrypt(ivec, ivec, key);
181 while (len--) {
182 out[n] = ivec[n] ^= in[n];
183 ++n;
184 }
185 }
186 *num = n;
187 return;
188 } while (0);
189 /* the rest would be commonly eliminated by x86* compiler */
190#endif
191 while (l < len) {
192 if (n == 0) {
193 aesni_encrypt(ivec, ivec, key);
194 }
195 out[l] = ivec[n] ^= in[l];
196 ++l;
197 n = (n + 1) % 16;
198 }
199 *num = n;
200 } else {
201#if !defined(OPENSSL_SMALL_FOOTPRINT)
202 if (16%sizeof(size_t) == 0) do { /* always true actually */
203 while (n && len) {
204 unsigned char c;
205 *(out++) = ivec[n] ^ (c = *(in++));
206 ivec[n] = c;
207 --len;
208 n = (n + 1) % 16;
209 }
210 while (len >= 16) {
211 aesni_encrypt(ivec, ivec, key);
212 for (n = 0; n < 16; n += sizeof(size_t)) {
213 size_t t = *(size_t*)(in + n);
214 *(size_t*)(out + n) = *(size_t*)(ivec + n) ^ t;
215 *(size_t*)(ivec + n) = t;
216 }
217 len -= 16;
218 out += 16;
219 in += 16;
220 }
221 n = 0;
222 if (len) {
223 aesni_encrypt(ivec, ivec, key);
224 while (len--) {
225 unsigned char c;
226 out[n] = ivec[n] ^ (c = in[n]);
227 ivec[n] = c;
228 ++n;
229 }
230 }
231 *num = n;
232 return;
233 } while (0);
234 /* the rest would be commonly eliminated by x86* compiler */
235#endif
236 while (l < len) {
237 unsigned char c;
238 if (n == 0) {
239 aesni_encrypt(ivec, ivec, key);
240 }
241 out[l] = ivec[n] ^ (c = in[l]);
242 ivec[n] = c;
243 ++l;
244 n = (n + 1) % 16;
245 }
246 *num = n;
247 }
248}
249
250/* The input and output encrypted as though 128bit ofb mode is being
251 * used. The extra state information to record how much of the
252 * 128bit block we have used is contained in *num;
253 */
254static void
255aesni_ofb128_encrypt(const unsigned char *in, unsigned char *out,
256 unsigned int len, const void *key, unsigned char ivec[16], int *num)
257{
258 unsigned int n;
259 size_t l = 0;
260
261 n = *num;
262
263#if !defined(OPENSSL_SMALL_FOOTPRINT)
264 if (16%sizeof(size_t) == 0) do { /* always true actually */
265 while (n && len) {
266 *(out++) = *(in++) ^ ivec[n];
267 --len;
268 n = (n + 1) % 16;
269 }
270 while (len >= 16) {
271 aesni_encrypt(ivec, ivec, key);
272 for (n = 0; n < 16; n += sizeof(size_t))
273 *(size_t*)(out + n) =
274 *(size_t*)(in + n) ^ *(size_t*)(ivec + n);
275 len -= 16;
276 out += 16;
277 in += 16;
278 }
279 n = 0;
280 if (len) {
281 aesni_encrypt(ivec, ivec, key);
282 while (len--) {
283 out[n] = in[n] ^ ivec[n];
284 ++n;
285 }
286 }
287 *num = n;
288 return;
289 } while (0);
290 /* the rest would be commonly eliminated by x86* compiler */
291#endif
292 while (l < len) {
293 if (n == 0) {
294 aesni_encrypt(ivec, ivec, key);
295 }
296 out[l] = in[l] ^ ivec[n];
297 ++l;
298 n = (n + 1) % 16;
299 }
300
301 *num = n;
302}
303/* ===== Engine "management" functions ===== */
304
305typedef unsigned long long IA32CAP;
306
307/* Prepare the ENGINE structure for registration */
308static int
309aesni_bind_helper(ENGINE *e)
310{
311 int engage;
312
313 if (sizeof(OPENSSL_ia32cap_P) > 4) {
314 engage = ((IA32CAP)OPENSSL_ia32cap_P >> 57) & 1;
315 } else {
316 IA32CAP OPENSSL_ia32_cpuid(void);
317 engage = (OPENSSL_ia32_cpuid() >> 57) & 1;
318 }
319
320 /* Register everything or return with an error */
321 if (!ENGINE_set_id(e, aesni_id) ||
322 !ENGINE_set_name(e, engage ? aesni_name : no_aesni_name) ||
323 !ENGINE_set_init_function(e, aesni_init) ||
324 (engage && !ENGINE_set_ciphers (e, aesni_ciphers)))
325 return 0;
326
327 /* Everything looks good */
328 return 1;
329}
330
331/* Constructor */
332static ENGINE *
333ENGINE_aesni(void)
334{
335 ENGINE *eng = ENGINE_new();
336
337 if (!eng) {
338 return NULL;
339 }
340
341 if (!aesni_bind_helper(eng)) {
342 ENGINE_free(eng);
343 return NULL;
344 }
345
346 return eng;
347}
348
349/* Check availability of the engine */
350static int
351aesni_init(ENGINE *e)
352{
353 return 1;
354}
355
356#if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
357#define NID_aes_128_cfb NID_aes_128_cfb128
358#endif
359
360#if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
361#define NID_aes_128_ofb NID_aes_128_ofb128
362#endif
363
364#if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
365#define NID_aes_192_cfb NID_aes_192_cfb128
366#endif
367
368#if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
369#define NID_aes_192_ofb NID_aes_192_ofb128
370#endif
371
372#if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
373#define NID_aes_256_cfb NID_aes_256_cfb128
374#endif
375
376#if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
377#define NID_aes_256_ofb NID_aes_256_ofb128
378#endif
379
380/* List of supported ciphers. */
381static int aesni_cipher_nids[] = {
382 NID_aes_128_ecb,
383 NID_aes_128_cbc,
384 NID_aes_128_cfb,
385 NID_aes_128_ofb,
386
387 NID_aes_192_ecb,
388 NID_aes_192_cbc,
389 NID_aes_192_cfb,
390 NID_aes_192_ofb,
391
392 NID_aes_256_ecb,
393 NID_aes_256_cbc,
394 NID_aes_256_cfb,
395 NID_aes_256_ofb,
396};
397static int aesni_cipher_nids_num =
398 (sizeof(aesni_cipher_nids) / sizeof(aesni_cipher_nids[0]));
399
400typedef struct {
401 AES_KEY ks;
402 unsigned int _pad1[3];
403} AESNI_KEY;
404
405static int
406aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *user_key,
407 const unsigned char *iv, int enc)
408{
409 int ret;
410 AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
411
412 if ((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CFB_MODE ||
413 (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_OFB_MODE ||
414 enc)
415 ret = aesni_set_encrypt_key(user_key, ctx->key_len * 8, key);
416 else
417 ret = aesni_set_decrypt_key(user_key, ctx->key_len * 8, key);
418
419 if (ret < 0) {
420 EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
421 return 0;
422 }
423
424 return 1;
425}
426
427static int
428aesni_cipher_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
429 const unsigned char *in, size_t inl)
430{
431 AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
432
433 aesni_ecb_encrypt(in, out, inl, key, ctx->encrypt);
434 return 1;
435}
436
437static int
438aesni_cipher_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
439 const unsigned char *in, size_t inl)
440{
441 AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
442
443 aesni_cbc_encrypt(in, out, inl, key, ctx->iv, ctx->encrypt);
444 return 1;
445}
446
447static int
448aesni_cipher_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
449 const unsigned char *in, size_t inl)
450{
451 AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
452
453 aesni_cfb128_encrypt(in, out, inl, key, ctx->iv, &ctx->num,
454 ctx->encrypt);
455 return 1;
456}
457
458static int
459aesni_cipher_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
460 const unsigned char *in, size_t inl)
461{
462 AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
463
464 aesni_ofb128_encrypt(in, out, inl, key, ctx->iv, &ctx->num);
465 return 1;
466}
467
468#define AES_BLOCK_SIZE 16
469
470#define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
471#define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
472#define EVP_CIPHER_block_size_OFB 1
473#define EVP_CIPHER_block_size_CFB 1
474
475/* Declaring so many ciphers by hand would be a pain.
476 Instead introduce a bit of preprocessor magic :-) */
477#define DECLARE_AES_EVP(ksize,lmode,umode) \
478static const EVP_CIPHER aesni_##ksize##_##lmode = { \
479 NID_aes_##ksize##_##lmode, \
480 EVP_CIPHER_block_size_##umode, \
481 ksize / 8, \
482 AES_BLOCK_SIZE, \
483 0 | EVP_CIPH_##umode##_MODE, \
484 aesni_init_key, \
485 aesni_cipher_##lmode, \
486 NULL, \
487 sizeof(AESNI_KEY), \
488 EVP_CIPHER_set_asn1_iv, \
489 EVP_CIPHER_get_asn1_iv, \
490 NULL, \
491 NULL \
492}
493
494DECLARE_AES_EVP(128, ecb, ECB);
495DECLARE_AES_EVP(128, cbc, CBC);
496DECLARE_AES_EVP(128, cfb, CFB);
497DECLARE_AES_EVP(128, ofb, OFB);
498
499DECLARE_AES_EVP(192, ecb, ECB);
500DECLARE_AES_EVP(192, cbc, CBC);
501DECLARE_AES_EVP(192, cfb, CFB);
502DECLARE_AES_EVP(192, ofb, OFB);
503
504DECLARE_AES_EVP(256, ecb, ECB);
505DECLARE_AES_EVP(256, cbc, CBC);
506DECLARE_AES_EVP(256, cfb, CFB);
507DECLARE_AES_EVP(256, ofb, OFB);
508
509static int
510aesni_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid)
511{
512 /* No specific cipher => return a list of supported nids ... */
513 if (!cipher) {
514 *nids = aesni_cipher_nids;
515 return aesni_cipher_nids_num;
516 }
517
518 /* ... or the requested "cipher" otherwise */
519 switch (nid) {
520 case NID_aes_128_ecb:
521 *cipher = &aesni_128_ecb;
522 break;
523 case NID_aes_128_cbc:
524 *cipher = &aesni_128_cbc;
525 break;
526 case NID_aes_128_cfb:
527 *cipher = &aesni_128_cfb;
528 break;
529 case NID_aes_128_ofb:
530 *cipher = &aesni_128_ofb;
531 break;
532
533 case NID_aes_192_ecb:
534 *cipher = &aesni_192_ecb;
535 break;
536 case NID_aes_192_cbc:
537 *cipher = &aesni_192_cbc;
538 break;
539 case NID_aes_192_cfb:
540 *cipher = &aesni_192_cfb;
541 break;
542 case NID_aes_192_ofb:
543 *cipher = &aesni_192_ofb;
544 break;
545
546 case NID_aes_256_ecb:
547 *cipher = &aesni_256_ecb;
548 break;
549 case NID_aes_256_cbc:
550 *cipher = &aesni_256_cbc;
551 break;
552 case NID_aes_256_cfb:
553 *cipher = &aesni_256_cfb;
554 break;
555 case NID_aes_256_ofb:
556 *cipher = &aesni_256_ofb;
557 break;
558
559 default:
560 /* Sorry, we don't support this NID */
561 *cipher = NULL;
562 return 0;
563 }
564 return 1;
565}
566
567#endif /* COMPILE_HW_AESNI */
568#endif /* !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AESNI) && !defined(OPENSSL_NO_AES) */