Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/evp/e_aria.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include "internal/deprecated.h"
12
13
#include "internal/cryptlib.h"
14
#ifndef OPENSSL_NO_ARIA
15
# include <openssl/evp.h>
16
# include <openssl/modes.h>
17
# include <openssl/rand.h>
18
# include "crypto/aria.h"
19
# include "crypto/evp.h"
20
# include "crypto/modes.h"
21
# include "evp_local.h"
22
23
/* ARIA subkey Structure */
24
typedef struct {
25
    ARIA_KEY ks;
26
} EVP_ARIA_KEY;
27
28
/* ARIA GCM context */
29
typedef struct {
30
    union {
31
        OSSL_UNION_ALIGN;
32
        ARIA_KEY ks;
33
    } ks;                       /* ARIA subkey to use */
34
    int key_set;                /* Set if key initialised */
35
    int iv_set;                 /* Set if an iv is set */
36
    GCM128_CONTEXT gcm;
37
    unsigned char *iv;          /* Temporary IV store */
38
    int ivlen;                  /* IV length */
39
    int taglen;
40
    int iv_gen;                 /* It is OK to generate IVs */
41
    int tls_aad_len;            /* TLS AAD length */
42
} EVP_ARIA_GCM_CTX;
43
44
/* ARIA CCM context */
45
typedef struct {
46
    union {
47
        OSSL_UNION_ALIGN;
48
        ARIA_KEY ks;
49
    } ks;                       /* ARIA key schedule to use */
50
    int key_set;                /* Set if key initialised */
51
    int iv_set;                 /* Set if an iv is set */
52
    int tag_set;                /* Set if tag is valid */
53
    int len_set;                /* Set if message length set */
54
    int L, M;                   /* L and M parameters from RFC3610 */
55
    int tls_aad_len;            /* TLS AAD length */
56
    CCM128_CONTEXT ccm;
57
    ccm128_f str;
58
} EVP_ARIA_CCM_CTX;
59
60
/* The subkey for ARIA is generated. */
61
static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
62
                            const unsigned char *iv, int enc)
63
0
{
64
0
    int ret;
65
0
    int mode = EVP_CIPHER_CTX_get_mode(ctx);
66
67
0
    if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
68
0
        ret = ossl_aria_set_encrypt_key(key,
69
0
                                        EVP_CIPHER_CTX_get_key_length(ctx) * 8,
70
0
                                        EVP_CIPHER_CTX_get_cipher_data(ctx));
71
0
    else
72
0
        ret = ossl_aria_set_decrypt_key(key,
73
0
                                        EVP_CIPHER_CTX_get_key_length(ctx) * 8,
74
0
                                        EVP_CIPHER_CTX_get_cipher_data(ctx));
75
0
    if (ret < 0) {
76
0
        ERR_raise(ERR_LIB_EVP,EVP_R_ARIA_KEY_SETUP_FAILED);
77
0
        return 0;
78
0
    }
79
0
    return 1;
80
0
}
81
82
static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out,
83
                             size_t len, const ARIA_KEY *key,
84
                             unsigned char *ivec, const int enc)
85
0
{
86
87
0
    if (enc)
88
0
        CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
89
0
                              (block128_f) ossl_aria_encrypt);
90
0
    else
91
0
        CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
92
0
                              (block128_f) ossl_aria_encrypt);
93
0
}
94
95
static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out,
96
                                size_t length, const ARIA_KEY *key,
97
                                unsigned char *ivec, int *num, const int enc)
98
0
{
99
100
0
    CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
101
0
                          (block128_f) ossl_aria_encrypt);
102
0
}
103
104
static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out,
105
                              size_t length, const ARIA_KEY *key,
106
                              unsigned char *ivec, int *num, const int enc)
107
0
{
108
0
    CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc,
109
0
                            (block128_f) ossl_aria_encrypt);
110
0
}
111
112
static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out,
113
                              size_t length, const ARIA_KEY *key,
114
                              unsigned char *ivec, int *num, const int enc)
115
0
{
116
0
    CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc,
117
0
                            (block128_f) ossl_aria_encrypt);
118
0
}
119
120
static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out,
121
                             const ARIA_KEY *key, const int enc)
122
0
{
123
0
    ossl_aria_encrypt(in, out, key);
124
0
}
125
126
static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out,
127
                             size_t length, const ARIA_KEY *key,
128
                             unsigned char *ivec, int *num)
129
0
{
130
0
    CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
131
0
                         (block128_f) ossl_aria_encrypt);
132
0
}
133
134
IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY,
135
                        NID_aria_128, 16, 16, 16, 128,
136
                        0, aria_init_key, NULL,
137
                        EVP_CIPHER_set_asn1_iv,
138
                        EVP_CIPHER_get_asn1_iv,
139
                        NULL)
140
IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY,
141
                        NID_aria_192, 16, 24, 16, 128,
142
                        0, aria_init_key, NULL,
143
                        EVP_CIPHER_set_asn1_iv,
144
                        EVP_CIPHER_get_asn1_iv,
145
                        NULL)
146
IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY,
147
                        NID_aria_256, 16, 32, 16, 128,
148
                        0, aria_init_key, NULL,
149
                        EVP_CIPHER_set_asn1_iv,
150
                        EVP_CIPHER_get_asn1_iv,
151
                        NULL)
152
153
# define IMPLEMENT_ARIA_CFBR(ksize,cbits) \
154
                IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0)
155
IMPLEMENT_ARIA_CFBR(128,1)
156
IMPLEMENT_ARIA_CFBR(192,1)
157
IMPLEMENT_ARIA_CFBR(256,1)
158
IMPLEMENT_ARIA_CFBR(128,8)
159
IMPLEMENT_ARIA_CFBR(192,8)
160
IMPLEMENT_ARIA_CFBR(256,8)
161
162
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
163
static const EVP_CIPHER aria_##keylen##_##mode = { \
164
        nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
165
        flags|EVP_CIPH_##MODE##_MODE,   \
166
        EVP_ORIG_GLOBAL,                \
167
        aria_init_key,                  \
168
        aria_##mode##_cipher,           \
169
        NULL,                           \
170
        sizeof(EVP_ARIA_KEY),           \
171
        NULL,NULL,NULL,NULL };          \
172
213
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
173
213
{ return &aria_##keylen##_##mode; }
EVP_aria_128_ctr
Line
Count
Source
172
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
173
71
{ return &aria_##keylen##_##mode; }
EVP_aria_192_ctr
Line
Count
Source
172
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
173
71
{ return &aria_##keylen##_##mode; }
EVP_aria_256_ctr
Line
Count
Source
172
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
173
71
{ return &aria_##keylen##_##mode; }
174
175
static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
176
                               const unsigned char *in, size_t len)
177
0
{
178
0
    int n = EVP_CIPHER_CTX_get_num(ctx);
179
0
    unsigned int num;
180
0
    EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY, ctx);
181
182
0
    if (n < 0)
183
0
        return 0;
184
0
    num = (unsigned int)n;
185
186
0
    CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv,
187
0
                          EVP_CIPHER_CTX_buf_noconst(ctx), &num,
188
0
                          (block128_f) ossl_aria_encrypt);
189
0
    EVP_CIPHER_CTX_set_num(ctx, num);
190
0
    return 1;
191
0
}
192
193
BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0)
194
BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0)
195
BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0)
196
197
/* Authenticated cipher modes (GCM/CCM) */
198
199
/* increment counter (64-bit int) by 1 */
200
static void ctr64_inc(unsigned char *counter)
201
0
{
202
0
    int n = 8;
203
0
    unsigned char c;
204
205
0
    do {
206
0
        --n;
207
0
        c = counter[n];
208
0
        ++c;
209
0
        counter[n] = c;
210
0
        if (c)
211
0
            return;
212
0
    } while (n);
213
0
}
214
215
static int aria_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
216
                                 const unsigned char *iv, int enc)
217
0
{
218
0
    int ret;
219
0
    EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
220
221
0
    if (!iv && !key)
222
0
        return 1;
223
0
    if (key) {
224
0
        ret = ossl_aria_set_encrypt_key(key,
225
0
                                        EVP_CIPHER_CTX_get_key_length(ctx) * 8,
226
0
                                        &gctx->ks.ks);
227
0
        CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
228
0
                           (block128_f) ossl_aria_encrypt);
229
0
        if (ret < 0) {
230
0
            ERR_raise(ERR_LIB_EVP,EVP_R_ARIA_KEY_SETUP_FAILED);
231
0
            return 0;
232
0
        }
233
234
        /*
235
         * If we have an iv can set it directly, otherwise use saved IV.
236
         */
237
0
        if (iv == NULL && gctx->iv_set)
238
0
            iv = gctx->iv;
239
0
        if (iv) {
240
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
241
0
            gctx->iv_set = 1;
242
0
        }
243
0
        gctx->key_set = 1;
244
0
    } else {
245
        /* If key set use IV, otherwise copy */
246
0
        if (gctx->key_set)
247
0
            CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
248
0
        else
249
0
            memcpy(gctx->iv, iv, gctx->ivlen);
250
0
        gctx->iv_set = 1;
251
0
        gctx->iv_gen = 0;
252
0
    }
253
0
    return 1;
254
0
}
255
256
static int aria_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
257
0
{
258
0
    EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,c);
259
260
0
    switch (type) {
261
0
    case EVP_CTRL_INIT:
262
0
        gctx->key_set = 0;
263
0
        gctx->iv_set = 0;
264
0
        gctx->ivlen = EVP_CIPHER_get_iv_length(c->cipher);
265
0
        gctx->iv = c->iv;
266
0
        gctx->taglen = -1;
267
0
        gctx->iv_gen = 0;
268
0
        gctx->tls_aad_len = -1;
269
0
        return 1;
270
271
0
    case EVP_CTRL_GET_IVLEN:
272
0
        *(int *)ptr = gctx->ivlen;
273
0
        return 1;
274
275
0
    case EVP_CTRL_AEAD_SET_IVLEN:
276
0
        if (arg <= 0)
277
0
            return 0;
278
        /* Allocate memory for IV if needed */
279
0
        if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
280
0
            if (gctx->iv != c->iv)
281
0
                OPENSSL_free(gctx->iv);
282
0
            if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) {
283
0
                ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
284
0
                return 0;
285
0
            }
286
0
        }
287
0
        gctx->ivlen = arg;
288
0
        return 1;
289
290
0
    case EVP_CTRL_AEAD_SET_TAG:
291
0
        if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_is_encrypting(c))
292
0
            return 0;
293
0
        memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
294
0
        gctx->taglen = arg;
295
0
        return 1;
296
297
0
    case EVP_CTRL_AEAD_GET_TAG:
298
0
        if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_is_encrypting(c)
299
0
            || gctx->taglen < 0)
300
0
            return 0;
301
0
        memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg);
302
0
        return 1;
303
304
0
    case EVP_CTRL_GCM_SET_IV_FIXED:
305
        /* Special case: -1 length restores whole IV */
306
0
        if (arg == -1) {
307
0
            memcpy(gctx->iv, ptr, gctx->ivlen);
308
0
            gctx->iv_gen = 1;
309
0
            return 1;
310
0
        }
311
        /*
312
         * Fixed field must be at least 4 bytes and invocation field at least
313
         * 8.
314
         */
315
0
        if ((arg < 4) || (gctx->ivlen - arg) < 8)
316
0
            return 0;
317
0
        if (arg)
318
0
            memcpy(gctx->iv, ptr, arg);
319
0
        if (EVP_CIPHER_CTX_is_encrypting(c)
320
0
            && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
321
0
            return 0;
322
0
        gctx->iv_gen = 1;
323
0
        return 1;
324
325
0
    case EVP_CTRL_GCM_IV_GEN:
326
0
        if (gctx->iv_gen == 0 || gctx->key_set == 0)
327
0
            return 0;
328
0
        CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
329
0
        if (arg <= 0 || arg > gctx->ivlen)
330
0
            arg = gctx->ivlen;
331
0
        memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
332
        /*
333
         * Invocation field will be at least 8 bytes in size and so no need
334
         * to check wrap around or increment more than last 8 bytes.
335
         */
336
0
        ctr64_inc(gctx->iv + gctx->ivlen - 8);
337
0
        gctx->iv_set = 1;
338
0
        return 1;
339
340
0
    case EVP_CTRL_GCM_SET_IV_INV:
341
0
        if (gctx->iv_gen == 0 || gctx->key_set == 0
342
0
            || EVP_CIPHER_CTX_is_encrypting(c))
343
0
            return 0;
344
0
        memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
345
0
        CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
346
0
        gctx->iv_set = 1;
347
0
        return 1;
348
349
0
    case EVP_CTRL_AEAD_TLS1_AAD:
350
        /* Save the AAD for later use */
351
0
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
352
0
            return 0;
353
0
        memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
354
0
        gctx->tls_aad_len = arg;
355
0
        {
356
0
            unsigned int len =
357
0
                EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
358
0
                | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
359
            /* Correct length for explicit IV */
360
0
            if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
361
0
                return 0;
362
0
            len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
363
            /* If decrypting correct for tag too */
364
0
            if (!EVP_CIPHER_CTX_is_encrypting(c)) {
365
0
                if (len < EVP_GCM_TLS_TAG_LEN)
366
0
                    return 0;
367
0
                len -= EVP_GCM_TLS_TAG_LEN;
368
0
            }
369
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
370
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
371
0
        }
372
        /* Extra padding: tag appended to record */
373
0
        return EVP_GCM_TLS_TAG_LEN;
374
375
0
    case EVP_CTRL_COPY:
376
0
        {
377
0
            EVP_CIPHER_CTX *out = ptr;
378
0
            EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX,out);
379
0
            if (gctx->gcm.key) {
380
0
                if (gctx->gcm.key != &gctx->ks)
381
0
                    return 0;
382
0
                gctx_out->gcm.key = &gctx_out->ks;
383
0
            }
384
0
            if (gctx->iv == c->iv)
385
0
                gctx_out->iv = out->iv;
386
0
            else {
387
0
                if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) {
388
0
                    ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
389
0
                    return 0;
390
0
                }
391
0
                memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
392
0
            }
393
0
            return 1;
394
0
        }
395
396
0
    default:
397
0
        return -1;
398
399
0
    }
400
0
}
401
402
static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
403
                              const unsigned char *in, size_t len)
404
0
{
405
0
    EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
406
0
    int rv = -1;
407
408
    /* Encrypt/decrypt must be performed in place */
409
0
    if (out != in
410
0
        || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
411
0
        return -1;
412
    /*
413
     * Set IV from start of buffer or generate IV and write to start of
414
     * buffer.
415
     */
416
0
    if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_is_encrypting(ctx) ?
417
0
                            EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
418
0
                            EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
419
0
        goto err;
420
    /* Use saved AAD */
421
0
    if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
422
0
                          gctx->tls_aad_len))
423
0
        goto err;
424
    /* Fix buffer and length to point to payload */
425
0
    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
426
0
    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
427
0
    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
428
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
429
        /* Encrypt payload */
430
0
        if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
431
0
            goto err;
432
0
        out += len;
433
        /* Finally write tag */
434
0
        CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
435
0
        rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
436
0
    } else {
437
        /* Decrypt */
438
0
        if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
439
0
            goto err;
440
        /* Retrieve tag */
441
0
        CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
442
0
                          EVP_GCM_TLS_TAG_LEN);
443
        /* If tag mismatch wipe buffer */
444
0
        if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
445
0
                          EVP_GCM_TLS_TAG_LEN)) {
446
0
            OPENSSL_cleanse(out, len);
447
0
            goto err;
448
0
        }
449
0
        rv = len;
450
0
    }
451
452
0
 err:
453
0
    gctx->iv_set = 0;
454
0
    gctx->tls_aad_len = -1;
455
0
    return rv;
456
0
}
457
458
static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
459
                          const unsigned char *in, size_t len)
460
0
{
461
0
    EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
462
463
    /* If not set up, return error */
464
0
    if (!gctx->key_set)
465
0
        return -1;
466
467
0
    if (gctx->tls_aad_len >= 0)
468
0
        return aria_gcm_tls_cipher(ctx, out, in, len);
469
470
0
    if (!gctx->iv_set)
471
0
        return -1;
472
0
    if (in) {
473
0
        if (out == NULL) {
474
0
            if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
475
0
                return -1;
476
0
        } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
477
0
            if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
478
0
                return -1;
479
0
        } else {
480
0
            if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
481
0
                return -1;
482
0
        }
483
0
        return len;
484
0
    }
485
0
    if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
486
0
        if (gctx->taglen < 0)
487
0
            return -1;
488
0
        if (CRYPTO_gcm128_finish(&gctx->gcm,
489
0
                                 EVP_CIPHER_CTX_buf_noconst(ctx),
490
0
                                 gctx->taglen) != 0)
491
0
            return -1;
492
0
        gctx->iv_set = 0;
493
0
        return 0;
494
0
    }
495
0
    CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
496
0
    gctx->taglen = 16;
497
    /* Don't reuse the IV */
498
0
    gctx->iv_set = 0;
499
0
    return 0;
500
0
}
501
502
static int aria_gcm_cleanup(EVP_CIPHER_CTX *ctx)
503
0
{
504
0
    EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
505
506
0
    if (gctx->iv != ctx->iv)
507
0
        OPENSSL_free(gctx->iv);
508
509
0
    return 1;
510
0
}
511
512
static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
513
                            const unsigned char *iv, int enc)
514
0
{
515
0
    int ret;
516
0
    EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
517
518
0
    if (!iv && !key)
519
0
        return 1;
520
521
0
    if (key) {
522
0
        ret = ossl_aria_set_encrypt_key(key,
523
0
                                        EVP_CIPHER_CTX_get_key_length(ctx) * 8,
524
0
                                        &cctx->ks.ks);
525
0
        CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
526
0
                           &cctx->ks, (block128_f) ossl_aria_encrypt);
527
0
        if (ret < 0) {
528
0
            ERR_raise(ERR_LIB_EVP,EVP_R_ARIA_KEY_SETUP_FAILED);
529
0
            return 0;
530
0
        }
531
0
        cctx->str = NULL;
532
0
        cctx->key_set = 1;
533
0
    }
534
0
    if (iv) {
535
0
        memcpy(ctx->iv, iv, 15 - cctx->L);
536
0
        cctx->iv_set = 1;
537
0
    }
538
0
    return 1;
539
0
}
540
541
static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
542
0
{
543
0
    EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,c);
544
545
0
    switch (type) {
546
0
    case EVP_CTRL_INIT:
547
0
        cctx->key_set = 0;
548
0
        cctx->iv_set = 0;
549
0
        cctx->L = 8;
550
0
        cctx->M = 12;
551
0
        cctx->tag_set = 0;
552
0
        cctx->len_set = 0;
553
0
        cctx->tls_aad_len = -1;
554
0
        return 1;
555
556
0
    case EVP_CTRL_GET_IVLEN:
557
0
        *(int *)ptr = 15 - cctx->L;
558
0
        return 1;
559
560
0
    case EVP_CTRL_AEAD_TLS1_AAD:
561
        /* Save the AAD for later use */
562
0
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
563
0
            return 0;
564
0
        memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
565
0
        cctx->tls_aad_len = arg;
566
0
        {
567
0
            uint16_t len =
568
0
                EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
569
0
                | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
570
            /* Correct length for explicit IV */
571
0
            if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
572
0
                return 0;
573
0
            len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
574
            /* If decrypting correct for tag too */
575
0
            if (!EVP_CIPHER_CTX_is_encrypting(c)) {
576
0
                if (len < cctx->M)
577
0
                    return 0;
578
0
                len -= cctx->M;
579
0
            }
580
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
581
0
            EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
582
0
        }
583
        /* Extra padding: tag appended to record */
584
0
        return cctx->M;
585
586
0
    case EVP_CTRL_CCM_SET_IV_FIXED:
587
        /* Sanity check length */
588
0
        if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
589
0
            return 0;
590
        /* Just copy to first part of IV */
591
0
        memcpy(c->iv, ptr, arg);
592
0
        return 1;
593
594
0
    case EVP_CTRL_AEAD_SET_IVLEN:
595
0
        arg = 15 - arg;
596
        /* fall through */
597
0
    case EVP_CTRL_CCM_SET_L:
598
0
        if (arg < 2 || arg > 8)
599
0
            return 0;
600
0
        cctx->L = arg;
601
0
        return 1;
602
0
    case EVP_CTRL_AEAD_SET_TAG:
603
0
        if ((arg & 1) || arg < 4 || arg > 16)
604
0
            return 0;
605
0
        if (EVP_CIPHER_CTX_is_encrypting(c) && ptr)
606
0
            return 0;
607
0
        if (ptr) {
608
0
            cctx->tag_set = 1;
609
0
            memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
610
0
        }
611
0
        cctx->M = arg;
612
0
        return 1;
613
614
0
    case EVP_CTRL_AEAD_GET_TAG:
615
0
        if (!EVP_CIPHER_CTX_is_encrypting(c) || !cctx->tag_set)
616
0
            return 0;
617
0
        if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
618
0
            return 0;
619
0
        cctx->tag_set = 0;
620
0
        cctx->iv_set = 0;
621
0
        cctx->len_set = 0;
622
0
        return 1;
623
624
0
    case EVP_CTRL_COPY:
625
0
        {
626
0
            EVP_CIPHER_CTX *out = ptr;
627
0
            EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX,out);
628
0
            if (cctx->ccm.key) {
629
0
                if (cctx->ccm.key != &cctx->ks)
630
0
                    return 0;
631
0
                cctx_out->ccm.key = &cctx_out->ks;
632
0
            }
633
0
            return 1;
634
0
        }
635
636
0
    default:
637
0
        return -1;
638
0
    }
639
0
}
640
641
static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
642
                              const unsigned char *in, size_t len)
643
0
{
644
0
    EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
645
0
    CCM128_CONTEXT *ccm = &cctx->ccm;
646
647
    /* Encrypt/decrypt must be performed in place */
648
0
    if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
649
0
        return -1;
650
    /* If encrypting set explicit IV from sequence number (start of AAD) */
651
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx))
652
0
        memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
653
0
               EVP_CCM_TLS_EXPLICIT_IV_LEN);
654
    /* Get rest of IV from explicit IV */
655
0
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in,
656
0
           EVP_CCM_TLS_EXPLICIT_IV_LEN);
657
    /* Correct length value */
658
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
659
0
    if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,
660
0
                            len))
661
0
            return -1;
662
    /* Use saved AAD */
663
0
    CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx),
664
0
                      cctx->tls_aad_len);
665
    /* Fix buffer to point to payload */
666
0
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
667
0
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
668
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
669
0
        if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
670
0
                      : CRYPTO_ccm128_encrypt(ccm, in, out, len))
671
0
            return -1;
672
0
        if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
673
0
            return -1;
674
0
        return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
675
0
    } else {
676
0
        if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str)
677
0
                      : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
678
0
            unsigned char tag[16];
679
0
            if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
680
0
                if (!CRYPTO_memcmp(tag, in + len, cctx->M))
681
0
                    return len;
682
0
            }
683
0
        }
684
0
        OPENSSL_cleanse(out, len);
685
0
        return -1;
686
0
    }
687
0
}
688
689
static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
690
                          const unsigned char *in, size_t len)
691
0
{
692
0
    EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
693
0
    CCM128_CONTEXT *ccm = &cctx->ccm;
694
695
    /* If not set up, return error */
696
0
    if (!cctx->key_set)
697
0
        return -1;
698
699
0
    if (cctx->tls_aad_len >= 0)
700
0
        return aria_ccm_tls_cipher(ctx, out, in, len);
701
702
    /* EVP_*Final() doesn't return any data */
703
0
    if (in == NULL && out != NULL)
704
0
        return 0;
705
706
0
    if (!cctx->iv_set)
707
0
        return -1;
708
709
0
    if (!out) {
710
0
        if (!in) {
711
0
            if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
712
0
                return -1;
713
0
            cctx->len_set = 1;
714
0
            return len;
715
0
        }
716
        /* If have AAD need message length */
717
0
        if (!cctx->len_set && len)
718
0
            return -1;
719
0
        CRYPTO_ccm128_aad(ccm, in, len);
720
0
        return len;
721
0
    }
722
723
    /* The tag must be set before actually decrypting data */
724
0
    if (!EVP_CIPHER_CTX_is_encrypting(ctx) && !cctx->tag_set)
725
0
        return -1;
726
727
    /* If not set length yet do it */
728
0
    if (!cctx->len_set) {
729
0
        if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
730
0
            return -1;
731
0
        cctx->len_set = 1;
732
0
    }
733
0
    if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
734
0
        if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
735
0
                      : CRYPTO_ccm128_encrypt(ccm, in, out, len))
736
0
            return -1;
737
0
        cctx->tag_set = 1;
738
0
        return len;
739
0
    } else {
740
0
        int rv = -1;
741
0
        if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
742
0
                                                     cctx->str) :
743
0
            !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
744
0
            unsigned char tag[16];
745
0
            if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
746
0
                if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
747
0
                                   cctx->M))
748
0
                    rv = len;
749
0
            }
750
0
        }
751
0
        if (rv == -1)
752
0
            OPENSSL_cleanse(out, len);
753
0
        cctx->iv_set = 0;
754
0
        cctx->tag_set = 0;
755
0
        cctx->len_set = 0;
756
0
        return rv;
757
0
    }
758
0
}
759
760
#define aria_ccm_cleanup    NULL
761
762
#define ARIA_AUTH_FLAGS  (EVP_CIPH_FLAG_DEFAULT_ASN1 \
763
                          | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
764
                          | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
765
                          | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER \
766
                          | EVP_CIPH_CUSTOM_IV_LENGTH)
767
768
#define BLOCK_CIPHER_aead(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
769
static const EVP_CIPHER aria_##keylen##_##mode = { \
770
        nid##_##keylen##_##nmode,                  \
771
        blocksize, keylen/8, ivlen,                \
772
        ARIA_AUTH_FLAGS|EVP_CIPH_##MODE##_MODE,    \
773
        EVP_ORIG_GLOBAL,                           \
774
        aria_##mode##_init_key,                    \
775
        aria_##mode##_cipher,                      \
776
        aria_##mode##_cleanup,                     \
777
        sizeof(EVP_ARIA_##MODE##_CTX),             \
778
        NULL,NULL,aria_##mode##_ctrl,NULL };       \
779
426
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
426
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_128_gcm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_192_gcm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_256_gcm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_128_ccm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_192_ccm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
EVP_aria_256_ccm
Line
Count
Source
779
71
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
780
71
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
781
782
BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, gcm, gcm, GCM, 0)
783
BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, gcm, gcm, GCM, 0)
784
BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, gcm, gcm, GCM, 0)
785
786
BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, ccm, ccm, CCM, 0)
787
BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, ccm, ccm, CCM, 0)
788
BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, ccm, ccm, CCM, 0)
789
790
#endif