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 RC2 cipher modes ecb, cbc, ofb, cfb */
11*b077aed3SPierre Pronchery
12*b077aed3SPierre Pronchery /*
13*b077aed3SPierre Pronchery * RC2 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_rc2.h"
20*b077aed3SPierre Pronchery #include "prov/implementations.h"
21*b077aed3SPierre Pronchery #include "prov/providercommon.h"
22*b077aed3SPierre Pronchery
23*b077aed3SPierre Pronchery #define RC2_40_MAGIC 0xa0
24*b077aed3SPierre Pronchery #define RC2_64_MAGIC 0x78
25*b077aed3SPierre Pronchery #define RC2_128_MAGIC 0x3a
26*b077aed3SPierre Pronchery #define RC2_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
27*b077aed3SPierre Pronchery
28*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn rc2_einit;
29*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn rc2_dinit;
30*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn rc2_freectx;
31*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_dupctx_fn rc2_dupctx;
32*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_gettable_ctx_params_fn rc2_gettable_ctx_params;
33*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_settable_ctx_params_fn rc2_settable_ctx_params;
34*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn rc2_set_ctx_params;
35*b077aed3SPierre Pronchery
rc2_freectx(void * vctx)36*b077aed3SPierre Pronchery static void rc2_freectx(void *vctx)
37*b077aed3SPierre Pronchery {
38*b077aed3SPierre Pronchery PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
39*b077aed3SPierre Pronchery
40*b077aed3SPierre Pronchery ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
41*b077aed3SPierre Pronchery OPENSSL_clear_free(ctx, sizeof(*ctx));
42*b077aed3SPierre Pronchery }
43*b077aed3SPierre Pronchery
rc2_dupctx(void * ctx)44*b077aed3SPierre Pronchery static void *rc2_dupctx(void *ctx)
45*b077aed3SPierre Pronchery {
46*b077aed3SPierre Pronchery PROV_RC2_CTX *in = (PROV_RC2_CTX *)ctx;
47*b077aed3SPierre Pronchery PROV_RC2_CTX *ret;
48*b077aed3SPierre Pronchery
49*b077aed3SPierre Pronchery if (!ossl_prov_is_running())
50*b077aed3SPierre Pronchery return NULL;
51*b077aed3SPierre Pronchery
52*b077aed3SPierre Pronchery ret = OPENSSL_malloc(sizeof(*ret));
53*b077aed3SPierre Pronchery if (ret == NULL) {
54*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
55*b077aed3SPierre Pronchery return NULL;
56*b077aed3SPierre Pronchery }
57*b077aed3SPierre Pronchery *ret = *in;
58*b077aed3SPierre Pronchery
59*b077aed3SPierre Pronchery return ret;
60*b077aed3SPierre Pronchery }
61*b077aed3SPierre Pronchery
rc2_keybits_to_magic(int keybits)62*b077aed3SPierre Pronchery static int rc2_keybits_to_magic(int keybits)
63*b077aed3SPierre Pronchery {
64*b077aed3SPierre Pronchery switch (keybits) {
65*b077aed3SPierre Pronchery case 128:
66*b077aed3SPierre Pronchery return RC2_128_MAGIC;
67*b077aed3SPierre Pronchery case 64:
68*b077aed3SPierre Pronchery return RC2_64_MAGIC;
69*b077aed3SPierre Pronchery case 40:
70*b077aed3SPierre Pronchery return RC2_40_MAGIC;
71*b077aed3SPierre Pronchery }
72*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
73*b077aed3SPierre Pronchery return 0;
74*b077aed3SPierre Pronchery }
75*b077aed3SPierre Pronchery
rc2_magic_to_keybits(int magic)76*b077aed3SPierre Pronchery static int rc2_magic_to_keybits(int magic)
77*b077aed3SPierre Pronchery {
78*b077aed3SPierre Pronchery switch (magic) {
79*b077aed3SPierre Pronchery case RC2_128_MAGIC:
80*b077aed3SPierre Pronchery return 128;
81*b077aed3SPierre Pronchery case RC2_64_MAGIC:
82*b077aed3SPierre Pronchery return 64;
83*b077aed3SPierre Pronchery case RC2_40_MAGIC:
84*b077aed3SPierre Pronchery return 40;
85*b077aed3SPierre Pronchery }
86*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
87*b077aed3SPierre Pronchery return 0;
88*b077aed3SPierre Pronchery }
89*b077aed3SPierre Pronchery
rc2_einit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])90*b077aed3SPierre Pronchery static int rc2_einit(void *ctx, const unsigned char *key, size_t keylen,
91*b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
92*b077aed3SPierre Pronchery const OSSL_PARAM params[])
93*b077aed3SPierre Pronchery {
94*b077aed3SPierre Pronchery if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
95*b077aed3SPierre Pronchery return 0;
96*b077aed3SPierre Pronchery return rc2_set_ctx_params(ctx, params);
97*b077aed3SPierre Pronchery }
98*b077aed3SPierre Pronchery
rc2_dinit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])99*b077aed3SPierre Pronchery static int rc2_dinit(void *ctx, const unsigned char *key, size_t keylen,
100*b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
101*b077aed3SPierre Pronchery const OSSL_PARAM params[])
102*b077aed3SPierre Pronchery {
103*b077aed3SPierre Pronchery if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
104*b077aed3SPierre Pronchery return 0;
105*b077aed3SPierre Pronchery return rc2_set_ctx_params(ctx, params);
106*b077aed3SPierre Pronchery }
107*b077aed3SPierre Pronchery
rc2_get_ctx_params(void * vctx,OSSL_PARAM params[])108*b077aed3SPierre Pronchery static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[])
109*b077aed3SPierre Pronchery {
110*b077aed3SPierre Pronchery PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
111*b077aed3SPierre Pronchery OSSL_PARAM *p;
112*b077aed3SPierre Pronchery
113*b077aed3SPierre Pronchery if (!ossl_cipher_generic_get_ctx_params(vctx, params))
114*b077aed3SPierre Pronchery return 0;
115*b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
116*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->key_bits)) {
117*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
118*b077aed3SPierre Pronchery return 0;
119*b077aed3SPierre Pronchery }
120*b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS);
121*b077aed3SPierre Pronchery if (p != NULL) {
122*b077aed3SPierre Pronchery long num;
123*b077aed3SPierre Pronchery int i;
124*b077aed3SPierre Pronchery ASN1_TYPE *type;
125*b077aed3SPierre Pronchery unsigned char *d = p->data;
126*b077aed3SPierre Pronchery unsigned char **dd = d == NULL ? NULL : &d;
127*b077aed3SPierre Pronchery
128*b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
129*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
130*b077aed3SPierre Pronchery return 0;
131*b077aed3SPierre Pronchery }
132*b077aed3SPierre Pronchery if ((type = ASN1_TYPE_new()) == NULL) {
133*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
134*b077aed3SPierre Pronchery return 0;
135*b077aed3SPierre Pronchery }
136*b077aed3SPierre Pronchery
137*b077aed3SPierre Pronchery /* Is this the original IV or the running IV? */
138*b077aed3SPierre Pronchery num = rc2_keybits_to_magic(ctx->key_bits);
139*b077aed3SPierre Pronchery if (!ASN1_TYPE_set_int_octetstring(type, num,
140*b077aed3SPierre Pronchery ctx->base.iv, ctx->base.ivlen)) {
141*b077aed3SPierre Pronchery ASN1_TYPE_free(type);
142*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
143*b077aed3SPierre Pronchery return 0;
144*b077aed3SPierre Pronchery }
145*b077aed3SPierre Pronchery /*
146*b077aed3SPierre Pronchery * IF the caller has a buffer, we pray to the gods they got the
147*b077aed3SPierre Pronchery * size right. There's no way to tell the i2d functions...
148*b077aed3SPierre Pronchery */
149*b077aed3SPierre Pronchery i = i2d_ASN1_TYPE(type, dd);
150*b077aed3SPierre Pronchery if (i >= 0)
151*b077aed3SPierre Pronchery p->return_size = (size_t)i;
152*b077aed3SPierre Pronchery
153*b077aed3SPierre Pronchery ASN1_TYPE_free(type);
154*b077aed3SPierre Pronchery if (i < 0) {
155*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
156*b077aed3SPierre Pronchery return 0;
157*b077aed3SPierre Pronchery }
158*b077aed3SPierre Pronchery }
159*b077aed3SPierre Pronchery return 1;
160*b077aed3SPierre Pronchery }
161*b077aed3SPierre Pronchery
rc2_set_ctx_params(void * vctx,const OSSL_PARAM params[])162*b077aed3SPierre Pronchery static int rc2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
163*b077aed3SPierre Pronchery {
164*b077aed3SPierre Pronchery PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
165*b077aed3SPierre Pronchery const OSSL_PARAM *p;
166*b077aed3SPierre Pronchery
167*b077aed3SPierre Pronchery if (params == NULL)
168*b077aed3SPierre Pronchery return 1;
169*b077aed3SPierre Pronchery
170*b077aed3SPierre Pronchery if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
171*b077aed3SPierre Pronchery return 0;
172*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
173*b077aed3SPierre Pronchery if (p != NULL) {
174*b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &ctx->key_bits)) {
175*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
176*b077aed3SPierre Pronchery return 0;
177*b077aed3SPierre Pronchery }
178*b077aed3SPierre Pronchery }
179*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS);
180*b077aed3SPierre Pronchery if (p != NULL) {
181*b077aed3SPierre Pronchery ASN1_TYPE *type = NULL;
182*b077aed3SPierre Pronchery long num = 0;
183*b077aed3SPierre Pronchery const unsigned char *d = p->data;
184*b077aed3SPierre Pronchery int ret = 1;
185*b077aed3SPierre Pronchery unsigned char iv[16];
186*b077aed3SPierre Pronchery
187*b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING
188*b077aed3SPierre Pronchery || ctx->base.ivlen > sizeof(iv)
189*b077aed3SPierre Pronchery || (type = d2i_ASN1_TYPE(NULL, &d, p->data_size)) == NULL
190*b077aed3SPierre Pronchery || ((size_t)ASN1_TYPE_get_int_octetstring(type, &num, iv,
191*b077aed3SPierre Pronchery ctx->base.ivlen)
192*b077aed3SPierre Pronchery != ctx->base.ivlen)
193*b077aed3SPierre Pronchery || !ossl_cipher_generic_initiv(&ctx->base, iv, ctx->base.ivlen)
194*b077aed3SPierre Pronchery || (ctx->key_bits = rc2_magic_to_keybits(num)) == 0) {
195*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
196*b077aed3SPierre Pronchery ret = 0;
197*b077aed3SPierre Pronchery }
198*b077aed3SPierre Pronchery ASN1_TYPE_free(type);
199*b077aed3SPierre Pronchery if (ret == 0)
200*b077aed3SPierre Pronchery return 0;
201*b077aed3SPierre Pronchery /*
202*b077aed3SPierre Pronchery * This code assumes that the caller will call
203*b077aed3SPierre Pronchery * EVP_CipherInit_ex() with a non NULL key in order to setup a key that
204*b077aed3SPierre Pronchery * uses the keylen and keybits that were set here.
205*b077aed3SPierre Pronchery */
206*b077aed3SPierre Pronchery ctx->base.keylen = ctx->key_bits / 8;
207*b077aed3SPierre Pronchery }
208*b077aed3SPierre Pronchery return 1;
209*b077aed3SPierre Pronchery }
210*b077aed3SPierre Pronchery
211*b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc2)
212*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
213*b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS, NULL, 0),
214*b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc2)
215*b077aed3SPierre Pronchery
216*b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc2)
217*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
218*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
219*b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS, NULL, 0),
220*b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc2)
221*b077aed3SPierre Pronchery
222*b077aed3SPierre Pronchery #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, blkbits, \
223*b077aed3SPierre Pronchery ivbits, typ) \
224*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
225*b077aed3SPierre Pronchery static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
226*b077aed3SPierre Pronchery { \
227*b077aed3SPierre Pronchery return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
228*b077aed3SPierre Pronchery flags, kbits, blkbits, ivbits); \
229*b077aed3SPierre Pronchery } \
230*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
231*b077aed3SPierre Pronchery static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
232*b077aed3SPierre Pronchery { \
233*b077aed3SPierre Pronchery PROV_##UCALG##_CTX *ctx; \
234*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) \
235*b077aed3SPierre Pronchery return NULL; \
236*b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(*ctx)); \
237*b077aed3SPierre Pronchery if (ctx != NULL) { \
238*b077aed3SPierre Pronchery ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
239*b077aed3SPierre Pronchery EVP_CIPH_##UCMODE##_MODE, flags, \
240*b077aed3SPierre Pronchery ossl_prov_cipher_hw_##alg##_##lcmode(kbits), \
241*b077aed3SPierre Pronchery NULL); \
242*b077aed3SPierre Pronchery ctx->key_bits = kbits; \
243*b077aed3SPierre Pronchery } \
244*b077aed3SPierre Pronchery return ctx; \
245*b077aed3SPierre Pronchery } \
246*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
247*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_NEWCTX, \
248*b077aed3SPierre Pronchery (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
249*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
250*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
251*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc2_einit }, \
252*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc2_dinit }, \
253*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
254*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
255*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
256*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_PARAMS, \
257*b077aed3SPierre Pronchery (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
258*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
259*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_generic_gettable_params }, \
260*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
261*b077aed3SPierre Pronchery (void (*)(void))rc2_get_ctx_params }, \
262*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
263*b077aed3SPierre Pronchery (void (*)(void))rc2_gettable_ctx_params }, \
264*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
265*b077aed3SPierre Pronchery (void (*)(void))rc2_set_ctx_params }, \
266*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
267*b077aed3SPierre Pronchery (void (*)(void))rc2_settable_ctx_params }, \
268*b077aed3SPierre Pronchery { 0, NULL } \
269*b077aed3SPierre Pronchery };
270*b077aed3SPierre Pronchery
271*b077aed3SPierre Pronchery /* ossl_rc2128ecb_functions */
272*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, ecb, ECB, RC2_FLAGS, 128, 64, 0, block)
273*b077aed3SPierre Pronchery /* ossl_rc2128cbc_functions */
274*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 128, 64, 64, block)
275*b077aed3SPierre Pronchery /* ossl_rc240cbc_functions */
276*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 40, 64, 64, block)
277*b077aed3SPierre Pronchery /* ossl_rc264cbc_functions */
278*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 64, 64, 64, block)
279*b077aed3SPierre Pronchery
280*b077aed3SPierre Pronchery /* ossl_rc2128ofb128_functions */
281*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, ofb128, OFB, RC2_FLAGS, 128, 8, 64, stream)
282*b077aed3SPierre Pronchery /* ossl_rc2128cfb128_functions */
283*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc2, RC2, cfb128, CFB, RC2_FLAGS, 128, 8, 64, stream)
284