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