e_padlock-x86_64.pl: fix typo.
[openssl.git] / engines / e_padlock.c
1 /* 
2  * Support for VIA PadLock Advanced Cryptography Engine (ACE)
3  * Written by Michal Ludvig <michal@logix.cz>
4  *            http://www.logix.cz/michal
5  *
6  * Big thanks to Andy Polyakov for a help with optimization, 
7  * assembler fixes, port to MS Windows and a lot of other 
8  * valuable work on this engine!
9  */
10
11 /* ====================================================================
12  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in
23  *    the documentation and/or other materials provided with the
24  *    distribution.
25  *
26  * 3. All advertising materials mentioning features or use of this
27  *    software must display the following acknowledgment:
28  *    "This product includes software developed by the OpenSSL Project
29  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
30  *
31  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
32  *    endorse or promote products derived from this software without
33  *    prior written permission. For written permission, please contact
34  *    licensing@OpenSSL.org.
35  *
36  * 5. Products derived from this software may not be called "OpenSSL"
37  *    nor may "OpenSSL" appear in their names without prior written
38  *    permission of the OpenSSL Project.
39  *
40  * 6. Redistributions of any form whatsoever must retain the following
41  *    acknowledgment:
42  *    "This product includes software developed by the OpenSSL Project
43  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
46  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
49  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56  * OF THE POSSIBILITY OF SUCH DAMAGE.
57  * ====================================================================
58  *
59  * This product includes cryptographic software written by Eric Young
60  * (eay@cryptsoft.com).  This product includes software written by Tim
61  * Hudson (tjh@cryptsoft.com).
62  *
63  */
64
65
66 #include <stdio.h>
67 #include <string.h>
68
69 #include <openssl/opensslconf.h>
70 #include <openssl/crypto.h>
71 #include <openssl/dso.h>
72 #include <openssl/engine.h>
73 #include <openssl/evp.h>
74 #ifndef OPENSSL_NO_AES
75 #include <openssl/aes.h>
76 #endif
77 #include <openssl/rand.h>
78 #include <openssl/err.h>
79
80 #ifndef OPENSSL_NO_HW
81 #ifndef OPENSSL_NO_HW_PADLOCK
82
83 /* Attempt to have a single source for both 0.9.7 and 0.9.8 :-) */
84 #if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
85 #  ifndef OPENSSL_NO_DYNAMIC_ENGINE
86 #    define DYNAMIC_ENGINE
87 #  endif
88 #elif (OPENSSL_VERSION_NUMBER >= 0x00907000L)
89 #  ifdef ENGINE_DYNAMIC_SUPPORT
90 #    define DYNAMIC_ENGINE
91 #  endif
92 #else
93 #  error "Only OpenSSL >= 0.9.7 is supported"
94 #endif
95
96 /* VIA PadLock AES is available *ONLY* on some x86 CPUs.
97    Not only that it doesn't exist elsewhere, but it
98    even can't be compiled on other platforms! */
99  
100 #undef COMPILE_HW_PADLOCK
101 #if !defined(I386_ONLY) && !defined(OPENSSL_NO_ASM)
102 # if    defined(__i386__) || defined(__i386) || \
103         defined(__x86_64__) || defined(__x86_64) || \
104         defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
105         defined(__INTEL__)
106 #  define COMPILE_HW_PADLOCK
107 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
108 static ENGINE *ENGINE_padlock (void);
109 #  endif
110 # endif
111 #endif
112
113 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
114
115 void ENGINE_load_padlock (void)
116 {
117 /* On non-x86 CPUs it just returns. */
118 #ifdef COMPILE_HW_PADLOCK
119         ENGINE *toadd = ENGINE_padlock ();
120         if (!toadd) return;
121         ENGINE_add (toadd);
122         ENGINE_free (toadd);
123         ERR_clear_error ();
124 #endif
125 }
126
127 #endif
128
129 #ifdef COMPILE_HW_PADLOCK
130
131 /* Function for ENGINE detection and control */
132 static int padlock_available(void);
133 static int padlock_init(ENGINE *e);
134
135 /* RNG Stuff */
136 static RAND_METHOD padlock_rand;
137
138 /* Cipher Stuff */
139 #ifndef OPENSSL_NO_AES
140 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid);
141 #endif
142
143 /* Engine names */
144 static const char *padlock_id = "padlock";
145 static char padlock_name[100];
146
147 /* Available features */
148 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
149 static int padlock_use_rng = 0; /* Random Number Generator */
150
151 /* ===== Engine "management" functions ===== */
152
153 /* Prepare the ENGINE structure for registration */
154 static int
155 padlock_bind_helper(ENGINE *e)
156 {
157         /* Check available features */
158         padlock_available();
159
160 #if 1   /* disable RNG for now, see commentary in vicinity of RNG code */
161         padlock_use_rng=0;
162 #endif
163
164         /* Generate a nice engine name with available features */
165         BIO_snprintf(padlock_name, sizeof(padlock_name),
166                 "VIA PadLock (%s, %s)", 
167                  padlock_use_rng ? "RNG" : "no-RNG",
168                  padlock_use_ace ? "ACE" : "no-ACE");
169
170         /* Register everything or return with an error */ 
171         if (!ENGINE_set_id(e, padlock_id) ||
172             !ENGINE_set_name(e, padlock_name) ||
173
174             !ENGINE_set_init_function(e, padlock_init) ||
175 #ifndef OPENSSL_NO_AES
176             (padlock_use_ace && !ENGINE_set_ciphers (e, padlock_ciphers)) ||
177 #endif
178             (padlock_use_rng && !ENGINE_set_RAND (e, &padlock_rand))) {
179                 return 0;
180         }
181
182         /* Everything looks good */
183         return 1;
184 }
185
186 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
187 /* Constructor */
188 static ENGINE *
189 ENGINE_padlock(void)
190 {
191         ENGINE *eng = ENGINE_new();
192
193         if (!eng) {
194                 return NULL;
195         }
196
197         if (!padlock_bind_helper(eng)) {
198                 ENGINE_free(eng);
199                 return NULL;
200         }
201
202         return eng;
203 }
204 #endif
205
206 /* Check availability of the engine */
207 static int
208 padlock_init(ENGINE *e)
209 {
210         return (padlock_use_rng || padlock_use_ace);
211 }
212
213 /* This stuff is needed if this ENGINE is being compiled into a self-contained
214  * shared-library.
215  */
216 #ifdef DYNAMIC_ENGINE
217 static int
218 padlock_bind_fn(ENGINE *e, const char *id)
219 {
220         if (id && (strcmp(id, padlock_id) != 0)) {
221                 return 0;
222         }
223
224         if (!padlock_bind_helper(e))  {
225                 return 0;
226         }
227
228         return 1;
229 }
230
231 IMPLEMENT_DYNAMIC_CHECK_FN()
232 IMPLEMENT_DYNAMIC_BIND_FN (padlock_bind_fn)
233 #endif /* DYNAMIC_ENGINE */
234
235 /* ===== Here comes the "real" engine ===== */
236
237 #ifndef OPENSSL_NO_AES
238 /* Some AES-related constants */
239 #define AES_BLOCK_SIZE          16
240 #define AES_KEY_SIZE_128        16
241 #define AES_KEY_SIZE_192        24
242 #define AES_KEY_SIZE_256        32
243
244 /* Here we store the status information relevant to the 
245    current context. */
246 /* BIG FAT WARNING:
247  *      Inline assembler in PADLOCK_XCRYPT_ASM()
248  *      depends on the order of items in this structure.
249  *      Don't blindly modify, reorder, etc!
250  */
251 struct padlock_cipher_data
252 {
253         unsigned char iv[AES_BLOCK_SIZE];       /* Initialization vector */
254         union { unsigned int pad[4];
255                 struct {
256                         int rounds:4;
257                         int dgst:1;     /* n/a in C3 */
258                         int align:1;    /* n/a in C3 */
259                         int ciphr:1;    /* n/a in C3 */
260                         unsigned int keygen:1;
261                         int interm:1;
262                         unsigned int encdec:1;
263                         int ksize:2;
264                 } b;
265         } cword;                /* Control word */
266         AES_KEY ks;             /* Encryption key */
267 };
268 #endif
269
270 /* Interface to assembler module */
271 unsigned int padlock_capability();
272 void padlock_key_bswap(AES_KEY *key);
273 void padlock_verify_context(struct padlock_cipher_data *ctx);
274 void padlock_reload_key();
275 void padlock_aes_block(void *out, const void *inp,
276                 struct padlock_cipher_data *ctx);
277 int  padlock_ecb_encrypt(void *out, const void *inp,
278                 struct padlock_cipher_data *ctx, size_t len);
279 int  padlock_cbc_encrypt(void *out, const void *inp,
280                 struct padlock_cipher_data *ctx, size_t len);
281 int  padlock_cfb_encrypt(void *out, const void *inp,
282                 struct padlock_cipher_data *ctx, size_t len);
283 int  padlock_ofb_encrypt(void *out, const void *inp,
284                 struct padlock_cipher_data *ctx, size_t len);
285 int  padlock_ctr32_encrypt(void *out, const void *inp,
286                 struct padlock_cipher_data *ctx, size_t len);
287 int  padlock_xstore(void *out,int edx);
288 void padlock_sha1_oneshot(void *ctx,const void *inp,size_t len);
289 void padlock_sha1(void *ctx,const void *inp,size_t len);
290 void padlock_sha256_oneshot(void *ctx,const void *inp,size_t len);
291 void padlock_sha256(void *ctx,const void *inp,size_t len);
292
293 /* Load supported features of the CPU to see if
294    the PadLock is available. */
295 static int
296 padlock_available(void)
297 {
298         unsigned int edx = padlock_capability();
299
300         /* Fill up some flags */
301         padlock_use_ace = ((edx & (0x3<<6)) == (0x3<<6));
302         padlock_use_rng = ((edx & (0x3<<2)) == (0x3<<2));
303
304         return padlock_use_ace + padlock_use_rng;
305 }
306
307 /* ===== AES encryption/decryption ===== */
308 #ifndef OPENSSL_NO_AES
309
310 #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
311 #define NID_aes_128_cfb NID_aes_128_cfb128
312 #endif
313
314 #if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
315 #define NID_aes_128_ofb NID_aes_128_ofb128
316 #endif
317
318 #if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
319 #define NID_aes_192_cfb NID_aes_192_cfb128
320 #endif
321
322 #if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
323 #define NID_aes_192_ofb NID_aes_192_ofb128
324 #endif
325
326 #if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
327 #define NID_aes_256_cfb NID_aes_256_cfb128
328 #endif
329
330 #if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
331 #define NID_aes_256_ofb NID_aes_256_ofb128
332 #endif
333
334 /* List of supported ciphers. */
335 static int padlock_cipher_nids[] = {
336         NID_aes_128_ecb,
337         NID_aes_128_cbc,
338         NID_aes_128_cfb,
339         NID_aes_128_ofb,
340
341         NID_aes_192_ecb,
342         NID_aes_192_cbc,
343         NID_aes_192_cfb,
344         NID_aes_192_ofb,
345
346         NID_aes_256_ecb,
347         NID_aes_256_cbc,
348         NID_aes_256_cfb,
349         NID_aes_256_ofb,
350 };
351 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids)/
352                                       sizeof(padlock_cipher_nids[0]));
353
354 /* Function prototypes ... */
355 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
356                                 const unsigned char *iv, int enc);
357
358 #define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) +         \
359         ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F )      )
360 #define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
361         NEAREST_ALIGNED(ctx->cipher_data))
362
363 static int
364 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
365                    const unsigned char *in_arg, size_t nbytes)
366 {
367         return padlock_ecb_encrypt(out_arg,in_arg,
368                         ALIGNED_CIPHER_DATA(ctx),nbytes);
369 }
370 static int
371 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
372                    const unsigned char *in_arg, size_t nbytes)
373 {
374         struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
375         int ret;
376
377         memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
378         if ((ret = padlock_cbc_encrypt(out_arg,in_arg,cdata,nbytes)))
379                 memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
380         return ret;
381 }
382
383 static int
384 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
385                    const unsigned char *in_arg, size_t nbytes)
386 {
387         struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
388         size_t chunk;
389
390         if ((chunk = ctx->num)) { /* borrow chunk variable */
391                 unsigned char *ivp=ctx->iv;
392
393                 if (chunk >= AES_BLOCK_SIZE)
394                         return 0; /* bogus value */
395
396                 if (ctx->encrypt)
397                         while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
398                                 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
399                                 chunk++, nbytes--;
400                         }
401                 else    while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
402                                 unsigned char c = *(in_arg++);
403                                 *(out_arg++) = c ^ ivp[chunk];
404                                 ivp[chunk++] = c, nbytes--;
405                         }
406
407                 ctx->num = chunk%AES_BLOCK_SIZE;
408         }
409
410         if (nbytes == 0)
411                 return 1;
412
413         memcpy (cdata->iv, ctx->iv, AES_BLOCK_SIZE);
414
415         if ((chunk = nbytes & ~(AES_BLOCK_SIZE-1))) {
416                 if (!padlock_cfb_encrypt(out_arg,in_arg,cdata,chunk))
417                         return 0;
418                 nbytes  -= chunk;
419         }
420
421         if (nbytes) {
422                 unsigned char *ivp = cdata->iv;
423
424                 out_arg += chunk;
425                 in_arg  += chunk;
426                 ctx->num = nbytes;
427                 if (cdata->cword.b.encdec) {
428                         cdata->cword.b.encdec=0;
429                         padlock_reload_key();
430                         padlock_aes_block(ivp,ivp,cdata);
431                         cdata->cword.b.encdec=1;
432                         padlock_reload_key();
433                         while(nbytes) {
434                                 unsigned char c = *(in_arg++);
435                                 *(out_arg++) = c ^ *ivp;
436                                 *(ivp++) = c, nbytes--;
437                         }
438                 }
439                 else {  padlock_reload_key();
440                         padlock_aes_block(ivp,ivp,cdata);
441                         padlock_reload_key();
442                         while (nbytes) {
443                                 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
444                                 ivp++, nbytes--;
445                         }
446                 }
447         }
448
449         memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
450
451         return 1;
452 }
453
454 static int
455 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
456                    const unsigned char *in_arg, size_t nbytes)
457 {
458         struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
459         size_t chunk;
460
461         /* ctx->num is maintained in byte-oriented modes,
462            such as CFB and OFB... */
463         if ((chunk = ctx->num)) { /* borrow chunk variable */
464                 unsigned char *ivp=ctx->iv;
465
466                 if (chunk >= AES_BLOCK_SIZE)
467                         return 0; /* bogus value */
468
469                 while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
470                         *(out_arg++) = *(in_arg++) ^ ivp[chunk];
471                         chunk++, nbytes--;
472                 }
473
474                 ctx->num = chunk%AES_BLOCK_SIZE;
475         }
476
477         if (nbytes == 0)
478                 return 1;
479
480         memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
481
482         if ((chunk = nbytes & ~(AES_BLOCK_SIZE-1))) {
483                 if (!padlock_ofb_encrypt(out_arg,in_arg,cdata,chunk))
484                         return 0;
485                 nbytes -= chunk;
486         }
487
488         if (nbytes) {
489                 unsigned char *ivp = cdata->iv;
490
491                 out_arg += chunk;
492                 in_arg  += chunk;
493                 ctx->num = nbytes;
494                 padlock_reload_key();   /* empirically found */
495                 padlock_aes_block(ivp,ivp,cdata);
496                 padlock_reload_key();   /* empirically found */
497                 while (nbytes) {
498                         *(out_arg++) = *(in_arg++) ^ *ivp;
499                         ivp++, nbytes--;
500                 }
501         }
502
503         memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
504
505         return 1;
506 }
507
508 #define EVP_CIPHER_block_size_ECB       AES_BLOCK_SIZE
509 #define EVP_CIPHER_block_size_CBC       AES_BLOCK_SIZE
510 #define EVP_CIPHER_block_size_OFB       1
511 #define EVP_CIPHER_block_size_CFB       1
512
513 /* Declaring so many ciphers by hand would be a pain.
514    Instead introduce a bit of preprocessor magic :-) */
515 #define DECLARE_AES_EVP(ksize,lmode,umode)      \
516 static const EVP_CIPHER padlock_aes_##ksize##_##lmode = {       \
517         NID_aes_##ksize##_##lmode,              \
518         EVP_CIPHER_block_size_##umode,  \
519         AES_KEY_SIZE_##ksize,           \
520         AES_BLOCK_SIZE,                 \
521         0 | EVP_CIPH_##umode##_MODE,    \
522         padlock_aes_init_key,           \
523         padlock_##lmode##_cipher,       \
524         NULL,                           \
525         sizeof(struct padlock_cipher_data) + 16,        \
526         EVP_CIPHER_set_asn1_iv,         \
527         EVP_CIPHER_get_asn1_iv,         \
528         NULL,                           \
529         NULL                            \
530 }
531
532 DECLARE_AES_EVP(128,ecb,ECB);
533 DECLARE_AES_EVP(128,cbc,CBC);
534 DECLARE_AES_EVP(128,cfb,CFB);
535 DECLARE_AES_EVP(128,ofb,OFB);
536
537 DECLARE_AES_EVP(192,ecb,ECB);
538 DECLARE_AES_EVP(192,cbc,CBC);
539 DECLARE_AES_EVP(192,cfb,CFB);
540 DECLARE_AES_EVP(192,ofb,OFB);
541
542 DECLARE_AES_EVP(256,ecb,ECB);
543 DECLARE_AES_EVP(256,cbc,CBC);
544 DECLARE_AES_EVP(256,cfb,CFB);
545 DECLARE_AES_EVP(256,ofb,OFB);
546
547 static int
548 padlock_ciphers (ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid)
549 {
550         /* No specific cipher => return a list of supported nids ... */
551         if (!cipher) {
552                 *nids = padlock_cipher_nids;
553                 return padlock_cipher_nids_num;
554         }
555
556         /* ... or the requested "cipher" otherwise */
557         switch (nid) {
558           case NID_aes_128_ecb:
559             *cipher = &padlock_aes_128_ecb;
560             break;
561           case NID_aes_128_cbc:
562             *cipher = &padlock_aes_128_cbc;
563             break;
564           case NID_aes_128_cfb:
565             *cipher = &padlock_aes_128_cfb;
566             break;
567           case NID_aes_128_ofb:
568             *cipher = &padlock_aes_128_ofb;
569             break;
570
571           case NID_aes_192_ecb:
572             *cipher = &padlock_aes_192_ecb;
573             break;
574           case NID_aes_192_cbc:
575             *cipher = &padlock_aes_192_cbc;
576             break;
577           case NID_aes_192_cfb:
578             *cipher = &padlock_aes_192_cfb;
579             break;
580           case NID_aes_192_ofb:
581             *cipher = &padlock_aes_192_ofb;
582             break;
583
584           case NID_aes_256_ecb:
585             *cipher = &padlock_aes_256_ecb;
586             break;
587           case NID_aes_256_cbc:
588             *cipher = &padlock_aes_256_cbc;
589             break;
590           case NID_aes_256_cfb:
591             *cipher = &padlock_aes_256_cfb;
592             break;
593           case NID_aes_256_ofb:
594             *cipher = &padlock_aes_256_ofb;
595             break;
596
597           default:
598             /* Sorry, we don't support this NID */
599             *cipher = NULL;
600             return 0;
601         }
602
603         return 1;
604 }
605
606 /* Prepare the encryption key for PadLock usage */
607 static int
608 padlock_aes_init_key (EVP_CIPHER_CTX *ctx, const unsigned char *key,
609                       const unsigned char *iv, int enc)
610 {
611         struct padlock_cipher_data *cdata;
612         int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
613
614         if (key==NULL) return 0;        /* ERROR */
615
616         cdata = ALIGNED_CIPHER_DATA(ctx);
617         memset(cdata, 0, sizeof(struct padlock_cipher_data));
618
619         /* Prepare Control word. */
620         if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE)
621                 cdata->cword.b.encdec = 0;
622         else
623                 cdata->cword.b.encdec = (ctx->encrypt == 0);
624         cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
625         cdata->cword.b.ksize = (key_len - 128) / 64;
626
627         switch(key_len) {
628                 case 128:
629                         /* PadLock can generate an extended key for
630                            AES128 in hardware */
631                         memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
632                         cdata->cword.b.keygen = 0;
633                         break;
634
635                 case 192:
636                 case 256:
637                         /* Generate an extended AES key in software.
638                            Needed for AES192/AES256 */
639                         /* Well, the above applies to Stepping 8 CPUs
640                            and is listed as hardware errata. They most
641                            likely will fix it at some point and then
642                            a check for stepping would be due here. */
643                         if ((EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_ECB_MODE ||
644                              EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CBC_MODE)
645                             && !enc)
646                                 AES_set_decrypt_key(key, key_len, &cdata->ks);
647                         else
648                                 AES_set_encrypt_key(key, key_len, &cdata->ks);
649 #ifndef AES_ASM
650                         /* OpenSSL C functions use byte-swapped extended key. */
651                         padlock_key_bswap(&cdata->ks);
652 #endif
653                         cdata->cword.b.keygen = 1;
654                         break;
655
656                 default:
657                         /* ERROR */
658                         return 0;
659         }
660
661         /*
662          * This is done to cover for cases when user reuses the
663          * context for new key. The catch is that if we don't do
664          * this, padlock_eas_cipher might proceed with old key...
665          */
666         padlock_reload_key ();
667
668         return 1;
669 }
670
671 #endif /* OPENSSL_NO_AES */
672
673 /* ===== Random Number Generator ===== */
674 /*
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...
679  */
680 /* Wrapper that provides an interface between the API and 
681    the raw PadLock RNG */
682 static int
683 padlock_rand_bytes(unsigned char *output, int count)
684 {
685         unsigned int eax, buf;
686
687         while (count >= 8) {
688                 eax = padlock_xstore(output, 0);
689                 if (!(eax&(1<<6)))      return 0; /* RNG disabled */
690                 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
691                 if (eax&(0x1F<<10))     return 0;
692                 if ((eax&0x1F)==0)      continue; /* no data, retry... */
693                 if ((eax&0x1F)!=8)      return 0; /* fatal failure...  */
694                 output += 8;
695                 count  -= 8;
696         }
697         while (count > 0) {
698                 eax = padlock_xstore(&buf, 3);
699                 if (!(eax&(1<<6)))      return 0; /* RNG disabled */
700                 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
701                 if (eax&(0x1F<<10))     return 0;
702                 if ((eax&0x1F)==0)      continue; /* no data, retry... */
703                 if ((eax&0x1F)!=1)      return 0; /* fatal failure...  */
704                 *output++ = (unsigned char)buf;
705                 count--;
706         }
707         *(volatile unsigned int *)&buf=0;
708
709         return 1;
710 }
711
712 /* Dummy but necessary function */
713 static int
714 padlock_rand_status(void)
715 {
716         return 1;
717 }
718
719 /* Prepare structure for registration */
720 static RAND_METHOD padlock_rand = {
721         NULL,                   /* seed */
722         padlock_rand_bytes,     /* bytes */
723         NULL,                   /* cleanup */
724         NULL,                   /* add */
725         padlock_rand_bytes,     /* pseudorand */
726         padlock_rand_status,    /* rand status */
727 };
728
729 #else  /* !COMPILE_HW_PADLOCK */
730 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
731 OPENSSL_EXPORT
732 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
733 OPENSSL_EXPORT
734 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; }
735 IMPLEMENT_DYNAMIC_CHECK_FN()
736 #endif
737 #endif /* COMPILE_HW_PADLOCK */
738
739 #endif /* !OPENSSL_NO_HW_PADLOCK */
740 #endif /* !OPENSSL_NO_HW */