]> git.codecow.com Git - Monocypher.git/commitdiff
Fixed (NULL + 0) undefined behaviour
authorLoup Vaillant <loup@loup-vaillant.fr>
Mon, 20 Apr 2020 11:59:31 +0000 (13:59 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Mon, 20 Apr 2020 11:59:31 +0000 (13:59 +0200)
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
src/optional/monocypher-ed25519.c

index 9671de5d96aac2169fbb13ebc33b7b7bcfbd8e9a..bad6c53fe20f9ecf990dd3ffee0bfd20659713b3 100644 (file)
@@ -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);
index 36d771b1198975c15865762b4906ffe9d60fcdd6..a2b39e2827cea586ef6ecf83eede0e53f77d6ec4 100644 (file)
@@ -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);