Coverage Report

Created: 2025-08-29 06:32

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