]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
SHA-PRF: Make code easier for static analyzers
authorJouni Malinen <j@w1.fi>
Sun, 9 Feb 2025 16:12:40 +0000 (18:12 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 9 Feb 2025 17:08:30 +0000 (19:08 +0200)
pos cannot be 0 when buf_len_bits != 0 and as such, buf[pos - 1] here
cannot point to invalid index. However, this is apparently too complex
for static analyzers, so make this more obvious to avoid false
positives.

Signed-off-by: Jouni Malinen <j@w1.fi>
src/crypto/sha256-prf.c
src/crypto/sha384-prf.c
src/crypto/sha512-prf.c

index d665a9983cf8a798cbc86dca8070018f8e9d1402..de7394a32a730038b628e79fbdc6ca3fc21d2da6 100644 (file)
@@ -97,7 +97,7 @@ int sha256_prf_bits(const u8 *key, size_t key_len, const char *label,
         * Mask out unused bits in the last octet if it does not use all the
         * bits.
         */
-       if (buf_len_bits % 8) {
+       if (pos > 0 && (buf_len_bits % 8)) {
                u8 mask = 0xff << (8 - buf_len_bits % 8);
                buf[pos - 1] &= mask;
        }
index 420e78c380cd918e7c64b5d76ddba8a73ccf91c2..fdf33165506b88382ebabac0caecd51f69b8d41b 100644 (file)
@@ -97,7 +97,7 @@ int sha384_prf_bits(const u8 *key, size_t key_len, const char *label,
         * Mask out unused bits in the last octet if it does not use all the
         * bits.
         */
-       if (buf_len_bits % 8) {
+       if (pos > 0 && (buf_len_bits % 8)) {
                u8 mask = 0xff << (8 - buf_len_bits % 8);
                buf[pos - 1] &= mask;
        }
index e48cf5f0566260ff715c699120b501051b6773e3..be458141dc54cf9e8127448a82b5d84ab0b72031 100644 (file)
@@ -97,7 +97,7 @@ int sha512_prf_bits(const u8 *key, size_t key_len, const char *label,
         * Mask out unused bits in the last octet if it does not use all the
         * bits.
         */
-       if (buf_len_bits % 8) {
+       if (pos > 0 && (buf_len_bits % 8)) {
                u8 mask = 0xff << (8 - buf_len_bits % 8);
                buf[pos - 1] &= mask;
        }