Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/e_rc2.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
/*
11
 * RC2 low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
19
#ifndef OPENSSL_NO_RC2
20
21
#include <openssl/evp.h>
22
#include <openssl/objects.h>
23
#include "crypto/evp.h"
24
#include <openssl/rc2.h>
25
#include "evp_local.h"
26
27
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28
    const unsigned char *iv, int enc);
29
static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
30
static int rc2_magic_to_meth(int i);
31
static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
32
static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
33
static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
34
35
typedef struct {
36
    int key_bits; /* effective key bits */
37
    RC2_KEY ks; /* key schedule */
38
} EVP_RC2_KEY;
39
40
0
#define data(ctx) EVP_C_DATA(EVP_RC2_KEY, ctx)
41
42
IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
43
    8,
44
    RC2_KEY_LENGTH, 8, 64,
45
    EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
46
    rc2_init_key, NULL,
47
    rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
48
    rc2_ctrl)
49
0
#define RC2_40_MAGIC 0xa0
50
0
#define RC2_64_MAGIC 0x78
51
0
#define RC2_128_MAGIC 0x3a
52
static const EVP_CIPHER r2_64_cbc_cipher = {
53
    NID_rc2_64_cbc,
54
    8, 8 /* 64 bit */, 8,
55
    EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
56
    EVP_ORIG_GLOBAL,
57
    rc2_init_key,
58
    rc2_cbc_cipher,
59
    NULL,
60
    sizeof(EVP_RC2_KEY),
61
    rc2_set_asn1_type_and_iv,
62
    rc2_get_asn1_type_and_iv,
63
    rc2_ctrl,
64
    NULL
65
};
66
67
static const EVP_CIPHER r2_40_cbc_cipher = {
68
    NID_rc2_40_cbc,
69
    8, 5 /* 40 bit */, 8,
70
    EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
71
    EVP_ORIG_GLOBAL,
72
    rc2_init_key,
73
    rc2_cbc_cipher,
74
    NULL,
75
    sizeof(EVP_RC2_KEY),
76
    rc2_set_asn1_type_and_iv,
77
    rc2_get_asn1_type_and_iv,
78
    rc2_ctrl,
79
    NULL
80
};
81
82
const EVP_CIPHER *EVP_rc2_64_cbc(void)
83
3
{
84
3
    return &r2_64_cbc_cipher;
85
3
}
86
87
const EVP_CIPHER *EVP_rc2_40_cbc(void)
88
3
{
89
3
    return &r2_40_cbc_cipher;
90
3
}
91
92
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
93
    const unsigned char *iv, int enc)
94
0
{
95
0
    RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_get_key_length(ctx),
96
0
        key, data(ctx)->key_bits);
97
0
    return 1;
98
0
}
99
100
static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
101
0
{
102
0
    int i;
103
104
0
    if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
105
0
        return 0;
106
0
    if (i == 128)
107
0
        return RC2_128_MAGIC;
108
0
    else if (i == 64)
109
0
        return RC2_64_MAGIC;
110
0
    else if (i == 40)
111
0
        return RC2_40_MAGIC;
112
0
    else
113
0
        return 0;
114
0
}
115
116
static int rc2_magic_to_meth(int i)
117
0
{
118
0
    if (i == RC2_128_MAGIC)
119
0
        return 128;
120
0
    else if (i == RC2_64_MAGIC)
121
0
        return 64;
122
0
    else if (i == RC2_40_MAGIC)
123
0
        return 40;
124
0
    else {
125
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_SIZE);
126
0
        return 0;
127
0
    }
128
0
}
129
130
static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
131
0
{
132
0
    long num = 0;
133
0
    int i = 0;
134
0
    int key_bits;
135
0
    unsigned int l;
136
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
137
138
0
    if (type != NULL) {
139
0
        l = EVP_CIPHER_CTX_get_iv_length(c);
140
0
        OPENSSL_assert(l <= sizeof(iv));
141
0
        i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
142
0
        if (i != (int)l)
143
0
            return -1;
144
0
        key_bits = rc2_magic_to_meth((int)num);
145
0
        if (!key_bits)
146
0
            return -1;
147
0
        if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
148
0
            return -1;
149
0
        if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
150
0
                NULL)
151
0
                <= 0
152
0
            || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
153
0
            return -1;
154
0
    }
155
0
    return i;
156
0
}
157
158
static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
159
0
{
160
0
    long num;
161
0
    int i = 0, j;
162
163
0
    if (type != NULL) {
164
0
        num = rc2_meth_to_magic(c);
165
0
        j = EVP_CIPHER_CTX_get_iv_length(c);
166
0
        i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j);
167
0
    }
168
0
    return i;
169
0
}
170
171
static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
172
0
{
173
0
    switch (type) {
174
0
    case EVP_CTRL_INIT:
175
0
        data(c)->key_bits = EVP_CIPHER_CTX_get_key_length(c) * 8;
176
0
        return 1;
177
178
0
    case EVP_CTRL_GET_RC2_KEY_BITS:
179
0
        *(int *)ptr = data(c)->key_bits;
180
0
        return 1;
181
182
0
    case EVP_CTRL_SET_RC2_KEY_BITS:
183
0
        if (arg > 0) {
184
0
            data(c)->key_bits = arg;
185
0
            return 1;
186
0
        }
187
0
        return 0;
188
#ifdef PBE_PRF_TEST
189
    case EVP_CTRL_PBE_PRF_NID:
190
        *(int *)ptr = NID_hmacWithMD5;
191
        return 1;
192
#endif
193
194
0
    default:
195
0
        return -1;
196
0
    }
197
0
}
198
199
#endif