Coverage Report

Created: 2023-09-25 06:41

/src/openssl/crypto/evp/e_aes.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2022 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 EVP AES ciphers.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <assert.h>
18
#include <openssl/opensslconf.h>
19
#include <openssl/crypto.h>
20
#include <openssl/evp.h>
21
#include <openssl/err.h>
22
#include <openssl/aes.h>
23
#include <openssl/rand.h>
24
#include <openssl/cmac.h>
25
#include "crypto/evp.h"
26
#include "internal/cryptlib.h"
27
#include "crypto/modes.h"
28
#include "crypto/siv.h"
29
#include "crypto/aes_platform.h"
30
#include "evp_local.h"
31
32
typedef struct {
33
    union {
34
        OSSL_UNION_ALIGN;
35
        AES_KEY ks;
36
    } ks;
37
    block128_f block;
38
    union {
39
        cbc128_f cbc;
40
        ctr128_f ctr;
41
    } stream;
42
} EVP_AES_KEY;
43
44
typedef struct {
45
    union {
46
        OSSL_UNION_ALIGN;
47
        AES_KEY ks;
48
    } ks;                       /* AES key schedule to use */
49
    int key_set;                /* Set if key initialised */
50
    int iv_set;                 /* Set if an iv is set */
51
    GCM128_CONTEXT gcm;
52
    unsigned char *iv;          /* Temporary IV store */
53
    int ivlen;                  /* IV length */
54
    int taglen;
55
    int iv_gen;                 /* It is OK to generate IVs */
56
    int iv_gen_rand;            /* No IV was specified, so generate a rand IV */
57
    int tls_aad_len;            /* TLS AAD length */
58
    uint64_t tls_enc_records;   /* Number of TLS records encrypted */
59
    ctr128_f ctr;
60
} EVP_AES_GCM_CTX;
61
62
typedef struct {
63
    union {
64
        OSSL_UNION_ALIGN;
65
        AES_KEY ks;
66
    } ks1, ks2;                 /* AES key schedules to use */
67
    XTS128_CONTEXT xts;
68
    void (*stream) (const unsigned char *in,
69
                    unsigned char *out, size_t length,
70
                    const AES_KEY *key1, const AES_KEY *key2,
71
                    const unsigned char iv[16]);
72
} EVP_AES_XTS_CTX;
73
74
#ifdef FIPS_MODULE
75
static const int allow_insecure_decrypt = 0;
76
#else
77
static const int allow_insecure_decrypt = 1;
78
#endif
79
80
typedef struct {
81
    union {
82
        OSSL_UNION_ALIGN;
83
        AES_KEY ks;
84
    } ks;                       /* AES key schedule to use */
85
    int key_set;                /* Set if key initialised */
86
    int iv_set;                 /* Set if an iv is set */
87
    int tag_set;                /* Set if tag is valid */
88
    int len_set;                /* Set if message length set */
89
    int L, M;                   /* L and M parameters from RFC3610 */
90
    int tls_aad_len;            /* TLS AAD length */
91
    CCM128_CONTEXT ccm;
92
    ccm128_f str;
93
} EVP_AES_CCM_CTX;
94
95
#ifndef OPENSSL_NO_OCB
96
typedef struct {
97
    union {
98
        OSSL_UNION_ALIGN;
99
        AES_KEY ks;
100
    } ksenc;                    /* AES key schedule to use for encryption */
101
    union {
102
        OSSL_UNION_ALIGN;
103
        AES_KEY ks;
104
    } ksdec;                    /* AES key schedule to use for decryption */
105
    int key_set;                /* Set if key initialised */
106
    int iv_set;                 /* Set if an iv is set */
107
    OCB128_CONTEXT ocb;
108
    unsigned char *iv;          /* Temporary IV store */
109
    unsigned char tag[16];
110
    unsigned char data_buf[16]; /* Store partial data blocks */
111
    unsigned char aad_buf[16];  /* Store partial AAD blocks */
112
    int data_buf_len;
113
    int aad_buf_len;
114
    int ivlen;                  /* IV length */
115
    int taglen;
116
} EVP_AES_OCB_CTX;
117
#endif
118
119
0
#define MAXBITCHUNK     ((size_t)1<<(sizeof(size_t)*8-4))
120
121
/* increment counter (64-bit int) by 1 */
122
static void ctr64_inc(unsigned char *counter)
123
0
{
124
0
    int n = 8;
125
0
    unsigned char c;
126
127
0
    do {
128
0
        --n;
129
0
        c = counter[n];
130
0
        ++c;
131
0
        counter[n] = c;
132
0
        if (c)
133
0
            return;
134
0
    } while (n);
135
0
}
136
137
#if defined(AESNI_CAPABLE)
138
# if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
139
#  define AES_GCM_ASM2(gctx)      (gctx->gcm.block==(block128_f)aesni_encrypt && \
140
                                 gctx->gcm.ghash==gcm_ghash_avx)
141
#  undef AES_GCM_ASM2          /* minor size optimization */
142
# endif
143
144
static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
145
                          const unsigned char *iv, int enc)
146
0
{
147
0
    int ret, mode;
148
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
149
0
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
150
151
0
    if (keylen <= 0) {
152
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
153
0
        return 0;
154
0
    }
155
0
    mode = EVP_CIPHER_CTX_get_mode(ctx);
156
0
    if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
157
0
        && !enc) {
158
0
        ret = aesni_set_decrypt_key(key, keylen, &dat->ks.ks);
159
0
        dat->block = (block128_f) aesni_decrypt;
160
0
        dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
161
0
            (cbc128_f) aesni_cbc_encrypt : NULL;
162
0
    } else {
163
0
        ret = aesni_set_encrypt_key(key, keylen, &dat->ks.ks);
164
0
        dat->block = (block128_f) aesni_encrypt;
165
0
        if (mode == EVP_CIPH_CBC_MODE)
166
0
            dat->stream.cbc = (cbc128_f) aesni_cbc_encrypt;
167
0
        else if (mode == EVP_CIPH_CTR_MODE)
168
0
            dat->stream.ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
169
0
        else
170
0
            dat->stream.cbc = NULL;
171
0
    }
172
173
0
    if (ret < 0) {
174
0
        ERR_raise(ERR_LIB_EVP, EVP_R_AES_KEY_SETUP_FAILED);
175
0
        return 0;
176
0
    }
177
178
0
    return 1;
179
0
}
180
181
static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
182
                            const unsigned char *in, size_t len)
183
0
{
184
0
    aesni_cbc_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
185
0
                      ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));
186
187
0
    return 1;
188
0
}
189
190
static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
191
                            const unsigned char *in, size_t len)
192
0
{
193
0
    size_t bl = EVP_CIPHER_CTX_get_block_size(ctx);
194
195
0
    if (len < bl)
196
0
        return 1;
197
198
0
    aesni_ecb_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
199
0
                      EVP_CIPHER_CTX_is_encrypting(ctx));
200
201
0
    return 1;
202
0
}
203
204
# define aesni_ofb_cipher aes_ofb_cipher
205
static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
206
                            const unsigned char *in, size_t len);
207
208
# define aesni_cfb_cipher aes_cfb_cipher
209
static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
210
                            const unsigned char *in, size_t len);
211
212
# define aesni_cfb8_cipher aes_cfb8_cipher
213
static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
214
                             const unsigned char *in, size_t len);
215
216
# define aesni_cfb1_cipher aes_cfb1_cipher
217
static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
218
                             const unsigned char *in, size_t len);
219
220
# define aesni_ctr_cipher aes_ctr_cipher
221
static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
222
                            const unsigned char *in, size_t len);
223
224
static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
225
                              const unsigned char *iv, int enc)
226
0
{
227
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX, ctx);
228
229
0
    if (iv == NULL && key == NULL)
230
0
        return 1;
231
232
0
    if (key) {
233
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
234
235
0
        if (keylen <= 0) {
236
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
237
0
            return 0;
238
0
        }
239
0
        aesni_set_encrypt_key(key, keylen, &gctx->ks.ks);
240
0
        CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f) aesni_encrypt);
241
0
        gctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
242
        /*
243
         * If we have an iv can set it directly, otherwise use saved IV.
244
         */
245
0
        if (iv == NULL && gctx->iv_set)
246
0
            iv = gctx->iv;
247
0
        if (iv) {
248
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
249
0
            gctx->iv_set = 1;
250
0
        }
251
0
        gctx->key_set = 1;
252
0
    } else {
253
        /* If key set use IV, otherwise copy */
254
0
        if (gctx->key_set)
255
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
256
0
        else
257
0
            memcpy(gctx->iv, iv, gctx->ivlen);
258
0
        gctx->iv_set = 1;
259
0
        gctx->iv_gen = 0;
260
0
    }
261
0
    return 1;
262
0
}
263
264
# define aesni_gcm_cipher aes_gcm_cipher
265
static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
266
                            const unsigned char *in, size_t len);
267
268
static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
269
                              const unsigned char *iv, int enc)
270
0
{
271
0
    EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
272
273
0
    if (iv == NULL && key == NULL)
274
0
        return 1;
275
276
0
    if (key) {
277
        /* The key is two half length keys in reality */
278
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
279
0
        const int bytes = keylen / 2;
280
0
        const int bits = bytes * 8;
281
282
0
        if (keylen <= 0) {
283
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
284
0
            return 0;
285
0
        }
286
        /*
287
         * Verify that the two keys are different.
288
         *
289
         * This addresses Rogaway's vulnerability.
290
         * See comment in aes_xts_init_key() below.
291
         */
292
0
        if ((!allow_insecure_decrypt || enc)
293
0
                && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
294
0
            ERR_raise(ERR_LIB_EVP, EVP_R_XTS_DUPLICATED_KEYS);
295
0
            return 0;
296
0
        }
297
298
        /* key_len is two AES keys */
299
0
        if (enc) {
300
0
            aesni_set_encrypt_key(key, bits, &xctx->ks1.ks);
301
0
            xctx->xts.block1 = (block128_f) aesni_encrypt;
302
0
            xctx->stream = aesni_xts_encrypt;
303
0
        } else {
304
0
            aesni_set_decrypt_key(key, bits, &xctx->ks1.ks);
305
0
            xctx->xts.block1 = (block128_f) aesni_decrypt;
306
0
            xctx->stream = aesni_xts_decrypt;
307
0
        }
308
309
0
        aesni_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
310
0
        xctx->xts.block2 = (block128_f) aesni_encrypt;
311
312
0
        xctx->xts.key1 = &xctx->ks1;
313
0
    }
314
315
0
    if (iv) {
316
0
        xctx->xts.key2 = &xctx->ks2;
317
0
        memcpy(ctx->iv, iv, 16);
318
0
    }
319
320
0
    return 1;
321
0
}
322
323
# define aesni_xts_cipher aes_xts_cipher
324
static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
325
                            const unsigned char *in, size_t len);
326
327
static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
328
                              const unsigned char *iv, int enc)
329
0
{
330
0
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
331
332
0
    if (iv == NULL && key == NULL)
333
0
        return 1;
334
335
0
    if (key != NULL) {
336
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
337
338
0
        if (keylen <= 0) {
339
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
340
0
            return 0;
341
0
        }
342
0
        aesni_set_encrypt_key(key, keylen, &cctx->ks.ks);
343
0
        CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
344
0
                           &cctx->ks, (block128_f) aesni_encrypt);
345
0
        cctx->str = enc ? (ccm128_f) aesni_ccm64_encrypt_blocks :
346
0
            (ccm128_f) aesni_ccm64_decrypt_blocks;
347
0
        cctx->key_set = 1;
348
0
    }
349
0
    if (iv) {
350
0
        memcpy(ctx->iv, iv, 15 - cctx->L);
351
0
        cctx->iv_set = 1;
352
0
    }
353
0
    return 1;
354
0
}
355
356
# define aesni_ccm_cipher aes_ccm_cipher
357
static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
358
                            const unsigned char *in, size_t len);
359
360
# ifndef OPENSSL_NO_OCB
361
static int aesni_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
362
                              const unsigned char *iv, int enc)
363
0
{
364
0
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
365
366
0
    if (iv == NULL && key == NULL)
367
0
        return 1;
368
369
0
    if (key != NULL) {
370
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
371
372
0
        if (keylen <= 0) {
373
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
374
0
            return 0;
375
0
        }
376
0
        do {
377
            /*
378
             * We set both the encrypt and decrypt key here because decrypt
379
             * needs both. We could possibly optimise to remove setting the
380
             * decrypt for an encryption operation.
381
             */
382
0
            aesni_set_encrypt_key(key, keylen, &octx->ksenc.ks);
383
0
            aesni_set_decrypt_key(key, keylen, &octx->ksdec.ks);
384
0
            if (!CRYPTO_ocb128_init(&octx->ocb,
385
0
                                    &octx->ksenc.ks, &octx->ksdec.ks,
386
0
                                    (block128_f) aesni_encrypt,
387
0
                                    (block128_f) aesni_decrypt,
388
0
                                    enc ? aesni_ocb_encrypt
389
0
                                        : aesni_ocb_decrypt))
390
0
                return 0;
391
0
        }
392
0
        while (0);
393
394
        /*
395
         * If we have an iv we can set it directly, otherwise use saved IV.
396
         */
397
0
        if (iv == NULL && octx->iv_set)
398
0
            iv = octx->iv;
399
0
        if (iv) {
400
0
            if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
401
0
                != 1)
402
0
                return 0;
403
0
            octx->iv_set = 1;
404
0
        }
405
0
        octx->key_set = 1;
406
0
    } else {
407
        /* If key set use IV, otherwise copy */
408
0
        if (octx->key_set)
409
0
            CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
410
0
        else
411
0
            memcpy(octx->iv, iv, octx->ivlen);
412
0
        octx->iv_set = 1;
413
0
    }
414
0
    return 1;
415
0
}
416
417
#  define aesni_ocb_cipher aes_ocb_cipher
418
static int aesni_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
419
                            const unsigned char *in, size_t len);
420
# endif                        /* OPENSSL_NO_OCB */
421
422
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
423
static const EVP_CIPHER aesni_##keylen##_##mode = { \
424
        nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
425
        flags|EVP_CIPH_##MODE##_MODE,   \
426
        EVP_ORIG_GLOBAL,                \
427
        aesni_init_key,                 \
428
        aesni_##mode##_cipher,          \
429
        NULL,                           \
430
        sizeof(EVP_AES_KEY),            \
431
        NULL,NULL,NULL,NULL }; \
432
static const EVP_CIPHER aes_##keylen##_##mode = { \
433
        nid##_##keylen##_##nmode,blocksize,     \
434
        keylen/8,ivlen,                 \
435
        flags|EVP_CIPH_##MODE##_MODE,   \
436
        EVP_ORIG_GLOBAL,                 \
437
        aes_init_key,                   \
438
        aes_##mode##_cipher,            \
439
        NULL,                           \
440
        sizeof(EVP_AES_KEY),            \
441
        NULL,NULL,NULL,NULL }; \
442
0
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
443
0
{ return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
Unexecuted instantiation: EVP_aes_128_cbc
Unexecuted instantiation: EVP_aes_128_ecb
Unexecuted instantiation: EVP_aes_128_ofb
Unexecuted instantiation: EVP_aes_128_cfb128
Unexecuted instantiation: EVP_aes_128_cfb1
Unexecuted instantiation: EVP_aes_128_cfb8
Unexecuted instantiation: EVP_aes_128_ctr
Unexecuted instantiation: EVP_aes_192_cbc
Unexecuted instantiation: EVP_aes_192_ecb
Unexecuted instantiation: EVP_aes_192_ofb
Unexecuted instantiation: EVP_aes_192_cfb128
Unexecuted instantiation: EVP_aes_192_cfb1
Unexecuted instantiation: EVP_aes_192_cfb8
Unexecuted instantiation: EVP_aes_192_ctr
Unexecuted instantiation: EVP_aes_256_cbc
Unexecuted instantiation: EVP_aes_256_ecb
Unexecuted instantiation: EVP_aes_256_ofb
Unexecuted instantiation: EVP_aes_256_cfb128
Unexecuted instantiation: EVP_aes_256_cfb1
Unexecuted instantiation: EVP_aes_256_cfb8
Unexecuted instantiation: EVP_aes_256_ctr
444
445
# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
446
static const EVP_CIPHER aesni_##keylen##_##mode = { \
447
        nid##_##keylen##_##mode,blocksize, \
448
        (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
449
        ivlen,                          \
450
        flags|EVP_CIPH_##MODE##_MODE,   \
451
        EVP_ORIG_GLOBAL,                \
452
        aesni_##mode##_init_key,        \
453
        aesni_##mode##_cipher,          \
454
        aes_##mode##_cleanup,           \
455
        sizeof(EVP_AES_##MODE##_CTX),   \
456
        NULL,NULL,aes_##mode##_ctrl,NULL }; \
457
static const EVP_CIPHER aes_##keylen##_##mode = { \
458
        nid##_##keylen##_##mode,blocksize, \
459
        (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
460
        ivlen,                          \
461
        flags|EVP_CIPH_##MODE##_MODE,   \
462
        EVP_ORIG_GLOBAL,                \
463
        aes_##mode##_init_key,          \
464
        aes_##mode##_cipher,            \
465
        aes_##mode##_cleanup,           \
466
        sizeof(EVP_AES_##MODE##_CTX),   \
467
        NULL,NULL,aes_##mode##_ctrl,NULL }; \
468
0
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
469
0
{ return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
Unexecuted instantiation: EVP_aes_128_gcm
Unexecuted instantiation: EVP_aes_192_gcm
Unexecuted instantiation: EVP_aes_256_gcm
Unexecuted instantiation: EVP_aes_128_xts
Unexecuted instantiation: EVP_aes_256_xts
Unexecuted instantiation: EVP_aes_128_ccm
Unexecuted instantiation: EVP_aes_192_ccm
Unexecuted instantiation: EVP_aes_256_ccm
Unexecuted instantiation: EVP_aes_128_ocb
Unexecuted instantiation: EVP_aes_192_ocb
Unexecuted instantiation: EVP_aes_256_ocb
470
471
#elif defined(SPARC_AES_CAPABLE)
472
473
static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
474
                           const unsigned char *iv, int enc)
475
{
476
    int ret, mode, bits;
477
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
478
479
    mode = EVP_CIPHER_CTX_get_mode(ctx);
480
    bits = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
481
    if (bits <= 0) {
482
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
483
        return 0;
484
    }
485
    if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
486
        && !enc) {
487
        ret = 0;
488
        aes_t4_set_decrypt_key(key, bits, &dat->ks.ks);
489
        dat->block = (block128_f) aes_t4_decrypt;
490
        switch (bits) {
491
        case 128:
492
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
493
                (cbc128_f) aes128_t4_cbc_decrypt : NULL;
494
            break;
495
        case 192:
496
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
497
                (cbc128_f) aes192_t4_cbc_decrypt : NULL;
498
            break;
499
        case 256:
500
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
501
                (cbc128_f) aes256_t4_cbc_decrypt : NULL;
502
            break;
503
        default:
504
            ret = -1;
505
        }
506
    } else {
507
        ret = 0;
508
        aes_t4_set_encrypt_key(key, bits, &dat->ks.ks);
509
        dat->block = (block128_f) aes_t4_encrypt;
510
        switch (bits) {
511
        case 128:
512
            if (mode == EVP_CIPH_CBC_MODE)
513
                dat->stream.cbc = (cbc128_f) aes128_t4_cbc_encrypt;
514
            else if (mode == EVP_CIPH_CTR_MODE)
515
                dat->stream.ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
516
            else
517
                dat->stream.cbc = NULL;
518
            break;
519
        case 192:
520
            if (mode == EVP_CIPH_CBC_MODE)
521
                dat->stream.cbc = (cbc128_f) aes192_t4_cbc_encrypt;
522
            else if (mode == EVP_CIPH_CTR_MODE)
523
                dat->stream.ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
524
            else
525
                dat->stream.cbc = NULL;
526
            break;
527
        case 256:
528
            if (mode == EVP_CIPH_CBC_MODE)
529
                dat->stream.cbc = (cbc128_f) aes256_t4_cbc_encrypt;
530
            else if (mode == EVP_CIPH_CTR_MODE)
531
                dat->stream.ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
532
            else
533
                dat->stream.cbc = NULL;
534
            break;
535
        default:
536
            ret = -1;
537
        }
538
    }
539
540
    if (ret < 0) {
541
        ERR_raise(ERR_LIB_EVP, EVP_R_AES_KEY_SETUP_FAILED);
542
        return 0;
543
    }
544
545
    return 1;
546
}
547
548
# define aes_t4_cbc_cipher aes_cbc_cipher
549
static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
550
                             const unsigned char *in, size_t len);
551
552
# define aes_t4_ecb_cipher aes_ecb_cipher
553
static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
554
                             const unsigned char *in, size_t len);
555
556
# define aes_t4_ofb_cipher aes_ofb_cipher
557
static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
558
                             const unsigned char *in, size_t len);
559
560
# define aes_t4_cfb_cipher aes_cfb_cipher
561
static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
562
                             const unsigned char *in, size_t len);
563
564
# define aes_t4_cfb8_cipher aes_cfb8_cipher
565
static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
566
                              const unsigned char *in, size_t len);
567
568
# define aes_t4_cfb1_cipher aes_cfb1_cipher
569
static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
570
                              const unsigned char *in, size_t len);
571
572
# define aes_t4_ctr_cipher aes_ctr_cipher
573
static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
574
                             const unsigned char *in, size_t len);
575
576
static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
577
                               const unsigned char *iv, int enc)
578
{
579
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
580
581
    if (iv == NULL && key == NULL)
582
        return 1;
583
    if (key) {
584
        const int bits = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
585
586
        if (bits <= 0) {
587
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
588
            return 0;
589
        }
590
        aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
591
        CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
592
                           (block128_f) aes_t4_encrypt);
593
        switch (bits) {
594
        case 128:
595
            gctx->ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
596
            break;
597
        case 192:
598
            gctx->ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
599
            break;
600
        case 256:
601
            gctx->ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
602
            break;
603
        default:
604
            return 0;
605
        }
606
        /*
607
         * If we have an iv can set it directly, otherwise use saved IV.
608
         */
609
        if (iv == NULL && gctx->iv_set)
610
            iv = gctx->iv;
611
        if (iv) {
612
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
613
            gctx->iv_set = 1;
614
        }
615
        gctx->key_set = 1;
616
    } else {
617
        /* If key set use IV, otherwise copy */
618
        if (gctx->key_set)
619
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
620
        else
621
            memcpy(gctx->iv, iv, gctx->ivlen);
622
        gctx->iv_set = 1;
623
        gctx->iv_gen = 0;
624
    }
625
    return 1;
626
}
627
628
# define aes_t4_gcm_cipher aes_gcm_cipher
629
static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
630
                             const unsigned char *in, size_t len);
631
632
static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
633
                               const unsigned char *iv, int enc)
634
{
635
    EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
636
637
    if (!iv && !key)
638
        return 1;
639
640
    if (key) {
641
        /* The key is two half length keys in reality */
642
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
643
        const int bytes = keylen / 2;
644
        const int bits = bytes * 8;
645
646
        if (keylen <= 0) {
647
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
648
            return 0;
649
        }
650
        /*
651
         * Verify that the two keys are different.
652
         *
653
         * This addresses Rogaway's vulnerability.
654
         * See comment in aes_xts_init_key() below.
655
         */
656
        if ((!allow_insecure_decrypt || enc)
657
                && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
658
            ERR_raise(ERR_LIB_EVP, EVP_R_XTS_DUPLICATED_KEYS);
659
            return 0;
660
        }
661
662
        xctx->stream = NULL;
663
        /* key_len is two AES keys */
664
        if (enc) {
665
            aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
666
            xctx->xts.block1 = (block128_f) aes_t4_encrypt;
667
            switch (bits) {
668
            case 128:
669
                xctx->stream = aes128_t4_xts_encrypt;
670
                break;
671
            case 256:
672
                xctx->stream = aes256_t4_xts_encrypt;
673
                break;
674
            default:
675
                return 0;
676
            }
677
        } else {
678
            aes_t4_set_decrypt_key(key, bits, &xctx->ks1.ks);
679
            xctx->xts.block1 = (block128_f) aes_t4_decrypt;
680
            switch (bits) {
681
            case 128:
682
                xctx->stream = aes128_t4_xts_decrypt;
683
                break;
684
            case 256:
685
                xctx->stream = aes256_t4_xts_decrypt;
686
                break;
687
            default:
688
                return 0;
689
            }
690
        }
691
692
        aes_t4_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
693
        xctx->xts.block2 = (block128_f) aes_t4_encrypt;
694
695
        xctx->xts.key1 = &xctx->ks1;
696
    }
697
698
    if (iv) {
699
        xctx->xts.key2 = &xctx->ks2;
700
        memcpy(ctx->iv, iv, 16);
701
    }
702
703
    return 1;
704
}
705
706
# define aes_t4_xts_cipher aes_xts_cipher
707
static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
708
                             const unsigned char *in, size_t len);
709
710
static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
711
                               const unsigned char *iv, int enc)
712
{
713
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
714
715
    if (iv == NULL && key == NULL)
716
        return 1;
717
718
    if (key != NULL) {
719
        const int bits = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
720
721
        if (bits <= 0) {
722
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
723
            return 0;
724
        }
725
        aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
726
        CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
727
                           &cctx->ks, (block128_f) aes_t4_encrypt);
728
        cctx->str = NULL;
729
        cctx->key_set = 1;
730
    }
731
    if (iv) {
732
        memcpy(ctx->iv, iv, 15 - cctx->L);
733
        cctx->iv_set = 1;
734
    }
735
    return 1;
736
}
737
738
# define aes_t4_ccm_cipher aes_ccm_cipher
739
static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
740
                             const unsigned char *in, size_t len);
741
742
# ifndef OPENSSL_NO_OCB
743
static int aes_t4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
744
                               const unsigned char *iv, int enc)
745
{
746
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
747
748
    if (iv == NULL && key == NULL)
749
        return 1;
750
751
    if (key != NULL) {
752
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
753
754
        if (keylen <= 0) {
755
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
756
            return 0;
757
        }
758
        do {
759
            /*
760
             * We set both the encrypt and decrypt key here because decrypt
761
             * needs both. We could possibly optimise to remove setting the
762
             * decrypt for an encryption operation.
763
             */
764
            aes_t4_set_encrypt_key(key, keylen, &octx->ksenc.ks);
765
            aes_t4_set_decrypt_key(key, keylen, &octx->ksdec.ks);
766
            if (!CRYPTO_ocb128_init(&octx->ocb,
767
                                    &octx->ksenc.ks, &octx->ksdec.ks,
768
                                    (block128_f) aes_t4_encrypt,
769
                                    (block128_f) aes_t4_decrypt,
770
                                    NULL))
771
                return 0;
772
        }
773
        while (0);
774
775
        /*
776
         * If we have an iv we can set it directly, otherwise use saved IV.
777
         */
778
        if (iv == NULL && octx->iv_set)
779
            iv = octx->iv;
780
        if (iv) {
781
            if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
782
                != 1)
783
                return 0;
784
            octx->iv_set = 1;
785
        }
786
        octx->key_set = 1;
787
    } else {
788
        /* If key set use IV, otherwise copy */
789
        if (octx->key_set)
790
            CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
791
        else
792
            memcpy(octx->iv, iv, octx->ivlen);
793
        octx->iv_set = 1;
794
    }
795
    return 1;
796
}
797
798
#  define aes_t4_ocb_cipher aes_ocb_cipher
799
static int aes_t4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
800
                             const unsigned char *in, size_t len);
801
# endif                        /* OPENSSL_NO_OCB */
802
803
# ifndef OPENSSL_NO_SIV
804
#  define aes_t4_siv_init_key aes_siv_init_key
805
#  define aes_t4_siv_cipher aes_siv_cipher
806
# endif /* OPENSSL_NO_SIV */
807
808
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
809
static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
810
        nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
811
        flags|EVP_CIPH_##MODE##_MODE,   \
812
        EVP_ORIG_GLOBAL,                \
813
        aes_t4_init_key,                \
814
        aes_t4_##mode##_cipher,         \
815
        NULL,                           \
816
        sizeof(EVP_AES_KEY),            \
817
        NULL,NULL,NULL,NULL }; \
818
static const EVP_CIPHER aes_##keylen##_##mode = { \
819
        nid##_##keylen##_##nmode,blocksize,     \
820
        keylen/8,ivlen, \
821
        flags|EVP_CIPH_##MODE##_MODE,   \
822
        EVP_ORIG_GLOBAL,                \
823
        aes_init_key,                   \
824
        aes_##mode##_cipher,            \
825
        NULL,                           \
826
        sizeof(EVP_AES_KEY),            \
827
        NULL,NULL,NULL,NULL }; \
828
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
829
{ return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
830
831
# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
832
static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
833
        nid##_##keylen##_##mode,blocksize, \
834
        (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
835
        ivlen,                          \
836
        flags|EVP_CIPH_##MODE##_MODE,   \
837
        EVP_ORIG_GLOBAL,                \
838
        aes_t4_##mode##_init_key,       \
839
        aes_t4_##mode##_cipher,         \
840
        aes_##mode##_cleanup,           \
841
        sizeof(EVP_AES_##MODE##_CTX),   \
842
        NULL,NULL,aes_##mode##_ctrl,NULL }; \
843
static const EVP_CIPHER aes_##keylen##_##mode = { \
844
        nid##_##keylen##_##mode,blocksize, \
845
        (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
846
        ivlen,                          \
847
        flags|EVP_CIPH_##MODE##_MODE,   \
848
        EVP_ORIG_GLOBAL,                \
849
        aes_##mode##_init_key,          \
850
        aes_##mode##_cipher,            \
851
        aes_##mode##_cleanup,           \
852
        sizeof(EVP_AES_##MODE##_CTX),   \
853
        NULL,NULL,aes_##mode##_ctrl,NULL }; \
854
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
855
{ return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
856
857
#elif defined(S390X_aes_128_CAPABLE)
858
/* IBM S390X support */
859
typedef struct {
860
    union {
861
        OSSL_UNION_ALIGN;
862
        /*-
863
         * KM-AES parameter block - begin
864
         * (see z/Architecture Principles of Operation >= SA22-7832-06)
865
         */
866
        struct {
867
            unsigned char k[32];
868
        } param;
869
        /* KM-AES parameter block - end */
870
    } km;
871
    unsigned int fc;
872
} S390X_AES_ECB_CTX;
873
874
typedef struct {
875
    union {
876
        OSSL_UNION_ALIGN;
877
        /*-
878
         * KMO-AES parameter block - begin
879
         * (see z/Architecture Principles of Operation >= SA22-7832-08)
880
         */
881
        struct {
882
            unsigned char cv[16];
883
            unsigned char k[32];
884
        } param;
885
        /* KMO-AES parameter block - end */
886
    } kmo;
887
    unsigned int fc;
888
889
    int res;
890
} S390X_AES_OFB_CTX;
891
892
typedef struct {
893
    union {
894
        OSSL_UNION_ALIGN;
895
        /*-
896
         * KMF-AES parameter block - begin
897
         * (see z/Architecture Principles of Operation >= SA22-7832-08)
898
         */
899
        struct {
900
            unsigned char cv[16];
901
            unsigned char k[32];
902
        } param;
903
        /* KMF-AES parameter block - end */
904
    } kmf;
905
    unsigned int fc;
906
907
    int res;
908
} S390X_AES_CFB_CTX;
909
910
typedef struct {
911
    union {
912
        OSSL_UNION_ALIGN;
913
        /*-
914
         * KMA-GCM-AES parameter block - begin
915
         * (see z/Architecture Principles of Operation >= SA22-7832-11)
916
         */
917
        struct {
918
            unsigned char reserved[12];
919
            union {
920
                unsigned int w;
921
                unsigned char b[4];
922
            } cv;
923
            union {
924
                unsigned long long g[2];
925
                unsigned char b[16];
926
            } t;
927
            unsigned char h[16];
928
            unsigned long long taadl;
929
            unsigned long long tpcl;
930
            union {
931
                unsigned long long g[2];
932
                unsigned int w[4];
933
            } j0;
934
            unsigned char k[32];
935
        } param;
936
        /* KMA-GCM-AES parameter block - end */
937
    } kma;
938
    unsigned int fc;
939
    int key_set;
940
941
    unsigned char *iv;
942
    int ivlen;
943
    int iv_set;
944
    int iv_gen;
945
946
    int taglen;
947
948
    unsigned char ares[16];
949
    unsigned char mres[16];
950
    unsigned char kres[16];
951
    int areslen;
952
    int mreslen;
953
    int kreslen;
954
955
    int tls_aad_len;
956
    uint64_t tls_enc_records;   /* Number of TLS records encrypted */
957
} S390X_AES_GCM_CTX;
958
959
typedef struct {
960
    union {
961
        OSSL_UNION_ALIGN;
962
        /*-
963
         * Padding is chosen so that ccm.kmac_param.k overlaps with key.k and
964
         * ccm.fc with key.k.rounds. Remember that on s390x, an AES_KEY's
965
         * rounds field is used to store the function code and that the key
966
         * schedule is not stored (if aes hardware support is detected).
967
         */
968
        struct {
969
            unsigned char pad[16];
970
            AES_KEY k;
971
        } key;
972
973
        struct {
974
            /*-
975
             * KMAC-AES parameter block - begin
976
             * (see z/Architecture Principles of Operation >= SA22-7832-08)
977
             */
978
            struct {
979
                union {
980
                    unsigned long long g[2];
981
                    unsigned char b[16];
982
                } icv;
983
                unsigned char k[32];
984
            } kmac_param;
985
            /* KMAC-AES parameter block - end */
986
987
            union {
988
                unsigned long long g[2];
989
                unsigned char b[16];
990
            } nonce;
991
            union {
992
                unsigned long long g[2];
993
                unsigned char b[16];
994
            } buf;
995
996
            unsigned long long blocks;
997
            int l;
998
            int m;
999
            int tls_aad_len;
1000
            int iv_set;
1001
            int tag_set;
1002
            int len_set;
1003
            int key_set;
1004
1005
            unsigned char pad[140];
1006
            unsigned int fc;
1007
        } ccm;
1008
    } aes;
1009
} S390X_AES_CCM_CTX;
1010
1011
# define s390x_aes_init_key aes_init_key
1012
static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1013
                              const unsigned char *iv, int enc);
1014
1015
# define S390X_AES_CBC_CTX              EVP_AES_KEY
1016
1017
# define s390x_aes_cbc_init_key aes_init_key
1018
1019
# define s390x_aes_cbc_cipher aes_cbc_cipher
1020
static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1021
                                const unsigned char *in, size_t len);
1022
1023
static int s390x_aes_ecb_init_key(EVP_CIPHER_CTX *ctx,
1024
                                  const unsigned char *key,
1025
                                  const unsigned char *iv, int enc)
1026
{
1027
    S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
1028
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1029
1030
    if (keylen <= 0) {
1031
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1032
        return 0;
1033
    }
1034
    cctx->fc = S390X_AES_FC(keylen);
1035
    if (!enc)
1036
        cctx->fc |= S390X_DECRYPT;
1037
1038
    memcpy(cctx->km.param.k, key, keylen);
1039
    return 1;
1040
}
1041
1042
static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1043
                                const unsigned char *in, size_t len)
1044
{
1045
    S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
1046
1047
    s390x_km(in, len, out, cctx->fc, &cctx->km.param);
1048
    return 1;
1049
}
1050
1051
static int s390x_aes_ofb_init_key(EVP_CIPHER_CTX *ctx,
1052
                                  const unsigned char *key,
1053
                                  const unsigned char *ivec, int enc)
1054
{
1055
    S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
1056
    const unsigned char *iv = ctx->oiv;
1057
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1058
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1059
1060
    if (keylen <= 0) {
1061
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1062
        return 0;
1063
    }
1064
    if (ivlen <= 0) {
1065
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
1066
        return 0;
1067
    }
1068
    memcpy(cctx->kmo.param.cv, iv, ivlen);
1069
    memcpy(cctx->kmo.param.k, key, keylen);
1070
    cctx->fc = S390X_AES_FC(keylen);
1071
    cctx->res = 0;
1072
    return 1;
1073
}
1074
1075
static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1076
                                const unsigned char *in, size_t len)
1077
{
1078
    S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
1079
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1080
    unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
1081
    int n = cctx->res;
1082
    int rem;
1083
1084
    memcpy(cctx->kmo.param.cv, iv, ivlen);
1085
    while (n && len) {
1086
        *out = *in ^ cctx->kmo.param.cv[n];
1087
        n = (n + 1) & 0xf;
1088
        --len;
1089
        ++in;
1090
        ++out;
1091
    }
1092
1093
    rem = len & 0xf;
1094
1095
    len &= ~(size_t)0xf;
1096
    if (len) {
1097
        s390x_kmo(in, len, out, cctx->fc, &cctx->kmo.param);
1098
1099
        out += len;
1100
        in += len;
1101
    }
1102
1103
    if (rem) {
1104
        s390x_km(cctx->kmo.param.cv, 16, cctx->kmo.param.cv, cctx->fc,
1105
                 cctx->kmo.param.k);
1106
1107
        while (rem--) {
1108
            out[n] = in[n] ^ cctx->kmo.param.cv[n];
1109
            ++n;
1110
        }
1111
    }
1112
1113
    memcpy(iv, cctx->kmo.param.cv, ivlen);
1114
    cctx->res = n;
1115
    return 1;
1116
}
1117
1118
static int s390x_aes_cfb_init_key(EVP_CIPHER_CTX *ctx,
1119
                                  const unsigned char *key,
1120
                                  const unsigned char *ivec, int enc)
1121
{
1122
    S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1123
    const unsigned char *iv = ctx->oiv;
1124
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1125
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1126
1127
    if (keylen <= 0) {
1128
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1129
        return 0;
1130
    }
1131
    if (ivlen <= 0) {
1132
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
1133
        return 0;
1134
    }
1135
    cctx->fc = S390X_AES_FC(keylen);
1136
    cctx->fc |= 16 << 24;   /* 16 bytes cipher feedback */
1137
    if (!enc)
1138
        cctx->fc |= S390X_DECRYPT;
1139
1140
    cctx->res = 0;
1141
    memcpy(cctx->kmf.param.cv, iv, ivlen);
1142
    memcpy(cctx->kmf.param.k, key, keylen);
1143
    return 1;
1144
}
1145
1146
static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1147
                                const unsigned char *in, size_t len)
1148
{
1149
    S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1150
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1151
    const int enc = EVP_CIPHER_CTX_is_encrypting(ctx);
1152
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1153
    unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
1154
    int n = cctx->res;
1155
    int rem;
1156
    unsigned char tmp;
1157
1158
    if (keylen <= 0) {
1159
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1160
        return 0;
1161
    }
1162
    if (ivlen <= 0) {
1163
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
1164
        return 0;
1165
    }
1166
    memcpy(cctx->kmf.param.cv, iv, ivlen);
1167
    while (n && len) {
1168
        tmp = *in;
1169
        *out = cctx->kmf.param.cv[n] ^ tmp;
1170
        cctx->kmf.param.cv[n] = enc ? *out : tmp;
1171
        n = (n + 1) & 0xf;
1172
        --len;
1173
        ++in;
1174
        ++out;
1175
    }
1176
1177
    rem = len & 0xf;
1178
1179
    len &= ~(size_t)0xf;
1180
    if (len) {
1181
        s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
1182
1183
        out += len;
1184
        in += len;
1185
    }
1186
1187
    if (rem) {
1188
        s390x_km(cctx->kmf.param.cv, 16, cctx->kmf.param.cv,
1189
                 S390X_AES_FC(keylen), cctx->kmf.param.k);
1190
1191
        while (rem--) {
1192
            tmp = in[n];
1193
            out[n] = cctx->kmf.param.cv[n] ^ tmp;
1194
            cctx->kmf.param.cv[n] = enc ? out[n] : tmp;
1195
            ++n;
1196
        }
1197
    }
1198
1199
    memcpy(iv, cctx->kmf.param.cv, ivlen);
1200
    cctx->res = n;
1201
    return 1;
1202
}
1203
1204
static int s390x_aes_cfb8_init_key(EVP_CIPHER_CTX *ctx,
1205
                                   const unsigned char *key,
1206
                                   const unsigned char *ivec, int enc)
1207
{
1208
    S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1209
    const unsigned char *iv = ctx->oiv;
1210
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1211
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1212
1213
    if (keylen <= 0) {
1214
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1215
        return 0;
1216
    }
1217
    if (ivlen <= 0) {
1218
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
1219
        return 0;
1220
    }
1221
    cctx->fc = S390X_AES_FC(keylen);
1222
    cctx->fc |= 1 << 24;   /* 1 byte cipher feedback */
1223
    if (!enc)
1224
        cctx->fc |= S390X_DECRYPT;
1225
1226
    memcpy(cctx->kmf.param.cv, iv, ivlen);
1227
    memcpy(cctx->kmf.param.k, key, keylen);
1228
    return 1;
1229
}
1230
1231
static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1232
                                 const unsigned char *in, size_t len)
1233
{
1234
    S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1235
    const int ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
1236
    unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
1237
1238
    memcpy(cctx->kmf.param.cv, iv, ivlen);
1239
    s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
1240
    memcpy(iv, cctx->kmf.param.cv, ivlen);
1241
    return 1;
1242
}
1243
1244
# define s390x_aes_cfb1_init_key aes_init_key
1245
1246
# define s390x_aes_cfb1_cipher aes_cfb1_cipher
1247
static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1248
                                 const unsigned char *in, size_t len);
1249
1250
# define S390X_AES_CTR_CTX              EVP_AES_KEY
1251
1252
# define s390x_aes_ctr_init_key aes_init_key
1253
1254
# define s390x_aes_ctr_cipher aes_ctr_cipher
1255
static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1256
                                const unsigned char *in, size_t len);
1257
1258
/* iv + padding length for iv lengths != 12 */
1259
# define S390X_gcm_ivpadlen(i)  ((((i) + 15) >> 4 << 4) + 16)
1260
1261
/*-
1262
 * Process additional authenticated data. Returns 0 on success. Code is
1263
 * big-endian.
1264
 */
1265
static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad,
1266
                             size_t len)
1267
{
1268
    unsigned long long alen;
1269
    int n, rem;
1270
1271
    if (ctx->kma.param.tpcl)
1272
        return -2;
1273
1274
    alen = ctx->kma.param.taadl + len;
1275
    if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
1276
        return -1;
1277
    ctx->kma.param.taadl = alen;
1278
1279
    n = ctx->areslen;
1280
    if (n) {
1281
        while (n && len) {
1282
            ctx->ares[n] = *aad;
1283
            n = (n + 1) & 0xf;
1284
            ++aad;
1285
            --len;
1286
        }
1287
        /* ctx->ares contains a complete block if offset has wrapped around */
1288
        if (!n) {
1289
            s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
1290
            ctx->fc |= S390X_KMA_HS;
1291
        }
1292
        ctx->areslen = n;
1293
    }
1294
1295
    rem = len & 0xf;
1296
1297
    len &= ~(size_t)0xf;
1298
    if (len) {
1299
        s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
1300
        aad += len;
1301
        ctx->fc |= S390X_KMA_HS;
1302
    }
1303
1304
    if (rem) {
1305
        ctx->areslen = rem;
1306
1307
        do {
1308
            --rem;
1309
            ctx->ares[rem] = aad[rem];
1310
        } while (rem);
1311
    }
1312
    return 0;
1313
}
1314
1315
/*-
1316
 * En/de-crypt plain/cipher-text and authenticate ciphertext. Returns 0 for
1317
 * success. Code is big-endian.
1318
 */
1319
static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in,
1320
                         unsigned char *out, size_t len)
1321
{
1322
    const unsigned char *inptr;
1323
    unsigned long long mlen;
1324
    union {
1325
        unsigned int w[4];
1326
        unsigned char b[16];
1327
    } buf;
1328
    size_t inlen;
1329
    int n, rem, i;
1330
1331
    mlen = ctx->kma.param.tpcl + len;
1332
    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1333
        return -1;
1334
    ctx->kma.param.tpcl = mlen;
1335
1336
    n = ctx->mreslen;
1337
    if (n) {
1338
        inptr = in;
1339
        inlen = len;
1340
        while (n && inlen) {
1341
            ctx->mres[n] = *inptr;
1342
            n = (n + 1) & 0xf;
1343
            ++inptr;
1344
            --inlen;
1345
        }
1346
        /* ctx->mres contains a complete block if offset has wrapped around */
1347
        if (!n) {
1348
            s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b,
1349
                      ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
1350
            ctx->fc |= S390X_KMA_HS;
1351
            ctx->areslen = 0;
1352
1353
            /* previous call already encrypted/decrypted its remainder,
1354
             * see comment below */
1355
            n = ctx->mreslen;
1356
            while (n) {
1357
                *out = buf.b[n];
1358
                n = (n + 1) & 0xf;
1359
                ++out;
1360
                ++in;
1361
                --len;
1362
            }
1363
            ctx->mreslen = 0;
1364
        }
1365
    }
1366
1367
    rem = len & 0xf;
1368
1369
    len &= ~(size_t)0xf;
1370
    if (len) {
1371
        s390x_kma(ctx->ares, ctx->areslen, in, len, out,
1372
                  ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
1373
        in += len;
1374
        out += len;
1375
        ctx->fc |= S390X_KMA_HS;
1376
        ctx->areslen = 0;
1377
    }
1378
1379
    /*-
1380
     * If there is a remainder, it has to be saved such that it can be
1381
     * processed by kma later. However, we also have to do the for-now
1382
     * unauthenticated encryption/decryption part here and now...
1383
     */
1384
    if (rem) {
1385
        if (!ctx->mreslen) {
1386
            buf.w[0] = ctx->kma.param.j0.w[0];
1387
            buf.w[1] = ctx->kma.param.j0.w[1];
1388
            buf.w[2] = ctx->kma.param.j0.w[2];
1389
            buf.w[3] = ctx->kma.param.cv.w + 1;
1390
            s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k);
1391
        }
1392
1393
        n = ctx->mreslen;
1394
        for (i = 0; i < rem; i++) {
1395
            ctx->mres[n + i] = in[i];
1396
            out[i] = in[i] ^ ctx->kres[n + i];
1397
        }
1398
1399
        ctx->mreslen += rem;
1400
    }
1401
    return 0;
1402
}
1403
1404
/*-
1405
 * Initialize context structure. Code is big-endian.
1406
 */
1407
static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
1408
                                const unsigned char *iv)
1409
{
1410
    ctx->kma.param.t.g[0] = 0;
1411
    ctx->kma.param.t.g[1] = 0;
1412
    ctx->kma.param.tpcl = 0;
1413
    ctx->kma.param.taadl = 0;
1414
    ctx->mreslen = 0;
1415
    ctx->areslen = 0;
1416
    ctx->kreslen = 0;
1417
1418
    if (ctx->ivlen == 12) {
1419
        memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
1420
        ctx->kma.param.j0.w[3] = 1;
1421
        ctx->kma.param.cv.w = 1;
1422
    } else {
1423
        /* ctx->iv has the right size and is already padded. */
1424
        memcpy(ctx->iv, iv, ctx->ivlen);
1425
        s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
1426
                  ctx->fc, &ctx->kma.param);
1427
        ctx->fc |= S390X_KMA_HS;
1428
1429
        ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0];
1430
        ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1];
1431
        ctx->kma.param.cv.w = ctx->kma.param.j0.w[3];
1432
        ctx->kma.param.t.g[0] = 0;
1433
        ctx->kma.param.t.g[1] = 0;
1434
    }
1435
}
1436
1437
/*-
1438
 * Performs various operations on the context structure depending on control
1439
 * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
1440
 * Code is big-endian.
1441
 */
1442
static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1443
{
1444
    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
1445
    S390X_AES_GCM_CTX *gctx_out;
1446
    EVP_CIPHER_CTX *out;
1447
    unsigned char *buf;
1448
    int ivlen, enc, len;
1449
1450
    switch (type) {
1451
    case EVP_CTRL_INIT:
1452
        ivlen = EVP_CIPHER_get_iv_length(c->cipher);
1453
        gctx->key_set = 0;
1454
        gctx->iv_set = 0;
1455
        gctx->ivlen = ivlen;
1456
        gctx->iv = c->iv;
1457
        gctx->taglen = -1;
1458
        gctx->iv_gen = 0;
1459
        gctx->tls_aad_len = -1;
1460
        return 1;
1461
1462
    case EVP_CTRL_GET_IVLEN:
1463
        *(int *)ptr = gctx->ivlen;
1464
        return 1;
1465
1466
    case EVP_CTRL_AEAD_SET_IVLEN:
1467
        if (arg <= 0)
1468
            return 0;
1469
1470
        if (arg != 12) {
1471
            len = S390X_gcm_ivpadlen(arg);
1472
1473
            /* Allocate memory for iv if needed. */
1474
            if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) {
1475
                if (gctx->iv != c->iv)
1476
                    OPENSSL_free(gctx->iv);
1477
1478
                if ((gctx->iv = OPENSSL_malloc(len)) == NULL)
1479
                    return 0;
1480
            }
1481
            /* Add padding. */
1482
            memset(gctx->iv + arg, 0, len - arg - 8);
1483
            *((unsigned long long *)(gctx->iv + len - 8)) = arg << 3;
1484
        }
1485
        gctx->ivlen = arg;
1486
        return 1;
1487
1488
    case EVP_CTRL_AEAD_SET_TAG:
1489
        buf = EVP_CIPHER_CTX_buf_noconst(c);
1490
        enc = EVP_CIPHER_CTX_is_encrypting(c);
1491
        if (arg <= 0 || arg > 16 || enc)
1492
            return 0;
1493
1494
        memcpy(buf, ptr, arg);
1495
        gctx->taglen = arg;
1496
        return 1;
1497
1498
    case EVP_CTRL_AEAD_GET_TAG:
1499
        enc = EVP_CIPHER_CTX_is_encrypting(c);
1500
        if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0)
1501
            return 0;
1502
1503
        memcpy(ptr, gctx->kma.param.t.b, arg);
1504
        return 1;
1505
1506
    case EVP_CTRL_GCM_SET_IV_FIXED:
1507
        /* Special case: -1 length restores whole iv */
1508
        if (arg == -1) {
1509
            memcpy(gctx->iv, ptr, gctx->ivlen);
1510
            gctx->iv_gen = 1;
1511
            return 1;
1512
        }
1513
        /*
1514
         * Fixed field must be at least 4 bytes and invocation field at least
1515
         * 8.
1516
         */
1517
        if ((arg < 4) || (gctx->ivlen - arg) < 8)
1518
            return 0;
1519
1520
        if (arg)
1521
            memcpy(gctx->iv, ptr, arg);
1522
1523
        enc = EVP_CIPHER_CTX_is_encrypting(c);
1524
        if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1525
            return 0;
1526
1527
        gctx->iv_gen = 1;
1528
        return 1;
1529
1530
    case EVP_CTRL_GCM_IV_GEN:
1531
        if (gctx->iv_gen == 0 || gctx->key_set == 0)
1532
            return 0;
1533
1534
        s390x_aes_gcm_setiv(gctx, gctx->iv);
1535
1536
        if (arg <= 0 || arg > gctx->ivlen)
1537
            arg = gctx->ivlen;
1538
1539
        memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1540
        /*
1541
         * Invocation field will be at least 8 bytes in size and so no need
1542
         * to check wrap around or increment more than last 8 bytes.
1543
         */
1544
        ctr64_inc(gctx->iv + gctx->ivlen - 8);
1545
        gctx->iv_set = 1;
1546
        return 1;
1547
1548
    case EVP_CTRL_GCM_SET_IV_INV:
1549
        enc = EVP_CIPHER_CTX_is_encrypting(c);
1550
        if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc)
1551
            return 0;
1552
1553
        memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1554
        s390x_aes_gcm_setiv(gctx, gctx->iv);
1555
        gctx->iv_set = 1;
1556
        return 1;
1557
1558
    case EVP_CTRL_AEAD_TLS1_AAD:
1559
        /* Save the aad for later use. */
1560
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
1561
            return 0;
1562
1563
        buf = EVP_CIPHER_CTX_buf_noconst(c);
1564
        memcpy(buf, ptr, arg);
1565
        gctx->tls_aad_len = arg;
1566
        gctx->tls_enc_records = 0;
1567
1568
        len = buf[arg - 2] << 8 | buf[arg - 1];
1569
        /* Correct length for explicit iv. */
1570
        if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
1571
            return 0;
1572
        len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1573
1574
        /* If decrypting correct for tag too. */
1575
        enc = EVP_CIPHER_CTX_is_encrypting(c);
1576
        if (!enc) {
1577
            if (len < EVP_GCM_TLS_TAG_LEN)
1578
                return 0;
1579
            len -= EVP_GCM_TLS_TAG_LEN;
1580
        }
1581
        buf[arg - 2] = len >> 8;
1582
        buf[arg - 1] = len & 0xff;
1583
        /* Extra padding: tag appended to record. */
1584
        return EVP_GCM_TLS_TAG_LEN;
1585
1586
    case EVP_CTRL_COPY:
1587
        out = ptr;
1588
        gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out);
1589
1590
        if (gctx->iv == c->iv) {
1591
            gctx_out->iv = out->iv;
1592
        } else {
1593
            len = S390X_gcm_ivpadlen(gctx->ivlen);
1594
1595
            if ((gctx_out->iv = OPENSSL_malloc(len)) == NULL)
1596
                return 0;
1597
1598
            memcpy(gctx_out->iv, gctx->iv, len);
1599
        }
1600
        return 1;
1601
1602
    default:
1603
        return -1;
1604
    }
1605
}
1606
1607
/*-
1608
 * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
1609
 */
1610
static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
1611
                                  const unsigned char *key,
1612
                                  const unsigned char *iv, int enc)
1613
{
1614
    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1615
    int keylen;
1616
1617
    if (iv == NULL && key == NULL)
1618
        return 1;
1619
1620
    if (key != NULL) {
1621
        keylen = EVP_CIPHER_CTX_get_key_length(ctx);
1622
        if (keylen <= 0) {
1623
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1624
            return 0;
1625
        }
1626
1627
        memcpy(&gctx->kma.param.k, key, keylen);
1628
1629
        gctx->fc = S390X_AES_FC(keylen);
1630
        if (!enc)
1631
            gctx->fc |= S390X_DECRYPT;
1632
1633
        if (iv == NULL && gctx->iv_set)
1634
            iv = gctx->iv;
1635
1636
        if (iv != NULL) {
1637
            s390x_aes_gcm_setiv(gctx, iv);
1638
            gctx->iv_set = 1;
1639
        }
1640
        gctx->key_set = 1;
1641
    } else {
1642
        if (gctx->key_set)
1643
            s390x_aes_gcm_setiv(gctx, iv);
1644
        else
1645
            memcpy(gctx->iv, iv, gctx->ivlen);
1646
1647
        gctx->iv_set = 1;
1648
        gctx->iv_gen = 0;
1649
    }
1650
    return 1;
1651
}
1652
1653
/*-
1654
 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1655
 * if successful. Otherwise -1 is returned. Code is big-endian.
1656
 */
1657
static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1658
                                    const unsigned char *in, size_t len)
1659
{
1660
    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1661
    const unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1662
    const int enc = EVP_CIPHER_CTX_is_encrypting(ctx);
1663
    int rv = -1;
1664
1665
    if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
1666
        return -1;
1667
1668
    /*
1669
     * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
1670
     * Requirements from SP 800-38D".  The requirements is for one party to the
1671
     * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
1672
     * side only.
1673
     */
1674
    if (enc && ++gctx->tls_enc_records == 0) {
1675
        ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_RECORDS);
1676
        goto err;
1677
    }
1678
1679
    if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN
1680
                                     : EVP_CTRL_GCM_SET_IV_INV,
1681
                            EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1682
        goto err;
1683
1684
    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1685
    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1686
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1687
1688
    gctx->kma.param.taadl = gctx->tls_aad_len << 3;
1689
    gctx->kma.param.tpcl = len << 3;
1690
    s390x_kma(buf, gctx->tls_aad_len, in, len, out,
1691
              gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
1692
1693
    if (enc) {
1694
        memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN);
1695
        rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1696
    } else {
1697
        if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len,
1698
                          EVP_GCM_TLS_TAG_LEN)) {
1699
            OPENSSL_cleanse(out, len);
1700
            goto err;
1701
        }
1702
        rv = len;
1703
    }
1704
err:
1705
    gctx->iv_set = 0;
1706
    gctx->tls_aad_len = -1;
1707
    return rv;
1708
}
1709
1710
/*-
1711
 * Called from EVP layer to initialize context, process additional
1712
 * authenticated data, en/de-crypt plain/cipher-text and authenticate
1713
 * ciphertext or process a TLS packet, depending on context. Returns bytes
1714
 * written on success. Otherwise -1 is returned. Code is big-endian.
1715
 */
1716
static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1717
                                const unsigned char *in, size_t len)
1718
{
1719
    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1720
    unsigned char *buf, tmp[16];
1721
    int enc;
1722
1723
    if (!gctx->key_set)
1724
        return -1;
1725
1726
    if (gctx->tls_aad_len >= 0)
1727
        return s390x_aes_gcm_tls_cipher(ctx, out, in, len);
1728
1729
    if (!gctx->iv_set)
1730
        return -1;
1731
1732
    if (in != NULL) {
1733
        if (out == NULL) {
1734
            if (s390x_aes_gcm_aad(gctx, in, len))
1735
                return -1;
1736
        } else {
1737
            if (s390x_aes_gcm(gctx, in, out, len))
1738
                return -1;
1739
        }
1740
        return len;
1741
    } else {
1742
        gctx->kma.param.taadl <<= 3;
1743
        gctx->kma.param.tpcl <<= 3;
1744
        s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp,
1745
                  gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
1746
        /* recall that we already did en-/decrypt gctx->mres
1747
         * and returned it to caller... */
1748
        OPENSSL_cleanse(tmp, gctx->mreslen);
1749
        gctx->iv_set = 0;
1750
1751
        enc = EVP_CIPHER_CTX_is_encrypting(ctx);
1752
        if (enc) {
1753
            gctx->taglen = 16;
1754
        } else {
1755
            if (gctx->taglen < 0)
1756
                return -1;
1757
1758
            buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1759
            if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen))
1760
                return -1;
1761
        }
1762
        return 0;
1763
    }
1764
}
1765
1766
static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1767
{
1768
    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
1769
1770
    if (gctx == NULL)
1771
        return 0;
1772
1773
    if (gctx->iv != c->iv)
1774
        OPENSSL_free(gctx->iv);
1775
1776
    OPENSSL_cleanse(gctx, sizeof(*gctx));
1777
    return 1;
1778
}
1779
1780
# define S390X_AES_XTS_CTX              EVP_AES_XTS_CTX
1781
1782
# define s390x_aes_xts_init_key aes_xts_init_key
1783
static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
1784
                                  const unsigned char *key,
1785
                                  const unsigned char *iv, int enc);
1786
# define s390x_aes_xts_cipher aes_xts_cipher
1787
static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1788
                                const unsigned char *in, size_t len);
1789
# define s390x_aes_xts_ctrl aes_xts_ctrl
1790
static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
1791
# define s390x_aes_xts_cleanup aes_xts_cleanup
1792
1793
/*-
1794
 * Set nonce and length fields. Code is big-endian.
1795
 */
1796
static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX *ctx,
1797
                                          const unsigned char *nonce,
1798
                                          size_t mlen)
1799
{
1800
    ctx->aes.ccm.nonce.b[0] &= ~S390X_CCM_AAD_FLAG;
1801
    ctx->aes.ccm.nonce.g[1] = mlen;
1802
    memcpy(ctx->aes.ccm.nonce.b + 1, nonce, 15 - ctx->aes.ccm.l);
1803
}
1804
1805
/*-
1806
 * Process additional authenticated data. Code is big-endian.
1807
 */
1808
static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX *ctx, const unsigned char *aad,
1809
                              size_t alen)
1810
{
1811
    unsigned char *ptr;
1812
    int i, rem;
1813
1814
    if (!alen)
1815
        return;
1816
1817
    ctx->aes.ccm.nonce.b[0] |= S390X_CCM_AAD_FLAG;
1818
1819
    /* Suppress 'type-punned pointer dereference' warning. */
1820
    ptr = ctx->aes.ccm.buf.b;
1821
1822
    if (alen < ((1 << 16) - (1 << 8))) {
1823
        *(uint16_t *)ptr = alen;
1824
        i = 2;
1825
    } else if (sizeof(alen) == 8
1826
               && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) {
1827
        *(uint16_t *)ptr = 0xffff;
1828
        *(uint64_t *)(ptr + 2) = alen;
1829
        i = 10;
1830
    } else {
1831
        *(uint16_t *)ptr = 0xfffe;
1832
        *(uint32_t *)(ptr + 2) = alen;
1833
        i = 6;
1834
    }
1835
1836
    while (i < 16 && alen) {
1837
        ctx->aes.ccm.buf.b[i] = *aad;
1838
        ++aad;
1839
        --alen;
1840
        ++i;
1841
    }
1842
    while (i < 16) {
1843
        ctx->aes.ccm.buf.b[i] = 0;
1844
        ++i;
1845
    }
1846
1847
    ctx->aes.ccm.kmac_param.icv.g[0] = 0;
1848
    ctx->aes.ccm.kmac_param.icv.g[1] = 0;
1849
    s390x_kmac(ctx->aes.ccm.nonce.b, 32, ctx->aes.ccm.fc,
1850
               &ctx->aes.ccm.kmac_param);
1851
    ctx->aes.ccm.blocks += 2;
1852
1853
    rem = alen & 0xf;
1854
    alen &= ~(size_t)0xf;
1855
    if (alen) {
1856
        s390x_kmac(aad, alen, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1857
        ctx->aes.ccm.blocks += alen >> 4;
1858
        aad += alen;
1859
    }
1860
    if (rem) {
1861
        for (i = 0; i < rem; i++)
1862
            ctx->aes.ccm.kmac_param.icv.b[i] ^= aad[i];
1863
1864
        s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1865
                 ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1866
                 ctx->aes.ccm.kmac_param.k);
1867
        ctx->aes.ccm.blocks++;
1868
    }
1869
}
1870
1871
/*-
1872
 * En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for
1873
 * success.
1874
 */
1875
static int s390x_aes_ccm(S390X_AES_CCM_CTX *ctx, const unsigned char *in,
1876
                         unsigned char *out, size_t len, int enc)
1877
{
1878
    size_t n, rem;
1879
    unsigned int i, l, num;
1880
    unsigned char flags;
1881
1882
    flags = ctx->aes.ccm.nonce.b[0];
1883
    if (!(flags & S390X_CCM_AAD_FLAG)) {
1884
        s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.kmac_param.icv.b,
1885
                 ctx->aes.ccm.fc, ctx->aes.ccm.kmac_param.k);
1886
        ctx->aes.ccm.blocks++;
1887
    }
1888
    l = flags & 0x7;
1889
    ctx->aes.ccm.nonce.b[0] = l;
1890
1891
    /*-
1892
     * Reconstruct length from encoded length field
1893
     * and initialize it with counter value.
1894
     */
1895
    n = 0;
1896
    for (i = 15 - l; i < 15; i++) {
1897
        n |= ctx->aes.ccm.nonce.b[i];
1898
        ctx->aes.ccm.nonce.b[i] = 0;
1899
        n <<= 8;
1900
    }
1901
    n |= ctx->aes.ccm.nonce.b[15];
1902
    ctx->aes.ccm.nonce.b[15] = 1;
1903
1904
    if (n != len)
1905
        return -1;              /* length mismatch */
1906
1907
    if (enc) {
1908
        /* Two operations per block plus one for tag encryption */
1909
        ctx->aes.ccm.blocks += (((len + 15) >> 4) << 1) + 1;
1910
        if (ctx->aes.ccm.blocks > (1ULL << 61))
1911
            return -2;          /* too much data */
1912
    }
1913
1914
    num = 0;
1915
    rem = len & 0xf;
1916
    len &= ~(size_t)0xf;
1917
1918
    if (enc) {
1919
        /* mac-then-encrypt */
1920
        if (len)
1921
            s390x_kmac(in, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1922
        if (rem) {
1923
            for (i = 0; i < rem; i++)
1924
                ctx->aes.ccm.kmac_param.icv.b[i] ^= in[len + i];
1925
1926
            s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1927
                     ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1928
                     ctx->aes.ccm.kmac_param.k);
1929
        }
1930
1931
        CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
1932
                                    ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
1933
                                    &num, (ctr128_f)AES_ctr32_encrypt);
1934
    } else {
1935
        /* decrypt-then-mac */
1936
        CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
1937
                                    ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
1938
                                    &num, (ctr128_f)AES_ctr32_encrypt);
1939
1940
        if (len)
1941
            s390x_kmac(out, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1942
        if (rem) {
1943
            for (i = 0; i < rem; i++)
1944
                ctx->aes.ccm.kmac_param.icv.b[i] ^= out[len + i];
1945
1946
            s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1947
                     ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1948
                     ctx->aes.ccm.kmac_param.k);
1949
        }
1950
    }
1951
    /* encrypt tag */
1952
    for (i = 15 - l; i < 16; i++)
1953
        ctx->aes.ccm.nonce.b[i] = 0;
1954
1955
    s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.buf.b, ctx->aes.ccm.fc,
1956
             ctx->aes.ccm.kmac_param.k);
1957
    ctx->aes.ccm.kmac_param.icv.g[0] ^= ctx->aes.ccm.buf.g[0];
1958
    ctx->aes.ccm.kmac_param.icv.g[1] ^= ctx->aes.ccm.buf.g[1];
1959
1960
    ctx->aes.ccm.nonce.b[0] = flags;    /* restore flags field */
1961
    return 0;
1962
}
1963
1964
/*-
1965
 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1966
 * if successful. Otherwise -1 is returned.
1967
 */
1968
static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1969
                                    const unsigned char *in, size_t len)
1970
{
1971
    S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
1972
    unsigned char *ivec = ctx->iv;
1973
    unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1974
    const int enc = EVP_CIPHER_CTX_is_encrypting(ctx);
1975
1976
    if (out != in
1977
            || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m))
1978
        return -1;
1979
1980
    if (enc) {
1981
        /* Set explicit iv (sequence number). */
1982
        memcpy(out, buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
1983
    }
1984
1985
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
1986
    /*-
1987
     * Get explicit iv (sequence number). We already have fixed iv
1988
     * (server/client_write_iv) here.
1989
     */
1990
    memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
1991
    s390x_aes_ccm_setiv(cctx, ivec, len);
1992
1993
    /* Process aad (sequence number|type|version|length) */
1994
    s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len);
1995
1996
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1997
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1998
1999
    if (enc) {
2000
        if (s390x_aes_ccm(cctx, in, out, len, enc))
2001
            return -1;
2002
2003
        memcpy(out + len, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
2004
        return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
2005
    } else {
2006
        if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
2007
            if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, in + len,
2008
                               cctx->aes.ccm.m))
2009
                return len;
2010
        }
2011
2012
        OPENSSL_cleanse(out, len);
2013
        return -1;
2014
    }
2015
}
2016
2017
/*-
2018
 * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
2019
 * returned.
2020
 */
2021
static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
2022
                                  const unsigned char *key,
2023
                                  const unsigned char *iv, int enc)
2024
{
2025
    S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
2026
    int keylen;
2027
2028
    if (iv == NULL && key == NULL)
2029
        return 1;
2030
2031
    if (key != NULL) {
2032
        keylen = EVP_CIPHER_CTX_get_key_length(ctx);
2033
        if (keylen <= 0) {
2034
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
2035
            return 0;
2036
        }
2037
2038
        cctx->aes.ccm.fc = S390X_AES_FC(keylen);
2039
        memcpy(cctx->aes.ccm.kmac_param.k, key, keylen);
2040
2041
        /* Store encoded m and l. */
2042
        cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
2043
                                 | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
2044
        memset(cctx->aes.ccm.nonce.b + 1, 0,
2045
               sizeof(cctx->aes.ccm.nonce.b));
2046
        cctx->aes.ccm.blocks = 0;
2047
2048
        cctx->aes.ccm.key_set = 1;
2049
    }
2050
2051
    if (iv != NULL) {
2052
        memcpy(ctx->iv, iv, 15 - cctx->aes.ccm.l);
2053
2054
        cctx->aes.ccm.iv_set = 1;
2055
    }
2056
2057
    return 1;
2058
}
2059
2060
/*-
2061
 * Called from EVP layer to initialize context, process additional
2062
 * authenticated data, en/de-crypt plain/cipher-text and authenticate
2063
 * plaintext or process a TLS packet, depending on context. Returns bytes
2064
 * written on success. Otherwise -1 is returned.
2065
 */
2066
static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2067
                                const unsigned char *in, size_t len)
2068
{
2069
    S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
2070
    const int enc = EVP_CIPHER_CTX_is_encrypting(ctx);
2071
    int rv;
2072
    unsigned char *buf;
2073
2074
    if (!cctx->aes.ccm.key_set)
2075
        return -1;
2076
2077
    if (cctx->aes.ccm.tls_aad_len >= 0)
2078
        return s390x_aes_ccm_tls_cipher(ctx, out, in, len);
2079
2080
    /*-
2081
     * Final(): Does not return any data. Recall that ccm is mac-then-encrypt
2082
     * so integrity must be checked already at Update() i.e., before
2083
     * potentially corrupted data is output.
2084
     */
2085
    if (in == NULL && out != NULL)
2086
        return 0;
2087
2088
    if (!cctx->aes.ccm.iv_set)
2089
        return -1;
2090
2091
    if (out == NULL) {
2092
        /* Update(): Pass message length. */
2093
        if (in == NULL) {
2094
            s390x_aes_ccm_setiv(cctx, ctx->iv, len);
2095
2096
            cctx->aes.ccm.len_set = 1;
2097
            return len;
2098
        }
2099
2100
        /* Update(): Process aad. */
2101
        if (!cctx->aes.ccm.len_set && len)
2102
            return -1;
2103
2104
        s390x_aes_ccm_aad(cctx, in, len);
2105
        return len;
2106
    }
2107
2108
    /* The tag must be set before actually decrypting data */
2109
    if (!enc && !cctx->aes.ccm.tag_set)
2110
        return -1;
2111
2112
    /* Update(): Process message. */
2113
2114
    if (!cctx->aes.ccm.len_set) {
2115
        /*-
2116
         * In case message length was not previously set explicitly via
2117
         * Update(), set it now.
2118
         */
2119
        s390x_aes_ccm_setiv(cctx, ctx->iv, len);
2120
2121
        cctx->aes.ccm.len_set = 1;
2122
    }
2123
2124
    if (enc) {
2125
        if (s390x_aes_ccm(cctx, in, out, len, enc))
2126
            return -1;
2127
2128
        cctx->aes.ccm.tag_set = 1;
2129
        return len;
2130
    } else {
2131
        rv = -1;
2132
2133
        if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
2134
            buf = EVP_CIPHER_CTX_buf_noconst(ctx);
2135
            if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, buf,
2136
                               cctx->aes.ccm.m))
2137
                rv = len;
2138
        }
2139
2140
        if (rv == -1)
2141
            OPENSSL_cleanse(out, len);
2142
2143
        cctx->aes.ccm.iv_set = 0;
2144
        cctx->aes.ccm.tag_set = 0;
2145
        cctx->aes.ccm.len_set = 0;
2146
        return rv;
2147
    }
2148
}
2149
2150
/*-
2151
 * Performs various operations on the context structure depending on control
2152
 * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
2153
 * Code is big-endian.
2154
 */
2155
static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2156
{
2157
    S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, c);
2158
    unsigned char *buf;
2159
    int enc, len;
2160
2161
    switch (type) {
2162
    case EVP_CTRL_INIT:
2163
        cctx->aes.ccm.key_set = 0;
2164
        cctx->aes.ccm.iv_set = 0;
2165
        cctx->aes.ccm.l = 8;
2166
        cctx->aes.ccm.m = 12;
2167
        cctx->aes.ccm.tag_set = 0;
2168
        cctx->aes.ccm.len_set = 0;
2169
        cctx->aes.ccm.tls_aad_len = -1;
2170
        return 1;
2171
2172
    case EVP_CTRL_GET_IVLEN:
2173
        *(int *)ptr = 15 - cctx->aes.ccm.l;
2174
        return 1;
2175
2176
    case EVP_CTRL_AEAD_TLS1_AAD:
2177
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
2178
            return 0;
2179
2180
        /* Save the aad for later use. */
2181
        buf = EVP_CIPHER_CTX_buf_noconst(c);
2182
        memcpy(buf, ptr, arg);
2183
        cctx->aes.ccm.tls_aad_len = arg;
2184
2185
        len = buf[arg - 2] << 8 | buf[arg - 1];
2186
        if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
2187
            return 0;
2188
2189
        /* Correct length for explicit iv. */
2190
        len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
2191
2192
        enc = EVP_CIPHER_CTX_is_encrypting(c);
2193
        if (!enc) {
2194
            if (len < cctx->aes.ccm.m)
2195
                return 0;
2196
2197
            /* Correct length for tag. */
2198
            len -= cctx->aes.ccm.m;
2199
        }
2200
2201
        buf[arg - 2] = len >> 8;
2202
        buf[arg - 1] = len & 0xff;
2203
2204
        /* Extra padding: tag appended to record. */
2205
        return cctx->aes.ccm.m;
2206
2207
    case EVP_CTRL_CCM_SET_IV_FIXED:
2208
        if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
2209
            return 0;
2210
2211
        /* Copy to first part of the iv. */
2212
        memcpy(c->iv, ptr, arg);
2213
        return 1;
2214
2215
    case EVP_CTRL_AEAD_SET_IVLEN:
2216
        arg = 15 - arg;
2217
        /* fall-through */
2218
2219
    case EVP_CTRL_CCM_SET_L:
2220
        if (arg < 2 || arg > 8)
2221
            return 0;
2222
2223
        cctx->aes.ccm.l = arg;
2224
        return 1;
2225
2226
    case EVP_CTRL_AEAD_SET_TAG:
2227
        if ((arg & 1) || arg < 4 || arg > 16)
2228
            return 0;
2229
2230
        enc = EVP_CIPHER_CTX_is_encrypting(c);
2231
        if (enc && ptr)
2232
            return 0;
2233
2234
        if (ptr) {
2235
            cctx->aes.ccm.tag_set = 1;
2236
            buf = EVP_CIPHER_CTX_buf_noconst(c);
2237
            memcpy(buf, ptr, arg);
2238
        }
2239
2240
        cctx->aes.ccm.m = arg;
2241
        return 1;
2242
2243
    case EVP_CTRL_AEAD_GET_TAG:
2244
        enc = EVP_CIPHER_CTX_is_encrypting(c);
2245
        if (!enc || !cctx->aes.ccm.tag_set)
2246
            return 0;
2247
2248
        if (arg < cctx->aes.ccm.m)
2249
            return 0;
2250
2251
        memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
2252
        cctx->aes.ccm.tag_set = 0;
2253
        cctx->aes.ccm.iv_set = 0;
2254
        cctx->aes.ccm.len_set = 0;
2255
        return 1;
2256
2257
    case EVP_CTRL_COPY:
2258
        return 1;
2259
2260
    default:
2261
        return -1;
2262
    }
2263
}
2264
2265
# define s390x_aes_ccm_cleanup aes_ccm_cleanup
2266
2267
# ifndef OPENSSL_NO_OCB
2268
#  define S390X_AES_OCB_CTX             EVP_AES_OCB_CTX
2269
2270
#  define s390x_aes_ocb_init_key aes_ocb_init_key
2271
static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2272
                                  const unsigned char *iv, int enc);
2273
#  define s390x_aes_ocb_cipher aes_ocb_cipher
2274
static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2275
                                const unsigned char *in, size_t len);
2276
#  define s390x_aes_ocb_cleanup aes_ocb_cleanup
2277
static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX *);
2278
#  define s390x_aes_ocb_ctrl aes_ocb_ctrl
2279
static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
2280
# endif
2281
2282
# ifndef OPENSSL_NO_SIV
2283
#  define S390X_AES_SIV_CTX             EVP_AES_SIV_CTX
2284
2285
#  define s390x_aes_siv_init_key aes_siv_init_key
2286
#  define s390x_aes_siv_cipher aes_siv_cipher
2287
#  define s390x_aes_siv_cleanup aes_siv_cleanup
2288
#  define s390x_aes_siv_ctrl aes_siv_ctrl
2289
# endif
2290
2291
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,    \
2292
                              MODE,flags)                               \
2293
static const EVP_CIPHER s390x_aes_##keylen##_##mode = {                 \
2294
    nid##_##keylen##_##nmode,blocksize,                                 \
2295
    keylen / 8,                                                         \
2296
    ivlen,                                                              \
2297
    flags | EVP_CIPH_##MODE##_MODE,                                     \
2298
    EVP_ORIG_GLOBAL,                                                    \
2299
    s390x_aes_##mode##_init_key,                                        \
2300
    s390x_aes_##mode##_cipher,                                          \
2301
    NULL,                                                               \
2302
    sizeof(S390X_AES_##MODE##_CTX),                                     \
2303
    NULL,                                                               \
2304
    NULL,                                                               \
2305
    NULL,                                                               \
2306
    NULL                                                                \
2307
};                                                                      \
2308
static const EVP_CIPHER aes_##keylen##_##mode = {                       \
2309
    nid##_##keylen##_##nmode,                                           \
2310
    blocksize,                                                          \
2311
    keylen / 8,                                                         \
2312
    ivlen,                                                              \
2313
    flags | EVP_CIPH_##MODE##_MODE,                                     \
2314
    EVP_ORIG_GLOBAL,                                                    \
2315
    aes_init_key,                                                       \
2316
    aes_##mode##_cipher,                                                \
2317
    NULL,                                                               \
2318
    sizeof(EVP_AES_KEY),                                                \
2319
    NULL,                                                               \
2320
    NULL,                                                               \
2321
    NULL,                                                               \
2322
    NULL                                                                \
2323
};                                                                      \
2324
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void)                       \
2325
{                                                                       \
2326
    return S390X_aes_##keylen##_##mode##_CAPABLE ?                      \
2327
           &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode;       \
2328
}
2329
2330
# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
2331
static const EVP_CIPHER s390x_aes_##keylen##_##mode = {                 \
2332
    nid##_##keylen##_##mode,                                            \
2333
    blocksize,                                                          \
2334
    (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8,        \
2335
    ivlen,                                                              \
2336
    flags | EVP_CIPH_##MODE##_MODE,                                     \
2337
    EVP_ORIG_GLOBAL,                                                    \
2338
    s390x_aes_##mode##_init_key,                                        \
2339
    s390x_aes_##mode##_cipher,                                          \
2340
    s390x_aes_##mode##_cleanup,                                         \
2341
    sizeof(S390X_AES_##MODE##_CTX),                                     \
2342
    NULL,                                                               \
2343
    NULL,                                                               \
2344
    s390x_aes_##mode##_ctrl,                                            \
2345
    NULL                                                                \
2346
};                                                                      \
2347
static const EVP_CIPHER aes_##keylen##_##mode = {                       \
2348
    nid##_##keylen##_##mode,blocksize,                                  \
2349
    (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8,        \
2350
    ivlen,                                                              \
2351
    flags | EVP_CIPH_##MODE##_MODE,                                     \
2352
    EVP_ORIG_GLOBAL,                                                    \
2353
    aes_##mode##_init_key,                                              \
2354
    aes_##mode##_cipher,                                                \
2355
    aes_##mode##_cleanup,                                               \
2356
    sizeof(EVP_AES_##MODE##_CTX),                                       \
2357
    NULL,                                                               \
2358
    NULL,                                                               \
2359
    aes_##mode##_ctrl,                                                  \
2360
    NULL                                                                \
2361
};                                                                      \
2362
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void)                       \
2363
{                                                                       \
2364
    return S390X_aes_##keylen##_##mode##_CAPABLE ?                      \
2365
           &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode;       \
2366
}
2367
2368
#else
2369
2370
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
2371
static const EVP_CIPHER aes_##keylen##_##mode = { \
2372
        nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
2373
        flags|EVP_CIPH_##MODE##_MODE,   \
2374
        EVP_ORIG_GLOBAL,                \
2375
        aes_init_key,                   \
2376
        aes_##mode##_cipher,            \
2377
        NULL,                           \
2378
        sizeof(EVP_AES_KEY),            \
2379
        NULL,NULL,NULL,NULL }; \
2380
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2381
{ return &aes_##keylen##_##mode; }
2382
2383
# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
2384
static const EVP_CIPHER aes_##keylen##_##mode = { \
2385
        nid##_##keylen##_##mode,blocksize, \
2386
        (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
2387
        ivlen,                          \
2388
        flags|EVP_CIPH_##MODE##_MODE,   \
2389
        EVP_ORIG_GLOBAL,                \
2390
        aes_##mode##_init_key,          \
2391
        aes_##mode##_cipher,            \
2392
        aes_##mode##_cleanup,           \
2393
        sizeof(EVP_AES_##MODE##_CTX),   \
2394
        NULL,NULL,aes_##mode##_ctrl,NULL }; \
2395
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2396
{ return &aes_##keylen##_##mode; }
2397
2398
#endif
2399
2400
#define BLOCK_CIPHER_generic_pack(nid,keylen,flags)             \
2401
        BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)     \
2402
        BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)      \
2403
        BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
2404
        BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)   \
2405
        BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags)       \
2406
        BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags)       \
2407
        BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
2408
2409
static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2410
                        const unsigned char *iv, int enc)
2411
0
{
2412
0
    int ret, mode;
2413
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2414
0
    const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
2415
2416
0
    if (keylen <= 0) {
2417
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
2418
0
        return 0;
2419
0
    }
2420
2421
0
    mode = EVP_CIPHER_CTX_get_mode(ctx);
2422
0
    if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
2423
0
        && !enc) {
2424
#ifdef HWAES_CAPABLE
2425
        if (HWAES_CAPABLE) {
2426
            ret = HWAES_set_decrypt_key(key, keylen, &dat->ks.ks);
2427
            dat->block = (block128_f) HWAES_decrypt;
2428
            dat->stream.cbc = NULL;
2429
# ifdef HWAES_cbc_encrypt
2430
            if (mode == EVP_CIPH_CBC_MODE)
2431
                dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
2432
# endif
2433
        } else
2434
#endif
2435
0
#ifdef BSAES_CAPABLE
2436
0
        if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) {
2437
0
            ret = AES_set_decrypt_key(key, keylen, &dat->ks.ks);
2438
0
            dat->block = (block128_f) AES_decrypt;
2439
0
            dat->stream.cbc = (cbc128_f) ossl_bsaes_cbc_encrypt;
2440
0
        } else
2441
0
#endif
2442
0
#ifdef VPAES_CAPABLE
2443
0
        if (VPAES_CAPABLE) {
2444
0
            ret = vpaes_set_decrypt_key(key, keylen, &dat->ks.ks);
2445
0
            dat->block = (block128_f) vpaes_decrypt;
2446
0
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2447
0
                (cbc128_f) vpaes_cbc_encrypt : NULL;
2448
0
        } else
2449
0
#endif
2450
0
        {
2451
0
            ret = AES_set_decrypt_key(key, keylen, &dat->ks.ks);
2452
0
            dat->block = (block128_f) AES_decrypt;
2453
0
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2454
0
                (cbc128_f) AES_cbc_encrypt : NULL;
2455
0
        }
2456
0
    } else
2457
#ifdef HWAES_CAPABLE
2458
    if (HWAES_CAPABLE) {
2459
        ret = HWAES_set_encrypt_key(key, keylen, &dat->ks.ks);
2460
        dat->block = (block128_f) HWAES_encrypt;
2461
        dat->stream.cbc = NULL;
2462
# ifdef HWAES_cbc_encrypt
2463
        if (mode == EVP_CIPH_CBC_MODE)
2464
            dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
2465
        else
2466
# endif
2467
# ifdef HWAES_ctr32_encrypt_blocks
2468
        if (mode == EVP_CIPH_CTR_MODE)
2469
            dat->stream.ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
2470
        else
2471
# endif
2472
            (void)0;            /* terminate potentially open 'else' */
2473
    } else
2474
#endif
2475
0
#ifdef BSAES_CAPABLE
2476
0
    if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) {
2477
0
        ret = AES_set_encrypt_key(key, keylen, &dat->ks.ks);
2478
0
        dat->block = (block128_f) AES_encrypt;
2479
0
        dat->stream.ctr = (ctr128_f) ossl_bsaes_ctr32_encrypt_blocks;
2480
0
    } else
2481
0
#endif
2482
0
#ifdef VPAES_CAPABLE
2483
0
    if (VPAES_CAPABLE) {
2484
0
        ret = vpaes_set_encrypt_key(key, keylen, &dat->ks.ks);
2485
0
        dat->block = (block128_f) vpaes_encrypt;
2486
0
        dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2487
0
            (cbc128_f) vpaes_cbc_encrypt : NULL;
2488
0
    } else
2489
0
#endif
2490
0
    {
2491
0
        ret = AES_set_encrypt_key(key, keylen, &dat->ks.ks);
2492
0
        dat->block = (block128_f) AES_encrypt;
2493
0
        dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2494
0
            (cbc128_f) AES_cbc_encrypt : NULL;
2495
#ifdef AES_CTR_ASM
2496
        if (mode == EVP_CIPH_CTR_MODE)
2497
            dat->stream.ctr = (ctr128_f) AES_ctr32_encrypt;
2498
#endif
2499
0
    }
2500
2501
0
    if (ret < 0) {
2502
0
        ERR_raise(ERR_LIB_EVP, EVP_R_AES_KEY_SETUP_FAILED);
2503
0
        return 0;
2504
0
    }
2505
2506
0
    return 1;
2507
0
}
2508
2509
static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2510
                          const unsigned char *in, size_t len)
2511
0
{
2512
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2513
2514
0
    if (dat->stream.cbc)
2515
0
        (*dat->stream.cbc) (in, out, len, &dat->ks, ctx->iv,
2516
0
                            EVP_CIPHER_CTX_is_encrypting(ctx));
2517
0
    else if (EVP_CIPHER_CTX_is_encrypting(ctx))
2518
0
        CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv,
2519
0
                              dat->block);
2520
0
    else
2521
0
        CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
2522
0
                              ctx->iv, dat->block);
2523
2524
0
    return 1;
2525
0
}
2526
2527
static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2528
                          const unsigned char *in, size_t len)
2529
0
{
2530
0
    size_t bl = EVP_CIPHER_CTX_get_block_size(ctx);
2531
0
    size_t i;
2532
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2533
2534
0
    if (len < bl)
2535
0
        return 1;
2536
2537
0
    for (i = 0, len -= bl; i <= len; i += bl)
2538
0
        (*dat->block) (in + i, out + i, &dat->ks);
2539
2540
0
    return 1;
2541
0
}
2542
2543
static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2544
                          const unsigned char *in, size_t len)
2545
0
{
2546
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2547
2548
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
2549
0
    CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
2550
0
                          ctx->iv, &num, dat->block);
2551
0
    EVP_CIPHER_CTX_set_num(ctx, num);
2552
0
    return 1;
2553
0
}
2554
2555
static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2556
                          const unsigned char *in, size_t len)
2557
0
{
2558
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2559
2560
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
2561
0
    CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
2562
0
                          ctx->iv, &num,
2563
0
                          EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
2564
0
    EVP_CIPHER_CTX_set_num(ctx, num);
2565
0
    return 1;
2566
0
}
2567
2568
static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2569
                           const unsigned char *in, size_t len)
2570
0
{
2571
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2572
2573
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
2574
0
    CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
2575
0
                            ctx->iv, &num,
2576
0
                            EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
2577
0
    EVP_CIPHER_CTX_set_num(ctx, num);
2578
0
    return 1;
2579
0
}
2580
2581
static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2582
                           const unsigned char *in, size_t len)
2583
0
{
2584
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2585
2586
0
    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
2587
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
2588
0
        CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
2589
0
                                ctx->iv, &num,
2590
0
                                EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
2591
0
        EVP_CIPHER_CTX_set_num(ctx, num);
2592
0
        return 1;
2593
0
    }
2594
2595
0
    while (len >= MAXBITCHUNK) {
2596
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
2597
0
        CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
2598
0
                                ctx->iv, &num,
2599
0
                                EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
2600
0
        EVP_CIPHER_CTX_set_num(ctx, num);
2601
0
        len -= MAXBITCHUNK;
2602
0
        out += MAXBITCHUNK;
2603
0
        in  += MAXBITCHUNK;
2604
0
    }
2605
0
    if (len) {
2606
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
2607
0
        CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
2608
0
                                ctx->iv, &num,
2609
0
                                EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
2610
0
        EVP_CIPHER_CTX_set_num(ctx, num);
2611
0
    }
2612
2613
0
    return 1;
2614
0
}
2615
2616
static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2617
                          const unsigned char *in, size_t len)
2618
0
{
2619
0
    int n = EVP_CIPHER_CTX_get_num(ctx);
2620
0
    unsigned int num;
2621
0
    EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2622
2623
0
    if (n < 0)
2624
0
        return 0;
2625
0
    num = (unsigned int)n;
2626
2627
0
    if (dat->stream.ctr)
2628
0
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
2629
0
                                    ctx->iv,
2630
0
                                    EVP_CIPHER_CTX_buf_noconst(ctx),
2631
0
                                    &num, dat->stream.ctr);
2632
0
    else
2633
0
        CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
2634
0
                              ctx->iv,
2635
0
                              EVP_CIPHER_CTX_buf_noconst(ctx), &num,
2636
0
                              dat->block);
2637
0
    EVP_CIPHER_CTX_set_num(ctx, num);
2638
0
    return 1;
2639
0
}
2640
2641
BLOCK_CIPHER_generic_pack(NID_aes, 128, 0)
2642
    BLOCK_CIPHER_generic_pack(NID_aes, 192, 0)
2643
    BLOCK_CIPHER_generic_pack(NID_aes, 256, 0)
2644
2645
static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
2646
0
{
2647
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
2648
0
    if (gctx == NULL)
2649
0
        return 0;
2650
0
    OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
2651
0
    if (gctx->iv != c->iv)
2652
0
        OPENSSL_free(gctx->iv);
2653
0
    return 1;
2654
0
}
2655
2656
static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2657
0
{
2658
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
2659
0
    switch (type) {
2660
0
    case EVP_CTRL_INIT:
2661
0
        gctx->key_set = 0;
2662
0
        gctx->iv_set = 0;
2663
0
        gctx->ivlen = EVP_CIPHER_get_iv_length(c->cipher);
2664
0
        gctx->iv = c->iv;
2665
0
        gctx->taglen = -1;
2666
0
        gctx->iv_gen = 0;
2667
0
        gctx->tls_aad_len = -1;
2668
0
        return 1;
2669
2670
0
    case EVP_CTRL_GET_IVLEN:
2671
0
        *(int *)ptr = gctx->ivlen;
2672
0
        return 1;
2673
2674
0
    case EVP_CTRL_AEAD_SET_IVLEN:
2675
0
        if (arg <= 0)
2676
0
            return 0;
2677
        /* Allocate memory for IV if needed */
2678
0
        if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
2679
0
            if (gctx->iv != c->iv)
2680
0
                OPENSSL_free(gctx->iv);
2681
0
            if ((gctx->iv = OPENSSL_malloc(arg)) == NULL)
2682
0
                return 0;
2683
0
        }
2684
0
        gctx->ivlen = arg;
2685
0
        return 1;
2686
2687
0
    case EVP_CTRL_AEAD_SET_TAG:
2688
0
        if (arg <= 0 || arg > 16 || c->encrypt)
2689
0
            return 0;
2690
0
        memcpy(c->buf, ptr, arg);
2691
0
        gctx->taglen = arg;
2692
0
        return 1;
2693
2694
0
    case EVP_CTRL_AEAD_GET_TAG:
2695
0
        if (arg <= 0 || arg > 16 || !c->encrypt
2696
0
            || gctx->taglen < 0)
2697
0
            return 0;
2698
0
        memcpy(ptr, c->buf, arg);
2699
0
        return 1;
2700
2701
0
    case EVP_CTRL_GCM_SET_IV_FIXED:
2702
        /* Special case: -1 length restores whole IV */
2703
0
        if (arg == -1) {
2704
0
            memcpy(gctx->iv, ptr, gctx->ivlen);
2705
0
            gctx->iv_gen = 1;
2706
0
            return 1;
2707
0
        }
2708
        /*
2709
         * Fixed field must be at least 4 bytes and invocation field at least
2710
         * 8.
2711
         */
2712
0
        if ((arg < 4) || (gctx->ivlen - arg) < 8)
2713
0
            return 0;
2714
0
        if (arg)
2715
0
            memcpy(gctx->iv, ptr, arg);
2716
0
        if (c->encrypt && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
2717
0
            return 0;
2718
0
        gctx->iv_gen = 1;
2719
0
        return 1;
2720
2721
0
    case EVP_CTRL_GCM_IV_GEN:
2722
0
        if (gctx->iv_gen == 0 || gctx->key_set == 0)
2723
0
            return 0;
2724
0
        CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
2725
0
        if (arg <= 0 || arg > gctx->ivlen)
2726
0
            arg = gctx->ivlen;
2727
0
        memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
2728
        /*
2729
         * Invocation field will be at least 8 bytes in size and so no need
2730
         * to check wrap around or increment more than last 8 bytes.
2731
         */
2732
0
        ctr64_inc(gctx->iv + gctx->ivlen - 8);
2733
0
        gctx->iv_set = 1;
2734
0
        return 1;
2735
2736
0
    case EVP_CTRL_GCM_SET_IV_INV:
2737
0
        if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
2738
0
            return 0;
2739
0
        memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
2740
0
        CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
2741
0
        gctx->iv_set = 1;
2742
0
        return 1;
2743
2744
0
    case EVP_CTRL_AEAD_TLS1_AAD:
2745
        /* Save the AAD for later use */
2746
0
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
2747
0
            return 0;
2748
0
        memcpy(c->buf, ptr, arg);
2749
0
        gctx->tls_aad_len = arg;
2750
0
        gctx->tls_enc_records = 0;
2751
0
        {
2752
0
            unsigned int len = c->buf[arg - 2] << 8 | c->buf[arg - 1];
2753
            /* Correct length for explicit IV */
2754
0
            if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
2755
0
                return 0;
2756
0
            len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
2757
            /* If decrypting correct for tag too */
2758
0
            if (!c->encrypt) {
2759
0
                if (len < EVP_GCM_TLS_TAG_LEN)
2760
0
                    return 0;
2761
0
                len -= EVP_GCM_TLS_TAG_LEN;
2762
0
            }
2763
0
            c->buf[arg - 2] = len >> 8;
2764
0
            c->buf[arg - 1] = len & 0xff;
2765
0
        }
2766
        /* Extra padding: tag appended to record */
2767
0
        return EVP_GCM_TLS_TAG_LEN;
2768
2769
0
    case EVP_CTRL_COPY:
2770
0
        {
2771
0
            EVP_CIPHER_CTX *out = ptr;
2772
0
            EVP_AES_GCM_CTX *gctx_out = EVP_C_DATA(EVP_AES_GCM_CTX,out);
2773
0
            if (gctx->gcm.key) {
2774
0
                if (gctx->gcm.key != &gctx->ks)
2775
0
                    return 0;
2776
0
                gctx_out->gcm.key = &gctx_out->ks;
2777
0
            }
2778
0
            if (gctx->iv == c->iv)
2779
0
                gctx_out->iv = out->iv;
2780
0
            else {
2781
0
                if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL)
2782
0
                    return 0;
2783
0
                memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
2784
0
            }
2785
0
            return 1;
2786
0
        }
2787
2788
0
    default:
2789
0
        return -1;
2790
2791
0
    }
2792
0
}
2793
2794
static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2795
                            const unsigned char *iv, int enc)
2796
0
{
2797
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
2798
2799
0
    if (iv == NULL && key == NULL)
2800
0
        return 1;
2801
2802
0
    if (key != NULL) {
2803
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
2804
2805
0
        if (keylen <= 0) {
2806
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
2807
0
            return 0;
2808
0
        }
2809
0
        do {
2810
#ifdef HWAES_CAPABLE
2811
            if (HWAES_CAPABLE) {
2812
                HWAES_set_encrypt_key(key, keylen, &gctx->ks.ks);
2813
                CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2814
                                   (block128_f) HWAES_encrypt);
2815
# ifdef HWAES_ctr32_encrypt_blocks
2816
                gctx->ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
2817
# else
2818
                gctx->ctr = NULL;
2819
# endif
2820
                break;
2821
            } else
2822
#endif
2823
0
#ifdef BSAES_CAPABLE
2824
0
            if (BSAES_CAPABLE) {
2825
0
                AES_set_encrypt_key(key, keylen, &gctx->ks.ks);
2826
0
                CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2827
0
                                   (block128_f) AES_encrypt);
2828
0
                gctx->ctr = (ctr128_f) ossl_bsaes_ctr32_encrypt_blocks;
2829
0
                break;
2830
0
            } else
2831
0
#endif
2832
0
#ifdef VPAES_CAPABLE
2833
0
            if (VPAES_CAPABLE) {
2834
0
                vpaes_set_encrypt_key(key, keylen, &gctx->ks.ks);
2835
0
                CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2836
0
                                   (block128_f) vpaes_encrypt);
2837
0
                gctx->ctr = NULL;
2838
0
                break;
2839
0
            } else
2840
0
#endif
2841
0
                (void)0;        /* terminate potentially open 'else' */
2842
2843
0
            AES_set_encrypt_key(key, keylen, &gctx->ks.ks);
2844
0
            CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2845
0
                               (block128_f) AES_encrypt);
2846
#ifdef AES_CTR_ASM
2847
            gctx->ctr = (ctr128_f) AES_ctr32_encrypt;
2848
#else
2849
0
            gctx->ctr = NULL;
2850
0
#endif
2851
0
        } while (0);
2852
2853
        /*
2854
         * If we have an iv can set it directly, otherwise use saved IV.
2855
         */
2856
0
        if (iv == NULL && gctx->iv_set)
2857
0
            iv = gctx->iv;
2858
0
        if (iv) {
2859
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
2860
0
            gctx->iv_set = 1;
2861
0
        }
2862
0
        gctx->key_set = 1;
2863
0
    } else {
2864
        /* If key set use IV, otherwise copy */
2865
0
        if (gctx->key_set)
2866
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
2867
0
        else
2868
0
            memcpy(gctx->iv, iv, gctx->ivlen);
2869
0
        gctx->iv_set = 1;
2870
0
        gctx->iv_gen = 0;
2871
0
    }
2872
0
    return 1;
2873
0
}
2874
2875
/*
2876
 * Handle TLS GCM packet format. This consists of the last portion of the IV
2877
 * followed by the payload and finally the tag. On encrypt generate IV,
2878
 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
2879
 * and verify tag.
2880
 */
2881
2882
static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2883
                              const unsigned char *in, size_t len)
2884
0
{
2885
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
2886
0
    int rv = -1;
2887
    /* Encrypt/decrypt must be performed in place */
2888
0
    if (out != in
2889
0
        || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
2890
0
        return -1;
2891
2892
    /*
2893
     * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
2894
     * Requirements from SP 800-38D".  The requirements is for one party to the
2895
     * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
2896
     * side only.
2897
     */
2898
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx) && ++gctx->tls_enc_records == 0) {
2899
0
        ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_RECORDS);
2900
0
        goto err;
2901
0
    }
2902
2903
    /*
2904
     * Set IV from start of buffer or generate IV and write to start of
2905
     * buffer.
2906
     */
2907
0
    if (EVP_CIPHER_CTX_ctrl(ctx,
2908
0
                            EVP_CIPHER_CTX_is_encrypting(ctx) ?
2909
0
                                EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
2910
0
                            EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
2911
0
        goto err;
2912
    /* Use saved AAD */
2913
0
    if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
2914
0
                          gctx->tls_aad_len))
2915
0
        goto err;
2916
    /* Fix buffer and length to point to payload */
2917
0
    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
2918
0
    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
2919
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
2920
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
2921
        /* Encrypt payload */
2922
0
        if (gctx->ctr) {
2923
0
            size_t bulk = 0;
2924
0
#if defined(AES_GCM_ASM)
2925
0
            if (len >= 32 && AES_GCM_ASM(gctx)) {
2926
0
                if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
2927
0
                    return -1;
2928
2929
0
                bulk = AES_gcm_encrypt(in, out, len,
2930
0
                                       gctx->gcm.key,
2931
0
                                       gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2932
0
                gctx->gcm.len.u[1] += bulk;
2933
0
            }
2934
0
#endif
2935
0
            if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
2936
0
                                            in + bulk,
2937
0
                                            out + bulk,
2938
0
                                            len - bulk, gctx->ctr))
2939
0
                goto err;
2940
0
        } else {
2941
0
            size_t bulk = 0;
2942
#if defined(AES_GCM_ASM2)
2943
            if (len >= 32 && AES_GCM_ASM2(gctx)) {
2944
                if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
2945
                    return -1;
2946
2947
                bulk = AES_gcm_encrypt(in, out, len,
2948
                                       gctx->gcm.key,
2949
                                       gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2950
                gctx->gcm.len.u[1] += bulk;
2951
            }
2952
#endif
2953
0
            if (CRYPTO_gcm128_encrypt(&gctx->gcm,
2954
0
                                      in + bulk, out + bulk, len - bulk))
2955
0
                goto err;
2956
0
        }
2957
0
        out += len;
2958
        /* Finally write tag */
2959
0
        CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
2960
0
        rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
2961
0
    } else {
2962
        /* Decrypt */
2963
0
        if (gctx->ctr) {
2964
0
            size_t bulk = 0;
2965
0
#if defined(AES_GCM_ASM)
2966
0
            if (len >= 16 && AES_GCM_ASM(gctx)) {
2967
0
                if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
2968
0
                    return -1;
2969
2970
0
                bulk = AES_gcm_decrypt(in, out, len,
2971
0
                                       gctx->gcm.key,
2972
0
                                       gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2973
0
                gctx->gcm.len.u[1] += bulk;
2974
0
            }
2975
0
#endif
2976
0
            if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
2977
0
                                            in + bulk,
2978
0
                                            out + bulk,
2979
0
                                            len - bulk, gctx->ctr))
2980
0
                goto err;
2981
0
        } else {
2982
0
            size_t bulk = 0;
2983
#if defined(AES_GCM_ASM2)
2984
            if (len >= 16 && AES_GCM_ASM2(gctx)) {
2985
                if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
2986
                    return -1;
2987
2988
                bulk = AES_gcm_decrypt(in, out, len,
2989
                                       gctx->gcm.key,
2990
                                       gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2991
                gctx->gcm.len.u[1] += bulk;
2992
            }
2993
#endif
2994
0
            if (CRYPTO_gcm128_decrypt(&gctx->gcm,
2995
0
                                      in + bulk, out + bulk, len - bulk))
2996
0
                goto err;
2997
0
        }
2998
        /* Retrieve tag */
2999
0
        CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
3000
0
                          EVP_GCM_TLS_TAG_LEN);
3001
        /* If tag mismatch wipe buffer */
3002
0
        if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
3003
0
                          EVP_GCM_TLS_TAG_LEN)) {
3004
0
            OPENSSL_cleanse(out, len);
3005
0
            goto err;
3006
0
        }
3007
0
        rv = len;
3008
0
    }
3009
3010
0
 err:
3011
0
    gctx->iv_set = 0;
3012
0
    gctx->tls_aad_len = -1;
3013
0
    return rv;
3014
0
}
3015
3016
#ifdef FIPS_MODULE
3017
/*
3018
 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
3019
 *
3020
 * See also 8.2.2 RBG-based construction.
3021
 * Random construction consists of a free field (which can be NULL) and a
3022
 * random field which will use a DRBG that can return at least 96 bits of
3023
 * entropy strength. (The DRBG must be seeded by the FIPS module).
3024
 */
3025
static int aes_gcm_iv_generate(EVP_AES_GCM_CTX *gctx, int offset)
3026
{
3027
    int sz = gctx->ivlen - offset;
3028
3029
    /* Must be at least 96 bits */
3030
    if (sz <= 0 || gctx->ivlen < 12)
3031
        return 0;
3032
3033
    /* Use DRBG to generate random iv */
3034
    if (RAND_bytes(gctx->iv + offset, sz) <= 0)
3035
        return 0;
3036
    return 1;
3037
}
3038
#endif /* FIPS_MODULE */
3039
3040
static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3041
                          const unsigned char *in, size_t len)
3042
0
{
3043
0
    EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
3044
3045
    /* If not set up, return error */
3046
0
    if (!gctx->key_set)
3047
0
        return -1;
3048
3049
0
    if (gctx->tls_aad_len >= 0)
3050
0
        return aes_gcm_tls_cipher(ctx, out, in, len);
3051
3052
#ifdef FIPS_MODULE
3053
    /*
3054
     * FIPS requires generation of AES-GCM IV's inside the FIPS module.
3055
     * The IV can still be set externally (the security policy will state that
3056
     * this is not FIPS compliant). There are some applications
3057
     * where setting the IV externally is the only option available.
3058
     */
3059
    if (!gctx->iv_set) {
3060
        if (!EVP_CIPHER_CTX_is_encrypting(ctx) || !aes_gcm_iv_generate(gctx, 0))
3061
            return -1;
3062
        CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
3063
        gctx->iv_set = 1;
3064
        gctx->iv_gen_rand = 1;
3065
    }
3066
#else
3067
0
    if (!gctx->iv_set)
3068
0
        return -1;
3069
0
#endif /* FIPS_MODULE */
3070
3071
0
    if (in) {
3072
0
        if (out == NULL) {
3073
0
            if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
3074
0
                return -1;
3075
0
        } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
3076
0
            if (gctx->ctr) {
3077
0
                size_t bulk = 0;
3078
0
#if defined(AES_GCM_ASM)
3079
0
                if (len >= 32 && AES_GCM_ASM(gctx)) {
3080
0
                    size_t res = (16 - gctx->gcm.mres) % 16;
3081
3082
0
                    if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
3083
0
                        return -1;
3084
3085
0
                    bulk = AES_gcm_encrypt(in + res,
3086
0
                                           out + res, len - res,
3087
0
                                           gctx->gcm.key, gctx->gcm.Yi.c,
3088
0
                                           gctx->gcm.Xi.u);
3089
0
                    gctx->gcm.len.u[1] += bulk;
3090
0
                    bulk += res;
3091
0
                }
3092
0
#endif
3093
0
                if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
3094
0
                                                in + bulk,
3095
0
                                                out + bulk,
3096
0
                                                len - bulk, gctx->ctr))
3097
0
                    return -1;
3098
0
            } else {
3099
0
                size_t bulk = 0;
3100
#if defined(AES_GCM_ASM2)
3101
                if (len >= 32 && AES_GCM_ASM2(gctx)) {
3102
                    size_t res = (16 - gctx->gcm.mres) % 16;
3103
3104
                    if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
3105
                        return -1;
3106
3107
                    bulk = AES_gcm_encrypt(in + res,
3108
                                           out + res, len - res,
3109
                                           gctx->gcm.key, gctx->gcm.Yi.c,
3110
                                           gctx->gcm.Xi.u);
3111
                    gctx->gcm.len.u[1] += bulk;
3112
                    bulk += res;
3113
                }
3114
#endif
3115
0
                if (CRYPTO_gcm128_encrypt(&gctx->gcm,
3116
0
                                          in + bulk, out + bulk, len - bulk))
3117
0
                    return -1;
3118
0
            }
3119
0
        } else {
3120
0
            if (gctx->ctr) {
3121
0
                size_t bulk = 0;
3122
0
#if defined(AES_GCM_ASM)
3123
0
                if (len >= 16 && AES_GCM_ASM(gctx)) {
3124
0
                    size_t res = (16 - gctx->gcm.mres) % 16;
3125
3126
0
                    if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
3127
0
                        return -1;
3128
3129
0
                    bulk = AES_gcm_decrypt(in + res,
3130
0
                                           out + res, len - res,
3131
0
                                           gctx->gcm.key,
3132
0
                                           gctx->gcm.Yi.c, gctx->gcm.Xi.u);
3133
0
                    gctx->gcm.len.u[1] += bulk;
3134
0
                    bulk += res;
3135
0
                }
3136
0
#endif
3137
0
                if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
3138
0
                                                in + bulk,
3139
0
                                                out + bulk,
3140
0
                                                len - bulk, gctx->ctr))
3141
0
                    return -1;
3142
0
            } else {
3143
0
                size_t bulk = 0;
3144
#if defined(AES_GCM_ASM2)
3145
                if (len >= 16 && AES_GCM_ASM2(gctx)) {
3146
                    size_t res = (16 - gctx->gcm.mres) % 16;
3147
3148
                    if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
3149
                        return -1;
3150
3151
                    bulk = AES_gcm_decrypt(in + res,
3152
                                           out + res, len - res,
3153
                                           gctx->gcm.key,
3154
                                           gctx->gcm.Yi.c, gctx->gcm.Xi.u);
3155
                    gctx->gcm.len.u[1] += bulk;
3156
                    bulk += res;
3157
                }
3158
#endif
3159
0
                if (CRYPTO_gcm128_decrypt(&gctx->gcm,
3160
0
                                          in + bulk, out + bulk, len - bulk))
3161
0
                    return -1;
3162
0
            }
3163
0
        }
3164
0
        return len;
3165
0
    } else {
3166
0
        if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
3167
0
            if (gctx->taglen < 0)
3168
0
                return -1;
3169
0
            if (CRYPTO_gcm128_finish(&gctx->gcm,
3170
0
                                     EVP_CIPHER_CTX_buf_noconst(ctx),
3171
0
                                     gctx->taglen) != 0)
3172
0
                return -1;
3173
0
            gctx->iv_set = 0;
3174
0
            return 0;
3175
0
        }
3176
0
        CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
3177
0
        gctx->taglen = 16;
3178
        /* Don't reuse the IV */
3179
0
        gctx->iv_set = 0;
3180
0
        return 0;
3181
0
    }
3182
3183
0
}
3184
3185
#define CUSTOM_FLAGS    (EVP_CIPH_FLAG_DEFAULT_ASN1 \
3186
                | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3187
                | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3188
                | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_CUSTOM_IV_LENGTH)
3189
3190
BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, gcm, GCM,
3191
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3192
BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, gcm, GCM,
3193
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3194
BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, gcm, GCM,
3195
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3196
3197
static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3198
0
{
3199
0
    EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX, c);
3200
3201
0
    if (type == EVP_CTRL_COPY) {
3202
0
        EVP_CIPHER_CTX *out = ptr;
3203
0
        EVP_AES_XTS_CTX *xctx_out = EVP_C_DATA(EVP_AES_XTS_CTX,out);
3204
3205
0
        if (xctx->xts.key1) {
3206
0
            if (xctx->xts.key1 != &xctx->ks1)
3207
0
                return 0;
3208
0
            xctx_out->xts.key1 = &xctx_out->ks1;
3209
0
        }
3210
0
        if (xctx->xts.key2) {
3211
0
            if (xctx->xts.key2 != &xctx->ks2)
3212
0
                return 0;
3213
0
            xctx_out->xts.key2 = &xctx_out->ks2;
3214
0
        }
3215
0
        return 1;
3216
0
    } else if (type != EVP_CTRL_INIT)
3217
0
        return -1;
3218
    /* key1 and key2 are used as an indicator both key and IV are set */
3219
0
    xctx->xts.key1 = NULL;
3220
0
    xctx->xts.key2 = NULL;
3221
0
    return 1;
3222
0
}
3223
3224
static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3225
                            const unsigned char *iv, int enc)
3226
0
{
3227
0
    EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
3228
3229
0
    if (iv == NULL && key == NULL)
3230
0
        return 1;
3231
3232
0
    if (key != NULL) {
3233
0
        do {
3234
            /* The key is two half length keys in reality */
3235
0
            const int keylen = EVP_CIPHER_CTX_get_key_length(ctx);
3236
0
            const int bytes = keylen / 2;
3237
0
            const int bits = bytes * 8;
3238
3239
0
            if (keylen <= 0) {
3240
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
3241
0
                return 0;
3242
0
            }
3243
            /*
3244
             * Verify that the two keys are different.
3245
             *
3246
             * This addresses the vulnerability described in Rogaway's
3247
             * September 2004 paper:
3248
             *
3249
             *      "Efficient Instantiations of Tweakable Blockciphers and
3250
             *       Refinements to Modes OCB and PMAC".
3251
             *      (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
3252
             *
3253
             * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
3254
             * that:
3255
             *      "The check for Key_1 != Key_2 shall be done at any place
3256
             *       BEFORE using the keys in the XTS-AES algorithm to process
3257
             *       data with them."
3258
             */
3259
0
            if ((!allow_insecure_decrypt || enc)
3260
0
                    && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
3261
0
                ERR_raise(ERR_LIB_EVP, EVP_R_XTS_DUPLICATED_KEYS);
3262
0
                return 0;
3263
0
            }
3264
3265
#ifdef AES_XTS_ASM
3266
            xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
3267
#else
3268
0
            xctx->stream = NULL;
3269
0
#endif
3270
            /* key_len is two AES keys */
3271
#ifdef HWAES_CAPABLE
3272
            if (HWAES_CAPABLE) {
3273
                if (enc) {
3274
                    HWAES_set_encrypt_key(key, bits, &xctx->ks1.ks);
3275
                    xctx->xts.block1 = (block128_f) HWAES_encrypt;
3276
# ifdef HWAES_xts_encrypt
3277
                    xctx->stream = HWAES_xts_encrypt;
3278
# endif
3279
                } else {
3280
                    HWAES_set_decrypt_key(key, bits, &xctx->ks1.ks);
3281
                    xctx->xts.block1 = (block128_f) HWAES_decrypt;
3282
# ifdef HWAES_xts_decrypt
3283
                    xctx->stream = HWAES_xts_decrypt;
3284
#endif
3285
                }
3286
3287
                HWAES_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3288
                xctx->xts.block2 = (block128_f) HWAES_encrypt;
3289
3290
                xctx->xts.key1 = &xctx->ks1;
3291
                break;
3292
            } else
3293
#endif
3294
0
#ifdef BSAES_CAPABLE
3295
0
            if (BSAES_CAPABLE)
3296
0
                xctx->stream = enc ? ossl_bsaes_xts_encrypt : ossl_bsaes_xts_decrypt;
3297
0
            else
3298
0
#endif
3299
0
#ifdef VPAES_CAPABLE
3300
0
            if (VPAES_CAPABLE) {
3301
0
                if (enc) {
3302
0
                    vpaes_set_encrypt_key(key, bits, &xctx->ks1.ks);
3303
0
                    xctx->xts.block1 = (block128_f) vpaes_encrypt;
3304
0
                } else {
3305
0
                    vpaes_set_decrypt_key(key, bits, &xctx->ks1.ks);
3306
0
                    xctx->xts.block1 = (block128_f) vpaes_decrypt;
3307
0
                }
3308
3309
0
                vpaes_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3310
0
                xctx->xts.block2 = (block128_f) vpaes_encrypt;
3311
3312
0
                xctx->xts.key1 = &xctx->ks1;
3313
0
                break;
3314
0
            } else
3315
0
#endif
3316
0
                (void)0;        /* terminate potentially open 'else' */
3317
3318
0
            if (enc) {
3319
0
                AES_set_encrypt_key(key, bits, &xctx->ks1.ks);
3320
0
                xctx->xts.block1 = (block128_f) AES_encrypt;
3321
0
            } else {
3322
0
                AES_set_decrypt_key(key, bits, &xctx->ks1.ks);
3323
0
                xctx->xts.block1 = (block128_f) AES_decrypt;
3324
0
            }
3325
3326
0
            AES_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3327
0
            xctx->xts.block2 = (block128_f) AES_encrypt;
3328
3329
0
            xctx->xts.key1 = &xctx->ks1;
3330
0
        } while (0);
3331
0
    }
3332
3333
0
    if (iv) {
3334
0
        xctx->xts.key2 = &xctx->ks2;
3335
0
        memcpy(ctx->iv, iv, 16);
3336
0
    }
3337
3338
0
    return 1;
3339
0
}
3340
3341
static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3342
                          const unsigned char *in, size_t len)
3343
0
{
3344
0
    EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
3345
3346
0
    if (xctx->xts.key1 == NULL
3347
0
            || xctx->xts.key2 == NULL
3348
0
            || out == NULL
3349
0
            || in == NULL
3350
0
            || len < AES_BLOCK_SIZE)
3351
0
        return 0;
3352
3353
    /*
3354
     * Impose a limit of 2^20 blocks per data unit as specified by
3355
     * IEEE Std 1619-2018.  The earlier and obsolete IEEE Std 1619-2007
3356
     * indicated that this was a SHOULD NOT rather than a MUST NOT.
3357
     * NIST SP 800-38E mandates the same limit.
3358
     */
3359
0
    if (len > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
3360
0
        ERR_raise(ERR_LIB_EVP, EVP_R_XTS_DATA_UNIT_IS_TOO_LARGE);
3361
0
        return 0;
3362
0
    }
3363
3364
0
    if (xctx->stream)
3365
0
        (*xctx->stream) (in, out, len,
3366
0
                         xctx->xts.key1, xctx->xts.key2,
3367
0
                         ctx->iv);
3368
0
    else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
3369
0
                                   EVP_CIPHER_CTX_is_encrypting(ctx)))
3370
0
        return 0;
3371
0
    return 1;
3372
0
}
3373
3374
#define aes_xts_cleanup NULL
3375
3376
#define XTS_FLAGS       (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
3377
                         | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3378
                         | EVP_CIPH_CUSTOM_COPY)
3379
3380
BLOCK_CIPHER_custom(NID_aes, 128, 1, 16, xts, XTS, XTS_FLAGS)
3381
BLOCK_CIPHER_custom(NID_aes, 256, 1, 16, xts, XTS, XTS_FLAGS)
3382
3383
static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3384
0
{
3385
0
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,c);
3386
0
    switch (type) {
3387
0
    case EVP_CTRL_INIT:
3388
0
        cctx->key_set = 0;
3389
0
        cctx->iv_set = 0;
3390
0
        cctx->L = 8;
3391
0
        cctx->M = 12;
3392
0
        cctx->tag_set = 0;
3393
0
        cctx->len_set = 0;
3394
0
        cctx->tls_aad_len = -1;
3395
0
        return 1;
3396
3397
0
    case EVP_CTRL_GET_IVLEN:
3398
0
        *(int *)ptr = 15 - cctx->L;
3399
0
        return 1;
3400
3401
0
    case EVP_CTRL_AEAD_TLS1_AAD:
3402
        /* Save the AAD for later use */
3403
0
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
3404
0
            return 0;
3405
0
        memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
3406
0
        cctx->tls_aad_len = arg;
3407
0
        {
3408
0
            uint16_t len =
3409
0
                EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
3410
0
                | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
3411
            /* Correct length for explicit IV */
3412
0
            if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
3413
0
                return 0;
3414
0
            len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
3415
            /* If decrypting correct for tag too */
3416
0
            if (!EVP_CIPHER_CTX_is_encrypting(c)) {
3417
0
                if (len < cctx->M)
3418
0
                    return 0;
3419
0
                len -= cctx->M;
3420
0
            }
3421
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
3422
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
3423
0
        }
3424
        /* Extra padding: tag appended to record */
3425
0
        return cctx->M;
3426
3427
0
    case EVP_CTRL_CCM_SET_IV_FIXED:
3428
        /* Sanity check length */
3429
0
        if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
3430
0
            return 0;
3431
        /* Just copy to first part of IV */
3432
0
        memcpy(c->iv, ptr, arg);
3433
0
        return 1;
3434
3435
0
    case EVP_CTRL_AEAD_SET_IVLEN:
3436
0
        arg = 15 - arg;
3437
        /* fall through */
3438
0
    case EVP_CTRL_CCM_SET_L:
3439
0
        if (arg < 2 || arg > 8)
3440
0
            return 0;
3441
0
        cctx->L = arg;
3442
0
        return 1;
3443
3444
0
    case EVP_CTRL_AEAD_SET_TAG:
3445
0
        if ((arg & 1) || arg < 4 || arg > 16)
3446
0
            return 0;
3447
0
        if (EVP_CIPHER_CTX_is_encrypting(c) && ptr)
3448
0
            return 0;
3449
0
        if (ptr) {
3450
0
            cctx->tag_set = 1;
3451
0
            memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
3452
0
        }
3453
0
        cctx->M = arg;
3454
0
        return 1;
3455
3456
0
    case EVP_CTRL_AEAD_GET_TAG:
3457
0
        if (!EVP_CIPHER_CTX_is_encrypting(c) || !cctx->tag_set)
3458
0
            return 0;
3459
0
        if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
3460
0
            return 0;
3461
0
        cctx->tag_set = 0;
3462
0
        cctx->iv_set = 0;
3463
0
        cctx->len_set = 0;
3464
0
        return 1;
3465
3466
0
    case EVP_CTRL_COPY:
3467
0
        {
3468
0
            EVP_CIPHER_CTX *out = ptr;
3469
0
            EVP_AES_CCM_CTX *cctx_out = EVP_C_DATA(EVP_AES_CCM_CTX,out);
3470
0
            if (cctx->ccm.key) {
3471
0
                if (cctx->ccm.key != &cctx->ks)
3472
0
                    return 0;
3473
0
                cctx_out->ccm.key = &cctx_out->ks;
3474
0
            }
3475
0
            return 1;
3476
0
        }
3477
3478
0
    default:
3479
0
        return -1;
3480
3481
0
    }
3482
0
}
3483
3484
static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3485
                            const unsigned char *iv, int enc)
3486
0
{
3487
0
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3488
3489
0
    if (iv == NULL && key == NULL)
3490
0
        return 1;
3491
3492
0
    if (key != NULL) {
3493
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
3494
3495
0
        if (keylen <= 0) {
3496
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
3497
0
            return 0;
3498
0
        }
3499
0
        do {
3500
#ifdef HWAES_CAPABLE
3501
            if (HWAES_CAPABLE) {
3502
                HWAES_set_encrypt_key(key, keylen, &cctx->ks.ks);
3503
3504
                CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3505
                                   &cctx->ks, (block128_f) HWAES_encrypt);
3506
                cctx->str = NULL;
3507
                cctx->key_set = 1;
3508
                break;
3509
            } else
3510
#endif
3511
0
#ifdef VPAES_CAPABLE
3512
0
            if (VPAES_CAPABLE) {
3513
0
                vpaes_set_encrypt_key(key, keylen, &cctx->ks.ks);
3514
0
                CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3515
0
                                   &cctx->ks, (block128_f) vpaes_encrypt);
3516
0
                cctx->str = NULL;
3517
0
                cctx->key_set = 1;
3518
0
                break;
3519
0
            }
3520
0
#endif
3521
0
            AES_set_encrypt_key(key, keylen, &cctx->ks.ks);
3522
0
            CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3523
0
                               &cctx->ks, (block128_f) AES_encrypt);
3524
0
            cctx->str = NULL;
3525
0
            cctx->key_set = 1;
3526
0
        } while (0);
3527
0
    }
3528
0
    if (iv != NULL) {
3529
0
        memcpy(ctx->iv, iv, 15 - cctx->L);
3530
0
        cctx->iv_set = 1;
3531
0
    }
3532
0
    return 1;
3533
0
}
3534
3535
static int aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3536
                              const unsigned char *in, size_t len)
3537
0
{
3538
0
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3539
0
    CCM128_CONTEXT *ccm = &cctx->ccm;
3540
    /* Encrypt/decrypt must be performed in place */
3541
0
    if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
3542
0
        return -1;
3543
    /* If encrypting set explicit IV from sequence number (start of AAD) */
3544
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx))
3545
0
        memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
3546
0
               EVP_CCM_TLS_EXPLICIT_IV_LEN);
3547
    /* Get rest of IV from explicit IV */
3548
0
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in,
3549
0
           EVP_CCM_TLS_EXPLICIT_IV_LEN);
3550
    /* Correct length value */
3551
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
3552
0
    if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,
3553
0
                            len))
3554
0
            return -1;
3555
    /* Use saved AAD */
3556
0
    CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx),
3557
0
                      cctx->tls_aad_len);
3558
    /* Fix buffer to point to payload */
3559
0
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
3560
0
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
3561
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
3562
0
        if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
3563
0
                                                    cctx->str) :
3564
0
            CRYPTO_ccm128_encrypt(ccm, in, out, len))
3565
0
            return -1;
3566
0
        if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
3567
0
            return -1;
3568
0
        return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
3569
0
    } else {
3570
0
        if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
3571
0
                                                     cctx->str) :
3572
0
            !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
3573
0
            unsigned char tag[16];
3574
0
            if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
3575
0
                if (!CRYPTO_memcmp(tag, in + len, cctx->M))
3576
0
                    return len;
3577
0
            }
3578
0
        }
3579
0
        OPENSSL_cleanse(out, len);
3580
0
        return -1;
3581
0
    }
3582
0
}
3583
3584
static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3585
                          const unsigned char *in, size_t len)
3586
0
{
3587
0
    EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3588
0
    CCM128_CONTEXT *ccm = &cctx->ccm;
3589
    /* If not set up, return error */
3590
0
    if (!cctx->key_set)
3591
0
        return -1;
3592
3593
0
    if (cctx->tls_aad_len >= 0)
3594
0
        return aes_ccm_tls_cipher(ctx, out, in, len);
3595
3596
    /* EVP_*Final() doesn't return any data */
3597
0
    if (in == NULL && out != NULL)
3598
0
        return 0;
3599
3600
0
    if (!cctx->iv_set)
3601
0
        return -1;
3602
3603
0
    if (!out) {
3604
0
        if (!in) {
3605
0
            if (CRYPTO_ccm128_setiv(ccm, ctx->iv,
3606
0
                                    15 - cctx->L, len))
3607
0
                return -1;
3608
0
            cctx->len_set = 1;
3609
0
            return len;
3610
0
        }
3611
        /* If have AAD need message length */
3612
0
        if (!cctx->len_set && len)
3613
0
            return -1;
3614
0
        CRYPTO_ccm128_aad(ccm, in, len);
3615
0
        return len;
3616
0
    }
3617
3618
    /* The tag must be set before actually decrypting data */
3619
0
    if (!EVP_CIPHER_CTX_is_encrypting(ctx) && !cctx->tag_set)
3620
0
        return -1;
3621
3622
    /* If not set length yet do it */
3623
0
    if (!cctx->len_set) {
3624
0
        if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
3625
0
            return -1;
3626
0
        cctx->len_set = 1;
3627
0
    }
3628
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
3629
0
        if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
3630
0
                                                    cctx->str) :
3631
0
            CRYPTO_ccm128_encrypt(ccm, in, out, len))
3632
0
            return -1;
3633
0
        cctx->tag_set = 1;
3634
0
        return len;
3635
0
    } else {
3636
0
        int rv = -1;
3637
0
        if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
3638
0
                                                     cctx->str) :
3639
0
            !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
3640
0
            unsigned char tag[16];
3641
0
            if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
3642
0
                if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
3643
0
                                   cctx->M))
3644
0
                    rv = len;
3645
0
            }
3646
0
        }
3647
0
        if (rv == -1)
3648
0
            OPENSSL_cleanse(out, len);
3649
0
        cctx->iv_set = 0;
3650
0
        cctx->tag_set = 0;
3651
0
        cctx->len_set = 0;
3652
0
        return rv;
3653
0
    }
3654
0
}
3655
3656
#define aes_ccm_cleanup NULL
3657
3658
BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, ccm, CCM,
3659
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3660
BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, ccm, CCM,
3661
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3662
BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, ccm, CCM,
3663
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3664
3665
typedef struct {
3666
    union {
3667
        OSSL_UNION_ALIGN;
3668
        AES_KEY ks;
3669
    } ks;
3670
    /* Indicates if IV has been set */
3671
    unsigned char *iv;
3672
} EVP_AES_WRAP_CTX;
3673
3674
static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3675
                             const unsigned char *iv, int enc)
3676
0
{
3677
0
    int len;
3678
0
    EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
3679
3680
0
    if (iv == NULL && key == NULL)
3681
0
        return 1;
3682
0
    if (key != NULL) {
3683
0
        const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
3684
3685
0
        if (keylen <= 0) {
3686
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
3687
0
            return 0;
3688
0
        }
3689
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx))
3690
0
            AES_set_encrypt_key(key, keylen, &wctx->ks.ks);
3691
0
        else
3692
0
            AES_set_decrypt_key(key, keylen, &wctx->ks.ks);
3693
0
        if (iv == NULL)
3694
0
            wctx->iv = NULL;
3695
0
    }
3696
0
    if (iv != NULL) {
3697
0
        if ((len = EVP_CIPHER_CTX_get_iv_length(ctx)) < 0)
3698
0
            return 0;
3699
0
        memcpy(ctx->iv, iv, len);
3700
0
        wctx->iv = ctx->iv;
3701
0
    }
3702
0
    return 1;
3703
0
}
3704
3705
static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3706
                           const unsigned char *in, size_t inlen)
3707
0
{
3708
0
    EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
3709
0
    size_t rv;
3710
    /* AES wrap with padding has IV length of 4, without padding 8 */
3711
0
    int pad = EVP_CIPHER_CTX_get_iv_length(ctx) == 4;
3712
    /* No final operation so always return zero length */
3713
0
    if (!in)
3714
0
        return 0;
3715
    /* Input length must always be non-zero */
3716
0
    if (!inlen)
3717
0
        return -1;
3718
    /* If decrypting need at least 16 bytes and multiple of 8 */
3719
0
    if (!EVP_CIPHER_CTX_is_encrypting(ctx) && (inlen < 16 || inlen & 0x7))
3720
0
        return -1;
3721
    /* If not padding input must be multiple of 8 */
3722
0
    if (!pad && inlen & 0x7)
3723
0
        return -1;
3724
0
    if (ossl_is_partially_overlapping(out, in, inlen)) {
3725
0
        ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
3726
0
        return 0;
3727
0
    }
3728
0
    if (!out) {
3729
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
3730
            /* If padding round up to multiple of 8 */
3731
0
            if (pad)
3732
0
                inlen = (inlen + 7) / 8 * 8;
3733
            /* 8 byte prefix */
3734
0
            return inlen + 8;
3735
0
        } else {
3736
            /*
3737
             * If not padding output will be exactly 8 bytes smaller than
3738
             * input. If padding it will be at least 8 bytes smaller but we
3739
             * don't know how much.
3740
             */
3741
0
            return inlen - 8;
3742
0
        }
3743
0
    }
3744
0
    if (pad) {
3745
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx))
3746
0
            rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
3747
0
                                     out, in, inlen,
3748
0
                                     (block128_f) AES_encrypt);
3749
0
        else
3750
0
            rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
3751
0
                                       out, in, inlen,
3752
0
                                       (block128_f) AES_decrypt);
3753
0
    } else {
3754
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx))
3755
0
            rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
3756
0
                                 out, in, inlen, (block128_f) AES_encrypt);
3757
0
        else
3758
0
            rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
3759
0
                                   out, in, inlen, (block128_f) AES_decrypt);
3760
0
    }
3761
0
    return rv ? (int)rv : -1;
3762
0
}
3763
3764
#define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE \
3765
                | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3766
                | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
3767
3768
static const EVP_CIPHER aes_128_wrap = {
3769
    NID_id_aes128_wrap,
3770
    8, 16, 8, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3771
    aes_wrap_init_key, aes_wrap_cipher,
3772
    NULL,
3773
    sizeof(EVP_AES_WRAP_CTX),
3774
    NULL, NULL, NULL, NULL
3775
};
3776
3777
const EVP_CIPHER *EVP_aes_128_wrap(void)
3778
0
{
3779
0
    return &aes_128_wrap;
3780
0
}
3781
3782
static const EVP_CIPHER aes_192_wrap = {
3783
    NID_id_aes192_wrap,
3784
    8, 24, 8, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3785
    aes_wrap_init_key, aes_wrap_cipher,
3786
    NULL,
3787
    sizeof(EVP_AES_WRAP_CTX),
3788
    NULL, NULL, NULL, NULL
3789
};
3790
3791
const EVP_CIPHER *EVP_aes_192_wrap(void)
3792
0
{
3793
0
    return &aes_192_wrap;
3794
0
}
3795
3796
static const EVP_CIPHER aes_256_wrap = {
3797
    NID_id_aes256_wrap,
3798
    8, 32, 8, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3799
    aes_wrap_init_key, aes_wrap_cipher,
3800
    NULL,
3801
    sizeof(EVP_AES_WRAP_CTX),
3802
    NULL, NULL, NULL, NULL
3803
};
3804
3805
const EVP_CIPHER *EVP_aes_256_wrap(void)
3806
0
{
3807
0
    return &aes_256_wrap;
3808
0
}
3809
3810
static const EVP_CIPHER aes_128_wrap_pad = {
3811
    NID_id_aes128_wrap_pad,
3812
    8, 16, 4, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3813
    aes_wrap_init_key, aes_wrap_cipher,
3814
    NULL,
3815
    sizeof(EVP_AES_WRAP_CTX),
3816
    NULL, NULL, NULL, NULL
3817
};
3818
3819
const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
3820
0
{
3821
0
    return &aes_128_wrap_pad;
3822
0
}
3823
3824
static const EVP_CIPHER aes_192_wrap_pad = {
3825
    NID_id_aes192_wrap_pad,
3826
    8, 24, 4, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3827
    aes_wrap_init_key, aes_wrap_cipher,
3828
    NULL,
3829
    sizeof(EVP_AES_WRAP_CTX),
3830
    NULL, NULL, NULL, NULL
3831
};
3832
3833
const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
3834
0
{
3835
0
    return &aes_192_wrap_pad;
3836
0
}
3837
3838
static const EVP_CIPHER aes_256_wrap_pad = {
3839
    NID_id_aes256_wrap_pad,
3840
    8, 32, 4, WRAP_FLAGS, EVP_ORIG_GLOBAL,
3841
    aes_wrap_init_key, aes_wrap_cipher,
3842
    NULL,
3843
    sizeof(EVP_AES_WRAP_CTX),
3844
    NULL, NULL, NULL, NULL
3845
};
3846
3847
const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
3848
0
{
3849
0
    return &aes_256_wrap_pad;
3850
0
}
3851
3852
#ifndef OPENSSL_NO_OCB
3853
static int aes_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3854
0
{
3855
0
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
3856
0
    EVP_CIPHER_CTX *newc;
3857
0
    EVP_AES_OCB_CTX *new_octx;
3858
3859
0
    switch (type) {
3860
0
    case EVP_CTRL_INIT:
3861
0
        octx->key_set = 0;
3862
0
        octx->iv_set = 0;
3863
0
        octx->ivlen = EVP_CIPHER_get_iv_length(c->cipher);
3864
0
        octx->iv = c->iv;
3865
0
        octx->taglen = 16;
3866
0
        octx->data_buf_len = 0;
3867
0
        octx->aad_buf_len = 0;
3868
0
        return 1;
3869
3870
0
    case EVP_CTRL_GET_IVLEN:
3871
0
        *(int *)ptr = octx->ivlen;
3872
0
        return 1;
3873
3874
0
    case EVP_CTRL_AEAD_SET_IVLEN:
3875
        /* IV len must be 1 to 15 */
3876
0
        if (arg <= 0 || arg > 15)
3877
0
            return 0;
3878
3879
0
        octx->ivlen = arg;
3880
0
        return 1;
3881
3882
0
    case EVP_CTRL_AEAD_SET_TAG:
3883
0
        if (ptr == NULL) {
3884
            /* Tag len must be 0 to 16 */
3885
0
            if (arg < 0 || arg > 16)
3886
0
                return 0;
3887
3888
0
            octx->taglen = arg;
3889
0
            return 1;
3890
0
        }
3891
0
        if (arg != octx->taglen || EVP_CIPHER_CTX_is_encrypting(c))
3892
0
            return 0;
3893
0
        memcpy(octx->tag, ptr, arg);
3894
0
        return 1;
3895
3896
0
    case EVP_CTRL_AEAD_GET_TAG:
3897
0
        if (arg != octx->taglen || !EVP_CIPHER_CTX_is_encrypting(c))
3898
0
            return 0;
3899
3900
0
        memcpy(ptr, octx->tag, arg);
3901
0
        return 1;
3902
3903
0
    case EVP_CTRL_COPY:
3904
0
        newc = (EVP_CIPHER_CTX *)ptr;
3905
0
        new_octx = EVP_C_DATA(EVP_AES_OCB_CTX,newc);
3906
0
        return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb,
3907
0
                                      &new_octx->ksenc.ks,
3908
0
                                      &new_octx->ksdec.ks);
3909
3910
0
    default:
3911
0
        return -1;
3912
3913
0
    }
3914
0
}
3915
3916
static int aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3917
                            const unsigned char *iv, int enc)
3918
0
{
3919
0
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
3920
3921
0
    if (iv == NULL && key == NULL)
3922
0
        return 1;
3923
3924
0
    if (key != NULL) {
3925
0
       const int keylen = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
3926
3927
0
        if (keylen <= 0) {
3928
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
3929
0
            return 0;
3930
0
        }
3931
0
        do {
3932
            /*
3933
             * We set both the encrypt and decrypt key here because decrypt
3934
             * needs both. We could possibly optimise to remove setting the
3935
             * decrypt for an encryption operation.
3936
             */
3937
# ifdef HWAES_CAPABLE
3938
            if (HWAES_CAPABLE) {
3939
                HWAES_set_encrypt_key(key, keylen, &octx->ksenc.ks);
3940
                HWAES_set_decrypt_key(key, keylen, &octx->ksdec.ks);
3941
                if (!CRYPTO_ocb128_init(&octx->ocb,
3942
                                        &octx->ksenc.ks, &octx->ksdec.ks,
3943
                                        (block128_f) HWAES_encrypt,
3944
                                        (block128_f) HWAES_decrypt,
3945
                                        enc ? HWAES_ocb_encrypt
3946
                                            : HWAES_ocb_decrypt))
3947
                    return 0;
3948
                break;
3949
            }
3950
# endif
3951
0
# ifdef VPAES_CAPABLE
3952
0
            if (VPAES_CAPABLE) {
3953
0
                vpaes_set_encrypt_key(key, keylen, &octx->ksenc.ks);
3954
0
                vpaes_set_decrypt_key(key, keylen, &octx->ksdec.ks);
3955
0
                if (!CRYPTO_ocb128_init(&octx->ocb,
3956
0
                                        &octx->ksenc.ks, &octx->ksdec.ks,
3957
0
                                        (block128_f) vpaes_encrypt,
3958
0
                                        (block128_f) vpaes_decrypt,
3959
0
                                        NULL))
3960
0
                    return 0;
3961
0
                break;
3962
0
            }
3963
0
# endif
3964
0
            AES_set_encrypt_key(key, keylen, &octx->ksenc.ks);
3965
0
            AES_set_decrypt_key(key, keylen, &octx->ksdec.ks);
3966
0
            if (!CRYPTO_ocb128_init(&octx->ocb,
3967
0
                                    &octx->ksenc.ks, &octx->ksdec.ks,
3968
0
                                    (block128_f) AES_encrypt,
3969
0
                                    (block128_f) AES_decrypt,
3970
0
                                    NULL))
3971
0
                return 0;
3972
0
        }
3973
0
        while (0);
3974
3975
        /*
3976
         * If we have an iv we can set it directly, otherwise use saved IV.
3977
         */
3978
0
        if (iv == NULL && octx->iv_set)
3979
0
            iv = octx->iv;
3980
0
        if (iv) {
3981
0
            if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
3982
0
                != 1)
3983
0
                return 0;
3984
0
            octx->iv_set = 1;
3985
0
        }
3986
0
        octx->key_set = 1;
3987
0
    } else {
3988
        /* If key set use IV, otherwise copy */
3989
0
        if (octx->key_set)
3990
0
            CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
3991
0
        else
3992
0
            memcpy(octx->iv, iv, octx->ivlen);
3993
0
        octx->iv_set = 1;
3994
0
    }
3995
0
    return 1;
3996
0
}
3997
3998
static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3999
                          const unsigned char *in, size_t len)
4000
0
{
4001
0
    unsigned char *buf;
4002
0
    int *buf_len;
4003
0
    int written_len = 0;
4004
0
    size_t trailing_len;
4005
0
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
4006
4007
    /* If IV or Key not set then return error */
4008
0
    if (!octx->iv_set)
4009
0
        return -1;
4010
4011
0
    if (!octx->key_set)
4012
0
        return -1;
4013
4014
0
    if (in != NULL) {
4015
        /*
4016
         * Need to ensure we are only passing full blocks to low-level OCB
4017
         * routines. We do it here rather than in EVP_EncryptUpdate/
4018
         * EVP_DecryptUpdate because we need to pass full blocks of AAD too
4019
         * and those routines don't support that
4020
         */
4021
4022
        /* Are we dealing with AAD or normal data here? */
4023
0
        if (out == NULL) {
4024
0
            buf = octx->aad_buf;
4025
0
            buf_len = &(octx->aad_buf_len);
4026
0
        } else {
4027
0
            buf = octx->data_buf;
4028
0
            buf_len = &(octx->data_buf_len);
4029
4030
0
            if (ossl_is_partially_overlapping(out + *buf_len, in, len)) {
4031
0
                ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
4032
0
                return 0;
4033
0
            }
4034
0
        }
4035
4036
        /*
4037
         * If we've got a partially filled buffer from a previous call then
4038
         * use that data first
4039
         */
4040
0
        if (*buf_len > 0) {
4041
0
            unsigned int remaining;
4042
4043
0
            remaining = AES_BLOCK_SIZE - (*buf_len);
4044
0
            if (remaining > len) {
4045
0
                memcpy(buf + (*buf_len), in, len);
4046
0
                *(buf_len) += len;
4047
0
                return 0;
4048
0
            }
4049
0
            memcpy(buf + (*buf_len), in, remaining);
4050
4051
            /*
4052
             * If we get here we've filled the buffer, so process it
4053
             */
4054
0
            len -= remaining;
4055
0
            in += remaining;
4056
0
            if (out == NULL) {
4057
0
                if (!CRYPTO_ocb128_aad(&octx->ocb, buf, AES_BLOCK_SIZE))
4058
0
                    return -1;
4059
0
            } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
4060
0
                if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out,
4061
0
                                           AES_BLOCK_SIZE))
4062
0
                    return -1;
4063
0
            } else {
4064
0
                if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out,
4065
0
                                           AES_BLOCK_SIZE))
4066
0
                    return -1;
4067
0
            }
4068
0
            written_len = AES_BLOCK_SIZE;
4069
0
            *buf_len = 0;
4070
0
            if (out != NULL)
4071
0
                out += AES_BLOCK_SIZE;
4072
0
        }
4073
4074
        /* Do we have a partial block to handle at the end? */
4075
0
        trailing_len = len % AES_BLOCK_SIZE;
4076
4077
        /*
4078
         * If we've got some full blocks to handle, then process these first
4079
         */
4080
0
        if (len != trailing_len) {
4081
0
            if (out == NULL) {
4082
0
                if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))
4083
0
                    return -1;
4084
0
            } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
4085
0
                if (!CRYPTO_ocb128_encrypt
4086
0
                    (&octx->ocb, in, out, len - trailing_len))
4087
0
                    return -1;
4088
0
            } else {
4089
0
                if (!CRYPTO_ocb128_decrypt
4090
0
                    (&octx->ocb, in, out, len - trailing_len))
4091
0
                    return -1;
4092
0
            }
4093
0
            written_len += len - trailing_len;
4094
0
            in += len - trailing_len;
4095
0
        }
4096
4097
        /* Handle any trailing partial block */
4098
0
        if (trailing_len > 0) {
4099
0
            memcpy(buf, in, trailing_len);
4100
0
            *buf_len = trailing_len;
4101
0
        }
4102
4103
0
        return written_len;
4104
0
    } else {
4105
        /*
4106
         * First of all empty the buffer of any partial block that we might
4107
         * have been provided - both for data and AAD
4108
         */
4109
0
        if (octx->data_buf_len > 0) {
4110
0
            if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
4111
0
                if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
4112
0
                                           octx->data_buf_len))
4113
0
                    return -1;
4114
0
            } else {
4115
0
                if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
4116
0
                                           octx->data_buf_len))
4117
0
                    return -1;
4118
0
            }
4119
0
            written_len = octx->data_buf_len;
4120
0
            octx->data_buf_len = 0;
4121
0
        }
4122
0
        if (octx->aad_buf_len > 0) {
4123
0
            if (!CRYPTO_ocb128_aad
4124
0
                (&octx->ocb, octx->aad_buf, octx->aad_buf_len))
4125
0
                return -1;
4126
0
            octx->aad_buf_len = 0;
4127
0
        }
4128
        /* If decrypting then verify */
4129
0
        if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
4130
0
            if (octx->taglen < 0)
4131
0
                return -1;
4132
0
            if (CRYPTO_ocb128_finish(&octx->ocb,
4133
0
                                     octx->tag, octx->taglen) != 0)
4134
0
                return -1;
4135
0
            octx->iv_set = 0;
4136
0
            return written_len;
4137
0
        }
4138
        /* If encrypting then just get the tag */
4139
0
        if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
4140
0
            return -1;
4141
        /* Don't reuse the IV */
4142
0
        octx->iv_set = 0;
4143
0
        return written_len;
4144
0
    }
4145
0
}
4146
4147
static int aes_ocb_cleanup(EVP_CIPHER_CTX *c)
4148
0
{
4149
0
    EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
4150
0
    CRYPTO_ocb128_cleanup(&octx->ocb);
4151
0
    return 1;
4152
0
}
4153
4154
BLOCK_CIPHER_custom(NID_aes, 128, 16, 12, ocb, OCB,
4155
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
4156
BLOCK_CIPHER_custom(NID_aes, 192, 16, 12, ocb, OCB,
4157
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
4158
BLOCK_CIPHER_custom(NID_aes, 256, 16, 12, ocb, OCB,
4159
                    EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
4160
#endif                         /* OPENSSL_NO_OCB */