xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/cipher_rc5.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /* Dispatch functions for RC5 cipher modes ecb, cbc, ofb, cfb */
11*b077aed3SPierre Pronchery 
12*b077aed3SPierre Pronchery /*
13*b077aed3SPierre Pronchery  * RC5 low level APIs are deprecated for public use, but still ok for internal
14*b077aed3SPierre Pronchery  * use.
15*b077aed3SPierre Pronchery  */
16*b077aed3SPierre Pronchery #include "internal/deprecated.h"
17*b077aed3SPierre Pronchery 
18*b077aed3SPierre Pronchery #include <openssl/proverr.h>
19*b077aed3SPierre Pronchery #include "cipher_rc5.h"
20*b077aed3SPierre Pronchery #include "prov/implementations.h"
21*b077aed3SPierre Pronchery #include "prov/providercommon.h"
22*b077aed3SPierre Pronchery 
23*b077aed3SPierre Pronchery #define RC5_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
24*b077aed3SPierre Pronchery 
25*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn rc5_einit;
26*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn rc5_dinit;
27*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
28*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
29*b077aed3SPierre Pronchery OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
30*b077aed3SPierre Pronchery OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
31*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn rc5_set_ctx_params;
32*b077aed3SPierre Pronchery 
rc5_freectx(void * vctx)33*b077aed3SPierre Pronchery static void rc5_freectx(void *vctx)
34*b077aed3SPierre Pronchery {
35*b077aed3SPierre Pronchery     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
36*b077aed3SPierre Pronchery 
37*b077aed3SPierre Pronchery     ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
38*b077aed3SPierre Pronchery     OPENSSL_clear_free(ctx,  sizeof(*ctx));
39*b077aed3SPierre Pronchery }
40*b077aed3SPierre Pronchery 
rc5_dupctx(void * ctx)41*b077aed3SPierre Pronchery static void *rc5_dupctx(void *ctx)
42*b077aed3SPierre Pronchery {
43*b077aed3SPierre Pronchery     PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
44*b077aed3SPierre Pronchery     PROV_RC5_CTX *ret;
45*b077aed3SPierre Pronchery 
46*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
47*b077aed3SPierre Pronchery         return NULL;
48*b077aed3SPierre Pronchery 
49*b077aed3SPierre Pronchery     ret = OPENSSL_malloc(sizeof(*ret));
50*b077aed3SPierre Pronchery     if (ret == NULL) {
51*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
52*b077aed3SPierre Pronchery         return NULL;
53*b077aed3SPierre Pronchery     }
54*b077aed3SPierre Pronchery     *ret = *in;
55*b077aed3SPierre Pronchery 
56*b077aed3SPierre Pronchery     return ret;
57*b077aed3SPierre Pronchery }
58*b077aed3SPierre Pronchery 
rc5_einit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])59*b077aed3SPierre Pronchery static int rc5_einit(void *ctx, const unsigned char *key, size_t keylen,
60*b077aed3SPierre Pronchery                           const unsigned char *iv, size_t ivlen,
61*b077aed3SPierre Pronchery                           const OSSL_PARAM params[])
62*b077aed3SPierre Pronchery {
63*b077aed3SPierre Pronchery     if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
64*b077aed3SPierre Pronchery         return 0;
65*b077aed3SPierre Pronchery     return rc5_set_ctx_params(ctx, params);
66*b077aed3SPierre Pronchery }
67*b077aed3SPierre Pronchery 
rc5_dinit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])68*b077aed3SPierre Pronchery static int rc5_dinit(void *ctx, const unsigned char *key, size_t keylen,
69*b077aed3SPierre Pronchery                           const unsigned char *iv, size_t ivlen,
70*b077aed3SPierre Pronchery                           const OSSL_PARAM params[])
71*b077aed3SPierre Pronchery {
72*b077aed3SPierre Pronchery     if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
73*b077aed3SPierre Pronchery         return 0;
74*b077aed3SPierre Pronchery     return rc5_set_ctx_params(ctx, params);
75*b077aed3SPierre Pronchery }
76*b077aed3SPierre Pronchery 
rc5_set_ctx_params(void * vctx,const OSSL_PARAM params[])77*b077aed3SPierre Pronchery static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
78*b077aed3SPierre Pronchery {
79*b077aed3SPierre Pronchery     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
80*b077aed3SPierre Pronchery     const OSSL_PARAM *p;
81*b077aed3SPierre Pronchery 
82*b077aed3SPierre Pronchery     if (params == NULL)
83*b077aed3SPierre Pronchery         return 1;
84*b077aed3SPierre Pronchery 
85*b077aed3SPierre Pronchery     if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
86*b077aed3SPierre Pronchery         return 0;
87*b077aed3SPierre Pronchery 
88*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
89*b077aed3SPierre Pronchery     if (p != NULL) {
90*b077aed3SPierre Pronchery         unsigned int rounds;
91*b077aed3SPierre Pronchery 
92*b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &rounds)) {
93*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
94*b077aed3SPierre Pronchery             return 0;
95*b077aed3SPierre Pronchery         }
96*b077aed3SPierre Pronchery         if (rounds != RC5_8_ROUNDS
97*b077aed3SPierre Pronchery             && rounds != RC5_12_ROUNDS
98*b077aed3SPierre Pronchery             && rounds != RC5_16_ROUNDS) {
99*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
100*b077aed3SPierre Pronchery             return 0;
101*b077aed3SPierre Pronchery         }
102*b077aed3SPierre Pronchery         ctx->rounds = rounds;
103*b077aed3SPierre Pronchery     }
104*b077aed3SPierre Pronchery     return 1;
105*b077aed3SPierre Pronchery }
106*b077aed3SPierre Pronchery 
107*b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS,NULL)108*b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
109*b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
110*b077aed3SPierre Pronchery 
111*b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
112*b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
113*b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
114*b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
115*b077aed3SPierre Pronchery 
116*b077aed3SPierre Pronchery 
117*b077aed3SPierre Pronchery static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
118*b077aed3SPierre Pronchery {
119*b077aed3SPierre Pronchery     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
120*b077aed3SPierre Pronchery     OSSL_PARAM *p;
121*b077aed3SPierre Pronchery 
122*b077aed3SPierre Pronchery     if (!ossl_cipher_generic_get_ctx_params(vctx, params))
123*b077aed3SPierre Pronchery         return 0;
124*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
125*b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
126*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
127*b077aed3SPierre Pronchery         return 0;
128*b077aed3SPierre Pronchery     }
129*b077aed3SPierre Pronchery     return 1;
130*b077aed3SPierre Pronchery }
131*b077aed3SPierre Pronchery 
132*b077aed3SPierre Pronchery #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,             \
133*b077aed3SPierre Pronchery                          blkbits, ivbits, typ)                                 \
134*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;   \
135*b077aed3SPierre Pronchery static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
136*b077aed3SPierre Pronchery {                                                                              \
137*b077aed3SPierre Pronchery     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
138*b077aed3SPierre Pronchery                                           flags, kbits, blkbits, ivbits);      \
139*b077aed3SPierre Pronchery }                                                                              \
140*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;           \
141*b077aed3SPierre Pronchery static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
142*b077aed3SPierre Pronchery {                                                                              \
143*b077aed3SPierre Pronchery      PROV_##UCALG##_CTX *ctx;                                                  \
144*b077aed3SPierre Pronchery      if (!ossl_prov_is_running())                                              \
145*b077aed3SPierre Pronchery         return NULL;                                                           \
146*b077aed3SPierre Pronchery      ctx = OPENSSL_zalloc(sizeof(*ctx));                                       \
147*b077aed3SPierre Pronchery      if (ctx != NULL) {                                                        \
148*b077aed3SPierre Pronchery          ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits,              \
149*b077aed3SPierre Pronchery                                      EVP_CIPH_##UCMODE##_MODE, flags,          \
150*b077aed3SPierre Pronchery                                      ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
151*b077aed3SPierre Pronchery                                 NULL);                                         \
152*b077aed3SPierre Pronchery          ctx->rounds = RC5_12_ROUNDS;                                          \
153*b077aed3SPierre Pronchery      }                                                                         \
154*b077aed3SPierre Pronchery      return ctx;                                                               \
155*b077aed3SPierre Pronchery }                                                                              \
156*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = {                \
157*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
158*b077aed3SPierre Pronchery       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
159*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
160*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
161*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc5_einit },              \
162*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc5_dinit },              \
163*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
164*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },  \
165*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },   \
166*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
167*b077aed3SPierre Pronchery       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
168*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
169*b077aed3SPierre Pronchery       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
170*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
171*b077aed3SPierre Pronchery       (void (*)(void))rc5_get_ctx_params },                                    \
172*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
173*b077aed3SPierre Pronchery       (void (*)(void))rc5_gettable_ctx_params },                               \
174*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
175*b077aed3SPierre Pronchery       (void (*)(void))rc5_set_ctx_params },                                    \
176*b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
177*b077aed3SPierre Pronchery      (void (*)(void))rc5_settable_ctx_params },                                \
178*b077aed3SPierre Pronchery     { 0, NULL }                                                                \
179*b077aed3SPierre Pronchery };
180*b077aed3SPierre Pronchery 
181*b077aed3SPierre Pronchery /* ossl_rc5128ecb_functions */
182*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc5, RC5, ecb, ECB, RC5_FLAGS, 128, 64, 0, block)
183*b077aed3SPierre Pronchery /* ossl_rc5128cbc_functions */
184*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc5, RC5, cbc, CBC, RC5_FLAGS, 128, 64, 64, block)
185*b077aed3SPierre Pronchery /* ossl_rc5128ofb64_functions */
186*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, RC5_FLAGS, 128, 8, 64, stream)
187*b077aed3SPierre Pronchery /* ossl_rc5128cfb64_functions */
188*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc5, RC5, cfb64,  CFB, RC5_FLAGS, 128, 8, 64, stream)
189