ARM64 assembly pack: make it Windows-friendly.
[openssl.git] / engines / e_padlock.c
1 /*
2  * Copyright 2004-2018 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 #include <stdio.h>
11 #include <string.h>
12
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>
21
22 #ifndef OPENSSL_NO_HW
23 # ifndef OPENSSL_NO_HW_PADLOCK
24
25 /*
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!
28  */
29
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);
35 #   endif
36 #  endif
37
38 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
39 void engine_load_padlock_int(void);
40 void engine_load_padlock_int(void)
41 {
42 /* On non-x86 CPUs it just returns. */
43 #   ifdef COMPILE_HW_PADLOCK
44     ENGINE *toadd = ENGINE_padlock();
45     if (!toadd)
46         return;
47     ENGINE_add(toadd);
48     ENGINE_free(toadd);
49     ERR_clear_error();
50 #   endif
51 }
52
53 #  endif
54
55 #  ifdef COMPILE_HW_PADLOCK
56
57 /* Function for ENGINE detection and control */
58 static int padlock_available(void);
59 static int padlock_init(ENGINE *e);
60
61 /* RNG Stuff */
62 static RAND_METHOD padlock_rand;
63
64 /* Cipher Stuff */
65 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
66                            const int **nids, int nid);
67
68 /* Engine names */
69 static const char *padlock_id = "padlock";
70 static char padlock_name[100];
71
72 /* Available features */
73 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
74 static int padlock_use_rng = 0; /* Random Number Generator */
75
76 /* ===== Engine "management" functions ===== */
77
78 /* Prepare the ENGINE structure for registration */
79 static int padlock_bind_helper(ENGINE *e)
80 {
81     /* Check available features */
82     padlock_available();
83
84     /*
85      * RNG is currently disabled for reasons discussed in commentary just
86      * before padlock_rand_bytes function.
87      */
88     padlock_use_rng = 0;
89
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");
95
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))) {
102         return 0;
103     }
104
105     /* Everything looks good */
106     return 1;
107 }
108
109 #   ifdef OPENSSL_NO_DYNAMIC_ENGINE
110 /* Constructor */
111 static ENGINE *ENGINE_padlock(void)
112 {
113     ENGINE *eng = ENGINE_new();
114
115     if (eng == NULL) {
116         return NULL;
117     }
118
119     if (!padlock_bind_helper(eng)) {
120         ENGINE_free(eng);
121         return NULL;
122     }
123
124     return eng;
125 }
126 #   endif
127
128 /* Check availability of the engine */
129 static int padlock_init(ENGINE *e)
130 {
131     return (padlock_use_rng || padlock_use_ace);
132 }
133
134 /*
135  * This stuff is needed if this ENGINE is being compiled into a
136  * self-contained shared-library.
137  */
138 #   ifdef DYNAMIC_ENGINE
139 static int padlock_bind_fn(ENGINE *e, const char *id)
140 {
141     if (id && (strcmp(id, padlock_id) != 0)) {
142         return 0;
143     }
144
145     if (!padlock_bind_helper(e)) {
146         return 0;
147     }
148
149     return 1;
150 }
151
152 IMPLEMENT_DYNAMIC_CHECK_FN()
153 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
154 #   endif                       /* DYNAMIC_ENGINE */
155 /* ===== Here comes the "real" engine ===== */
156
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
162     /*
163      * Here we store the status information relevant to the current context.
164      */
165     /*
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,
168      * etc!
169      */
170 struct padlock_cipher_data {
171     unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
172     union {
173         unsigned int pad[4];
174         struct {
175             int rounds:4;
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;
180             int interm:1;
181             unsigned int encdec:1;
182             int ksize:2;
183         } b;
184     } cword;                    /* Control word */
185     AES_KEY ks;                 /* Encryption key */
186 };
187
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);
210
211 /*
212  * Load supported features of the CPU to see if the PadLock is available.
213  */
214 static int padlock_available(void)
215 {
216     unsigned int edx = padlock_capability();
217
218     /* Fill up some flags */
219     padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
220     padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
221
222     return padlock_use_ace + padlock_use_rng;
223 }
224
225 /* ===== AES encryption/decryption ===== */
226
227 #   if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
228 #    define NID_aes_128_cfb NID_aes_128_cfb128
229 #   endif
230
231 #   if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
232 #    define NID_aes_128_ofb NID_aes_128_ofb128
233 #   endif
234
235 #   if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
236 #    define NID_aes_192_cfb NID_aes_192_cfb128
237 #   endif
238
239 #   if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
240 #    define NID_aes_192_ofb NID_aes_192_ofb128
241 #   endif
242
243 #   if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
244 #    define NID_aes_256_cfb NID_aes_256_cfb128
245 #   endif
246
247 #   if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
248 #    define NID_aes_256_ofb NID_aes_256_ofb128
249 #   endif
250
251 /* List of supported ciphers. */
252 static const int padlock_cipher_nids[] = {
253     NID_aes_128_ecb,
254     NID_aes_128_cbc,
255     NID_aes_128_cfb,
256     NID_aes_128_ofb,
257     NID_aes_128_ctr,
258
259     NID_aes_192_ecb,
260     NID_aes_192_cbc,
261     NID_aes_192_cfb,
262     NID_aes_192_ofb,
263     NID_aes_192_ctr,
264
265     NID_aes_256_ecb,
266     NID_aes_256_cbc,
267     NID_aes_256_cfb,
268     NID_aes_256_ofb,
269     NID_aes_256_ctr
270 };
271
272 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
273                                       sizeof(padlock_cipher_nids[0]));
274
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);
278
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)))
283
284 static int
285 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
286                    const unsigned char *in_arg, size_t nbytes)
287 {
288     return padlock_ecb_encrypt(out_arg, in_arg,
289                                ALIGNED_CIPHER_DATA(ctx), nbytes);
290 }
291
292 static int
293 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
294                    const unsigned char *in_arg, size_t nbytes)
295 {
296     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
297     int ret;
298
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);
302     return ret;
303 }
304
305 static int
306 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
307                    const unsigned char *in_arg, size_t nbytes)
308 {
309     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
310     size_t chunk;
311
312     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
313         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
314
315         if (chunk >= AES_BLOCK_SIZE)
316             return 0;           /* bogus value */
317
318         if (EVP_CIPHER_CTX_encrypting(ctx))
319             while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
320                 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
321                 chunk++, nbytes--;
322         } else
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--;
327             }
328
329         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
330     }
331
332     if (nbytes == 0)
333         return 1;
334
335     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
336
337     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
338         if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
339             return 0;
340         nbytes -= chunk;
341     }
342
343     if (nbytes) {
344         unsigned char *ivp = cdata->iv;
345
346         out_arg += chunk;
347         in_arg += chunk;
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();
355             while (nbytes) {
356                 unsigned char c = *(in_arg++);
357                 *(out_arg++) = c ^ *ivp;
358                 *(ivp++) = c, nbytes--;
359             }
360         } else {
361             padlock_reload_key();
362             padlock_aes_block(ivp, ivp, cdata);
363             padlock_reload_key();
364             while (nbytes) {
365                 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
366                 ivp++, nbytes--;
367             }
368         }
369     }
370
371     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
372
373     return 1;
374 }
375
376 static int
377 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
378                    const unsigned char *in_arg, size_t nbytes)
379 {
380     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
381     size_t chunk;
382
383     /*
384      * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
385      */
386     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
387         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
388
389         if (chunk >= AES_BLOCK_SIZE)
390             return 0;           /* bogus value */
391
392         while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
393             *(out_arg++) = *(in_arg++) ^ ivp[chunk];
394             chunk++, nbytes--;
395         }
396
397         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
398     }
399
400     if (nbytes == 0)
401         return 1;
402
403     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
404
405     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
406         if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
407             return 0;
408         nbytes -= chunk;
409     }
410
411     if (nbytes) {
412         unsigned char *ivp = cdata->iv;
413
414         out_arg += chunk;
415         in_arg += chunk;
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 */
420         while (nbytes) {
421             *(out_arg++) = *(in_arg++) ^ *ivp;
422             ivp++, nbytes--;
423         }
424     }
425
426     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
427
428     return 1;
429 }
430
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)
435 {
436     memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
437     padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
438 }
439
440 static int
441 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
442                    const unsigned char *in_arg, size_t nbytes)
443 {
444     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
445     unsigned int num = EVP_CIPHER_CTX_num(ctx);
446
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);
451
452     EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
453     return 1;
454 }
455
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
461
462 /*
463  * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
464  * of preprocessor magic :-)
465  */
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) \
469 {                                                                       \
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, \
476                                               AES_BLOCK_SIZE)           \
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;                           \
491     }                                                                   \
492     return _hidden_aes_##ksize##_##lmode;                               \
493 }
494
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)
500
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)
506
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)
512
513 static int
514 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
515                 int nid)
516 {
517     /* No specific cipher => return a list of supported nids ... */
518     if (!cipher) {
519         *nids = padlock_cipher_nids;
520         return padlock_cipher_nids_num;
521     }
522
523     /* ... or the requested "cipher" otherwise */
524     switch (nid) {
525     case NID_aes_128_ecb:
526         *cipher = padlock_aes_128_ecb();
527         break;
528     case NID_aes_128_cbc:
529         *cipher = padlock_aes_128_cbc();
530         break;
531     case NID_aes_128_cfb:
532         *cipher = padlock_aes_128_cfb();
533         break;
534     case NID_aes_128_ofb:
535         *cipher = padlock_aes_128_ofb();
536         break;
537     case NID_aes_128_ctr:
538         *cipher = padlock_aes_128_ctr();
539         break;
540
541     case NID_aes_192_ecb:
542         *cipher = padlock_aes_192_ecb();
543         break;
544     case NID_aes_192_cbc:
545         *cipher = padlock_aes_192_cbc();
546         break;
547     case NID_aes_192_cfb:
548         *cipher = padlock_aes_192_cfb();
549         break;
550     case NID_aes_192_ofb:
551         *cipher = padlock_aes_192_ofb();
552         break;
553     case NID_aes_192_ctr:
554         *cipher = padlock_aes_192_ctr();
555         break;
556
557     case NID_aes_256_ecb:
558         *cipher = padlock_aes_256_ecb();
559         break;
560     case NID_aes_256_cbc:
561         *cipher = padlock_aes_256_cbc();
562         break;
563     case NID_aes_256_cfb:
564         *cipher = padlock_aes_256_cfb();
565         break;
566     case NID_aes_256_ofb:
567         *cipher = padlock_aes_256_ofb();
568         break;
569     case NID_aes_256_ctr:
570         *cipher = padlock_aes_256_ctr();
571         break;
572
573     default:
574         /* Sorry, we don't support this NID */
575         *cipher = NULL;
576         return 0;
577     }
578
579     return 1;
580 }
581
582 /* Prepare the encryption key for PadLock usage */
583 static int
584 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
585                      const unsigned char *iv, int enc)
586 {
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);
590
591     if (key == NULL)
592         return 0;               /* ERROR */
593
594     cdata = ALIGNED_CIPHER_DATA(ctx);
595     memset(cdata, 0, sizeof(*cdata));
596
597     /* Prepare Control word. */
598     if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
599         cdata->cword.b.encdec = 0;
600     else
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;
604
605     switch (key_len) {
606     case 128:
607         /*
608          * PadLock can generate an extended key for AES128 in hardware
609          */
610         memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
611         cdata->cword.b.keygen = 0;
612         break;
613
614     case 192:
615     case 256:
616         /*
617          * Generate an extended AES key in software. Needed for AES192/AES256
618          */
619         /*
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.
623          */
624         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
625             && !enc)
626             AES_set_decrypt_key(key, key_len, &cdata->ks);
627         else
628             AES_set_encrypt_key(key, key_len, &cdata->ks);
629 #   ifndef AES_ASM
630         /*
631          * OpenSSL C functions use byte-swapped extended key.
632          */
633         padlock_key_bswap(&cdata->ks);
634 #   endif
635         cdata->cword.b.keygen = 1;
636         break;
637
638     default:
639         /* ERROR */
640         return 0;
641     }
642
643     /*
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...
647      */
648     padlock_reload_key();
649
650     return 1;
651 }
652
653 /* ===== Random Number Generator ===== */
654 /*
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...
659  */
660 /*
661  * Wrapper that provides an interface between the API and the raw PadLock
662  * RNG
663  */
664 static int padlock_rand_bytes(unsigned char *output, int count)
665 {
666     unsigned int eax, buf;
667
668     while (count >= 8) {
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))
674             return 0;
675         if ((eax & 0x1F) == 0)
676             continue;           /* no data, retry... */
677         if ((eax & 0x1F) != 8)
678             return 0;           /* fatal failure...  */
679         output += 8;
680         count -= 8;
681     }
682     while (count > 0) {
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))
688             return 0;
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;
694         count--;
695     }
696     OPENSSL_cleanse(&buf, sizeof(buf));
697
698     return 1;
699 }
700
701 /* Dummy but necessary function */
702 static int padlock_rand_status(void)
703 {
704     return 1;
705 }
706
707 /* Prepare structure for registration */
708 static RAND_METHOD padlock_rand = {
709     NULL,                       /* seed */
710     padlock_rand_bytes,         /* bytes */
711     NULL,                       /* cleanup */
712     NULL,                       /* add */
713     padlock_rand_bytes,         /* pseudorand */
714     padlock_rand_status,        /* rand status */
715 };
716
717 #  endif                        /* COMPILE_HW_PADLOCK */
718 # endif                         /* !OPENSSL_NO_HW_PADLOCK */
719 #endif                          /* !OPENSSL_NO_HW */
720
721 #if defined(OPENSSL_NO_HW) || defined(OPENSSL_NO_HW_PADLOCK) \
722         || !defined(COMPILE_HW_PADLOCK)
723 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
724 OPENSSL_EXPORT
725     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
726 OPENSSL_EXPORT
727     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
728 {
729     return 0;
730 }
731
732 IMPLEMENT_DYNAMIC_CHECK_FN()
733 # endif
734 #endif