2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * This file uses the low level AES functions (which are deprecated for
12 * non-internal use) in order to implement the padlock engine AES ciphers.
14 #define OPENSSL_SUPPRESS_DEPRECATED
19 #include <openssl/opensslconf.h>
20 #include <openssl/crypto.h>
21 #include <openssl/engine.h>
22 #include <openssl/evp.h>
23 #include <openssl/aes.h>
24 #include <openssl/rand.h>
25 #include <openssl/err.h>
26 #include <openssl/modes.h>
28 #ifndef OPENSSL_NO_PADLOCKENG
31 * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
32 * doesn't exist elsewhere, but it even can't be compiled on other platforms!
35 # undef COMPILE_PADLOCKENG
36 # if defined(PADLOCK_ASM)
37 # define COMPILE_PADLOCKENG
38 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
39 static ENGINE *ENGINE_padlock(void);
43 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
44 void engine_load_padlock_int(void);
45 void engine_load_padlock_int(void)
47 /* On non-x86 CPUs it just returns. */
48 # ifdef COMPILE_PADLOCKENG
49 ENGINE *toadd = ENGINE_padlock();
60 # ifdef COMPILE_PADLOCKENG
62 /* Function for ENGINE detection and control */
63 static int padlock_available(void);
64 static int padlock_init(ENGINE *e);
67 static RAND_METHOD padlock_rand;
70 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
71 const int **nids, int nid);
74 static const char *padlock_id = "padlock";
75 static char padlock_name[100];
77 /* Available features */
78 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
79 static int padlock_use_rng = 0; /* Random Number Generator */
81 /* ===== Engine "management" functions ===== */
83 /* Prepare the ENGINE structure for registration */
84 static int padlock_bind_helper(ENGINE *e)
86 /* Check available features */
90 * RNG is currently disabled for reasons discussed in commentary just
91 * before padlock_rand_bytes function.
95 /* Generate a nice engine name with available features */
96 BIO_snprintf(padlock_name, sizeof(padlock_name),
97 "VIA PadLock (%s, %s)",
98 padlock_use_rng ? "RNG" : "no-RNG",
99 padlock_use_ace ? "ACE" : "no-ACE");
101 /* Register everything or return with an error */
102 if (!ENGINE_set_id(e, padlock_id) ||
103 !ENGINE_set_name(e, padlock_name) ||
104 !ENGINE_set_init_function(e, padlock_init) ||
105 (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
106 (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
110 /* Everything looks good */
114 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
116 static ENGINE *ENGINE_padlock(void)
118 ENGINE *eng = ENGINE_new();
124 if (!padlock_bind_helper(eng)) {
133 /* Check availability of the engine */
134 static int padlock_init(ENGINE *e)
136 return (padlock_use_rng || padlock_use_ace);
140 * This stuff is needed if this ENGINE is being compiled into a
141 * self-contained shared-library.
143 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
144 static int padlock_bind_fn(ENGINE *e, const char *id)
146 if (id && (strcmp(id, padlock_id) != 0)) {
150 if (!padlock_bind_helper(e)) {
157 IMPLEMENT_DYNAMIC_CHECK_FN()
158 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
159 # endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
160 /* ===== Here comes the "real" engine ===== */
162 /* Some AES-related constants */
163 # define AES_BLOCK_SIZE 16
164 # define AES_KEY_SIZE_128 16
165 # define AES_KEY_SIZE_192 24
166 # define AES_KEY_SIZE_256 32
168 * Here we store the status information relevant to the current context.
171 * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
172 * the order of items in this structure. Don't blindly modify, reorder,
175 struct padlock_cipher_data {
176 unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
181 int dgst:1; /* n/a in C3 */
182 int align:1; /* n/a in C3 */
183 int ciphr:1; /* n/a in C3 */
184 unsigned int keygen:1;
186 unsigned int encdec:1;
189 } cword; /* Control word */
190 AES_KEY ks; /* Encryption key */
193 /* Interface to assembler module */
194 unsigned int padlock_capability(void);
195 void padlock_key_bswap(AES_KEY *key);
196 void padlock_verify_context(struct padlock_cipher_data *ctx);
197 void padlock_reload_key(void);
198 void padlock_aes_block(void *out, const void *inp,
199 struct padlock_cipher_data *ctx);
200 int padlock_ecb_encrypt(void *out, const void *inp,
201 struct padlock_cipher_data *ctx, size_t len);
202 int padlock_cbc_encrypt(void *out, const void *inp,
203 struct padlock_cipher_data *ctx, size_t len);
204 int padlock_cfb_encrypt(void *out, const void *inp,
205 struct padlock_cipher_data *ctx, size_t len);
206 int padlock_ofb_encrypt(void *out, const void *inp,
207 struct padlock_cipher_data *ctx, size_t len);
208 int padlock_ctr32_encrypt(void *out, const void *inp,
209 struct padlock_cipher_data *ctx, size_t len);
210 int padlock_xstore(void *out, int edx);
211 void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
212 void padlock_sha1(void *ctx, const void *inp, size_t len);
213 void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
214 void padlock_sha256(void *ctx, const void *inp, size_t len);
217 * Load supported features of the CPU to see if the PadLock is available.
219 static int padlock_available(void)
221 unsigned int edx = padlock_capability();
223 /* Fill up some flags */
224 padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
225 padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
227 return padlock_use_ace + padlock_use_rng;
230 /* ===== AES encryption/decryption ===== */
232 # if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
233 # define NID_aes_128_cfb NID_aes_128_cfb128
236 # if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
237 # define NID_aes_128_ofb NID_aes_128_ofb128
240 # if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
241 # define NID_aes_192_cfb NID_aes_192_cfb128
244 # if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
245 # define NID_aes_192_ofb NID_aes_192_ofb128
248 # if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
249 # define NID_aes_256_cfb NID_aes_256_cfb128
252 # if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
253 # define NID_aes_256_ofb NID_aes_256_ofb128
256 /* List of supported ciphers. */
257 static const int padlock_cipher_nids[] = {
277 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
278 sizeof(padlock_cipher_nids[0]));
280 /* Function prototypes ... */
281 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
282 const unsigned char *iv, int enc);
284 # define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
285 ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
286 # define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
287 NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
290 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
291 const unsigned char *in_arg, size_t nbytes)
293 return padlock_ecb_encrypt(out_arg, in_arg,
294 ALIGNED_CIPHER_DATA(ctx), nbytes);
298 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
299 const unsigned char *in_arg, size_t nbytes)
301 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
304 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
305 if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
306 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
311 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
312 const unsigned char *in_arg, size_t nbytes)
314 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
317 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
318 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
320 if (chunk >= AES_BLOCK_SIZE)
321 return 0; /* bogus value */
323 if (EVP_CIPHER_CTX_encrypting(ctx))
324 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
325 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
328 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
329 unsigned char c = *(in_arg++);
330 *(out_arg++) = c ^ ivp[chunk];
331 ivp[chunk++] = c, nbytes--;
334 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
340 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
342 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
343 if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
349 unsigned char *ivp = cdata->iv;
353 EVP_CIPHER_CTX_set_num(ctx, nbytes);
354 if (cdata->cword.b.encdec) {
355 cdata->cword.b.encdec = 0;
356 padlock_reload_key();
357 padlock_aes_block(ivp, ivp, cdata);
358 cdata->cword.b.encdec = 1;
359 padlock_reload_key();
361 unsigned char c = *(in_arg++);
362 *(out_arg++) = c ^ *ivp;
363 *(ivp++) = c, nbytes--;
366 padlock_reload_key();
367 padlock_aes_block(ivp, ivp, cdata);
368 padlock_reload_key();
370 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
376 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
382 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
383 const unsigned char *in_arg, size_t nbytes)
385 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
389 * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
391 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
392 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
394 if (chunk >= AES_BLOCK_SIZE)
395 return 0; /* bogus value */
397 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
398 *(out_arg++) = *(in_arg++) ^ ivp[chunk];
402 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
408 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
410 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
411 if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
417 unsigned char *ivp = cdata->iv;
421 EVP_CIPHER_CTX_set_num(ctx, nbytes);
422 padlock_reload_key(); /* empirically found */
423 padlock_aes_block(ivp, ivp, cdata);
424 padlock_reload_key(); /* empirically found */
426 *(out_arg++) = *(in_arg++) ^ *ivp;
431 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
436 static void padlock_ctr32_encrypt_glue(const unsigned char *in,
437 unsigned char *out, size_t blocks,
438 struct padlock_cipher_data *ctx,
439 const unsigned char *ivec)
441 memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
442 padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
446 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
447 const unsigned char *in_arg, size_t nbytes)
449 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
450 unsigned int num = EVP_CIPHER_CTX_num(ctx);
452 CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
453 cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
454 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
455 (ctr128_f) padlock_ctr32_encrypt_glue);
457 EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
461 # define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
462 # define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
463 # define EVP_CIPHER_block_size_OFB 1
464 # define EVP_CIPHER_block_size_CFB 1
465 # define EVP_CIPHER_block_size_CTR 1
468 * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
469 * of preprocessor magic :-)
471 # define DECLARE_AES_EVP(ksize,lmode,umode) \
472 static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
473 static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
475 if (_hidden_aes_##ksize##_##lmode == NULL \
476 && ((_hidden_aes_##ksize##_##lmode = \
477 EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
478 EVP_CIPHER_block_size_##umode, \
479 AES_KEY_SIZE_##ksize)) == NULL \
480 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
482 || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
483 0 | EVP_CIPH_##umode##_MODE) \
484 || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
485 padlock_aes_init_key) \
486 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
487 padlock_##lmode##_cipher) \
488 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
489 sizeof(struct padlock_cipher_data) + 16) \
490 || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
491 EVP_CIPHER_set_asn1_iv) \
492 || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
493 EVP_CIPHER_get_asn1_iv))) { \
494 EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \
495 _hidden_aes_##ksize##_##lmode = NULL; \
497 return _hidden_aes_##ksize##_##lmode; \
500 DECLARE_AES_EVP(128, ecb, ECB)
501 DECLARE_AES_EVP(128, cbc, CBC)
502 DECLARE_AES_EVP(128, cfb, CFB)
503 DECLARE_AES_EVP(128, ofb, OFB)
504 DECLARE_AES_EVP(128, ctr, CTR)
506 DECLARE_AES_EVP(192, ecb, ECB)
507 DECLARE_AES_EVP(192, cbc, CBC)
508 DECLARE_AES_EVP(192, cfb, CFB)
509 DECLARE_AES_EVP(192, ofb, OFB)
510 DECLARE_AES_EVP(192, ctr, CTR)
512 DECLARE_AES_EVP(256, ecb, ECB)
513 DECLARE_AES_EVP(256, cbc, CBC)
514 DECLARE_AES_EVP(256, cfb, CFB)
515 DECLARE_AES_EVP(256, ofb, OFB)
516 DECLARE_AES_EVP(256, ctr, CTR)
519 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
522 /* No specific cipher => return a list of supported nids ... */
524 *nids = padlock_cipher_nids;
525 return padlock_cipher_nids_num;
528 /* ... or the requested "cipher" otherwise */
530 case NID_aes_128_ecb:
531 *cipher = padlock_aes_128_ecb();
533 case NID_aes_128_cbc:
534 *cipher = padlock_aes_128_cbc();
536 case NID_aes_128_cfb:
537 *cipher = padlock_aes_128_cfb();
539 case NID_aes_128_ofb:
540 *cipher = padlock_aes_128_ofb();
542 case NID_aes_128_ctr:
543 *cipher = padlock_aes_128_ctr();
546 case NID_aes_192_ecb:
547 *cipher = padlock_aes_192_ecb();
549 case NID_aes_192_cbc:
550 *cipher = padlock_aes_192_cbc();
552 case NID_aes_192_cfb:
553 *cipher = padlock_aes_192_cfb();
555 case NID_aes_192_ofb:
556 *cipher = padlock_aes_192_ofb();
558 case NID_aes_192_ctr:
559 *cipher = padlock_aes_192_ctr();
562 case NID_aes_256_ecb:
563 *cipher = padlock_aes_256_ecb();
565 case NID_aes_256_cbc:
566 *cipher = padlock_aes_256_cbc();
568 case NID_aes_256_cfb:
569 *cipher = padlock_aes_256_cfb();
571 case NID_aes_256_ofb:
572 *cipher = padlock_aes_256_ofb();
574 case NID_aes_256_ctr:
575 *cipher = padlock_aes_256_ctr();
579 /* Sorry, we don't support this NID */
587 /* Prepare the encryption key for PadLock usage */
589 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
590 const unsigned char *iv, int enc)
592 struct padlock_cipher_data *cdata;
593 int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
594 unsigned long mode = EVP_CIPHER_CTX_mode(ctx);
597 return 0; /* ERROR */
599 cdata = ALIGNED_CIPHER_DATA(ctx);
600 memset(cdata, 0, sizeof(*cdata));
602 /* Prepare Control word. */
603 if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
604 cdata->cword.b.encdec = 0;
606 cdata->cword.b.encdec = (EVP_CIPHER_CTX_encrypting(ctx) == 0);
607 cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
608 cdata->cword.b.ksize = (key_len - 128) / 64;
613 * PadLock can generate an extended key for AES128 in hardware
615 memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
616 cdata->cword.b.keygen = 0;
622 * Generate an extended AES key in software. Needed for AES192/AES256
625 * Well, the above applies to Stepping 8 CPUs and is listed as
626 * hardware errata. They most likely will fix it at some point and
627 * then a check for stepping would be due here.
629 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
631 AES_set_decrypt_key(key, key_len, &cdata->ks);
633 AES_set_encrypt_key(key, key_len, &cdata->ks);
636 * OpenSSL C functions use byte-swapped extended key.
638 padlock_key_bswap(&cdata->ks);
640 cdata->cword.b.keygen = 1;
649 * This is done to cover for cases when user reuses the
650 * context for new key. The catch is that if we don't do
651 * this, padlock_eas_cipher might proceed with old key...
653 padlock_reload_key();
658 /* ===== Random Number Generator ===== */
660 * This code is not engaged. The reason is that it does not comply
661 * with recommendations for VIA RNG usage for secure applications
662 * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
663 * provide meaningful error control...
666 * Wrapper that provides an interface between the API and the raw PadLock
669 static int padlock_rand_bytes(unsigned char *output, int count)
671 unsigned int eax, buf;
674 eax = padlock_xstore(output, 0);
675 if (!(eax & (1 << 6)))
676 return 0; /* RNG disabled */
677 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
678 if (eax & (0x1F << 10))
680 if ((eax & 0x1F) == 0)
681 continue; /* no data, retry... */
682 if ((eax & 0x1F) != 8)
683 return 0; /* fatal failure... */
688 eax = padlock_xstore(&buf, 3);
689 if (!(eax & (1 << 6)))
690 return 0; /* RNG disabled */
691 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
692 if (eax & (0x1F << 10))
694 if ((eax & 0x1F) == 0)
695 continue; /* no data, retry... */
696 if ((eax & 0x1F) != 1)
697 return 0; /* fatal failure... */
698 *output++ = (unsigned char)buf;
701 OPENSSL_cleanse(&buf, sizeof(buf));
706 /* Dummy but necessary function */
707 static int padlock_rand_status(void)
712 /* Prepare structure for registration */
713 static RAND_METHOD padlock_rand = {
715 padlock_rand_bytes, /* bytes */
718 padlock_rand_bytes, /* pseudorand */
719 padlock_rand_status, /* rand status */
722 # endif /* COMPILE_PADLOCKENG */
723 #endif /* !OPENSSL_NO_PADLOCKENG */
725 #if defined(OPENSSL_NO_PADLOCKENG) || !defined(COMPILE_PADLOCKENG)
726 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
728 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
730 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
735 IMPLEMENT_DYNAMIC_CHECK_FN()