From: Dan Sheppard Date: Fri, 11 Apr 2025 09:38:46 +0000 (+0100) Subject: stack overflow audit. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~dans/git?a=commitdiff_plain;h=04e3783dda29c25cdf9c6e37d062ba79f605e7f6;p=coquet.git stack overflow audit. --- diff --git a/sha2.c b/sha2.c index e956672..5d0cb7d 100644 --- a/sha2.c +++ b/sha2.c @@ -82,6 +82,29 @@ uint64_t sha512_k[80] = { 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 }; +/* Stack allocated buffer non-overrun audits: + * (1) w: only accessed by loop-index i, which never exceeds size of w[] + * j: only written by memcpy len=64; is 8*64-bits = 64 bytes + * m: only written by + * a. memcpy len=56 at pointer offset 1. Is 8*64-bits, so at offset 1 + * there's 7*64-bits, = 56 bytes. + * + * (2) pad_length is at most SHA2_MAX_BLOCK_LEN. It is calculated by taking + * block_size and subtracting positives. If this is greater than zero, + * this is <= SHA2_MAX_BLOCK_LEN. If less than zero, block_size is + * added to negative, therefore also variety->bits; block_size = 2*bits; @@ -283,7 +306,7 @@ static void xor_blit(uint8_t *data, int v, int n) { } int sha2_finish(struct sha2_ctx_t *ctx, uint8_t *out, int max_len) { - uint8_t tmp[SHA2_MAX_BLOCK_LENGTH]; + uint8_t tmp[SHA2_MAX_BLOCK_LENGTH]; /* (3) */ struct sha2_ctx_t outer; int block_size,len; @@ -318,7 +341,7 @@ static void derive_key(uint8_t *out, int sha2_variety, uint8_t *key, int key_len void sha2_init_hmac(struct sha2_ctx_t *ctx, int sha2_variety, uint8_t *key, int key_len) { - uint8_t i_pad[SHA2_MAX_BLOCK_LENGTH]; + uint8_t i_pad[SHA2_MAX_BLOCK_LENGTH]; /* (4) */ int block_size; block_size = block_length(&sha2_variety_def[sha2_variety]);