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