2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
25 /* Attempt to have a single source for both 0.9.7 and 0.9.8 :-) */
26 # if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
27 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
28 # define DYNAMIC_ENGINE
30 # elif (OPENSSL_VERSION_NUMBER >= 0x00907000L)
31 # ifdef ENGINE_DYNAMIC_SUPPORT
32 # define DYNAMIC_ENGINE
35 # error "Only OpenSSL >= 0.9.7 is supported"
39 * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
40 * doesn't exist elsewhere, but it even can't be compiled on other platforms!
43 # undef COMPILE_HW_PADLOCK
44 # if !defined(I386_ONLY) && !defined(OPENSSL_NO_ASM)
45 # if defined(__i386__) || defined(__i386) || \
46 defined(__x86_64__) || defined(__x86_64) || \
47 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)
48 # define COMPILE_HW_PADLOCK
49 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
50 static ENGINE *ENGINE_padlock(void);
55 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
56 void engine_load_padlock_int(void);
57 void engine_load_padlock_int(void)
59 /* On non-x86 CPUs it just returns. */
60 # ifdef COMPILE_HW_PADLOCK
61 ENGINE *toadd = ENGINE_padlock();
72 # ifdef COMPILE_HW_PADLOCK
74 /* Function for ENGINE detection and control */
75 static int padlock_available(void);
76 static int padlock_init(ENGINE *e);
79 static RAND_METHOD padlock_rand;
82 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
83 const int **nids, int nid);
86 static const char *padlock_id = "padlock";
87 static char padlock_name[100];
89 /* Available features */
90 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
91 static int padlock_use_rng = 0; /* Random Number Generator */
93 /* ===== Engine "management" functions ===== */
95 /* Prepare the ENGINE structure for registration */
96 static int padlock_bind_helper(ENGINE *e)
98 /* Check available features */
102 * RNG is currently disabled for reasons discussed in commentary just
103 * before padlock_rand_bytes function.
107 /* Generate a nice engine name with available features */
108 BIO_snprintf(padlock_name, sizeof(padlock_name),
109 "VIA PadLock (%s, %s)",
110 padlock_use_rng ? "RNG" : "no-RNG",
111 padlock_use_ace ? "ACE" : "no-ACE");
113 /* Register everything or return with an error */
114 if (!ENGINE_set_id(e, padlock_id) ||
115 !ENGINE_set_name(e, padlock_name) ||
116 !ENGINE_set_init_function(e, padlock_init) ||
117 (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
118 (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
122 /* Everything looks good */
126 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
128 static ENGINE *ENGINE_padlock(void)
130 ENGINE *eng = ENGINE_new();
136 if (!padlock_bind_helper(eng)) {
145 /* Check availability of the engine */
146 static int padlock_init(ENGINE *e)
148 return (padlock_use_rng || padlock_use_ace);
152 * This stuff is needed if this ENGINE is being compiled into a
153 * self-contained shared-library.
155 # ifdef DYNAMIC_ENGINE
156 static int padlock_bind_fn(ENGINE *e, const char *id)
158 if (id && (strcmp(id, padlock_id) != 0)) {
162 if (!padlock_bind_helper(e)) {
169 IMPLEMENT_DYNAMIC_CHECK_FN()
170 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
171 # endif /* DYNAMIC_ENGINE */
172 /* ===== Here comes the "real" engine ===== */
174 /* Some AES-related constants */
175 # define AES_BLOCK_SIZE 16
176 # define AES_KEY_SIZE_128 16
177 # define AES_KEY_SIZE_192 24
178 # define AES_KEY_SIZE_256 32
180 * Here we store the status information relevant to the current context.
183 * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
184 * the order of items in this structure. Don't blindly modify, reorder,
187 struct padlock_cipher_data {
188 unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
193 int dgst:1; /* n/a in C3 */
194 int align:1; /* n/a in C3 */
195 int ciphr:1; /* n/a in C3 */
196 unsigned int keygen:1;
198 unsigned int encdec:1;
201 } cword; /* Control word */
202 AES_KEY ks; /* Encryption key */
205 /* Interface to assembler module */
206 unsigned int padlock_capability();
207 void padlock_key_bswap(AES_KEY *key);
208 void padlock_verify_context(struct padlock_cipher_data *ctx);
209 void padlock_reload_key();
210 void padlock_aes_block(void *out, const void *inp,
211 struct padlock_cipher_data *ctx);
212 int padlock_ecb_encrypt(void *out, const void *inp,
213 struct padlock_cipher_data *ctx, size_t len);
214 int padlock_cbc_encrypt(void *out, const void *inp,
215 struct padlock_cipher_data *ctx, size_t len);
216 int padlock_cfb_encrypt(void *out, const void *inp,
217 struct padlock_cipher_data *ctx, size_t len);
218 int padlock_ofb_encrypt(void *out, const void *inp,
219 struct padlock_cipher_data *ctx, size_t len);
220 int padlock_ctr32_encrypt(void *out, const void *inp,
221 struct padlock_cipher_data *ctx, size_t len);
222 int padlock_xstore(void *out, int edx);
223 void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
224 void padlock_sha1(void *ctx, const void *inp, size_t len);
225 void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
226 void padlock_sha256(void *ctx, const void *inp, size_t len);
229 * Load supported features of the CPU to see if the PadLock is available.
231 static int padlock_available(void)
233 unsigned int edx = padlock_capability();
235 /* Fill up some flags */
236 padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
237 padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
239 return padlock_use_ace + padlock_use_rng;
242 /* ===== AES encryption/decryption ===== */
244 # if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
245 # define NID_aes_128_cfb NID_aes_128_cfb128
248 # if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
249 # define NID_aes_128_ofb NID_aes_128_ofb128
252 # if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
253 # define NID_aes_192_cfb NID_aes_192_cfb128
256 # if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
257 # define NID_aes_192_ofb NID_aes_192_ofb128
260 # if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
261 # define NID_aes_256_cfb NID_aes_256_cfb128
264 # if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
265 # define NID_aes_256_ofb NID_aes_256_ofb128
268 /* List of supported ciphers. */
269 static const int padlock_cipher_nids[] = {
289 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
290 sizeof(padlock_cipher_nids[0]));
292 /* Function prototypes ... */
293 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
294 const unsigned char *iv, int enc);
296 # define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
297 ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
298 # define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
299 NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
302 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
303 const unsigned char *in_arg, size_t nbytes)
305 return padlock_ecb_encrypt(out_arg, in_arg,
306 ALIGNED_CIPHER_DATA(ctx), nbytes);
310 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
311 const unsigned char *in_arg, size_t nbytes)
313 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
316 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
317 if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
318 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
323 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
324 const unsigned char *in_arg, size_t nbytes)
326 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
329 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
330 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
332 if (chunk >= AES_BLOCK_SIZE)
333 return 0; /* bogus value */
335 if (EVP_CIPHER_CTX_encrypting(ctx))
336 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
337 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
340 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
341 unsigned char c = *(in_arg++);
342 *(out_arg++) = c ^ ivp[chunk];
343 ivp[chunk++] = c, nbytes--;
346 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
352 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
354 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
355 if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
361 unsigned char *ivp = cdata->iv;
365 EVP_CIPHER_CTX_set_num(ctx, nbytes);
366 if (cdata->cword.b.encdec) {
367 cdata->cword.b.encdec = 0;
368 padlock_reload_key();
369 padlock_aes_block(ivp, ivp, cdata);
370 cdata->cword.b.encdec = 1;
371 padlock_reload_key();
373 unsigned char c = *(in_arg++);
374 *(out_arg++) = c ^ *ivp;
375 *(ivp++) = c, nbytes--;
378 padlock_reload_key();
379 padlock_aes_block(ivp, ivp, cdata);
380 padlock_reload_key();
382 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
388 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
394 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
395 const unsigned char *in_arg, size_t nbytes)
397 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
401 * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
403 if ((chunk = EVP_CIPHER_CTX_num(ctx))) { /* borrow chunk variable */
404 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
406 if (chunk >= AES_BLOCK_SIZE)
407 return 0; /* bogus value */
409 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
410 *(out_arg++) = *(in_arg++) ^ ivp[chunk];
414 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
420 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
422 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
423 if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
429 unsigned char *ivp = cdata->iv;
433 EVP_CIPHER_CTX_set_num(ctx, nbytes);
434 padlock_reload_key(); /* empirically found */
435 padlock_aes_block(ivp, ivp, cdata);
436 padlock_reload_key(); /* empirically found */
438 *(out_arg++) = *(in_arg++) ^ *ivp;
443 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
448 static void padlock_ctr32_encrypt_glue(const unsigned char *in,
449 unsigned char *out, size_t blocks,
450 struct padlock_cipher_data *ctx,
451 const unsigned char *ivec)
453 memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
454 padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
458 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
459 const unsigned char *in_arg, size_t nbytes)
461 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
462 unsigned int num = EVP_CIPHER_CTX_num(ctx);
464 CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
465 cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
466 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
467 (ctr128_f) padlock_ctr32_encrypt_glue);
469 EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
473 # define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
474 # define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
475 # define EVP_CIPHER_block_size_OFB 1
476 # define EVP_CIPHER_block_size_CFB 1
477 # define EVP_CIPHER_block_size_CTR 1
480 * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
481 * of preprocessor magic :-)
483 # define DECLARE_AES_EVP(ksize,lmode,umode) \
484 static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
485 static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
487 if (_hidden_aes_##ksize##_##lmode == NULL \
488 && ((_hidden_aes_##ksize##_##lmode = \
489 EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
490 EVP_CIPHER_block_size_##umode, \
491 AES_KEY_SIZE_##ksize)) == NULL \
492 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
494 || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
495 0 | EVP_CIPH_##umode##_MODE) \
496 || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
497 padlock_aes_init_key) \
498 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
499 padlock_##lmode##_cipher) \
500 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
501 sizeof(struct padlock_cipher_data) + 16) \
502 || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
503 EVP_CIPHER_set_asn1_iv) \
504 || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
505 EVP_CIPHER_get_asn1_iv))) { \
506 EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \
507 _hidden_aes_##ksize##_##lmode = NULL; \
509 return _hidden_aes_##ksize##_##lmode; \
512 DECLARE_AES_EVP(128, ecb, ECB)
513 DECLARE_AES_EVP(128, cbc, CBC)
514 DECLARE_AES_EVP(128, cfb, CFB)
515 DECLARE_AES_EVP(128, ofb, OFB)
516 DECLARE_AES_EVP(128, ctr, CTR)
518 DECLARE_AES_EVP(192, ecb, ECB)
519 DECLARE_AES_EVP(192, cbc, CBC)
520 DECLARE_AES_EVP(192, cfb, CFB)
521 DECLARE_AES_EVP(192, ofb, OFB)
522 DECLARE_AES_EVP(192, ctr, CTR)
524 DECLARE_AES_EVP(256, ecb, ECB)
525 DECLARE_AES_EVP(256, cbc, CBC)
526 DECLARE_AES_EVP(256, cfb, CFB)
527 DECLARE_AES_EVP(256, ofb, OFB)
528 DECLARE_AES_EVP(256, ctr, CTR)
531 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
534 /* No specific cipher => return a list of supported nids ... */
536 *nids = padlock_cipher_nids;
537 return padlock_cipher_nids_num;
540 /* ... or the requested "cipher" otherwise */
542 case NID_aes_128_ecb:
543 *cipher = padlock_aes_128_ecb();
545 case NID_aes_128_cbc:
546 *cipher = padlock_aes_128_cbc();
548 case NID_aes_128_cfb:
549 *cipher = padlock_aes_128_cfb();
551 case NID_aes_128_ofb:
552 *cipher = padlock_aes_128_ofb();
554 case NID_aes_128_ctr:
555 *cipher = padlock_aes_128_ctr();
558 case NID_aes_192_ecb:
559 *cipher = padlock_aes_192_ecb();
561 case NID_aes_192_cbc:
562 *cipher = padlock_aes_192_cbc();
564 case NID_aes_192_cfb:
565 *cipher = padlock_aes_192_cfb();
567 case NID_aes_192_ofb:
568 *cipher = padlock_aes_192_ofb();
570 case NID_aes_192_ctr:
571 *cipher = padlock_aes_192_ctr();
574 case NID_aes_256_ecb:
575 *cipher = padlock_aes_256_ecb();
577 case NID_aes_256_cbc:
578 *cipher = padlock_aes_256_cbc();
580 case NID_aes_256_cfb:
581 *cipher = padlock_aes_256_cfb();
583 case NID_aes_256_ofb:
584 *cipher = padlock_aes_256_ofb();
586 case NID_aes_256_ctr:
587 *cipher = padlock_aes_256_ctr();
591 /* Sorry, we don't support this NID */
599 /* Prepare the encryption key for PadLock usage */
601 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
602 const unsigned char *iv, int enc)
604 struct padlock_cipher_data *cdata;
605 int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
606 unsigned long mode = EVP_CIPHER_CTX_mode(ctx);
609 return 0; /* ERROR */
611 cdata = ALIGNED_CIPHER_DATA(ctx);
612 memset(cdata, 0, sizeof(*cdata));
614 /* Prepare Control word. */
615 if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
616 cdata->cword.b.encdec = 0;
618 cdata->cword.b.encdec = (EVP_CIPHER_CTX_encrypting(ctx) == 0);
619 cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
620 cdata->cword.b.ksize = (key_len - 128) / 64;
625 * PadLock can generate an extended key for AES128 in hardware
627 memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
628 cdata->cword.b.keygen = 0;
634 * Generate an extended AES key in software. Needed for AES192/AES256
637 * Well, the above applies to Stepping 8 CPUs and is listed as
638 * hardware errata. They most likely will fix it at some point and
639 * then a check for stepping would be due here.
641 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
643 AES_set_decrypt_key(key, key_len, &cdata->ks);
645 AES_set_encrypt_key(key, key_len, &cdata->ks);
648 * OpenSSL C functions use byte-swapped extended key.
650 padlock_key_bswap(&cdata->ks);
652 cdata->cword.b.keygen = 1;
661 * This is done to cover for cases when user reuses the
662 * context for new key. The catch is that if we don't do
663 * this, padlock_eas_cipher might proceed with old key...
665 padlock_reload_key();
670 /* ===== Random Number Generator ===== */
672 * This code is not engaged. The reason is that it does not comply
673 * with recommendations for VIA RNG usage for secure applications
674 * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
675 * provide meaningful error control...
678 * Wrapper that provides an interface between the API and the raw PadLock
681 static int padlock_rand_bytes(unsigned char *output, int count)
683 unsigned int eax, buf;
686 eax = padlock_xstore(output, 0);
687 if (!(eax & (1 << 6)))
688 return 0; /* RNG disabled */
689 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
690 if (eax & (0x1F << 10))
692 if ((eax & 0x1F) == 0)
693 continue; /* no data, retry... */
694 if ((eax & 0x1F) != 8)
695 return 0; /* fatal failure... */
700 eax = padlock_xstore(&buf, 3);
701 if (!(eax & (1 << 6)))
702 return 0; /* RNG disabled */
703 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
704 if (eax & (0x1F << 10))
706 if ((eax & 0x1F) == 0)
707 continue; /* no data, retry... */
708 if ((eax & 0x1F) != 1)
709 return 0; /* fatal failure... */
710 *output++ = (unsigned char)buf;
713 OPENSSL_cleanse(&buf, sizeof(buf));
718 /* Dummy but necessary function */
719 static int padlock_rand_status(void)
724 /* Prepare structure for registration */
725 static RAND_METHOD padlock_rand = {
727 padlock_rand_bytes, /* bytes */
730 padlock_rand_bytes, /* pseudorand */
731 padlock_rand_status, /* rand status */
734 # endif /* COMPILE_HW_PADLOCK */
735 # endif /* !OPENSSL_NO_HW_PADLOCK */
736 #endif /* !OPENSSL_NO_HW */
738 #if defined(OPENSSL_NO_HW) || defined(OPENSSL_NO_HW_PADLOCK) \
739 || !defined(COMPILE_HW_PADLOCK)
740 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
742 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
744 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
749 IMPLEMENT_DYNAMIC_CHECK_FN()