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