From ea6a80ccdcbaef6faeb03d0d80c0cbb2e070ed48 Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Mon, 20 Apr 2020 13:59:31 +0200 Subject: [PATCH] Fixed (NULL + 0) undefined behaviour It appears that arithmetic on NULL pointers is undefined, even when we just add zero. Monocypher generally allows input buffers to be NULL pointers if their length is zero. This is because we never dereference those pointers in this case. Likewise, we should not perform any arithmetic on them. The fix is to return immediately when the input buffer length is zero. --- src/monocypher.c | 6 ++++++ src/optional/monocypher-ed25519.c | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/monocypher.c b/src/monocypher.c index 9671de5..bad6c53 100644 --- a/src/monocypher.c +++ b/src/monocypher.c @@ -409,6 +409,9 @@ void crypto_poly1305_init(crypto_poly1305_ctx *ctx, const u8 key[32]) void crypto_poly1305_update(crypto_poly1305_ctx *ctx, const u8 *message, size_t message_size) { + if (message_size == 0) { + return; + } // Align ourselves with block boundaries size_t align = MIN(ALIGN(ctx->c_idx, 16), message_size); poly_update(ctx, message, align); @@ -614,6 +617,9 @@ void crypto_blake2b_init(crypto_blake2b_ctx *ctx) void crypto_blake2b_update(crypto_blake2b_ctx *ctx, const u8 *message, size_t message_size) { + if (message_size == 0) { + return; + } // Align ourselves with block boundaries size_t align = MIN(ALIGN(ctx->input_idx, 128), message_size); blake2b_update(ctx, message, align); diff --git a/src/optional/monocypher-ed25519.c b/src/optional/monocypher-ed25519.c index 36d771b..a2b39e2 100644 --- a/src/optional/monocypher-ed25519.c +++ b/src/optional/monocypher-ed25519.c @@ -215,6 +215,9 @@ void crypto_sha512_init(crypto_sha512_ctx *ctx) void crypto_sha512_update(crypto_sha512_ctx *ctx, const u8 *message, size_t message_size) { + if (message_size == 0) { + return; + } // Align ourselves with block boundaries size_t align = MIN(ALIGN(ctx->input_idx, 128), message_size); sha512_update(ctx, message, align); -- 2.47.3