fix memory leak
[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    In addition, because of the heavy use of inline assembler,
101    compiler choice is limited to GCC and Microsoft C. */
102 #undef COMPILE_HW_PADLOCK
103 #if !defined(I386_ONLY) && !defined(OPENSSL_NO_INLINE_ASM)
104 # if (defined(__GNUC__) && __GNUC__>=2 && \
105         (defined(__i386__) || defined(__i386) || \
106          defined(__x86_64__) || defined(__x86_64)) \
107      ) || \
108      (defined(_MSC_VER) && defined(_M_IX86))
109 #  define COMPILE_HW_PADLOCK
110 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
111 static ENGINE *ENGINE_padlock (void);
112 #  endif
113 # endif
114 #endif
115
116 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
117
118 void ENGINE_load_padlock (void)
119 {
120 /* On non-x86 CPUs it just returns. */
121 #ifdef COMPILE_HW_PADLOCK
122         ENGINE *toadd = ENGINE_padlock ();
123         if (!toadd) return;
124         ENGINE_add (toadd);
125         ENGINE_free (toadd);
126         ERR_clear_error ();
127 #endif
128 }
129
130 #endif
131
132 #ifdef COMPILE_HW_PADLOCK
133 /* We do these includes here to avoid header problems on platforms that
134    do not have the VIA padlock anyway... */
135 #include <stdlib.h>
136 #ifdef _WIN32
137 # include <malloc.h>
138 # ifndef alloca
139 #  define alloca _alloca
140 # endif
141 #elif defined(__GNUC__)
142 # ifndef alloca
143 #  define alloca(s) __builtin_alloca((s))
144 # endif
145 #endif
146
147 /* Function for ENGINE detection and control */
148 static int padlock_available(void);
149 static int padlock_init(ENGINE *e);
150
151 /* RNG Stuff */
152 static RAND_METHOD padlock_rand;
153
154 /* Cipher Stuff */
155 #ifndef OPENSSL_NO_AES
156 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid);
157 #endif
158
159 /* Engine names */
160 static const char *padlock_id = "padlock";
161 static char padlock_name[100];
162
163 /* Available features */
164 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
165 static int padlock_use_rng = 0; /* Random Number Generator */
166 #ifndef OPENSSL_NO_AES
167 static int padlock_aes_align_required = 1;
168 #endif
169
170 /* ===== Engine "management" functions ===== */
171
172 /* Prepare the ENGINE structure for registration */
173 static int
174 padlock_bind_helper(ENGINE *e)
175 {
176         /* Check available features */
177         padlock_available();
178
179 #if 1   /* disable RNG for now, see commentary in vicinity of RNG code */
180         padlock_use_rng=0;
181 #endif
182
183         /* Generate a nice engine name with available features */
184         BIO_snprintf(padlock_name, sizeof(padlock_name),
185                 "VIA PadLock (%s, %s)", 
186                  padlock_use_rng ? "RNG" : "no-RNG",
187                  padlock_use_ace ? "ACE" : "no-ACE");
188
189         /* Register everything or return with an error */ 
190         if (!ENGINE_set_id(e, padlock_id) ||
191             !ENGINE_set_name(e, padlock_name) ||
192
193             !ENGINE_set_init_function(e, padlock_init) ||
194 #ifndef OPENSSL_NO_AES
195             (padlock_use_ace && !ENGINE_set_ciphers (e, padlock_ciphers)) ||
196 #endif
197             (padlock_use_rng && !ENGINE_set_RAND (e, &padlock_rand))) {
198                 return 0;
199         }
200
201         /* Everything looks good */
202         return 1;
203 }
204
205 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
206 /* Constructor */
207 static ENGINE *
208 ENGINE_padlock(void)
209 {
210         ENGINE *eng = ENGINE_new();
211
212         if (!eng) {
213                 return NULL;
214         }
215
216         if (!padlock_bind_helper(eng)) {
217                 ENGINE_free(eng);
218                 return NULL;
219         }
220
221         return eng;
222 }
223 #endif
224
225 /* Check availability of the engine */
226 static int
227 padlock_init(ENGINE *e)
228 {
229         return (padlock_use_rng || padlock_use_ace);
230 }
231
232 /* This stuff is needed if this ENGINE is being compiled into a self-contained
233  * shared-library.
234  */
235 #ifdef DYNAMIC_ENGINE
236 static int
237 padlock_bind_fn(ENGINE *e, const char *id)
238 {
239         if (id && (strcmp(id, padlock_id) != 0)) {
240                 return 0;
241         }
242
243         if (!padlock_bind_helper(e))  {
244                 return 0;
245         }
246
247         return 1;
248 }
249
250 IMPLEMENT_DYNAMIC_CHECK_FN()
251 IMPLEMENT_DYNAMIC_BIND_FN (padlock_bind_fn)
252 #endif /* DYNAMIC_ENGINE */
253
254 /* ===== Here comes the "real" engine ===== */
255
256 #ifndef OPENSSL_NO_AES
257 /* Some AES-related constants */
258 #define AES_BLOCK_SIZE          16
259 #define AES_KEY_SIZE_128        16
260 #define AES_KEY_SIZE_192        24
261 #define AES_KEY_SIZE_256        32
262
263 /* Here we store the status information relevant to the 
264    current context. */
265 /* BIG FAT WARNING:
266  *      Inline assembler in PADLOCK_XCRYPT_ASM()
267  *      depends on the order of items in this structure.
268  *      Don't blindly modify, reorder, etc!
269  */
270 struct padlock_cipher_data
271 {
272         unsigned char iv[AES_BLOCK_SIZE];       /* Initialization vector */
273         union { unsigned int pad[4];
274                 struct {
275                         int rounds:4;
276                         int dgst:1;     /* n/a in C3 */
277                         int align:1;    /* n/a in C3 */
278                         int ciphr:1;    /* n/a in C3 */
279                         unsigned int keygen:1;
280                         int interm:1;
281                         unsigned int encdec:1;
282                         int ksize:2;
283                 } b;
284         } cword;                /* Control word */
285         AES_KEY ks;             /* Encryption key */
286 };
287
288 /*
289  * Essentially this variable belongs in thread local storage.
290  * Having this variable global on the other hand can only cause
291  * few bogus key reloads [if any at all on single-CPU system],
292  * so we accept the penatly...
293  */
294 static volatile struct padlock_cipher_data *padlock_saved_context;
295 #endif
296
297 /*
298  * =======================================================
299  * Inline assembler section(s).
300  * =======================================================
301  * Order of arguments is chosen to facilitate Windows port
302  * using __fastcall calling convention. If you wish to add
303  * more routines, keep in mind that first __fastcall
304  * argument is passed in %ecx and second - in %edx.
305  * =======================================================
306  */
307 #if defined(__GNUC__) && __GNUC__>=2
308 #if defined(__i386__) || defined(__i386)
309 /*
310  * As for excessive "push %ebx"/"pop %ebx" found all over.
311  * When generating position-independent code GCC won't let
312  * us use "b" in assembler templates nor even respect "ebx"
313  * in "clobber description." Therefore the trouble...
314  */
315
316 /* Helper function - check if a CPUID instruction
317    is available on this CPU */
318 static int
319 padlock_insn_cpuid_available(void)
320 {
321         int result = -1;
322
323         /* We're checking if the bit #21 of EFLAGS 
324            can be toggled. If yes = CPUID is available. */
325         asm volatile (
326                 "pushf\n"
327                 "popl %%eax\n"
328                 "xorl $0x200000, %%eax\n"
329                 "movl %%eax, %%ecx\n"
330                 "andl $0x200000, %%ecx\n"
331                 "pushl %%eax\n"
332                 "popf\n"
333                 "pushf\n"
334                 "popl %%eax\n"
335                 "andl $0x200000, %%eax\n"
336                 "xorl %%eax, %%ecx\n"
337                 "movl %%ecx, %0\n"
338                 : "=r" (result) : : "eax", "ecx");
339         
340         return (result == 0);
341 }
342
343 /* Load supported features of the CPU to see if
344    the PadLock is available. */
345 static int
346 padlock_available(void)
347 {
348         char vendor_string[16];
349         unsigned int eax, edx;
350
351         /* First check if the CPUID instruction is available at all... */
352         if (! padlock_insn_cpuid_available())
353                 return 0;
354
355         /* Are we running on the Centaur (VIA) CPU? */
356         eax = 0x00000000;
357         vendor_string[12] = 0;
358         asm volatile (
359                 "pushl  %%ebx\n"
360                 "cpuid\n"
361                 "movl   %%ebx,(%%edi)\n"
362                 "movl   %%edx,4(%%edi)\n"
363                 "movl   %%ecx,8(%%edi)\n"
364                 "popl   %%ebx"
365                 : "+a"(eax) : "D"(vendor_string) : "ecx", "edx");
366         if (strcmp(vendor_string, "CentaurHauls") != 0)
367                 return 0;
368
369         /* Check for Centaur Extended Feature Flags presence */
370         eax = 0xC0000000;
371         asm volatile ("pushl %%ebx; cpuid; popl %%ebx"
372                 : "+a"(eax) : : "ecx", "edx");
373         if (eax < 0xC0000001)
374                 return 0;
375
376         /* Read the Centaur Extended Feature Flags */
377         eax = 0xC0000001;
378         asm volatile ("pushl %%ebx; cpuid; popl %%ebx"
379                 : "+a"(eax), "=d"(edx) : : "ecx");
380
381         /* Fill up some flags */
382         padlock_use_ace = ((edx & (0x3<<6)) == (0x3<<6));
383         padlock_use_rng = ((edx & (0x3<<2)) == (0x3<<2));
384
385         return padlock_use_ace + padlock_use_rng;
386 }
387
388 /* Force key reload from memory to the CPU microcode.
389    Loading EFLAGS from the stack clears EFLAGS[30] 
390    which does the trick. */
391 static inline void
392 padlock_reload_key(void)
393 {
394         asm volatile ("pushfl; popfl");
395 }
396
397 #ifndef OPENSSL_NO_AES
398 /*
399  * This is heuristic key context tracing. At first one
400  * believes that one should use atomic swap instructions,
401  * but it's not actually necessary. Point is that if
402  * padlock_saved_context was changed by another thread
403  * after we've read it and before we compare it with cdata,
404  * our key *shall* be reloaded upon thread context switch
405  * and we are therefore set in either case...
406  */
407 static inline void
408 padlock_verify_context(struct padlock_cipher_data *cdata)
409 {
410         asm volatile (
411         "pushfl\n"
412 "       btl     $30,(%%esp)\n"
413 "       jnc     1f\n"
414 "       cmpl    %2,%1\n"
415 "       je      1f\n"
416 "       popfl\n"
417 "       subl    $4,%%esp\n"
418 "1:     addl    $4,%%esp\n"
419 "       movl    %2,%0"
420         :"+m"(padlock_saved_context)
421         : "r"(padlock_saved_context), "r"(cdata) : "cc");
422 }
423
424 /* Template for padlock_xcrypt_* modes */
425 /* BIG FAT WARNING: 
426  *      The offsets used with 'leal' instructions
427  *      describe items of the 'padlock_cipher_data'
428  *      structure.
429  */
430 #define PADLOCK_XCRYPT_ASM(name,rep_xcrypt)     \
431 static inline void *name(size_t cnt,            \
432         struct padlock_cipher_data *cdata,      \
433         void *out, const void *inp)             \
434 {       void *iv;                               \
435         asm volatile ( "pushl   %%ebx\n"        \
436                 "       leal    16(%0),%%edx\n" \
437                 "       leal    32(%0),%%ebx\n" \
438                         rep_xcrypt "\n"         \
439                 "       popl    %%ebx"          \
440                 : "=a"(iv), "=c"(cnt), "=D"(out), "=S"(inp) \
441                 : "0"(cdata), "1"(cnt), "2"(out), "3"(inp)  \
442                 : "edx", "cc", "memory");       \
443         return iv;                              \
444 }
445 #endif
446
447 #elif defined(__x86_64__) || defined(__x86_64)
448
449 /* Load supported features of the CPU to see if
450    the PadLock is available. */
451 static int
452 padlock_available(void)
453 {
454         char vendor_string[16];
455         unsigned int eax, edx;
456         size_t  scratch;
457
458         /* Are we running on the Centaur (VIA) CPU? */
459         eax = 0x00000000;
460         vendor_string[12] = 0;
461         asm volatile (
462                 "movq   %%rbx,%1\n"
463                 "cpuid\n"
464                 "movl   %%ebx,(%2)\n"
465                 "movl   %%edx,4(%2)\n"
466                 "movl   %%ecx,8(%2)\n"
467                 "movq   %1,%%rbx"
468                 : "+a"(eax), "=&r"(scratch) : "r"(vendor_string) : "rcx", "rdx");
469         if (strcmp(vendor_string, "CentaurHauls") != 0)
470                 return 0;
471
472         /* Check for Centaur Extended Feature Flags presence */
473         eax = 0xC0000000;
474         asm volatile ("movq %%rbx,%1; cpuid; movq %1,%%rbx"
475                 : "+a"(eax), "=&r"(scratch) : : "rcx", "rdx");
476         if (eax < 0xC0000001)
477                 return 0;
478
479         /* Read the Centaur Extended Feature Flags */
480         eax = 0xC0000001;
481         asm volatile ("movq %%rbx,%2; cpuid; movq %2,%%rbx"
482                 : "+a"(eax), "=d"(edx), "=&r"(scratch) : : "rcx");
483
484         /* Fill up some flags */
485         padlock_use_ace = ((edx & (0x3<<6)) == (0x3<<6));
486         padlock_use_rng = ((edx & (0x3<<2)) == (0x3<<2));
487
488         return padlock_use_ace + padlock_use_rng;
489 }
490
491 /* Force key reload from memory to the CPU microcode.
492    Loading EFLAGS from the stack clears EFLAGS[30] 
493    which does the trick. */
494 static inline void
495 padlock_reload_key(void)
496 {
497         asm volatile ("pushfq; popfq");
498 }
499
500 #ifndef OPENSSL_NO_AES
501 /*
502  * This is heuristic key context tracing. At first one
503  * believes that one should use atomic swap instructions,
504  * but it's not actually necessary. Point is that if
505  * padlock_saved_context was changed by another thread
506  * after we've read it and before we compare it with cdata,
507  * our key *shall* be reloaded upon thread context switch
508  * and we are therefore set in either case...
509  */
510 static inline void
511 padlock_verify_context(struct padlock_cipher_data *cdata)
512 {
513         asm volatile (
514         "pushfq\n"
515 "       btl     $30,(%%rsp)\n"
516 "       jnc     1f\n"
517 "       cmpq    %2,%1\n"
518 "       je      1f\n"
519 "       popfq\n"
520 "       subq    $8,%%rsp\n"
521 "1:     addq    $8,%%rsp\n"
522 "       movq    %2,%0"
523         :"+m"(padlock_saved_context)
524         : "r"(padlock_saved_context), "r"(cdata) : "cc");
525 }
526
527 /* Template for padlock_xcrypt_* modes */
528 /* BIG FAT WARNING: 
529  *      The offsets used with 'leal' instructions
530  *      describe items of the 'padlock_cipher_data'
531  *      structure.
532  */
533 #define PADLOCK_XCRYPT_ASM(name,rep_xcrypt)     \
534 static inline void *name(size_t cnt,            \
535         struct padlock_cipher_data *cdata,      \
536         void *out, const void *inp)             \
537 {       void *iv;                               \
538         size_t scratch;                         \
539         asm volatile ( "movq    %%rbx,%4\n"     \
540                 "       leaq    16(%0),%%rdx\n" \
541                 "       leaq    32(%0),%%rbx\n" \
542                         rep_xcrypt "\n"         \
543                 "       movq    %4,%%rbx"       \
544                 : "=a"(iv), "=c"(cnt), "=D"(out), "=S"(inp), "=&r"(scratch) \
545                 : "0"(cdata), "1"(cnt), "2"(out), "3"(inp)  \
546                 : "rdx", "cc", "memory");       \
547         return iv;                              \
548 }
549 #endif
550
551 #endif  /* cpu */
552
553 #ifndef OPENSSL_NO_AES
554 /* Generate all functions with appropriate opcodes */
555 PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb, ".byte 0xf3,0x0f,0xa7,0xc8")     /* rep xcryptecb */
556 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cbc, ".byte 0xf3,0x0f,0xa7,0xd0")     /* rep xcryptcbc */
557 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb, ".byte 0xf3,0x0f,0xa7,0xe0")     /* rep xcryptcfb */
558 PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb, ".byte 0xf3,0x0f,0xa7,0xe8")     /* rep xcryptofb */
559
560 /* Our own htonl()/ntohl() */
561 static inline void
562 padlock_bswapl(AES_KEY *ks)
563 {
564         size_t i = sizeof(ks->rd_key)/sizeof(ks->rd_key[0]);
565         unsigned int *key = ks->rd_key;
566
567         while (i--) {
568                 asm volatile ("bswapl %0" : "+r"(*key));
569                 key++;
570         }
571 }
572 #endif
573
574 /* The RNG call itself */
575 static inline unsigned int
576 padlock_xstore(void *addr, unsigned int edx_in)
577 {
578         unsigned int eax_out;
579
580         asm volatile (".byte 0x0f,0xa7,0xc0"    /* xstore */
581             : "=a"(eax_out),"=m"(*(unsigned *)addr)
582             : "D"(addr), "d" (edx_in)
583             );
584
585         return eax_out;
586 }
587
588 /* Why not inline 'rep movsd'? I failed to find information on what
589  * value in Direction Flag one can expect and consequently have to
590  * apply "better-safe-than-sorry" approach and assume "undefined."
591  * I could explicitly clear it and restore the original value upon
592  * return from padlock_aes_cipher, but it's presumably too much
593  * trouble for too little gain...
594  *
595  * In case you wonder 'rep xcrypt*' instructions above are *not*
596  * affected by the Direction Flag and pointers advance toward
597  * larger addresses unconditionally.
598  */ 
599 static inline unsigned char *
600 padlock_memcpy(void *dst,const void *src,size_t n)
601 {
602         size_t       *d=dst;
603         const size_t *s=src;
604
605         n /= sizeof(*d);
606         do { *d++ = *s++; } while (--n);
607
608         return dst;
609 }
610
611 #elif defined(_MSC_VER)
612 /*
613  * Unlike GCC these are real functions. In order to minimize impact
614  * on performance we adhere to __fastcall calling convention in
615  * order to get two first arguments passed through %ecx and %edx.
616  * Which kind of suits very well, as instructions in question use
617  * both %ecx and %edx as input:-)
618  */
619 #define REP_XCRYPT(code)                \
620         _asm _emit 0xf3                 \
621         _asm _emit 0x0f _asm _emit 0xa7 \
622         _asm _emit code
623
624 /* BIG FAT WARNING: 
625  *      The offsets used with 'lea' instructions
626  *      describe items of the 'padlock_cipher_data'
627  *      structure.
628  */
629 #define PADLOCK_XCRYPT_ASM(name,code)   \
630 static void * __fastcall                \
631         name (size_t cnt, void *cdata,  \
632         void *outp, const void *inp)    \
633 {       _asm    mov     eax,edx         \
634         _asm    lea     edx,[eax+16]    \
635         _asm    lea     ebx,[eax+32]    \
636         _asm    mov     edi,outp        \
637         _asm    mov     esi,inp         \
638         REP_XCRYPT(code)                \
639 }
640
641 PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb,0xc8)
642 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cbc,0xd0)
643 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb,0xe0)
644 PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb,0xe8)
645
646 static int __fastcall
647 padlock_xstore(void *outp,unsigned int code)
648 {       _asm    mov     edi,ecx
649         _asm _emit 0x0f _asm _emit 0xa7 _asm _emit 0xc0
650 }
651
652 static void __fastcall
653 padlock_reload_key(void)
654 {       _asm pushfd _asm popfd          }
655
656 static void __fastcall
657 padlock_verify_context(void *cdata)
658 {       _asm    {
659                 pushfd
660                 bt      DWORD PTR[esp],30
661                 jnc     skip
662                 cmp     ecx,padlock_saved_context
663                 je      skip
664                 popfd
665                 sub     esp,4
666         skip:   add     esp,4
667                 mov     padlock_saved_context,ecx
668                 }
669 }
670
671 static int
672 padlock_available(void)
673 {       _asm    {
674                 pushfd
675                 pop     eax
676                 mov     ecx,eax
677                 xor     eax,1<<21
678                 push    eax
679                 popfd
680                 pushfd
681                 pop     eax
682                 xor     eax,ecx
683                 bt      eax,21
684                 jnc     noluck
685                 mov     eax,0
686                 cpuid
687                 xor     eax,eax
688                 cmp     ebx,'tneC'
689                 jne     noluck
690                 cmp     edx,'Hrua'
691                 jne     noluck
692                 cmp     ecx,'slua'
693                 jne     noluck
694                 mov     eax,0xC0000000
695                 cpuid
696                 mov     edx,eax
697                 xor     eax,eax
698                 cmp     edx,0xC0000001
699                 jb      noluck
700                 mov     eax,0xC0000001
701                 cpuid
702                 xor     eax,eax
703                 bt      edx,6
704                 jnc     skip_a
705                 bt      edx,7
706                 jnc     skip_a
707                 mov     padlock_use_ace,1
708                 inc     eax
709         skip_a: bt      edx,2
710                 jnc     skip_r
711                 bt      edx,3
712                 jnc     skip_r
713                 mov     padlock_use_rng,1
714                 inc     eax
715         skip_r:
716         noluck:
717                 }
718 }
719
720 static void __fastcall
721 padlock_bswapl(void *key)
722 {       _asm    {
723                 pushfd
724                 cld
725                 mov     esi,ecx
726                 mov     edi,ecx
727                 mov     ecx,60
728         up:     lodsd
729                 bswap   eax
730                 stosd
731                 loop    up
732                 popfd
733                 }
734 }
735
736 /* MS actually specifies status of Direction Flag and compiler even
737  * manages to compile following as 'rep movsd' all by itself...
738  */
739 #define padlock_memcpy(o,i,n) ((unsigned char *)memcpy((o),(i),(n)&~3U))
740 #endif
741
742 /* ===== AES encryption/decryption ===== */
743 #ifndef OPENSSL_NO_AES
744
745 #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
746 #define NID_aes_128_cfb NID_aes_128_cfb128
747 #endif
748
749 #if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
750 #define NID_aes_128_ofb NID_aes_128_ofb128
751 #endif
752
753 #if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
754 #define NID_aes_192_cfb NID_aes_192_cfb128
755 #endif
756
757 #if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
758 #define NID_aes_192_ofb NID_aes_192_ofb128
759 #endif
760
761 #if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
762 #define NID_aes_256_cfb NID_aes_256_cfb128
763 #endif
764
765 #if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
766 #define NID_aes_256_ofb NID_aes_256_ofb128
767 #endif
768
769 /* List of supported ciphers. */
770 static int padlock_cipher_nids[] = {
771         NID_aes_128_ecb,
772         NID_aes_128_cbc,
773         NID_aes_128_cfb,
774         NID_aes_128_ofb,
775
776         NID_aes_192_ecb,
777         NID_aes_192_cbc,
778         NID_aes_192_cfb,
779         NID_aes_192_ofb,
780
781         NID_aes_256_ecb,
782         NID_aes_256_cbc,
783         NID_aes_256_cfb,
784         NID_aes_256_ofb,
785 };
786 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids)/
787                                       sizeof(padlock_cipher_nids[0]));
788
789 /* Function prototypes ... */
790 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
791                                 const unsigned char *iv, int enc);
792 static int padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
793                               const unsigned char *in, size_t nbytes);
794
795 #define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) +         \
796         ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F )      )
797 #define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
798         NEAREST_ALIGNED(ctx->cipher_data))
799
800 #define EVP_CIPHER_block_size_ECB       AES_BLOCK_SIZE
801 #define EVP_CIPHER_block_size_CBC       AES_BLOCK_SIZE
802 #define EVP_CIPHER_block_size_OFB       1
803 #define EVP_CIPHER_block_size_CFB       1
804
805 /* Declaring so many ciphers by hand would be a pain.
806    Instead introduce a bit of preprocessor magic :-) */
807 #define DECLARE_AES_EVP(ksize,lmode,umode)      \
808 static const EVP_CIPHER padlock_aes_##ksize##_##lmode = {       \
809         NID_aes_##ksize##_##lmode,              \
810         EVP_CIPHER_block_size_##umode,  \
811         AES_KEY_SIZE_##ksize,           \
812         AES_BLOCK_SIZE,                 \
813         0 | EVP_CIPH_##umode##_MODE,    \
814         padlock_aes_init_key,           \
815         padlock_aes_cipher,             \
816         NULL,                           \
817         sizeof(struct padlock_cipher_data) + 16,        \
818         EVP_CIPHER_set_asn1_iv,         \
819         EVP_CIPHER_get_asn1_iv,         \
820         NULL,                           \
821         NULL                            \
822 }
823
824 DECLARE_AES_EVP(128,ecb,ECB);
825 DECLARE_AES_EVP(128,cbc,CBC);
826 DECLARE_AES_EVP(128,cfb,CFB);
827 DECLARE_AES_EVP(128,ofb,OFB);
828
829 DECLARE_AES_EVP(192,ecb,ECB);
830 DECLARE_AES_EVP(192,cbc,CBC);
831 DECLARE_AES_EVP(192,cfb,CFB);
832 DECLARE_AES_EVP(192,ofb,OFB);
833
834 DECLARE_AES_EVP(256,ecb,ECB);
835 DECLARE_AES_EVP(256,cbc,CBC);
836 DECLARE_AES_EVP(256,cfb,CFB);
837 DECLARE_AES_EVP(256,ofb,OFB);
838
839 static int
840 padlock_ciphers (ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid)
841 {
842         /* No specific cipher => return a list of supported nids ... */
843         if (!cipher) {
844                 *nids = padlock_cipher_nids;
845                 return padlock_cipher_nids_num;
846         }
847
848         /* ... or the requested "cipher" otherwise */
849         switch (nid) {
850           case NID_aes_128_ecb:
851             *cipher = &padlock_aes_128_ecb;
852             break;
853           case NID_aes_128_cbc:
854             *cipher = &padlock_aes_128_cbc;
855             break;
856           case NID_aes_128_cfb:
857             *cipher = &padlock_aes_128_cfb;
858             break;
859           case NID_aes_128_ofb:
860             *cipher = &padlock_aes_128_ofb;
861             break;
862
863           case NID_aes_192_ecb:
864             *cipher = &padlock_aes_192_ecb;
865             break;
866           case NID_aes_192_cbc:
867             *cipher = &padlock_aes_192_cbc;
868             break;
869           case NID_aes_192_cfb:
870             *cipher = &padlock_aes_192_cfb;
871             break;
872           case NID_aes_192_ofb:
873             *cipher = &padlock_aes_192_ofb;
874             break;
875
876           case NID_aes_256_ecb:
877             *cipher = &padlock_aes_256_ecb;
878             break;
879           case NID_aes_256_cbc:
880             *cipher = &padlock_aes_256_cbc;
881             break;
882           case NID_aes_256_cfb:
883             *cipher = &padlock_aes_256_cfb;
884             break;
885           case NID_aes_256_ofb:
886             *cipher = &padlock_aes_256_ofb;
887             break;
888
889           default:
890             /* Sorry, we don't support this NID */
891             *cipher = NULL;
892             return 0;
893         }
894
895         return 1;
896 }
897
898 /* Prepare the encryption key for PadLock usage */
899 static int
900 padlock_aes_init_key (EVP_CIPHER_CTX *ctx, const unsigned char *key,
901                       const unsigned char *iv, int enc)
902 {
903         struct padlock_cipher_data *cdata;
904         int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
905
906         if (key==NULL) return 0;        /* ERROR */
907
908         cdata = ALIGNED_CIPHER_DATA(ctx);
909         memset(cdata, 0, sizeof(struct padlock_cipher_data));
910
911         /* Prepare Control word. */
912         if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE)
913                 cdata->cword.b.encdec = 0;
914         else
915                 cdata->cword.b.encdec = (ctx->encrypt == 0);
916         cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
917         cdata->cword.b.ksize = (key_len - 128) / 64;
918
919         switch(key_len) {
920                 case 128:
921                         /* PadLock can generate an extended key for
922                            AES128 in hardware */
923                         memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
924                         cdata->cword.b.keygen = 0;
925                         break;
926
927                 case 192:
928                 case 256:
929                         /* Generate an extended AES key in software.
930                            Needed for AES192/AES256 */
931                         /* Well, the above applies to Stepping 8 CPUs
932                            and is listed as hardware errata. They most
933                            likely will fix it at some point and then
934                            a check for stepping would be due here. */
935                         if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE ||
936                             EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE ||
937                             enc)
938                                 AES_set_encrypt_key(key, key_len, &cdata->ks);
939                         else
940                                 AES_set_decrypt_key(key, key_len, &cdata->ks);
941 #ifndef AES_ASM
942                         /* OpenSSL C functions use byte-swapped extended key. */
943                         padlock_bswapl(&cdata->ks);
944 #endif
945                         cdata->cword.b.keygen = 1;
946                         break;
947
948                 default:
949                         /* ERROR */
950                         return 0;
951         }
952
953         /*
954          * This is done to cover for cases when user reuses the
955          * context for new key. The catch is that if we don't do
956          * this, padlock_eas_cipher might proceed with old key...
957          */
958         padlock_reload_key ();
959
960         return 1;
961 }
962
963 /* 
964  * Simplified version of padlock_aes_cipher() used when
965  * 1) both input and output buffers are at aligned addresses.
966  * or when
967  * 2) running on a newer CPU that doesn't require aligned buffers.
968  */
969 static int
970 padlock_aes_cipher_omnivorous(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
971                 const unsigned char *in_arg, size_t nbytes)
972 {
973         struct padlock_cipher_data *cdata;
974         void  *iv;
975
976         cdata = ALIGNED_CIPHER_DATA(ctx);
977         padlock_verify_context(cdata);
978
979         switch (EVP_CIPHER_CTX_mode(ctx)) {
980         case EVP_CIPH_ECB_MODE:
981                 padlock_xcrypt_ecb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);
982                 break;
983
984         case EVP_CIPH_CBC_MODE:
985                 memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
986                 iv = padlock_xcrypt_cbc(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);
987                 memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
988                 break;
989
990         case EVP_CIPH_CFB_MODE:
991                 memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
992                 iv = padlock_xcrypt_cfb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);
993                 memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
994                 break;
995
996         case EVP_CIPH_OFB_MODE:
997                 memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
998                 padlock_xcrypt_ofb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);
999                 memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
1000                 break;
1001
1002         default:
1003                 return 0;
1004         }
1005
1006         memset(cdata->iv, 0, AES_BLOCK_SIZE);
1007
1008         return 1;
1009 }
1010
1011 #ifndef  PADLOCK_CHUNK
1012 # define PADLOCK_CHUNK  512     /* Must be a power of 2 larger than 16 */
1013 #endif
1014 #if PADLOCK_CHUNK<16 || PADLOCK_CHUNK&(PADLOCK_CHUNK-1)
1015 # error "insane PADLOCK_CHUNK..."
1016 #endif
1017
1018 /* Re-align the arguments to 16-Bytes boundaries and run the 
1019    encryption function itself. This function is not AES-specific. */
1020 static int
1021 padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
1022                    const unsigned char *in_arg, size_t nbytes)
1023 {
1024         struct padlock_cipher_data *cdata;
1025         const  void *inp;
1026         unsigned char  *out;
1027         void  *iv;
1028         int    inp_misaligned, out_misaligned, realign_in_loop;
1029         size_t chunk, allocated=0;
1030
1031         /* ctx->num is maintained in byte-oriented modes,
1032            such as CFB and OFB... */
1033         if ((chunk = ctx->num)) { /* borrow chunk variable */
1034                 unsigned char *ivp=ctx->iv;
1035
1036                 switch (EVP_CIPHER_CTX_mode(ctx)) {
1037                 case EVP_CIPH_CFB_MODE:
1038                         if (chunk >= AES_BLOCK_SIZE)
1039                                 return 0; /* bogus value */
1040
1041                         if (ctx->encrypt)
1042                                 while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
1043                                         ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
1044                                         chunk++, nbytes--;
1045                                 }
1046                         else    while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
1047                                         unsigned char c = *(in_arg++);
1048                                         *(out_arg++) = c ^ ivp[chunk];
1049                                         ivp[chunk++] = c, nbytes--;
1050                                 }
1051
1052                         ctx->num = chunk%AES_BLOCK_SIZE;
1053                         break;
1054                 case EVP_CIPH_OFB_MODE:
1055                         if (chunk >= AES_BLOCK_SIZE)
1056                                 return 0; /* bogus value */
1057
1058                         while (chunk<AES_BLOCK_SIZE && nbytes!=0) {
1059                                 *(out_arg++) = *(in_arg++) ^ ivp[chunk];
1060                                 chunk++, nbytes--;
1061                         }
1062
1063                         ctx->num = chunk%AES_BLOCK_SIZE;
1064                         break;
1065                 }
1066         }
1067
1068         if (nbytes == 0)
1069                 return 1;
1070 #if 0
1071         if (nbytes % AES_BLOCK_SIZE)
1072                 return 0; /* are we expected to do tail processing? */
1073 #else
1074         /* nbytes is always multiple of AES_BLOCK_SIZE in ECB and CBC
1075            modes and arbitrary value in byte-oriented modes, such as
1076            CFB and OFB... */
1077 #endif
1078
1079         /* VIA promises CPUs that won't require alignment in the future.
1080            For now padlock_aes_align_required is initialized to 1 and
1081            the condition is never met... */
1082         /* C7 core is capable to manage unaligned input in non-ECB[!]
1083            mode, but performance penalties appear to be approximately
1084            same as for software alignment below or ~3x. They promise to
1085            improve it in the future, but for now we can just as well
1086            pretend that it can only handle aligned input... */
1087         if (!padlock_aes_align_required && (nbytes%AES_BLOCK_SIZE)==0)
1088                 return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);
1089
1090         inp_misaligned = (((size_t)in_arg) & 0x0F);
1091         out_misaligned = (((size_t)out_arg) & 0x0F);
1092
1093         /* Note that even if output is aligned and input not,
1094          * I still prefer to loop instead of copy the whole
1095          * input and then encrypt in one stroke. This is done
1096          * in order to improve L1 cache utilization... */
1097         realign_in_loop = out_misaligned|inp_misaligned;
1098
1099         if (!realign_in_loop && (nbytes%AES_BLOCK_SIZE)==0)
1100                 return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);
1101
1102         /* this takes one "if" out of the loops */
1103         chunk  = nbytes;
1104         chunk %= PADLOCK_CHUNK;
1105         if (chunk==0) chunk = PADLOCK_CHUNK;
1106
1107         if (out_misaligned) {
1108                 /* optmize for small input */
1109                 allocated = (chunk<nbytes?PADLOCK_CHUNK:nbytes);
1110                 out = alloca(0x10 + allocated);
1111                 out = NEAREST_ALIGNED(out);
1112         }
1113         else
1114                 out = out_arg;
1115
1116         cdata = ALIGNED_CIPHER_DATA(ctx);
1117         padlock_verify_context(cdata);
1118
1119         switch (EVP_CIPHER_CTX_mode(ctx)) {
1120         case EVP_CIPH_ECB_MODE:
1121                 do      {
1122                         if (inp_misaligned)
1123                                 inp = padlock_memcpy(out, in_arg, chunk);
1124                         else
1125                                 inp = in_arg;
1126                         in_arg += chunk;
1127
1128                         padlock_xcrypt_ecb(chunk/AES_BLOCK_SIZE, cdata, out, inp);
1129
1130                         if (out_misaligned)
1131                                 out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
1132                         else
1133                                 out     = out_arg+=chunk;
1134
1135                         nbytes -= chunk;
1136                         chunk   = PADLOCK_CHUNK;
1137                 } while (nbytes);
1138                 break;
1139
1140         case EVP_CIPH_CBC_MODE:
1141                 memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
1142                 goto cbc_shortcut;
1143                 do      {
1144                         if (iv != cdata->iv)
1145                                 memcpy(cdata->iv, iv, AES_BLOCK_SIZE);
1146                         chunk = PADLOCK_CHUNK;
1147                 cbc_shortcut: /* optimize for small input */
1148                         if (inp_misaligned)
1149                                 inp = padlock_memcpy(out, in_arg, chunk);
1150                         else
1151                                 inp = in_arg;
1152                         in_arg += chunk;
1153
1154                         iv = padlock_xcrypt_cbc(chunk/AES_BLOCK_SIZE, cdata, out, inp);
1155
1156                         if (out_misaligned)
1157                                 out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
1158                         else
1159                                 out     = out_arg+=chunk;
1160
1161                 } while (nbytes -= chunk);
1162                 memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
1163                 break;
1164
1165         case EVP_CIPH_CFB_MODE:
1166                 memcpy (iv = cdata->iv, ctx->iv, AES_BLOCK_SIZE);
1167                 chunk &= ~(AES_BLOCK_SIZE-1);
1168                 if (chunk)      goto cfb_shortcut;
1169                 else            goto cfb_skiploop;
1170                 do      {
1171                         if (iv != cdata->iv)
1172                                 memcpy(cdata->iv, iv, AES_BLOCK_SIZE);
1173                         chunk = PADLOCK_CHUNK;
1174                 cfb_shortcut: /* optimize for small input */
1175                         if (inp_misaligned)
1176                                 inp = padlock_memcpy(out, in_arg, chunk);
1177                         else
1178                                 inp = in_arg;
1179                         in_arg += chunk;
1180
1181                         iv = padlock_xcrypt_cfb(chunk/AES_BLOCK_SIZE, cdata, out, inp);
1182
1183                         if (out_misaligned)
1184                                 out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
1185                         else
1186                                 out     = out_arg+=chunk;
1187
1188                         nbytes -= chunk;
1189                 } while (nbytes >= AES_BLOCK_SIZE);
1190
1191                 cfb_skiploop:
1192                 if (nbytes) {
1193                         unsigned char *ivp = cdata->iv;
1194
1195                         if (iv != ivp) {
1196                                 memcpy(ivp, iv, AES_BLOCK_SIZE);
1197                                 iv = ivp;
1198                         }
1199                         ctx->num = nbytes;
1200                         if (cdata->cword.b.encdec) {
1201                                 cdata->cword.b.encdec=0;
1202                                 padlock_reload_key();
1203                                 padlock_xcrypt_ecb(1,cdata,ivp,ivp);
1204                                 cdata->cword.b.encdec=1;
1205                                 padlock_reload_key();
1206                                 while(nbytes) {
1207                                         unsigned char c = *(in_arg++);
1208                                         *(out_arg++) = c ^ *ivp;
1209                                         *(ivp++) = c, nbytes--;
1210                                 }
1211                         }
1212                         else {  padlock_reload_key();
1213                                 padlock_xcrypt_ecb(1,cdata,ivp,ivp);
1214                                 padlock_reload_key();
1215                                 while (nbytes) {
1216                                         *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
1217                                         ivp++, nbytes--;
1218                                 }
1219                         }
1220                 }
1221
1222                 memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
1223                 break;
1224
1225         case EVP_CIPH_OFB_MODE:
1226                 memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
1227                 chunk &= ~(AES_BLOCK_SIZE-1);
1228                 if (chunk) do   {
1229                         if (inp_misaligned)
1230                                 inp = padlock_memcpy(out, in_arg, chunk);
1231                         else
1232                                 inp = in_arg;
1233                         in_arg += chunk;
1234
1235                         padlock_xcrypt_ofb(chunk/AES_BLOCK_SIZE, cdata, out, inp);
1236
1237                         if (out_misaligned)
1238                                 out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
1239                         else
1240                                 out     = out_arg+=chunk;
1241
1242                         nbytes -= chunk;
1243                         chunk   = PADLOCK_CHUNK;
1244                 } while (nbytes >= AES_BLOCK_SIZE);
1245
1246                 if (nbytes) {
1247                         unsigned char *ivp = cdata->iv;
1248
1249                         ctx->num = nbytes;
1250                         padlock_reload_key();   /* empirically found */
1251                         padlock_xcrypt_ecb(1,cdata,ivp,ivp);
1252                         padlock_reload_key();   /* empirically found */
1253                         while (nbytes) {
1254                                 *(out_arg++) = *(in_arg++) ^ *ivp;
1255                                 ivp++, nbytes--;
1256                         }
1257                 }
1258
1259                 memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
1260                 break;
1261
1262         default:
1263                 return 0;
1264         }
1265
1266         /* Clean the realign buffer if it was used */
1267         if (out_misaligned) {
1268                 volatile unsigned long *p=(void *)out;
1269                 size_t   n = allocated/sizeof(*p);
1270                 while (n--) *p++=0;
1271         }
1272
1273         memset(cdata->iv, 0, AES_BLOCK_SIZE);
1274
1275         return 1;
1276 }
1277
1278 #endif /* OPENSSL_NO_AES */
1279
1280 /* ===== Random Number Generator ===== */
1281 /*
1282  * This code is not engaged. The reason is that it does not comply
1283  * with recommendations for VIA RNG usage for secure applications
1284  * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
1285  * provide meaningful error control...
1286  */
1287 /* Wrapper that provides an interface between the API and 
1288    the raw PadLock RNG */
1289 static int
1290 padlock_rand_bytes(unsigned char *output, int count)
1291 {
1292         unsigned int eax, buf;
1293
1294         while (count >= 8) {
1295                 eax = padlock_xstore(output, 0);
1296                 if (!(eax&(1<<6)))      return 0; /* RNG disabled */
1297                 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
1298                 if (eax&(0x1F<<10))     return 0;
1299                 if ((eax&0x1F)==0)      continue; /* no data, retry... */
1300                 if ((eax&0x1F)!=8)      return 0; /* fatal failure...  */
1301                 output += 8;
1302                 count  -= 8;
1303         }
1304         while (count > 0) {
1305                 eax = padlock_xstore(&buf, 3);
1306                 if (!(eax&(1<<6)))      return 0; /* RNG disabled */
1307                 /* this ---vv--- covers DC bias, Raw Bits and String Filter */
1308                 if (eax&(0x1F<<10))     return 0;
1309                 if ((eax&0x1F)==0)      continue; /* no data, retry... */
1310                 if ((eax&0x1F)!=1)      return 0; /* fatal failure...  */
1311                 *output++ = (unsigned char)buf;
1312                 count--;
1313         }
1314         *(volatile unsigned int *)&buf=0;
1315
1316         return 1;
1317 }
1318
1319 /* Dummy but necessary function */
1320 static int
1321 padlock_rand_status(void)
1322 {
1323         return 1;
1324 }
1325
1326 /* Prepare structure for registration */
1327 static RAND_METHOD padlock_rand = {
1328         NULL,                   /* seed */
1329         padlock_rand_bytes,     /* bytes */
1330         NULL,                   /* cleanup */
1331         NULL,                   /* add */
1332         padlock_rand_bytes,     /* pseudorand */
1333         padlock_rand_status,    /* rand status */
1334 };
1335
1336 #else  /* !COMPILE_HW_PADLOCK */
1337 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1338 OPENSSL_EXPORT
1339 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
1340 OPENSSL_EXPORT
1341 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; }
1342 IMPLEMENT_DYNAMIC_CHECK_FN()
1343 #endif
1344 #endif /* COMPILE_HW_PADLOCK */
1345
1346 #endif /* !OPENSSL_NO_HW_PADLOCK */
1347 #endif /* !OPENSSL_NO_HW */