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
13 #include <openssl/opensslconf.h>
14 #include <openssl/crypto.h>
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
20 #include <openssl/modes.h>
23 # ifndef OPENSSL_NO_HW_PADLOCK
26 * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
27 * doesn't exist elsewhere, but it even can't be compiled on other platforms!
30 # undef COMPILE_HW_PADLOCK
31 # if !defined(I386_ONLY) && defined(PADLOCK_ASM)
32 # define COMPILE_HW_PADLOCK
33 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
34 static ENGINE *ENGINE_padlock(void);
38 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
39 void engine_load_padlock_int(void);
40 void engine_load_padlock_int(void)
42 /* On non-x86 CPUs it just returns. */
43 # ifdef COMPILE_HW_PADLOCK
44 ENGINE *toadd = ENGINE_padlock();
55 # ifdef COMPILE_HW_PADLOCK
57 /* Function for ENGINE detection and control */
58 static int padlock_available(void);
59 static int padlock_init(ENGINE *e);
62 static RAND_METHOD padlock_rand;
65 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
66 const int **nids, int nid);
69 static const char *padlock_id = "padlock";
70 static char padlock_name[100];
72 /* Available features */
73 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
74 static int padlock_use_rng = 0; /* Random Number Generator */
76 /* ===== Engine "management" functions ===== */
78 /* Prepare the ENGINE structure for registration */
79 static int padlock_bind_helper(ENGINE *e)
81 /* Check available features */
85 * RNG is currently disabled for reasons discussed in commentary just
86 * before padlock_rand_bytes function.
90 /* Generate a nice engine name with available features */
91 BIO_snprintf(padlock_name, sizeof(padlock_name),
92 "VIA PadLock (%s, %s)",
93 padlock_use_rng ? "RNG" : "no-RNG",
94 padlock_use_ace ? "ACE" : "no-ACE");
96 /* Register everything or return with an error */
97 if (!ENGINE_set_id(e, padlock_id) ||
98 !ENGINE_set_name(e, padlock_name) ||
99 !ENGINE_set_init_function(e, padlock_init) ||
100 (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
101 (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
105 /* Everything looks good */
109 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
111 static ENGINE *ENGINE_padlock(void)
113 ENGINE *eng = ENGINE_new();
119 if (!padlock_bind_helper(eng)) {
128 /* Check availability of the engine */
129 static int padlock_init(ENGINE *e)
131 return (padlock_use_rng || padlock_use_ace);
135 * This stuff is needed if this ENGINE is being compiled into a
136 * self-contained shared-library.
138 # ifdef DYNAMIC_ENGINE
139 static int padlock_bind_fn(ENGINE *e, const char *id)
141 if (id && (strcmp(id, padlock_id) != 0)) {
145 if (!padlock_bind_helper(e)) {
152 IMPLEMENT_DYNAMIC_CHECK_FN()
153 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
154 # endif /* DYNAMIC_ENGINE */
155 /* ===== Here comes the "real" engine ===== */
157 /* Some AES-related constants */
158 # define AES_BLOCK_SIZE 16
159 # define AES_KEY_SIZE_128 16
160 # define AES_KEY_SIZE_192 24
161 # define AES_KEY_SIZE_256 32
163 * Here we store the status information relevant to the current context.
166 * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
167 * the order of items in this structure. Don't blindly modify, reorder,
170 struct padlock_cipher_data {
171 unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
176 int dgst:1; /* n/a in C3 */
177 int align:1; /* n/a in C3 */
178 int ciphr:1; /* n/a in C3 */
179 unsigned int keygen:1;
181 unsigned int encdec:1;
184 } cword; /* Control word */
185 AES_KEY ks; /* Encryption key */
188 /* Interface to assembler module */
189 unsigned int padlock_capability(void);
190 void padlock_key_bswap(AES_KEY *key);
191 void padlock_verify_context(struct padlock_cipher_data *ctx);
192 void padlock_reload_key(void);
193 void padlock_aes_block(void *out, const void *inp,
194 struct padlock_cipher_data *ctx);
195 int padlock_ecb_encrypt(void *out, const void *inp,
196 struct padlock_cipher_data *ctx, size_t len);
197 int padlock_cbc_encrypt(void *out, const void *inp,
198 struct padlock_cipher_data *ctx, size_t len);
199 int padlock_cfb_encrypt(void *out, const void *inp,
200 struct padlock_cipher_data *ctx, size_t len);
201 int padlock_ofb_encrypt(void *out, const void *inp,
202 struct padlock_cipher_data *ctx, size_t len);
203 int padlock_ctr32_encrypt(void *out, const void *inp,
204 struct padlock_cipher_data *ctx, size_t len);
205 int padlock_xstore(void *out, int edx);
206 void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
207 void padlock_sha1(void *ctx, const void *inp, size_t len);
208 void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
209 void padlock_sha256(void *ctx, const void *inp, size_t len);
212 * Load supported features of the CPU to see if the PadLock is available.
214 static int padlock_available(void)
216 unsigned int edx = padlock_capability();
218 /* Fill up some flags */
219 padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
220 padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
222 return padlock_use_ace + padlock_use_rng;
225 /* ===== AES encryption/decryption ===== */
227 # if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
228 # define NID_aes_128_cfb NID_aes_128_cfb128
231 # if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
232 # define NID_aes_128_ofb NID_aes_128_ofb128
235 # if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
236 # define NID_aes_192_cfb NID_aes_192_cfb128
239 # if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
240 # define NID_aes_192_ofb NID_aes_192_ofb128
243 # if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
244 # define NID_aes_256_cfb NID_aes_256_cfb128
247 # if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
248 # define NID_aes_256_ofb NID_aes_256_ofb128
251 /* List of supported ciphers. */
252 static const int padlock_cipher_nids[] = {
272 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
273 sizeof(padlock_cipher_nids[0]));
275 /* Function prototypes ... */
276 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
277 const unsigned char *iv, int enc);
279 # define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
280 ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
281 # define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
282 NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
285 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
286 const unsigned char *in_arg, size_t nbytes)
288 return padlock_ecb_encrypt(out_arg, in_arg,
289 ALIGNED_CIPHER_DATA(ctx), nbytes);
293 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
294 const unsigned char *in_arg, size_t nbytes)
296 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
299 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
300 if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
301 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
306 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
307 const unsigned char *in_arg, size_t nbytes)
309 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
312 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
313 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
315 if (chunk >= AES_BLOCK_SIZE)
316 return 0; /* bogus value */
318 if (EVP_CIPHER_CTX_encrypting(ctx))
319 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
320 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
323 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
324 unsigned char c = *(in_arg++);
325 *(out_arg++) = c ^ ivp[chunk];
326 ivp[chunk++] = c, nbytes--;
329 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
335 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
337 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
338 if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
344 unsigned char *ivp = cdata->iv;
348 EVP_CIPHER_CTX_set_num(ctx, nbytes);
349 if (cdata->cword.b.encdec) {
350 cdata->cword.b.encdec = 0;
351 padlock_reload_key();
352 padlock_aes_block(ivp, ivp, cdata);
353 cdata->cword.b.encdec = 1;
354 padlock_reload_key();
356 unsigned char c = *(in_arg++);
357 *(out_arg++) = c ^ *ivp;
358 *(ivp++) = c, nbytes--;
361 padlock_reload_key();
362 padlock_aes_block(ivp, ivp, cdata);
363 padlock_reload_key();
365 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
371 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
377 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
378 const unsigned char *in_arg, size_t nbytes)
380 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
384 * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
386 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
387 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
389 if (chunk >= AES_BLOCK_SIZE)
390 return 0; /* bogus value */
392 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
393 *(out_arg++) = *(in_arg++) ^ ivp[chunk];
397 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
403 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
405 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
406 if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
412 unsigned char *ivp = cdata->iv;
416 EVP_CIPHER_CTX_set_num(ctx, nbytes);
417 padlock_reload_key(); /* empirically found */
418 padlock_aes_block(ivp, ivp, cdata);
419 padlock_reload_key(); /* empirically found */
421 *(out_arg++) = *(in_arg++) ^ *ivp;
426 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
431 static void padlock_ctr32_encrypt_glue(const unsigned char *in,
432 unsigned char *out, size_t blocks,
433 struct padlock_cipher_data *ctx,
434 const unsigned char *ivec)
436 memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
437 padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
441 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
442 const unsigned char *in_arg, size_t nbytes)
444 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
445 unsigned int num = EVP_CIPHER_CTX_num(ctx);
447 CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
448 cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
449 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
450 (ctr128_f) padlock_ctr32_encrypt_glue);
452 EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
456 # define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
457 # define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
458 # define EVP_CIPHER_block_size_OFB 1
459 # define EVP_CIPHER_block_size_CFB 1
460 # define EVP_CIPHER_block_size_CTR 1
463 * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
464 * of preprocessor magic :-)
466 # define DECLARE_AES_EVP(ksize,lmode,umode) \
467 static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
468 static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
470 if (_hidden_aes_##ksize##_##lmode == NULL \
471 && ((_hidden_aes_##ksize##_##lmode = \
472 EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
473 EVP_CIPHER_block_size_##umode, \
474 AES_KEY_SIZE_##ksize)) == NULL \
475 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
477 || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
478 0 | EVP_CIPH_##umode##_MODE) \
479 || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
480 padlock_aes_init_key) \
481 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
482 padlock_##lmode##_cipher) \
483 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
484 sizeof(struct padlock_cipher_data) + 16) \
485 || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
486 EVP_CIPHER_set_asn1_iv) \
487 || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
488 EVP_CIPHER_get_asn1_iv))) { \
489 EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \
490 _hidden_aes_##ksize##_##lmode = NULL; \
492 return _hidden_aes_##ksize##_##lmode; \
495 DECLARE_AES_EVP(128, ecb, ECB)
496 DECLARE_AES_EVP(128, cbc, CBC)
497 DECLARE_AES_EVP(128, cfb, CFB)
498 DECLARE_AES_EVP(128, ofb, OFB)
499 DECLARE_AES_EVP(128, ctr, CTR)
501 DECLARE_AES_EVP(192, ecb, ECB)
502 DECLARE_AES_EVP(192, cbc, CBC)
503 DECLARE_AES_EVP(192, cfb, CFB)
504 DECLARE_AES_EVP(192, ofb, OFB)
505 DECLARE_AES_EVP(192, ctr, CTR)
507 DECLARE_AES_EVP(256, ecb, ECB)
508 DECLARE_AES_EVP(256, cbc, CBC)
509 DECLARE_AES_EVP(256, cfb, CFB)
510 DECLARE_AES_EVP(256, ofb, OFB)
511 DECLARE_AES_EVP(256, ctr, CTR)
514 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
517 /* No specific cipher => return a list of supported nids ... */
519 *nids = padlock_cipher_nids;
520 return padlock_cipher_nids_num;
523 /* ... or the requested "cipher" otherwise */
525 case NID_aes_128_ecb:
526 *cipher = padlock_aes_128_ecb();
528 case NID_aes_128_cbc:
529 *cipher = padlock_aes_128_cbc();
531 case NID_aes_128_cfb:
532 *cipher = padlock_aes_128_cfb();
534 case NID_aes_128_ofb:
535 *cipher = padlock_aes_128_ofb();
537 case NID_aes_128_ctr:
538 *cipher = padlock_aes_128_ctr();
541 case NID_aes_192_ecb:
542 *cipher = padlock_aes_192_ecb();
544 case NID_aes_192_cbc:
545 *cipher = padlock_aes_192_cbc();
547 case NID_aes_192_cfb:
548 *cipher = padlock_aes_192_cfb();
550 case NID_aes_192_ofb:
551 *cipher = padlock_aes_192_ofb();
553 case NID_aes_192_ctr:
554 *cipher = padlock_aes_192_ctr();
557 case NID_aes_256_ecb:
558 *cipher = padlock_aes_256_ecb();
560 case NID_aes_256_cbc:
561 *cipher = padlock_aes_256_cbc();
563 case NID_aes_256_cfb:
564 *cipher = padlock_aes_256_cfb();
566 case NID_aes_256_ofb:
567 *cipher = padlock_aes_256_ofb();
569 case NID_aes_256_ctr:
570 *cipher = padlock_aes_256_ctr();
574 /* Sorry, we don't support this NID */
582 /* Prepare the encryption key for PadLock usage */
584 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
585 const unsigned char *iv, int enc)
587 struct padlock_cipher_data *cdata;
588 int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
589 unsigned long mode = EVP_CIPHER_CTX_mode(ctx);
592 return 0; /* ERROR */
594 cdata = ALIGNED_CIPHER_DATA(ctx);
595 memset(cdata, 0, sizeof(*cdata));
597 /* Prepare Control word. */
598 if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
599 cdata->cword.b.encdec = 0;
601 cdata->cword.b.encdec = (EVP_CIPHER_CTX_encrypting(ctx) == 0);
602 cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
603 cdata->cword.b.ksize = (key_len - 128) / 64;
608 * PadLock can generate an extended key for AES128 in hardware
610 memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
611 cdata->cword.b.keygen = 0;
617 * Generate an extended AES key in software. Needed for AES192/AES256
620 * Well, the above applies to Stepping 8 CPUs and is listed as
621 * hardware errata. They most likely will fix it at some point and
622 * then a check for stepping would be due here.
624 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
626 AES_set_decrypt_key(key, key_len, &cdata->ks);
628 AES_set_encrypt_key(key, key_len, &cdata->ks);
631 * OpenSSL C functions use byte-swapped extended key.
633 padlock_key_bswap(&cdata->ks);
635 cdata->cword.b.keygen = 1;
644 * This is done to cover for cases when user reuses the
645 * context for new key. The catch is that if we don't do
646 * this, padlock_eas_cipher might proceed with old key...
648 padlock_reload_key();
653 /* ===== Random Number Generator ===== */
655 * This code is not engaged. The reason is that it does not comply
656 * with recommendations for VIA RNG usage for secure applications
657 * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
658 * provide meaningful error control...
661 * Wrapper that provides an interface between the API and the raw PadLock
664 static int padlock_rand_bytes(unsigned char *output, int count)
666 unsigned int eax, buf;
669 eax = padlock_xstore(output, 0);
670 if (!(eax & (1 << 6)))
671 return 0; /* RNG disabled */
672 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
673 if (eax & (0x1F << 10))
675 if ((eax & 0x1F) == 0)
676 continue; /* no data, retry... */
677 if ((eax & 0x1F) != 8)
678 return 0; /* fatal failure... */
683 eax = padlock_xstore(&buf, 3);
684 if (!(eax & (1 << 6)))
685 return 0; /* RNG disabled */
686 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
687 if (eax & (0x1F << 10))
689 if ((eax & 0x1F) == 0)
690 continue; /* no data, retry... */
691 if ((eax & 0x1F) != 1)
692 return 0; /* fatal failure... */
693 *output++ = (unsigned char)buf;
696 OPENSSL_cleanse(&buf, sizeof(buf));
701 /* Dummy but necessary function */
702 static int padlock_rand_status(void)
707 /* Prepare structure for registration */
708 static RAND_METHOD padlock_rand = {
710 padlock_rand_bytes, /* bytes */
713 padlock_rand_bytes, /* pseudorand */
714 padlock_rand_status, /* rand status */
717 # endif /* COMPILE_HW_PADLOCK */
718 # endif /* !OPENSSL_NO_HW_PADLOCK */
719 #endif /* !OPENSSL_NO_HW */
721 #if defined(OPENSSL_NO_HW) || defined(OPENSSL_NO_HW_PADLOCK) \
722 || !defined(COMPILE_HW_PADLOCK)
723 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
725 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
727 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
732 IMPLEMENT_DYNAMIC_CHECK_FN()