Coverage Report

Created: 2024-05-15 07:14

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