Fix capi engine for no-dsa
[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 #include <stdio.h>
66 #include <string.h>
67
68 #include <openssl/opensslconf.h>
69 #include <openssl/crypto.h>
70 #include <openssl/engine.h>
71 #include <openssl/evp.h>
72 #include <openssl/aes.h>
73 #include <openssl/rand.h>
74 #include <openssl/err.h>
75 #include <openssl/modes.h>
76
77 #ifndef OPENSSL_NO_HW
78 # ifndef OPENSSL_NO_HW_PADLOCK
79
80 /* Attempt to have a single source for both 0.9.7 and 0.9.8 :-) */
81 #  if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
82 #   ifndef OPENSSL_NO_DYNAMIC_ENGINE
83 #    define DYNAMIC_ENGINE
84 #   endif
85 #  elif (OPENSSL_VERSION_NUMBER >= 0x00907000L)
86 #   ifdef ENGINE_DYNAMIC_SUPPORT
87 #    define DYNAMIC_ENGINE
88 #   endif
89 #  else
90 #   error "Only OpenSSL >= 0.9.7 is supported"
91 #  endif
92
93 /*
94  * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
95  * doesn't exist elsewhere, but it even can't be compiled on other platforms!
96  */
97
98 #  undef COMPILE_HW_PADLOCK
99 #  if !defined(I386_ONLY) && !defined(OPENSSL_NO_ASM)
100 #   if    defined(__i386__) || defined(__i386) ||    \
101         defined(__x86_64__) || defined(__x86_64) || \
102         defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
103         defined(__INTEL__)
104 #    define COMPILE_HW_PADLOCK
105 #    ifdef OPENSSL_NO_DYNAMIC_ENGINE
106 static ENGINE *ENGINE_padlock(void);
107 #    endif
108 #   endif
109 #  endif
110
111 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
112 void engine_load_padlock_int(void);
113 void engine_load_padlock_int(void)
114 {
115 /* On non-x86 CPUs it just returns. */
116 #   ifdef COMPILE_HW_PADLOCK
117     ENGINE *toadd = ENGINE_padlock();
118     if (!toadd)
119         return;
120     ENGINE_add(toadd);
121     ENGINE_free(toadd);
122     ERR_clear_error();
123 #   endif
124 }
125
126 #  endif
127
128 #  ifdef COMPILE_HW_PADLOCK
129
130 /* Function for ENGINE detection and control */
131 static int padlock_available(void);
132 static int padlock_init(ENGINE *e);
133
134 /* RNG Stuff */
135 static RAND_METHOD padlock_rand;
136
137 /* Cipher Stuff */
138 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
139                            const int **nids, int nid);
140
141 /* Engine names */
142 static const char *padlock_id = "padlock";
143 static char padlock_name[100];
144
145 /* Available features */
146 static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
147 static int padlock_use_rng = 0; /* Random Number Generator */
148
149 /* ===== Engine "management" functions ===== */
150
151 /* Prepare the ENGINE structure for registration */
152 static int padlock_bind_helper(ENGINE *e)
153 {
154     /* Check available features */
155     padlock_available();
156
157     /*
158      * RNG is currently disabled for reasons discussed in commentary just
159      * before padlock_rand_bytes function.
160      */
161     padlock_use_rng = 0;
162
163     /* Generate a nice engine name with available features */
164     BIO_snprintf(padlock_name, sizeof(padlock_name),
165                  "VIA PadLock (%s, %s)",
166                  padlock_use_rng ? "RNG" : "no-RNG",
167                  padlock_use_ace ? "ACE" : "no-ACE");
168
169     /* Register everything or return with an error */
170     if (!ENGINE_set_id(e, padlock_id) ||
171         !ENGINE_set_name(e, padlock_name) ||
172         !ENGINE_set_init_function(e, padlock_init) ||
173         (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
174         (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
175         return 0;
176     }
177
178     /* Everything looks good */
179     return 1;
180 }
181
182 #   ifdef OPENSSL_NO_DYNAMIC_ENGINE
183 /* Constructor */
184 static ENGINE *ENGINE_padlock(void)
185 {
186     ENGINE *eng = ENGINE_new();
187
188     if (eng == NULL) {
189         return NULL;
190     }
191
192     if (!padlock_bind_helper(eng)) {
193         ENGINE_free(eng);
194         return NULL;
195     }
196
197     return eng;
198 }
199 #   endif
200
201 /* Check availability of the engine */
202 static int padlock_init(ENGINE *e)
203 {
204     return (padlock_use_rng || padlock_use_ace);
205 }
206
207 /*
208  * This stuff is needed if this ENGINE is being compiled into a
209  * self-contained shared-library.
210  */
211 #   ifdef DYNAMIC_ENGINE
212 static int padlock_bind_fn(ENGINE *e, const char *id)
213 {
214     if (id && (strcmp(id, padlock_id) != 0)) {
215         return 0;
216     }
217
218     if (!padlock_bind_helper(e)) {
219         return 0;
220     }
221
222     return 1;
223 }
224
225 IMPLEMENT_DYNAMIC_CHECK_FN()
226 IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
227 #   endif                       /* DYNAMIC_ENGINE */
228 /* ===== Here comes the "real" engine ===== */
229
230 /* Some AES-related constants */
231 #   define AES_BLOCK_SIZE          16
232 #   define AES_KEY_SIZE_128        16
233 #   define AES_KEY_SIZE_192        24
234 #   define AES_KEY_SIZE_256        32
235     /*
236      * Here we store the status information relevant to the current context.
237      */
238     /*
239      * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
240      * the order of items in this structure.  Don't blindly modify, reorder,
241      * etc!
242      */
243 struct padlock_cipher_data {
244     unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
245     union {
246         unsigned int pad[4];
247         struct {
248             int rounds:4;
249             int dgst:1;         /* n/a in C3 */
250             int align:1;        /* n/a in C3 */
251             int ciphr:1;        /* n/a in C3 */
252             unsigned int keygen:1;
253             int interm:1;
254             unsigned int encdec:1;
255             int ksize:2;
256         } b;
257     } cword;                    /* Control word */
258     AES_KEY ks;                 /* Encryption key */
259 };
260
261 /* Interface to assembler module */
262 unsigned int padlock_capability();
263 void padlock_key_bswap(AES_KEY *key);
264 void padlock_verify_context(struct padlock_cipher_data *ctx);
265 void padlock_reload_key();
266 void padlock_aes_block(void *out, const void *inp,
267                        struct padlock_cipher_data *ctx);
268 int padlock_ecb_encrypt(void *out, const void *inp,
269                         struct padlock_cipher_data *ctx, size_t len);
270 int padlock_cbc_encrypt(void *out, const void *inp,
271                         struct padlock_cipher_data *ctx, size_t len);
272 int padlock_cfb_encrypt(void *out, const void *inp,
273                         struct padlock_cipher_data *ctx, size_t len);
274 int padlock_ofb_encrypt(void *out, const void *inp,
275                         struct padlock_cipher_data *ctx, size_t len);
276 int padlock_ctr32_encrypt(void *out, const void *inp,
277                           struct padlock_cipher_data *ctx, size_t len);
278 int padlock_xstore(void *out, int edx);
279 void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
280 void padlock_sha1(void *ctx, const void *inp, size_t len);
281 void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
282 void padlock_sha256(void *ctx, const void *inp, size_t len);
283
284 /*
285  * Load supported features of the CPU to see if the PadLock is available.
286  */
287 static int padlock_available(void)
288 {
289     unsigned int edx = padlock_capability();
290
291     /* Fill up some flags */
292     padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
293     padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
294
295     return padlock_use_ace + padlock_use_rng;
296 }
297
298 /* ===== AES encryption/decryption ===== */
299
300 #   if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
301 #    define NID_aes_128_cfb NID_aes_128_cfb128
302 #   endif
303
304 #   if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
305 #    define NID_aes_128_ofb NID_aes_128_ofb128
306 #   endif
307
308 #   if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
309 #    define NID_aes_192_cfb NID_aes_192_cfb128
310 #   endif
311
312 #   if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
313 #    define NID_aes_192_ofb NID_aes_192_ofb128
314 #   endif
315
316 #   if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
317 #    define NID_aes_256_cfb NID_aes_256_cfb128
318 #   endif
319
320 #   if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
321 #    define NID_aes_256_ofb NID_aes_256_ofb128
322 #   endif
323
324 /* List of supported ciphers. */
325 static const int padlock_cipher_nids[] = {
326     NID_aes_128_ecb,
327     NID_aes_128_cbc,
328     NID_aes_128_cfb,
329     NID_aes_128_ofb,
330     NID_aes_128_ctr,
331
332     NID_aes_192_ecb,
333     NID_aes_192_cbc,
334     NID_aes_192_cfb,
335     NID_aes_192_ofb,
336     NID_aes_192_ctr,
337
338     NID_aes_256_ecb,
339     NID_aes_256_cbc,
340     NID_aes_256_cfb,
341     NID_aes_256_ofb,
342     NID_aes_256_ctr
343 };
344
345 static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
346                                       sizeof(padlock_cipher_nids[0]));
347
348 /* Function prototypes ... */
349 static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
350                                 const unsigned char *iv, int enc);
351
352 #   define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) +         \
353         ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F )      )
354 #   define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
355         NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
356
357 static int
358 padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
359                    const unsigned char *in_arg, size_t nbytes)
360 {
361     return padlock_ecb_encrypt(out_arg, in_arg,
362                                ALIGNED_CIPHER_DATA(ctx), nbytes);
363 }
364
365 static int
366 padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
367                    const unsigned char *in_arg, size_t nbytes)
368 {
369     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
370     int ret;
371
372     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
373     if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
374         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
375     return ret;
376 }
377
378 static int
379 padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
380                    const unsigned char *in_arg, size_t nbytes)
381 {
382     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
383     size_t chunk;
384
385     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
386         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
387
388         if (chunk >= AES_BLOCK_SIZE)
389             return 0;           /* bogus value */
390
391         if (EVP_CIPHER_CTX_encrypting(ctx))
392             while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
393                 ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
394                 chunk++, nbytes--;
395         } else
396             while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
397                 unsigned char c = *(in_arg++);
398                 *(out_arg++) = c ^ ivp[chunk];
399                 ivp[chunk++] = c, nbytes--;
400             }
401
402         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
403     }
404
405     if (nbytes == 0)
406         return 1;
407
408     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
409
410     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
411         if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
412             return 0;
413         nbytes -= chunk;
414     }
415
416     if (nbytes) {
417         unsigned char *ivp = cdata->iv;
418
419         out_arg += chunk;
420         in_arg += chunk;
421         EVP_CIPHER_CTX_set_num(ctx, nbytes);
422         if (cdata->cword.b.encdec) {
423             cdata->cword.b.encdec = 0;
424             padlock_reload_key();
425             padlock_aes_block(ivp, ivp, cdata);
426             cdata->cword.b.encdec = 1;
427             padlock_reload_key();
428             while (nbytes) {
429                 unsigned char c = *(in_arg++);
430                 *(out_arg++) = c ^ *ivp;
431                 *(ivp++) = c, nbytes--;
432             }
433         } else {
434             padlock_reload_key();
435             padlock_aes_block(ivp, ivp, cdata);
436             padlock_reload_key();
437             while (nbytes) {
438                 *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
439                 ivp++, nbytes--;
440             }
441         }
442     }
443
444     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
445
446     return 1;
447 }
448
449 static int
450 padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
451                    const unsigned char *in_arg, size_t nbytes)
452 {
453     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
454     size_t chunk;
455
456     /*
457      * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
458      */
459     if ((chunk = EVP_CIPHER_CTX_num(ctx))) {   /* borrow chunk variable */
460         unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
461
462         if (chunk >= AES_BLOCK_SIZE)
463             return 0;           /* bogus value */
464
465         while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
466             *(out_arg++) = *(in_arg++) ^ ivp[chunk];
467             chunk++, nbytes--;
468         }
469
470         EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
471     }
472
473     if (nbytes == 0)
474         return 1;
475
476     memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
477
478     if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
479         if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
480             return 0;
481         nbytes -= chunk;
482     }
483
484     if (nbytes) {
485         unsigned char *ivp = cdata->iv;
486
487         out_arg += chunk;
488         in_arg += chunk;
489         EVP_CIPHER_CTX_set_num(ctx, nbytes);
490         padlock_reload_key();   /* empirically found */
491         padlock_aes_block(ivp, ivp, cdata);
492         padlock_reload_key();   /* empirically found */
493         while (nbytes) {
494             *(out_arg++) = *(in_arg++) ^ *ivp;
495             ivp++, nbytes--;
496         }
497     }
498
499     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
500
501     return 1;
502 }
503
504 static void padlock_ctr32_encrypt_glue(const unsigned char *in,
505                                        unsigned char *out, size_t blocks,
506                                        struct padlock_cipher_data *ctx,
507                                        const unsigned char *ivec)
508 {
509     memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
510     padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
511 }
512
513 static int
514 padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
515                    const unsigned char *in_arg, size_t nbytes)
516 {
517     struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
518     unsigned int num = EVP_CIPHER_CTX_num(ctx);
519
520     CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
521                                 cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
522                                 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
523                                 (ctr128_f) padlock_ctr32_encrypt_glue);
524
525     EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
526     return 1;
527 }
528
529 #   define EVP_CIPHER_block_size_ECB       AES_BLOCK_SIZE
530 #   define EVP_CIPHER_block_size_CBC       AES_BLOCK_SIZE
531 #   define EVP_CIPHER_block_size_OFB       1
532 #   define EVP_CIPHER_block_size_CFB       1
533 #   define EVP_CIPHER_block_size_CTR       1
534
535 /*
536  * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
537  * of preprocessor magic :-)
538  */
539 #   define DECLARE_AES_EVP(ksize,lmode,umode)      \
540 static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
541 static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
542 {                                                                       \
543     if (_hidden_aes_##ksize##_##lmode == NULL                           \
544         && ((_hidden_aes_##ksize##_##lmode =                            \
545              EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode,             \
546                                  EVP_CIPHER_block_size_##umode,         \
547                                  AES_KEY_SIZE_##ksize)) == NULL         \
548             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
549                                               AES_BLOCK_SIZE)           \
550             || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
551                                           0 | EVP_CIPH_##umode##_MODE)  \
552             || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
553                                          padlock_aes_init_key)          \
554             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
555                                               padlock_##lmode##_cipher) \
556             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
557                                                   sizeof(struct padlock_cipher_data) + 16) \
558             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
559                                                     EVP_CIPHER_set_asn1_iv) \
560             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
561                                                     EVP_CIPHER_get_asn1_iv))) { \
562         EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode);            \
563         _hidden_aes_##ksize##_##lmode = NULL;                           \
564     }                                                                   \
565     return _hidden_aes_##ksize##_##lmode;                               \
566 }
567
568 DECLARE_AES_EVP(128, ecb, ECB)
569 DECLARE_AES_EVP(128, cbc, CBC)
570 DECLARE_AES_EVP(128, cfb, CFB)
571 DECLARE_AES_EVP(128, ofb, OFB)
572 DECLARE_AES_EVP(128, ctr, CTR)
573
574 DECLARE_AES_EVP(192, ecb, ECB)
575 DECLARE_AES_EVP(192, cbc, CBC)
576 DECLARE_AES_EVP(192, cfb, CFB)
577 DECLARE_AES_EVP(192, ofb, OFB)
578 DECLARE_AES_EVP(192, ctr, CTR)
579
580 DECLARE_AES_EVP(256, ecb, ECB)
581 DECLARE_AES_EVP(256, cbc, CBC)
582 DECLARE_AES_EVP(256, cfb, CFB)
583 DECLARE_AES_EVP(256, ofb, OFB)
584 DECLARE_AES_EVP(256, ctr, CTR)
585
586 static int
587 padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
588                 int nid)
589 {
590     /* No specific cipher => return a list of supported nids ... */
591     if (!cipher) {
592         *nids = padlock_cipher_nids;
593         return padlock_cipher_nids_num;
594     }
595
596     /* ... or the requested "cipher" otherwise */
597     switch (nid) {
598     case NID_aes_128_ecb:
599         *cipher = padlock_aes_128_ecb();
600         break;
601     case NID_aes_128_cbc:
602         *cipher = padlock_aes_128_cbc();
603         break;
604     case NID_aes_128_cfb:
605         *cipher = padlock_aes_128_cfb();
606         break;
607     case NID_aes_128_ofb:
608         *cipher = padlock_aes_128_ofb();
609         break;
610     case NID_aes_128_ctr:
611         *cipher = padlock_aes_128_ctr();
612         break;
613
614     case NID_aes_192_ecb:
615         *cipher = padlock_aes_192_ecb();
616         break;
617     case NID_aes_192_cbc:
618         *cipher = padlock_aes_192_cbc();
619         break;
620     case NID_aes_192_cfb:
621         *cipher = padlock_aes_192_cfb();
622         break;
623     case NID_aes_192_ofb:
624         *cipher = padlock_aes_192_ofb();
625         break;
626     case NID_aes_192_ctr:
627         *cipher = padlock_aes_192_ctr();
628         break;
629
630     case NID_aes_256_ecb:
631         *cipher = padlock_aes_256_ecb();
632         break;
633     case NID_aes_256_cbc:
634         *cipher = padlock_aes_256_cbc();
635         break;
636     case NID_aes_256_cfb:
637         *cipher = padlock_aes_256_cfb();
638         break;
639     case NID_aes_256_ofb:
640         *cipher = padlock_aes_256_ofb();
641         break;
642     case NID_aes_256_ctr:
643         *cipher = padlock_aes_256_ctr();
644         break;
645
646     default:
647         /* Sorry, we don't support this NID */
648         *cipher = NULL;
649         return 0;
650     }
651
652     return 1;
653 }
654
655 /* Prepare the encryption key for PadLock usage */
656 static int
657 padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
658                      const unsigned char *iv, int enc)
659 {
660     struct padlock_cipher_data *cdata;
661     int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
662     unsigned long mode = EVP_CIPHER_CTX_mode(ctx);
663
664     if (key == NULL)
665         return 0;               /* ERROR */
666
667     cdata = ALIGNED_CIPHER_DATA(ctx);
668     memset(cdata, 0, sizeof(*cdata));
669
670     /* Prepare Control word. */
671     if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
672         cdata->cword.b.encdec = 0;
673     else
674         cdata->cword.b.encdec = (EVP_CIPHER_CTX_encrypting(ctx) == 0);
675     cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
676     cdata->cword.b.ksize = (key_len - 128) / 64;
677
678     switch (key_len) {
679     case 128:
680         /*
681          * PadLock can generate an extended key for AES128 in hardware
682          */
683         memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
684         cdata->cword.b.keygen = 0;
685         break;
686
687     case 192:
688     case 256:
689         /*
690          * Generate an extended AES key in software. Needed for AES192/AES256
691          */
692         /*
693          * Well, the above applies to Stepping 8 CPUs and is listed as
694          * hardware errata. They most likely will fix it at some point and
695          * then a check for stepping would be due here.
696          */
697         if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
698             && !enc)
699             AES_set_decrypt_key(key, key_len, &cdata->ks);
700         else
701             AES_set_encrypt_key(key, key_len, &cdata->ks);
702 #   ifndef AES_ASM
703         /*
704          * OpenSSL C functions use byte-swapped extended key.
705          */
706         padlock_key_bswap(&cdata->ks);
707 #   endif
708         cdata->cword.b.keygen = 1;
709         break;
710
711     default:
712         /* ERROR */
713         return 0;
714     }
715
716     /*
717      * This is done to cover for cases when user reuses the
718      * context for new key. The catch is that if we don't do
719      * this, padlock_eas_cipher might proceed with old key...
720      */
721     padlock_reload_key();
722
723     return 1;
724 }
725
726 /* ===== Random Number Generator ===== */
727 /*
728  * This code is not engaged. The reason is that it does not comply
729  * with recommendations for VIA RNG usage for secure applications
730  * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
731  * provide meaningful error control...
732  */
733 /*
734  * Wrapper that provides an interface between the API and the raw PadLock
735  * RNG
736  */
737 static int padlock_rand_bytes(unsigned char *output, int count)
738 {
739     unsigned int eax, buf;
740
741     while (count >= 8) {
742         eax = padlock_xstore(output, 0);
743         if (!(eax & (1 << 6)))
744             return 0;           /* RNG disabled */
745         /* this ---vv--- covers DC bias, Raw Bits and String Filter */
746         if (eax & (0x1F << 10))
747             return 0;
748         if ((eax & 0x1F) == 0)
749             continue;           /* no data, retry... */
750         if ((eax & 0x1F) != 8)
751             return 0;           /* fatal failure...  */
752         output += 8;
753         count -= 8;
754     }
755     while (count > 0) {
756         eax = padlock_xstore(&buf, 3);
757         if (!(eax & (1 << 6)))
758             return 0;           /* RNG disabled */
759         /* this ---vv--- covers DC bias, Raw Bits and String Filter */
760         if (eax & (0x1F << 10))
761             return 0;
762         if ((eax & 0x1F) == 0)
763             continue;           /* no data, retry... */
764         if ((eax & 0x1F) != 1)
765             return 0;           /* fatal failure...  */
766         *output++ = (unsigned char)buf;
767         count--;
768     }
769     OPENSSL_cleanse(&buf, sizeof(buf));
770
771     return 1;
772 }
773
774 /* Dummy but necessary function */
775 static int padlock_rand_status(void)
776 {
777     return 1;
778 }
779
780 /* Prepare structure for registration */
781 static RAND_METHOD padlock_rand = {
782     NULL,                       /* seed */
783     padlock_rand_bytes,         /* bytes */
784     NULL,                       /* cleanup */
785     NULL,                       /* add */
786     padlock_rand_bytes,         /* pseudorand */
787     padlock_rand_status,        /* rand status */
788 };
789
790 #  endif                        /* COMPILE_HW_PADLOCK */
791 # endif                         /* !OPENSSL_NO_HW_PADLOCK */
792 #endif                          /* !OPENSSL_NO_HW */
793
794 #if defined(OPENSSL_NO_HW) || defined(OPENSSL_NO_HW_PADLOCK) \
795         || !defined(COMPILE_HW_PADLOCK)
796 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
797 OPENSSL_EXPORT
798     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
799 OPENSSL_EXPORT
800     int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
801 {
802     return 0;
803 }
804
805 IMPLEMENT_DYNAMIC_CHECK_FN()
806 # endif
807 #endif