Fix grammar in certificates.txt
[openssl.git] / engines / e_padlock.c
1 /*
2  * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
13  */
14 #define OPENSSL_SUPPRESS_DEPRECATED
15
16 #include <stdio.h>
17 #include <string.h>
18
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>
27
28 #ifndef OPENSSL_NO_PADLOCKENG
29
30 /*
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!
33  */
34
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);
40 #  endif
41 # endif
42
43 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
44 void engine_load_padlock_int(void);
45 void engine_load_padlock_int(void)
46 {
47 /* On non-x86 CPUs it just returns. */
48 #  ifdef COMPILE_PADLOCKENG
49     ENGINE *toadd = ENGINE_padlock();
50     if (!toadd)
51         return;
52     ENGINE_add(toadd);
53     ENGINE_free(toadd);
54     ERR_clear_error();
55 #  endif
56 }
57
58 # endif
59
60 # ifdef COMPILE_PADLOCKENG
61
62 /* Function for ENGINE detection and control */
63 static int padlock_available(void);
64 static int padlock_init(ENGINE *e);
65
66 /* RNG Stuff */
67 static RAND_METHOD padlock_rand;
68
69 /* Cipher Stuff */
70 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
71                            const int **nids, int nid);
72
73 /* Engine names */
74 static const char *padlock_id = "padlock";
75 static char padlock_name[100];
76
77 /* Available features */
78 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
79 static int padlock_use_rng = 0; /* Random Number Generator */
80
81 /* ===== Engine "management" functions ===== */
82
83 /* Prepare the ENGINE structure for registration */
84 static int padlock_bind_helper(ENGINE *e)
85 {
86     /* Check available features */
87     padlock_available();
88
89     /*
90      * RNG is currently disabled for reasons discussed in commentary just
91      * before padlock_rand_bytes function.
92      */
93     padlock_use_rng = 0;
94
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");
100
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))) {
107         return 0;
108     }
109
110     /* Everything looks good */
111     return 1;
112 }
113
114 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
115 /* Constructor */
116 static ENGINE *ENGINE_padlock(void)
117 {
118     ENGINE *eng = ENGINE_new();
119
120     if (eng == NULL) {
121         return NULL;
122     }
123
124     if (!padlock_bind_helper(eng)) {
125         ENGINE_free(eng);
126         return NULL;
127     }
128
129     return eng;
130 }
131 #  endif
132
133 /* Check availability of the engine */
134 static int padlock_init(ENGINE *e)
135 {
136     return (padlock_use_rng || padlock_use_ace);
137 }
138
139 /*
140  * This stuff is needed if this ENGINE is being compiled into a
141  * self-contained shared-library.
142  */
143 #  ifndef OPENSSL_NO_DYNAMIC_ENGINE
144 static int padlock_bind_fn(ENGINE *e, const char *id)
145 {
146     if (id && (strcmp(id, padlock_id) != 0)) {
147         return 0;
148     }
149
150     if (!padlock_bind_helper(e)) {
151         return 0;
152     }
153
154     return 1;
155 }
156
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 ===== */
161
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
167     /*
168      * Here we store the status information relevant to the current context.
169      */
170     /*
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,
173      * etc!
174      */
175 struct padlock_cipher_data {
176     unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
177     union {
178         unsigned int pad[4];
179         struct {
180             int rounds:4;
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;
185             int interm:1;
186             unsigned int encdec:1;
187             int ksize:2;
188         } b;
189     } cword;                    /* Control word */
190     AES_KEY ks;                 /* Encryption key */
191 };
192
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);
215
216 /*
217  * Load supported features of the CPU to see if the PadLock is available.
218  */
219 static int padlock_available(void)
220 {
221     unsigned int edx = padlock_capability();
222
223     /* Fill up some flags */
224     padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
225     padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
226
227     return padlock_use_ace + padlock_use_rng;
228 }
229
230 /* ===== AES encryption/decryption ===== */
231
232 #  if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
233 #   define NID_aes_128_cfb NID_aes_128_cfb128
234 #  endif
235
236 #  if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
237 #   define NID_aes_128_ofb NID_aes_128_ofb128
238 #  endif
239
240 #  if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
241 #   define NID_aes_192_cfb NID_aes_192_cfb128
242 #  endif
243
244 #  if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
245 #   define NID_aes_192_ofb NID_aes_192_ofb128
246 #  endif
247
248 #  if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
249 #   define NID_aes_256_cfb NID_aes_256_cfb128
250 #  endif
251
252 #  if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
253 #   define NID_aes_256_ofb NID_aes_256_ofb128
254 #  endif
255
256 /* List of supported ciphers. */
257 static const int padlock_cipher_nids[] = {
258     NID_aes_128_ecb,
259     NID_aes_128_cbc,
260     NID_aes_128_cfb,
261     NID_aes_128_ofb,
262     NID_aes_128_ctr,
263
264     NID_aes_192_ecb,
265     NID_aes_192_cbc,
266     NID_aes_192_cfb,
267     NID_aes_192_ofb,
268     NID_aes_192_ctr,
269
270     NID_aes_256_ecb,
271     NID_aes_256_cbc,
272     NID_aes_256_cfb,
273     NID_aes_256_ofb,
274     NID_aes_256_ctr
275 };
276
277 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
278                                       sizeof(padlock_cipher_nids[0]));
279
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);
283
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)))
288
289 static int
290 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
291                    const unsigned char *in_arg, size_t nbytes)
292 {
293     return padlock_ecb_encrypt(out_arg, in_arg,
294                                ALIGNED_CIPHER_DATA(ctx), nbytes);
295 }
296
297 static int
298 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
299                    const unsigned char *in_arg, size_t nbytes)
300 {
301     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
302     int ret;
303
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);
307     return ret;
308 }
309
310 static int
311 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
312                    const unsigned char *in_arg, size_t nbytes)
313 {
314     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
315     size_t chunk;
316
317     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
318         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
319
320         if (chunk >= AES_BLOCK_SIZE)
321             return 0;           /* bogus value */
322
323         if (EVP_CIPHER_CTX_encrypting(ctx))
324             while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
325                 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
326                 chunk++, nbytes--;
327         } else
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--;
332             }
333
334         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
335     }
336
337     if (nbytes == 0)
338         return 1;
339
340     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
341
342     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
343         if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
344             return 0;
345         nbytes -= chunk;
346     }
347
348     if (nbytes) {
349         unsigned char *ivp = cdata->iv;
350
351         out_arg += chunk;
352         in_arg += chunk;
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();
360             while (nbytes) {
361                 unsigned char c = *(in_arg++);
362                 *(out_arg++) = c ^ *ivp;
363                 *(ivp++) = c, nbytes--;
364             }
365         } else {
366             padlock_reload_key();
367             padlock_aes_block(ivp, ivp, cdata);
368             padlock_reload_key();
369             while (nbytes) {
370                 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
371                 ivp++, nbytes--;
372             }
373         }
374     }
375
376     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
377
378     return 1;
379 }
380
381 static int
382 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
383                    const unsigned char *in_arg, size_t nbytes)
384 {
385     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
386     size_t chunk;
387
388     /*
389      * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
390      */
391     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
392         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
393
394         if (chunk >= AES_BLOCK_SIZE)
395             return 0;           /* bogus value */
396
397         while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
398             *(out_arg++) = *(in_arg++) ^ ivp[chunk];
399             chunk++, nbytes--;
400         }
401
402         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
403     }
404
405     if (nbytes == 0)
406         return 1;
407
408     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
409
410     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
411         if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
412             return 0;
413         nbytes -= chunk;
414     }
415
416     if (nbytes) {
417         unsigned char *ivp = cdata->iv;
418
419         out_arg += chunk;
420         in_arg += chunk;
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 */
425         while (nbytes) {
426             *(out_arg++) = *(in_arg++) ^ *ivp;
427             ivp++, nbytes--;
428         }
429     }
430
431     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
432
433     return 1;
434 }
435
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)
440 {
441     memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
442     padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
443 }
444
445 static int
446 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
447                    const unsigned char *in_arg, size_t nbytes)
448 {
449     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
450     unsigned int num = EVP_CIPHER_CTX_num(ctx);
451
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);
456
457     EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
458     return 1;
459 }
460
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
466
467 /*
468  * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
469  * of preprocessor magic :-)
470  */
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) \
474 {                                                                       \
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, \
481                                               AES_BLOCK_SIZE)           \
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;                           \
496     }                                                                   \
497     return _hidden_aes_##ksize##_##lmode;                               \
498 }
499
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)
505
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)
511
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)
517
518 static int
519 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
520                 int nid)
521 {
522     /* No specific cipher => return a list of supported nids ... */
523     if (!cipher) {
524         *nids = padlock_cipher_nids;
525         return padlock_cipher_nids_num;
526     }
527
528     /* ... or the requested "cipher" otherwise */
529     switch (nid) {
530     case NID_aes_128_ecb:
531         *cipher = padlock_aes_128_ecb();
532         break;
533     case NID_aes_128_cbc:
534         *cipher = padlock_aes_128_cbc();
535         break;
536     case NID_aes_128_cfb:
537         *cipher = padlock_aes_128_cfb();
538         break;
539     case NID_aes_128_ofb:
540         *cipher = padlock_aes_128_ofb();
541         break;
542     case NID_aes_128_ctr:
543         *cipher = padlock_aes_128_ctr();
544         break;
545
546     case NID_aes_192_ecb:
547         *cipher = padlock_aes_192_ecb();
548         break;
549     case NID_aes_192_cbc:
550         *cipher = padlock_aes_192_cbc();
551         break;
552     case NID_aes_192_cfb:
553         *cipher = padlock_aes_192_cfb();
554         break;
555     case NID_aes_192_ofb:
556         *cipher = padlock_aes_192_ofb();
557         break;
558     case NID_aes_192_ctr:
559         *cipher = padlock_aes_192_ctr();
560         break;
561
562     case NID_aes_256_ecb:
563         *cipher = padlock_aes_256_ecb();
564         break;
565     case NID_aes_256_cbc:
566         *cipher = padlock_aes_256_cbc();
567         break;
568     case NID_aes_256_cfb:
569         *cipher = padlock_aes_256_cfb();
570         break;
571     case NID_aes_256_ofb:
572         *cipher = padlock_aes_256_ofb();
573         break;
574     case NID_aes_256_ctr:
575         *cipher = padlock_aes_256_ctr();
576         break;
577
578     default:
579         /* Sorry, we don't support this NID */
580         *cipher = NULL;
581         return 0;
582     }
583
584     return 1;
585 }
586
587 /* Prepare the encryption key for PadLock usage */
588 static int
589 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
590                      const unsigned char *iv, int enc)
591 {
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);
595
596     if (key == NULL)
597         return 0;               /* ERROR */
598
599     cdata = ALIGNED_CIPHER_DATA(ctx);
600     memset(cdata, 0, sizeof(*cdata));
601
602     /* Prepare Control word. */
603     if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
604         cdata->cword.b.encdec = 0;
605     else
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;
609
610     switch (key_len) {
611     case 128:
612         /*
613          * PadLock can generate an extended key for AES128 in hardware
614          */
615         memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
616         cdata->cword.b.keygen = 0;
617         break;
618
619     case 192:
620     case 256:
621         /*
622          * Generate an extended AES key in software. Needed for AES192/AES256
623          */
624         /*
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.
628          */
629         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
630             && !enc)
631             AES_set_decrypt_key(key, key_len, &cdata->ks);
632         else
633             AES_set_encrypt_key(key, key_len, &cdata->ks);
634 #  ifndef AES_ASM
635         /*
636          * OpenSSL C functions use byte-swapped extended key.
637          */
638         padlock_key_bswap(&cdata->ks);
639 #  endif
640         cdata->cword.b.keygen = 1;
641         break;
642
643     default:
644         /* ERROR */
645         return 0;
646     }
647
648     /*
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...
652      */
653     padlock_reload_key();
654
655     return 1;
656 }
657
658 /* ===== Random Number Generator ===== */
659 /*
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...
664  */
665 /*
666  * Wrapper that provides an interface between the API and the raw PadLock
667  * RNG
668  */
669 static int padlock_rand_bytes(unsigned char *output, int count)
670 {
671     unsigned int eax, buf;
672
673     while (count >= 8) {
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))
679             return 0;
680         if ((eax & 0x1F) == 0)
681             continue;           /* no data, retry... */
682         if ((eax & 0x1F) != 8)
683             return 0;           /* fatal failure...  */
684         output += 8;
685         count -= 8;
686     }
687     while (count > 0) {
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))
693             return 0;
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;
699         count--;
700     }
701     OPENSSL_cleanse(&buf, sizeof(buf));
702
703     return 1;
704 }
705
706 /* Dummy but necessary function */
707 static int padlock_rand_status(void)
708 {
709     return 1;
710 }
711
712 /* Prepare structure for registration */
713 static RAND_METHOD padlock_rand = {
714     NULL,                       /* seed */
715     padlock_rand_bytes,         /* bytes */
716     NULL,                       /* cleanup */
717     NULL,                       /* add */
718     padlock_rand_bytes,         /* pseudorand */
719     padlock_rand_status,        /* rand status */
720 };
721
722 # endif                        /* COMPILE_PADLOCKENG */
723 #endif                         /* !OPENSSL_NO_PADLOCKENG */
724
725 #if defined(OPENSSL_NO_PADLOCKENG) || !defined(COMPILE_PADLOCKENG)
726 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
727 OPENSSL_EXPORT
728     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
729 OPENSSL_EXPORT
730     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
731 {
732     return 0;
733 }
734
735 IMPLEMENT_DYNAMIC_CHECK_FN()
736 # endif
737 #endif