Skip to content

Commit 792cb4e

Browse files
committed
Ensure that rc5 doesn't try to use a key longer than 2040 bits
The maximum key length for rc5 is 2040 bits so we should not attempt to use keys longer than this. Issue found by OSS-Fuzz and Guido Vranken. Reviewed-by: Paul Dale <[email protected]> (Merged from openssl#8834)
1 parent 0860761 commit 792cb4e

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

crypto/err/openssl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ EVP_F_PKEY_SET_TYPE:158:pkey_set_type
889889
EVP_F_POLY1305_CTRL:216:poly1305_ctrl
890890
EVP_F_RC2_MAGIC_TO_METH:109:rc2_magic_to_meth
891891
EVP_F_RC5_CTRL:125:rc5_ctrl
892+
EVP_F_R_32_12_16_INIT_KEY:242:r_32_12_16_init_key
892893
EVP_F_S390X_AES_GCM_CTRL:201:s390x_aes_gcm_ctrl
893894
EVP_F_S390X_AES_GCM_TLS_CIPHER:208:s390x_aes_gcm_tls_cipher
894895
EVP_F_SCRYPT_ALG:228:scrypt_alg
@@ -2385,6 +2386,7 @@ ESS_R_ESS_SIGNING_CERT_V2_ADD_ERROR:101:ess signing cert v2 add error
23852386
EVP_R_AES_KEY_SETUP_FAILED:143:aes key setup failed
23862387
EVP_R_ARIA_KEY_SETUP_FAILED:176:aria key setup failed
23872388
EVP_R_BAD_DECRYPT:100:bad decrypt
2389+
EVP_R_BAD_KEY_LENGTH:195:bad key length
23882390
EVP_R_BUFFER_TOO_SMALL:155:buffer too small
23892391
EVP_R_CAMELLIA_KEY_SETUP_FAILED:157:camellia key setup failed
23902392
EVP_R_CIPHER_NOT_GCM_MODE:184:cipher not gcm mode

crypto/evp/e_rc5.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
6666
static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
6767
const unsigned char *iv, int enc)
6868
{
69+
if (EVP_CIPHER_CTX_key_length(ctx) > 255) {
70+
EVPerr(EVP_F_R_32_12_16_INIT_KEY, EVP_R_BAD_KEY_LENGTH);
71+
return 0;
72+
}
6973
RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
7074
key, data(ctx)->rounds);
7175
return 1;

crypto/evp/evp_err.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ static const ERR_STRING_DATA EVP_str_functs[] = {
185185
{ERR_PACK(ERR_LIB_EVP, EVP_F_POLY1305_CTRL, 0), "poly1305_ctrl"},
186186
{ERR_PACK(ERR_LIB_EVP, EVP_F_RC2_MAGIC_TO_METH, 0), "rc2_magic_to_meth"},
187187
{ERR_PACK(ERR_LIB_EVP, EVP_F_RC5_CTRL, 0), "rc5_ctrl"},
188+
{ERR_PACK(ERR_LIB_EVP, EVP_F_R_32_12_16_INIT_KEY, 0),
189+
"r_32_12_16_init_key"},
188190
{ERR_PACK(ERR_LIB_EVP, EVP_F_S390X_AES_GCM_CTRL, 0), "s390x_aes_gcm_ctrl"},
189191
{ERR_PACK(ERR_LIB_EVP, EVP_F_S390X_AES_GCM_TLS_CIPHER, 0),
190192
"s390x_aes_gcm_tls_cipher"},
@@ -199,6 +201,7 @@ static const ERR_STRING_DATA EVP_str_reasons[] = {
199201
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ARIA_KEY_SETUP_FAILED),
200202
"aria key setup failed"},
201203
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BAD_DECRYPT), "bad decrypt"},
204+
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BAD_KEY_LENGTH), "bad key length"},
202205
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BUFFER_TOO_SMALL), "buffer too small"},
203206
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_CAMELLIA_KEY_SETUP_FAILED),
204207
"camellia key setup failed"},

doc/man3/EVP_rc5_32_12_16_cbc.pod

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,26 @@ EVP_rc5_32_12_16_ofb()
3333

3434
RC5 encryption algorithm in CBC, CFB, ECB and OFB modes respectively. This is a
3535
variable key length cipher with an additional "number of rounds" parameter. By
36-
default the key length is set to 128 bits and 12 rounds.
36+
default the key length is set to 128 bits and 12 rounds. Alternative key lengths
37+
can be set using L<EVP_CIPHER_CTX_set_key_length(3)>. The maximum key length is
38+
2040 bits.
39+
40+
The following rc5 specific I<ctrl>s are supported (see
41+
L<EVP_CIPHER_CTX_ctrl(3)>).
42+
43+
=over 4
44+
45+
=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)
46+
47+
Sets the number of rounds to B<rounds>. This must be one of RC5_8_ROUNDS,
48+
RC5_12_ROUNDS or RC5_16_ROUNDS.
49+
50+
=item EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, &rounds)
51+
52+
Stores the number of rounds currently configured in B<*rounds> where B<*rounds>
53+
is an int.
54+
55+
=back
3756

3857
=back
3958

@@ -43,10 +62,6 @@ These functions return an B<EVP_CIPHER> structure that contains the
4362
implementation of the symmetric cipher. See L<EVP_CIPHER_meth_new(3)> for
4463
details of the B<EVP_CIPHER> structure.
4564

46-
=head1 BUGS
47-
48-
Currently the number of rounds in RC5 can only be set to 8, 12 or 16.
49-
This is a limitation of the current RC5 code rather than the EVP interface.
5065

5166
=head1 SEE ALSO
5267

include/openssl/evperr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ int ERR_load_EVP_strings(void);
151151
# define EVP_F_POLY1305_CTRL 216
152152
# define EVP_F_RC2_MAGIC_TO_METH 109
153153
# define EVP_F_RC5_CTRL 125
154+
# define EVP_F_R_32_12_16_INIT_KEY 242
154155
# define EVP_F_S390X_AES_GCM_CTRL 201
155156
# define EVP_F_S390X_AES_GCM_TLS_CIPHER 208
156157
# define EVP_F_SCRYPT_ALG 228
@@ -162,6 +163,7 @@ int ERR_load_EVP_strings(void);
162163
# define EVP_R_AES_KEY_SETUP_FAILED 143
163164
# define EVP_R_ARIA_KEY_SETUP_FAILED 176
164165
# define EVP_R_BAD_DECRYPT 100
166+
# define EVP_R_BAD_KEY_LENGTH 195
165167
# define EVP_R_BUFFER_TOO_SMALL 155
166168
# define EVP_R_CAMELLIA_KEY_SETUP_FAILED 157
167169
# define EVP_R_CIPHER_NOT_GCM_MODE 184

0 commit comments

Comments
 (0)