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