Make the naming scheme for dispatched functions more consistent
[oweals/openssl.git] / providers / implementations / ciphers / cipher_rc5.c
1 /*
2  * Copyright 2019-2020 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 RC5 cipher modes ecb, cbc, ofb, cfb */
11
12 /*
13  * RC5 low level APIs are deprecated for public use, but still ok for internal
14  * use.
15  */
16 #include "internal/deprecated.h"
17
18 #include "cipher_rc5.h"
19 #include "prov/implementations.h"
20 #include "prov/providercommonerr.h"
21
22 static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
23 static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
24 OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
25 OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
26
27 static void rc5_freectx(void *vctx)
28 {
29     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
30
31     OPENSSL_clear_free(ctx,  sizeof(*ctx));
32 }
33
34 static void *rc5_dupctx(void *ctx)
35 {
36     PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
37     PROV_RC5_CTX *ret = OPENSSL_malloc(sizeof(*ret));
38
39     if (ret == NULL) {
40         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
41         return NULL;
42     }
43     *ret = *in;
44
45     return ret;
46 }
47
48 static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
49 {
50     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
51     const OSSL_PARAM *p;
52
53     if (!cipher_var_keylen_set_ctx_params(vctx, params))
54         return 0;
55
56     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
57     if (p != NULL) {
58         unsigned int rounds;
59
60         if (!OSSL_PARAM_get_uint(p, &rounds)) {
61             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
62             return 0;
63         }
64         if (rounds != RC5_8_ROUNDS
65             && rounds != RC5_12_ROUNDS
66             && rounds != RC5_16_ROUNDS) {
67             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
68             return 0;
69         }
70         ctx->rounds = rounds;
71     }
72     return 1;
73 }
74
75 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
76     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
77 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
78
79 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
80     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
81     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
82 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
83
84
85 static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
86 {
87     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
88     OSSL_PARAM *p;
89
90     if (!cipher_generic_get_ctx_params(vctx, params))
91         return 0;
92     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
93     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
94         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
95         return 0;
96     }
97     return 1;
98 }
99
100 #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,             \
101                          blkbits, ivbits, typ)                                 \
102 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;   \
103 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
104 {                                                                              \
105     return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags,  \
106                                      kbits, blkbits, ivbits);                  \
107 }                                                                              \
108 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;           \
109 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
110 {                                                                              \
111      PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));                   \
112      if (ctx != NULL) {                                                        \
113          cipher_generic_initkey(ctx, kbits, blkbits, ivbits,                   \
114                                 EVP_CIPH_##UCMODE##_MODE, flags,               \
115                                 PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
116          ctx->rounds = RC5_12_ROUNDS;                                          \
117      }                                                                         \
118      return ctx;                                                               \
119 }                                                                              \
120 const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = {                       \
121     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
122       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
123     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
124     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
125     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit },   \
126     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit },   \
127     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
128     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final },  \
129     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher },        \
130     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
131       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
132     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
133       (void (*)(void))cipher_generic_gettable_params },                        \
134     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
135       (void (*)(void))rc5_get_ctx_params },                                    \
136     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
137       (void (*)(void))rc5_gettable_ctx_params },                               \
138     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
139       (void (*)(void))rc5_set_ctx_params },                                    \
140     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
141      (void (*)(void))rc5_settable_ctx_params },                                \
142     { 0, NULL }                                                                \
143 };
144
145 /* rc5128ecb_functions */
146 IMPLEMENT_cipher(rc5, RC5, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
147 /* rc5128cbc_functions */
148 IMPLEMENT_cipher(rc5, RC5, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
149 /* rc5128ofb64_functions */
150 IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)
151 /* rc5128cfb64_functions */
152 IMPLEMENT_cipher(rc5, RC5, cfb64,  CFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)