Skip to content

Commit 6174f83

Browse files
authored
chore: limit number of random bytes generated (#773)
* chore: limit number of random bytes generated
1 parent 2158a7c commit 6174f83

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

include/aws/cryptosdk/private/cipher.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ int aws_cryptosdk_encrypt_body(
147147
uint8_t *tag, /* out */
148148
int body_frame_type);
149149

150+
// Even though `len` is of type `size_t`, this function is limited
151+
// by the underlying OpenSSL function, which takes an `int`
152+
// and so aws_cryptosdk_genrandom will return an error if asked for
153+
// more than INT_MAX (2 billion) bytes of randomness.
150154
int aws_cryptosdk_genrandom(uint8_t *buf, size_t len);
151155

152156
// TODO: Footer

source/cipher.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,19 @@ int aws_cryptosdk_decrypt_body(
805805
}
806806
}
807807

808+
// Even though `len` is of type `size_t`, this function is limited
809+
// by the underlying OpenSSL function, which takes an `int`
810+
// and so aws_cryptosdk_genrandom will return an error if asked for
811+
// more than INT_MAX (2 billion) bytes of randomness.
808812
int aws_cryptosdk_genrandom(uint8_t *buf, size_t len) {
809813
AWS_FATAL_PRECONDITION(AWS_MEM_IS_WRITABLE(buf, len));
810814

811815
if (len == 0) {
812816
return 0;
813817
}
818+
if (len > INT_MAX) {
819+
return aws_raise_error(AWS_CRYPTOSDK_ERR_LIMIT_EXCEEDED);
820+
}
814821
int rc = RAND_bytes(buf, len);
815822

816823
if (rc != 1) {

0 commit comments

Comments
 (0)