Skip to content

Commit 9a131ad

Browse files
committed
Change RC5_32_set_key to return an int type
If the key is too long we now return an error. Reviewed-by: Paul Dale <[email protected]> (Merged from openssl#8834)
1 parent 792cb4e commit 9a131ad

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
Changes between 1.1.1 and 3.0.0 [xx XXX xxxx]
1111

12+
*) RC5_32_set_key has been changed to return an int type, with 0 indicating
13+
an error and 1 indicating success. In previous versions of OpenSSL this
14+
was a void type. If a key was set longer than the maximum possible this
15+
would crash.
16+
[Matt Caswell]
17+
1218
*) Support SM2 signing and verification schemes with X509 certificate.
1319
[Paul Yang]
1420

apps/speed.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,10 @@ int speed_main(int argc, char **argv)
19851985
RC2_set_key(&rc2_ks, 16, key16, 128);
19861986
#endif
19871987
#ifndef OPENSSL_NO_RC5
1988-
RC5_32_set_key(&rc5_ks, 16, key16, 12);
1988+
if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
1989+
BIO_printf(bio_err, "Failed setting RC5 key\n");
1990+
goto end;
1991+
}
19891992
#endif
19901993
#ifndef OPENSSL_NO_BF
19911994
BF_set_key(&bf_ks, 16, key16);

crypto/evp/e_rc5.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
7070
EVPerr(EVP_F_R_32_12_16_INIT_KEY, EVP_R_BAD_KEY_LENGTH);
7171
return 0;
7272
}
73-
RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
74-
key, data(ctx)->rounds);
75-
return 1;
73+
return RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
74+
key, data(ctx)->rounds);
7675
}
7776

7877
#endif

crypto/rc5/rc5_skey.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
#include <openssl/rc5.h>
1111
#include "rc5_locl.h"
1212

13-
void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
14-
int rounds)
13+
int RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
14+
int rounds)
1515
{
1616
RC5_32_INT L[64], l, ll, A, B, *S, k;
1717
int i, j, m, c, t, ii, jj;
1818

19+
if (len > 255)
20+
return 0;
21+
1922
if ((rounds != RC5_16_ROUNDS) &&
2023
(rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS))
2124
rounds = RC5_16_ROUNDS;
@@ -58,4 +61,6 @@ void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
5861
if (++jj >= c)
5962
jj = 0;
6063
}
64+
65+
return 1;
6166
}

include/openssl/rc5.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ typedef struct rc5_key_st {
3939
RC5_32_INT data[2 * (RC5_16_ROUNDS + 1)];
4040
} RC5_32_KEY;
4141

42-
void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
43-
int rounds);
42+
int RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
43+
int rounds);
4444
void RC5_32_ecb_encrypt(const unsigned char *in, unsigned char *out,
4545
RC5_32_KEY *key, int enc);
4646
void RC5_32_encrypt(unsigned long *data, RC5_32_KEY *key);

test/rc5test.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ static int test_rc5_ecb(int n)
181181
RC5_32_KEY key;
182182
unsigned char buf[8], buf2[8];
183183

184-
RC5_32_set_key(&key, 16, &RC5key[n][0], 12);
184+
if (!TEST_true(RC5_32_set_key(&key, 16, &RC5key[n][0], 12)))
185+
return 0;
185186

186187
RC5_32_ecb_encrypt(&RC5plain[n][0], buf, &key, RC5_ENCRYPT);
187188
if (!TEST_mem_eq(&RC5cipher[n][0], sizeof(RC5cipher[0]), buf, sizeof(buf)))
@@ -203,7 +204,9 @@ static int test_rc5_cbc(int n)
203204

204205
i = rc5_cbc_rounds[n];
205206
if (i >= 8) {
206-
RC5_32_set_key(&key, rc5_cbc_key[n][0], &rc5_cbc_key[n][1], i);
207+
if (!TEST_true(RC5_32_set_key(&key, rc5_cbc_key[n][0],
208+
&rc5_cbc_key[n][1], i)))
209+
return 0;
207210

208211
memcpy(ivb, &rc5_cbc_iv[n][0], 8);
209212
RC5_32_cbc_encrypt(&rc5_cbc_plain[n][0], buf, 8,

0 commit comments

Comments
 (0)