Skip to content

Commit

Permalink
Change RC5_32_set_key to return an int type
Browse files Browse the repository at this point in the history
If the key is too long we now return an error.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from #8834)
  • Loading branch information
mattcaswell committed Jul 1, 2019
1 parent 792cb4e commit 9a131ad
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

Changes between 1.1.1 and 3.0.0 [xx XXX xxxx]

*) RC5_32_set_key has been changed to return an int type, with 0 indicating
an error and 1 indicating success. In previous versions of OpenSSL this
was a void type. If a key was set longer than the maximum possible this
would crash.
[Matt Caswell]

*) Support SM2 signing and verification schemes with X509 certificate.
[Paul Yang]

Expand Down
5 changes: 4 additions & 1 deletion apps/speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,10 @@ int speed_main(int argc, char **argv)
RC2_set_key(&rc2_ks, 16, key16, 128);
#endif
#ifndef OPENSSL_NO_RC5
RC5_32_set_key(&rc5_ks, 16, key16, 12);
if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
BIO_printf(bio_err, "Failed setting RC5 key\n");
goto end;
}
#endif
#ifndef OPENSSL_NO_BF
BF_set_key(&bf_ks, 16, key16);
Expand Down
5 changes: 2 additions & 3 deletions crypto/evp/e_rc5.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
EVPerr(EVP_F_R_32_12_16_INIT_KEY, EVP_R_BAD_KEY_LENGTH);
return 0;
}
RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
key, data(ctx)->rounds);
return 1;
return RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
key, data(ctx)->rounds);
}

#endif
9 changes: 7 additions & 2 deletions crypto/rc5/rc5_skey.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include <openssl/rc5.h>
#include "rc5_locl.h"

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

if (len > 255)
return 0;

if ((rounds != RC5_16_ROUNDS) &&
(rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS))
rounds = RC5_16_ROUNDS;
Expand Down Expand Up @@ -58,4 +61,6 @@ void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
if (++jj >= c)
jj = 0;
}

return 1;
}
4 changes: 2 additions & 2 deletions include/openssl/rc5.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ typedef struct rc5_key_st {
RC5_32_INT data[2 * (RC5_16_ROUNDS + 1)];
} RC5_32_KEY;

void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
int rounds);
int RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
int rounds);
void RC5_32_ecb_encrypt(const unsigned char *in, unsigned char *out,
RC5_32_KEY *key, int enc);
void RC5_32_encrypt(unsigned long *data, RC5_32_KEY *key);
Expand Down
7 changes: 5 additions & 2 deletions test/rc5test.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ static int test_rc5_ecb(int n)
RC5_32_KEY key;
unsigned char buf[8], buf2[8];

RC5_32_set_key(&key, 16, &RC5key[n][0], 12);
if (!TEST_true(RC5_32_set_key(&key, 16, &RC5key[n][0], 12)))
return 0;

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

i = rc5_cbc_rounds[n];
if (i >= 8) {
RC5_32_set_key(&key, rc5_cbc_key[n][0], &rc5_cbc_key[n][1], i);
if (!TEST_true(RC5_32_set_key(&key, rc5_cbc_key[n][0],
&rc5_cbc_key[n][1], i)))
return 0;

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

0 comments on commit 9a131ad

Please sign in to comment.