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