From 2cbb5de48389966cec8046442d0bc3f8b2dfb094 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 26 Nov 2015 12:34:49 +0100 Subject: [PATCH] Move size checks to the main chacha20 encryption function --- .../crypto_stream/chacha20/ref/stream_chacha20_ref.c | 6 +++--- .../crypto_stream/chacha20/vec/stream_chacha20_vec.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c index c4436789..fa8e8352 100644 --- a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +++ b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c @@ -112,6 +112,9 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long byt if (!bytes) { return; /* LCOV_EXCL_LINE */ } + if (bytes > 64ULL * (1ULL << 32) - 64ULL) { + abort(); + } j0 = ctx->input[0]; j1 = ctx->input[1]; j2 = ctx->input[2]; @@ -267,9 +270,6 @@ stream_ietf_ref(unsigned char *c, unsigned long long clen, if (!clen) { return 0; } - if (clen > 64ULL * (1ULL << 32) - 64ULL) { - abort(); - } (void) sizeof(int[crypto_stream_chacha20_KEYBYTES == 256 / 8 ? 1 : -1]); chacha_keysetup(&ctx, k); chacha_ietf_ivsetup(&ctx, n, NULL); diff --git a/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.c b/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.c index fcbf301f..b376d261 100644 --- a/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.c +++ b/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.c @@ -137,6 +137,9 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *in, uint8_t *out, unsigned long long iters; unsigned long long i; + if (inlen > 64ULL * (1ULL << 32) - 64ULL) { + abort(); + } s0 = LOAD_ALIGNED(chacha_const); s1 = ctx->s1; s2 = ctx->s2; @@ -234,7 +237,7 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *in, uint8_t *out, buf[0] = REVV_BE(v0 + s0); } for (i = inlen & ~15ULL; i < inlen; i++) { - ((char *)op)[i] = ((const char *)ip)[i] ^ ((char *)buf)[i]; + ((char *) op)[i] = ((const char *) ip)[i] ^ ((char *) buf)[i]; } } } @@ -267,9 +270,6 @@ stream_ietf_vec(unsigned char *c, unsigned long long clen, if (!clen) { return 0; } - if (clen > 64ULL * (1ULL << 32) - 64ULL) { - abort(); - } (void) sizeof(int[crypto_stream_chacha20_KEYBYTES == 256 / 8 ? 1 : -1]); chacha_keysetup(&ctx, k); chacha_ietf_ivsetup(&ctx, n, 0ULL);