]>
Commit | Line | Data |
---|---|---|
7cc355c2 | 1 | /* |
a28d06f3 | 2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. |
7cc355c2 SL |
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 AES CBC CTS ciphers */ | |
11 | ||
2741128e | 12 | #include <openssl/proverr.h> |
7cc355c2 | 13 | #include "cipher_aes_cts.h" |
7cc355c2 | 14 | |
a054d15c SL |
15 | #define AES_CTS_FLAGS PROV_CIPHER_FLAG_CTS |
16 | ||
7cc355c2 SL |
17 | static OSSL_FUNC_cipher_get_ctx_params_fn aes_cbc_cts_get_ctx_params; |
18 | static OSSL_FUNC_cipher_set_ctx_params_fn aes_cbc_cts_set_ctx_params; | |
19 | static OSSL_FUNC_cipher_gettable_ctx_params_fn aes_cbc_cts_gettable_ctx_params; | |
20 | static OSSL_FUNC_cipher_settable_ctx_params_fn aes_cbc_cts_settable_ctx_params; | |
21 | ||
22 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(aes_cbc_cts) | |
23 | OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0), | |
24 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(aes_cbc_cts) | |
25 | ||
26 | static int aes_cbc_cts_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
27 | { | |
28 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
29 | OSSL_PARAM *p; | |
30 | ||
31 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS_MODE); | |
32 | if (p != NULL) { | |
5b60f9c3 | 33 | const char *name = ossl_aes_cbc_cts_mode_id2name(ctx->cts_mode); |
7cc355c2 SL |
34 | |
35 | if (name == NULL || !OSSL_PARAM_set_utf8_string(p, name)) { | |
36 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
37 | return 0; | |
38 | } | |
39 | } | |
592dcfd3 | 40 | return ossl_cipher_generic_get_ctx_params(vctx, params); |
7cc355c2 SL |
41 | } |
42 | ||
43 | CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(aes_cbc_cts) | |
44 | OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0), | |
45 | CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(aes_cbc_cts) | |
46 | ||
47 | static int aes_cbc_cts_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
48 | { | |
49 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
50 | const OSSL_PARAM *p; | |
51 | int id; | |
52 | ||
53 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_CTS_MODE); | |
54 | if (p != NULL) { | |
55 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
56 | goto err; | |
5b60f9c3 | 57 | id = ossl_aes_cbc_cts_mode_name2id(p->data); |
7cc355c2 SL |
58 | if (id < 0) |
59 | goto err; | |
60 | ||
61 | ctx->cts_mode = (unsigned int)id; | |
62 | } | |
592dcfd3 | 63 | return ossl_cipher_generic_set_ctx_params(vctx, params); |
7cc355c2 SL |
64 | err: |
65 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | /* NOTE: The underlying block cipher is AES CBC so we reuse most of the code */ | |
70 | #define IMPLEMENT_cts_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ | |
71 | blkbits, ivbits, typ) \ | |
72 | static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \ | |
73 | static int alg##_cts_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ | |
74 | { \ | |
592dcfd3 P |
75 | return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ |
76 | kbits, blkbits, ivbits); \ | |
7cc355c2 | 77 | } \ |
1be63951 | 78 | const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_cts_functions[] = { \ |
7cc355c2 SL |
79 | { OSSL_FUNC_CIPHER_NEWCTX, \ |
80 | (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ | |
81 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ | |
82 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ | |
592dcfd3 P |
83 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \ |
84 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \ | |
7cc355c2 | 85 | { OSSL_FUNC_CIPHER_UPDATE, \ |
592dcfd3 | 86 | (void (*)(void)) ossl_##alg##_##lcmode##_cts_block_update }, \ |
7cc355c2 | 87 | { OSSL_FUNC_CIPHER_FINAL, \ |
592dcfd3 P |
88 | (void (*)(void)) ossl_##alg##_##lcmode##_cts_block_final }, \ |
89 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ | |
7cc355c2 SL |
90 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
91 | (void (*)(void)) alg##_cts_##kbits##_##lcmode##_get_params }, \ | |
92 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | |
592dcfd3 | 93 | (void (*)(void))ossl_cipher_generic_gettable_params }, \ |
7cc355c2 SL |
94 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ |
95 | (void (*)(void))aes_cbc_cts_get_ctx_params }, \ | |
96 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
97 | (void (*)(void))aes_cbc_cts_set_ctx_params }, \ | |
98 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
99 | (void (*)(void))aes_cbc_cts_gettable_ctx_params }, \ | |
100 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
101 | (void (*)(void))aes_cbc_cts_settable_ctx_params }, \ | |
102 | { 0, NULL } \ | |
103 | }; | |
104 | ||
1be63951 | 105 | /* ossl_aes256cbc_cts_functions */ |
a054d15c | 106 | IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, AES_CTS_FLAGS, 256, 128, 128, block) |
1be63951 | 107 | /* ossl_aes192cbc_cts_functions */ |
a054d15c | 108 | IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, AES_CTS_FLAGS, 192, 128, 128, block) |
1be63951 | 109 | /* ossl_aes128cbc_cts_functions */ |
a054d15c | 110 | IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, AES_CTS_FLAGS, 128, 128, 128, block) |