2 * Copyright 2004-2021 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 and engine functions (which are deprecated
12 * for 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();
55 * If the "add" worked, it gets a structural reference. So either way, we
56 * release our just-created reference.
60 * If the "add" didn't work, it was probably a conflict because it was
61 * already added (eg. someone calling ENGINE_load_blah then calling
62 * ENGINE_load_builtin_engines() perhaps).
70 # ifdef COMPILE_PADLOCKENG
72 /* Function for ENGINE detection and control */
73 static int padlock_available(void);
74 static int padlock_init(ENGINE *e);
77 static RAND_METHOD padlock_rand;
80 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
81 const int **nids, int nid);
84 static const char *padlock_id = "padlock";
85 static char padlock_name[100];
87 /* Available features */
88 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
89 static int padlock_use_rng = 0; /* Random Number Generator */
91 /* ===== Engine "management" functions ===== */
93 /* Prepare the ENGINE structure for registration */
94 static int padlock_bind_helper(ENGINE *e)
96 /* Check available features */
100 * RNG is currently disabled for reasons discussed in commentary just
101 * before padlock_rand_bytes function.
105 /* Generate a nice engine name with available features */
106 BIO_snprintf(padlock_name, sizeof(padlock_name),
107 "VIA PadLock (%s, %s)",
108 padlock_use_rng ? "RNG" : "no-RNG",
109 padlock_use_ace ? "ACE" : "no-ACE");
111 /* Register everything or return with an error */
112 if (!ENGINE_set_id(e, padlock_id) ||
113 !ENGINE_set_name(e, padlock_name) ||
114 !ENGINE_set_init_function(e, padlock_init) ||
115 (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
116 (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
120 /* Everything looks good */
124 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
126 static ENGINE *ENGINE_padlock(void)
128 ENGINE *eng = ENGINE_new();
134 if (!padlock_bind_helper(eng)) {
143 /* Check availability of the engine */
144 static int padlock_init(ENGINE *e)
146 return (padlock_use_rng || padlock_use_ace);
150 * This stuff is needed if this ENGINE is being compiled into a
151 * self-contained shared-library.
153 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
154 static int padlock_bind_fn(ENGINE *e, const char *id)
156 if (id && (strcmp(id, padlock_id) != 0)) {
160 if (!padlock_bind_helper(e)) {
167 IMPLEMENT_DYNAMIC_CHECK_FN()
168 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
169 # endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
170 /* ===== Here comes the "real" engine ===== */
172 /* Some AES-related constants */
173 # define AES_BLOCK_SIZE 16
174 # define AES_KEY_SIZE_128 16
175 # define AES_KEY_SIZE_192 24
176 # define AES_KEY_SIZE_256 32
178 * Here we store the status information relevant to the current context.
181 * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
182 * the order of items in this structure. Don't blindly modify, reorder,
185 struct padlock_cipher_data {
186 unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
191 int dgst:1; /* n/a in C3 */
192 int align:1; /* n/a in C3 */
193 int ciphr:1; /* n/a in C3 */
194 unsigned int keygen:1;
196 unsigned int encdec:1;
199 } cword; /* Control word */
200 AES_KEY ks; /* Encryption key */
203 /* Interface to assembler module */
204 unsigned int padlock_capability(void);
205 void padlock_key_bswap(AES_KEY *key);
206 void padlock_verify_context(struct padlock_cipher_data *ctx);
207 void padlock_reload_key(void);
208 void padlock_aes_block(void *out, const void *inp,
209 struct padlock_cipher_data *ctx);
210 int padlock_ecb_encrypt(void *out, const void *inp,
211 struct padlock_cipher_data *ctx, size_t len);
212 int padlock_cbc_encrypt(void *out, const void *inp,
213 struct padlock_cipher_data *ctx, size_t len);
214 int padlock_cfb_encrypt(void *out, const void *inp,
215 struct padlock_cipher_data *ctx, size_t len);
216 int padlock_ofb_encrypt(void *out, const void *inp,
217 struct padlock_cipher_data *ctx, size_t len);
218 int padlock_ctr32_encrypt(void *out, const void *inp,
219 struct padlock_cipher_data *ctx, size_t len);
220 int padlock_xstore(void *out, int edx);
221 void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
222 void padlock_sha1(void *ctx, const void *inp, size_t len);
223 void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
224 void padlock_sha256(void *ctx, const void *inp, size_t len);
227 * Load supported features of the CPU to see if the PadLock is available.
229 static int padlock_available(void)
231 unsigned int edx = padlock_capability();
233 /* Fill up some flags */
234 padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
235 padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
237 return padlock_use_ace + padlock_use_rng;
240 /* ===== AES encryption/decryption ===== */
242 # if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
243 # define NID_aes_128_cfb NID_aes_128_cfb128
246 # if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
247 # define NID_aes_128_ofb NID_aes_128_ofb128
250 # if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
251 # define NID_aes_192_cfb NID_aes_192_cfb128
254 # if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
255 # define NID_aes_192_ofb NID_aes_192_ofb128
258 # if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
259 # define NID_aes_256_cfb NID_aes_256_cfb128
262 # if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
263 # define NID_aes_256_ofb NID_aes_256_ofb128
266 /* List of supported ciphers. */
267 static const int padlock_cipher_nids[] = {
287 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
288 sizeof(padlock_cipher_nids[0]));
290 /* Function prototypes ... */
291 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
292 const unsigned char *iv, int enc);
294 # define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
295 ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
296 # define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
297 NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
300 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
301 const unsigned char *in_arg, size_t nbytes)
303 return padlock_ecb_encrypt(out_arg, in_arg,
304 ALIGNED_CIPHER_DATA(ctx), nbytes);
308 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
309 const unsigned char *in_arg, size_t nbytes)
311 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
314 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
315 if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
316 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
321 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
322 const unsigned char *in_arg, size_t nbytes)
324 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
327 if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */
328 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
330 if (chunk >= AES_BLOCK_SIZE)
331 return 0; /* bogus value */
333 if (EVP_CIPHER_CTX_is_encrypting(ctx))
334 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
335 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
338 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
339 unsigned char c = *(in_arg++);
340 *(out_arg++) = c ^ ivp[chunk];
341 ivp[chunk++] = c, nbytes--;
344 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
350 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
352 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
353 if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
359 unsigned char *ivp = cdata->iv;
363 EVP_CIPHER_CTX_set_num(ctx, nbytes);
364 if (cdata->cword.b.encdec) {
365 cdata->cword.b.encdec = 0;
366 padlock_reload_key();
367 padlock_aes_block(ivp, ivp, cdata);
368 cdata->cword.b.encdec = 1;
369 padlock_reload_key();
371 unsigned char c = *(in_arg++);
372 *(out_arg++) = c ^ *ivp;
373 *(ivp++) = c, nbytes--;
376 padlock_reload_key();
377 padlock_aes_block(ivp, ivp, cdata);
378 padlock_reload_key();
380 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
386 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
392 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
393 const unsigned char *in_arg, size_t nbytes)
395 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
399 * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
401 if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */
402 unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
404 if (chunk >= AES_BLOCK_SIZE)
405 return 0; /* bogus value */
407 while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
408 *(out_arg++) = *(in_arg++) ^ ivp[chunk];
412 EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
418 memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
420 if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
421 if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
427 unsigned char *ivp = cdata->iv;
431 EVP_CIPHER_CTX_set_num(ctx, nbytes);
432 padlock_reload_key(); /* empirically found */
433 padlock_aes_block(ivp, ivp, cdata);
434 padlock_reload_key(); /* empirically found */
436 *(out_arg++) = *(in_arg++) ^ *ivp;
441 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
446 static void padlock_ctr32_encrypt_glue(const unsigned char *in,
447 unsigned char *out, size_t blocks,
448 struct padlock_cipher_data *ctx,
449 const unsigned char *ivec)
451 memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
452 padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
456 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
457 const unsigned char *in_arg, size_t nbytes)
459 struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
460 int n = EVP_CIPHER_CTX_get_num(ctx);
465 num = (unsigned int)n;
467 CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
468 cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
469 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
470 (ctr128_f) padlock_ctr32_encrypt_glue);
472 EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
476 # define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
477 # define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
478 # define EVP_CIPHER_block_size_OFB 1
479 # define EVP_CIPHER_block_size_CFB 1
480 # define EVP_CIPHER_block_size_CTR 1
483 * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
484 * of preprocessor magic :-)
486 # define DECLARE_AES_EVP(ksize,lmode,umode) \
487 static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
488 static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
490 if (_hidden_aes_##ksize##_##lmode == NULL \
491 && ((_hidden_aes_##ksize##_##lmode = \
492 EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
493 EVP_CIPHER_block_size_##umode, \
494 AES_KEY_SIZE_##ksize)) == NULL \
495 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
497 || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
498 0 | EVP_CIPH_##umode##_MODE) \
499 || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
500 padlock_aes_init_key) \
501 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
502 padlock_##lmode##_cipher) \
503 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
504 sizeof(struct padlock_cipher_data) + 16) \
505 || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
506 EVP_CIPHER_set_asn1_iv) \
507 || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
508 EVP_CIPHER_get_asn1_iv))) { \
509 EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \
510 _hidden_aes_##ksize##_##lmode = NULL; \
512 return _hidden_aes_##ksize##_##lmode; \
515 DECLARE_AES_EVP(128, ecb, ECB)
516 DECLARE_AES_EVP(128, cbc, CBC)
517 DECLARE_AES_EVP(128, cfb, CFB)
518 DECLARE_AES_EVP(128, ofb, OFB)
519 DECLARE_AES_EVP(128, ctr, CTR)
521 DECLARE_AES_EVP(192, ecb, ECB)
522 DECLARE_AES_EVP(192, cbc, CBC)
523 DECLARE_AES_EVP(192, cfb, CFB)
524 DECLARE_AES_EVP(192, ofb, OFB)
525 DECLARE_AES_EVP(192, ctr, CTR)
527 DECLARE_AES_EVP(256, ecb, ECB)
528 DECLARE_AES_EVP(256, cbc, CBC)
529 DECLARE_AES_EVP(256, cfb, CFB)
530 DECLARE_AES_EVP(256, ofb, OFB)
531 DECLARE_AES_EVP(256, ctr, CTR)
534 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
537 /* No specific cipher => return a list of supported nids ... */
539 *nids = padlock_cipher_nids;
540 return padlock_cipher_nids_num;
543 /* ... or the requested "cipher" otherwise */
545 case NID_aes_128_ecb:
546 *cipher = padlock_aes_128_ecb();
548 case NID_aes_128_cbc:
549 *cipher = padlock_aes_128_cbc();
551 case NID_aes_128_cfb:
552 *cipher = padlock_aes_128_cfb();
554 case NID_aes_128_ofb:
555 *cipher = padlock_aes_128_ofb();
557 case NID_aes_128_ctr:
558 *cipher = padlock_aes_128_ctr();
561 case NID_aes_192_ecb:
562 *cipher = padlock_aes_192_ecb();
564 case NID_aes_192_cbc:
565 *cipher = padlock_aes_192_cbc();
567 case NID_aes_192_cfb:
568 *cipher = padlock_aes_192_cfb();
570 case NID_aes_192_ofb:
571 *cipher = padlock_aes_192_ofb();
573 case NID_aes_192_ctr:
574 *cipher = padlock_aes_192_ctr();
577 case NID_aes_256_ecb:
578 *cipher = padlock_aes_256_ecb();
580 case NID_aes_256_cbc:
581 *cipher = padlock_aes_256_cbc();
583 case NID_aes_256_cfb:
584 *cipher = padlock_aes_256_cfb();
586 case NID_aes_256_ofb:
587 *cipher = padlock_aes_256_ofb();
589 case NID_aes_256_ctr:
590 *cipher = padlock_aes_256_ctr();
594 /* Sorry, we don't support this NID */
602 /* Prepare the encryption key for PadLock usage */
604 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
605 const unsigned char *iv, int enc)
607 struct padlock_cipher_data *cdata;
608 int key_len = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
609 unsigned long mode = EVP_CIPHER_CTX_get_mode(ctx);
612 return 0; /* ERROR */
614 cdata = ALIGNED_CIPHER_DATA(ctx);
615 memset(cdata, 0, sizeof(*cdata));
617 /* Prepare Control word. */
618 if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
619 cdata->cword.b.encdec = 0;
621 cdata->cword.b.encdec = (EVP_CIPHER_CTX_is_encrypting(ctx) == 0);
622 cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
623 cdata->cword.b.ksize = (key_len - 128) / 64;
628 * PadLock can generate an extended key for AES128 in hardware
630 memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
631 cdata->cword.b.keygen = 0;
637 * Generate an extended AES key in software. Needed for AES192/AES256
640 * Well, the above applies to Stepping 8 CPUs and is listed as
641 * hardware errata. They most likely will fix it at some point and
642 * then a check for stepping would be due here.
644 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
646 AES_set_decrypt_key(key, key_len, &cdata->ks);
648 AES_set_encrypt_key(key, key_len, &cdata->ks);
651 * OpenSSL C functions use byte-swapped extended key.
653 padlock_key_bswap(&cdata->ks);
655 cdata->cword.b.keygen = 1;
664 * This is done to cover for cases when user reuses the
665 * context for new key. The catch is that if we don't do
666 * this, padlock_eas_cipher might proceed with old key...
668 padlock_reload_key();
673 /* ===== Random Number Generator ===== */
675 * This code is not engaged. The reason is that it does not comply
676 * with recommendations for VIA RNG usage for secure applications
677 * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
678 * provide meaningful error control...
681 * Wrapper that provides an interface between the API and the raw PadLock
684 static int padlock_rand_bytes(unsigned char *output, int count)
686 unsigned int eax, buf;
689 eax = padlock_xstore(output, 0);
690 if (!(eax & (1 << 6)))
691 return 0; /* RNG disabled */
692 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
693 if (eax & (0x1F << 10))
695 if ((eax & 0x1F) == 0)
696 continue; /* no data, retry... */
697 if ((eax & 0x1F) != 8)
698 return 0; /* fatal failure... */
703 eax = padlock_xstore(&buf, 3);
704 if (!(eax & (1 << 6)))
705 return 0; /* RNG disabled */
706 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
707 if (eax & (0x1F << 10))
709 if ((eax & 0x1F) == 0)
710 continue; /* no data, retry... */
711 if ((eax & 0x1F) != 1)
712 return 0; /* fatal failure... */
713 *output++ = (unsigned char)buf;
716 OPENSSL_cleanse(&buf, sizeof(buf));
721 /* Dummy but necessary function */
722 static int padlock_rand_status(void)
727 /* Prepare structure for registration */
728 static RAND_METHOD padlock_rand = {
730 padlock_rand_bytes, /* bytes */
733 padlock_rand_bytes, /* pseudorand */
734 padlock_rand_status, /* rand status */
737 # endif /* COMPILE_PADLOCKENG */
738 #endif /* !OPENSSL_NO_PADLOCKENG */
740 #if defined(OPENSSL_NO_PADLOCKENG) || !defined(COMPILE_PADLOCKENG)
741 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
743 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
745 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
750 IMPLEMENT_DYNAMIC_CHECK_FN()