Coverage Report

Created: 2024-05-21 06:33

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