1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* Dispatch functions for CAMELLIA CBC CTS ciphers */
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include <openssl/proverr.h>
13e1051a39Sopenharmony_ci#include "cipher_cts.h"
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci#define CTS_FLAGS PROV_CIPHER_FLAG_CTS
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_encrypt_init_fn camellia_cbc_cts_einit;
18e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_decrypt_init_fn camellia_cbc_cts_dinit;
19e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_get_ctx_params_fn camellia_cbc_cts_get_ctx_params;
20e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_set_ctx_params_fn camellia_cbc_cts_set_ctx_params;
21e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_gettable_ctx_params_fn camellia_cbc_cts_gettable_ctx_params;
22e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_settable_ctx_params_fn camellia_cbc_cts_settable_ctx_params;
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ciCIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(camellia_cbc_cts)
25e1051a39Sopenharmony_ciOSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
26e1051a39Sopenharmony_ciCIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(camellia_cbc_cts)
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cistatic int camellia_cbc_cts_einit(void *ctx, const unsigned char *key, size_t keylen,
29e1051a39Sopenharmony_ci                             const unsigned char *iv, size_t ivlen,
30e1051a39Sopenharmony_ci                             const OSSL_PARAM params[])
31e1051a39Sopenharmony_ci{
32e1051a39Sopenharmony_ci    if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
33e1051a39Sopenharmony_ci        return 0;
34e1051a39Sopenharmony_ci    return camellia_cbc_cts_set_ctx_params(ctx, params);
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistatic int camellia_cbc_cts_dinit(void *ctx, const unsigned char *key, size_t keylen,
38e1051a39Sopenharmony_ci                             const unsigned char *iv, size_t ivlen,
39e1051a39Sopenharmony_ci                             const OSSL_PARAM params[])
40e1051a39Sopenharmony_ci{
41e1051a39Sopenharmony_ci    if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
42e1051a39Sopenharmony_ci        return 0;
43e1051a39Sopenharmony_ci    return camellia_cbc_cts_set_ctx_params(ctx, params);
44e1051a39Sopenharmony_ci}
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cistatic int camellia_cbc_cts_get_ctx_params(void *vctx, OSSL_PARAM params[])
47e1051a39Sopenharmony_ci{
48e1051a39Sopenharmony_ci    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
49e1051a39Sopenharmony_ci    OSSL_PARAM *p;
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS_MODE);
52e1051a39Sopenharmony_ci    if (p != NULL) {
53e1051a39Sopenharmony_ci        const char *name = ossl_cipher_cbc_cts_mode_id2name(ctx->cts_mode);
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci        if (name == NULL || !OSSL_PARAM_set_utf8_string(p, name)) {
56e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
57e1051a39Sopenharmony_ci            return 0;
58e1051a39Sopenharmony_ci        }
59e1051a39Sopenharmony_ci    }
60e1051a39Sopenharmony_ci    return ossl_cipher_generic_get_ctx_params(vctx, params);
61e1051a39Sopenharmony_ci}
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ciCIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(camellia_cbc_cts)
64e1051a39Sopenharmony_ciOSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
65e1051a39Sopenharmony_ciCIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(camellia_cbc_cts)
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_cistatic int camellia_cbc_cts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
68e1051a39Sopenharmony_ci{
69e1051a39Sopenharmony_ci    PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
70e1051a39Sopenharmony_ci    const OSSL_PARAM *p;
71e1051a39Sopenharmony_ci    int id;
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_CTS_MODE);
74e1051a39Sopenharmony_ci    if (p != NULL) {
75e1051a39Sopenharmony_ci        if (p->data_type != OSSL_PARAM_UTF8_STRING)
76e1051a39Sopenharmony_ci            goto err;
77e1051a39Sopenharmony_ci        id = ossl_cipher_cbc_cts_mode_name2id(p->data);
78e1051a39Sopenharmony_ci        if (id < 0)
79e1051a39Sopenharmony_ci            goto err;
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci        ctx->cts_mode = (unsigned int)id;
82e1051a39Sopenharmony_ci    }
83e1051a39Sopenharmony_ci    return ossl_cipher_generic_set_ctx_params(vctx, params);
84e1051a39Sopenharmony_cierr:
85e1051a39Sopenharmony_ci    ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
86e1051a39Sopenharmony_ci    return 0;
87e1051a39Sopenharmony_ci}
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci/* ossl_camellia256cbc_cts_functions */
90e1051a39Sopenharmony_ciIMPLEMENT_cts_cipher(camellia, CAMELLIA, cbc, CBC, CTS_FLAGS, 256, 128, 128, block)
91e1051a39Sopenharmony_ci/* ossl_camellia192cbc_cts_functions */
92e1051a39Sopenharmony_ciIMPLEMENT_cts_cipher(camellia, CAMELLIA, cbc, CBC, CTS_FLAGS, 192, 128, 128, block)
93e1051a39Sopenharmony_ci/* ossl_camellia128cbc_cts_functions */
94e1051a39Sopenharmony_ciIMPLEMENT_cts_cipher(camellia, CAMELLIA, cbc, CBC, CTS_FLAGS, 128, 128, 128, block)
95