From f21202611c867d06b26aad9363eb57c39e0cb892 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:11:41 +0100 Subject: [PATCH] Use sizetypes in Sha1 code Must use size types instead of unsigned integers to prevent arithmetic overflow on larger (>4gb) buffers. This also suppresses the compiler warnings generated from this particular file. --- r5dev/mathlib/sha1.cpp | 10 +++++----- r5dev/mathlib/sha1.h | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/r5dev/mathlib/sha1.cpp b/r5dev/mathlib/sha1.cpp index e907c806..645ae214 100644 --- a/r5dev/mathlib/sha1.cpp +++ b/r5dev/mathlib/sha1.cpp @@ -69,7 +69,7 @@ std::string SHA1::final() /* Padding */ buffer += 0x80; - unsigned int orig_size = buffer.size(); + size_t orig_size = buffer.size(); while (buffer.size() < BLOCK_BYTES) { buffer += (char)0x00; @@ -81,7 +81,7 @@ std::string SHA1::final() if (orig_size > BLOCK_BYTES - 8) { transform(block); - for (unsigned int i = 0; i < BLOCK_INTS - 2; i++) + for (size_t i = 0; i < BLOCK_INTS - 2; i++) { block[i] = 0; } @@ -94,7 +94,7 @@ std::string SHA1::final() /* Hex std::string */ std::ostringstream result; - for (unsigned int i = 0; i < DIGEST_INTS; i++) + for (size_t i = 0; i < DIGEST_INTS; i++) { result << std::hex << std::setfill('0') << std::setw(8); result << (digest[i] & 0xffffffff); @@ -242,7 +242,7 @@ void SHA1::transform(uint32 block[BLOCK_BYTES]) void SHA1::buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES]) { /* Convert the std::string (byte buffer) to a uint32 array (MSB) */ - for (unsigned int i = 0; i < BLOCK_INTS; i++) + for (size_t i = 0; i < BLOCK_INTS; i++) { block[i] = (buffer[4*i+3] & 0xff) | (buffer[4*i+2] & 0xff)<<8 @@ -252,7 +252,7 @@ void SHA1::buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES]) } -void SHA1::read(std::istream &is, std::string &s, int max) +void SHA1::read(std::istream &is, std::string &s, size_t max) { char* sbuf = new char[max]; is.read(sbuf, max); diff --git a/r5dev/mathlib/sha1.h b/r5dev/mathlib/sha1.h index ef5cf152..9a9964e8 100644 --- a/r5dev/mathlib/sha1.h +++ b/r5dev/mathlib/sha1.h @@ -35,9 +35,9 @@ private: typedef unsigned long int uint32; /* just needs to be at least 32bit */ typedef unsigned long long uint64; /* just needs to be at least 64bit */ - static const unsigned int DIGEST_INTS = 5; /* number of 32bit integers per SHA1 digest */ - static const unsigned int BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ - static const unsigned int BLOCK_BYTES = BLOCK_INTS * 4; + static const size_t DIGEST_INTS = 5; /* number of 32bit integers per SHA1 digest */ + static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ + static const size_t BLOCK_BYTES = BLOCK_INTS * 4; uint32 digest[DIGEST_INTS]; std::string buffer; @@ -47,7 +47,7 @@ private: void transform(uint32 block[BLOCK_BYTES]); static void buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES]); - static void read(std::istream &is, std::string &s, int max); + static void read(std::istream &is, std::string &s, size_t max); }; std::string sha1(const std::string &string);