Fixed md5 buffer not being correctly zero-initialized and refactored code. (#2507)

master
TJ 2024-09-05 23:55:02 +10:00 committed by GitHub
parent 61627f6524
commit bacf9cfeab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 280 additions and 327 deletions

View File

@ -68,10 +68,6 @@ documentation and/or software.
*/ */
#include "linden_common.h" #include "linden_common.h"
#include "llmd5.h" #include "llmd5.h"
@ -81,27 +77,22 @@ documentation and/or software.
// how many bytes to grab at a time when checking files // how many bytes to grab at a time when checking files
const int LLMD5::BLOCK_LEN = 4096; const int LLMD5::BLOCK_LEN = 4096;
// LLMD5 simple initialization method // LLMD5 simple initialization method
LLMD5::LLMD5() LLMD5::LLMD5()
{ {
init(); init();
} }
// MD5 block update operation. Continues an MD5 message-digest // MD5 block update operation. Continues an MD5 message-digest
// operation, processing another message block, and updating the // operation, processing another message block, and updating the
// context. // context.
void LLMD5::update(const uint8_t* input, const size_t input_length)
void LLMD5::update (const uint8_t *input, const size_t input_length) { {
size_t input_index, buffer_index; size_t input_index, buffer_index;
size_t buffer_space; // how much space is left in buffer size_t buffer_space; // how much space is left in buffer
if (finalized){ // so we can't update! if (finalized)
{ // so we can't update!
std::cerr << "LLMD5::update: Can't update a finalized digest!" << std::endl; std::cerr << "LLMD5::update: Can't update a finalized digest!" << std::endl;
return; return;
} }
@ -115,13 +106,15 @@ void LLMD5::update (const uint8_t *input, const size_t input_length) {
buffer_space = 64 - buffer_index; // how much space is left in buffer buffer_space = 64 - buffer_index; // how much space is left in buffer
// now, transform each 64-byte piece of the input, bypassing the buffer // now, transform each 64-byte piece of the input, bypassing the buffer
if (input == NULL || input_length == 0){ if (input == NULL || input_length == 0)
{
std::cerr << "LLMD5::update: Invalid input!" << std::endl; std::cerr << "LLMD5::update: Invalid input!" << std::endl;
return; return;
} }
// Transform as many times as possible. // Transform as many times as possible.
if (input_length >= buffer_space) { // ie. we have enough to fill the buffer if (input_length >= buffer_space) // ie. we have enough to fill the buffer
{
// fill the rest of the buffer and transform // fill the rest of the buffer and transform
memcpy(/* Flawfinder: ignore */ memcpy(/* Flawfinder: ignore */
buffer + buffer_index, buffer + buffer_index,
@ -129,8 +122,7 @@ void LLMD5::update (const uint8_t *input, const size_t input_length) {
buffer_space); buffer_space);
transform(buffer); transform(buffer);
for (input_index = buffer_space; input_index + 63 < input_length; for (input_index = buffer_space; input_index + 63 < input_length; input_index += 64)
input_index += 64)
transform(input + input_index); transform(input + input_index);
buffer_index = 0; // so we can buffer remaining buffer_index = 0; // so we can buffer remaining
@ -138,18 +130,14 @@ void LLMD5::update (const uint8_t *input, const size_t input_length) {
else else
input_index = 0; // so we can buffer the whole input input_index = 0; // so we can buffer the whole input
// and here we do the buffering: // and here we do the buffering:
memcpy(buffer + buffer_index, input + input_index, input_length - input_index); /* Flawfinder: ignore */ memcpy(buffer + buffer_index, input + input_index, input_length - input_index); /* Flawfinder: ignore */
} }
// MD5 update for files. // MD5 update for files.
// Like above, except that it works on files (and uses above as a primitive.) // Like above, except that it works on files (and uses above as a primitive.)
void LLMD5::update(FILE* file)
void LLMD5::update(FILE* file){ {
unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */ unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */
int len; int len;
@ -157,23 +145,21 @@ void LLMD5::update(FILE* file){
update(buffer, len); update(buffer, len);
fclose(file); fclose(file);
} }
// MD5 update for istreams. // MD5 update for istreams.
// Like update for files; see above. // Like update for files; see above.
void LLMD5::update(std::istream& stream)
void LLMD5::update(std::istream& stream){ {
unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */ unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */
int len; int len;
while (stream.good()){ while (stream.good())
{
stream.read((char*)buffer, BLOCK_LEN); /* Flawfinder: ignore */ // note that return value of read is unusable. stream.read((char*)buffer, BLOCK_LEN); /* Flawfinder: ignore */ // note that return value of read is unusable.
len = (int)stream.gcount(); len = (int)stream.gcount();
update(buffer, len); update(buffer, len);
} }
} }
void LLMD5::update(const std::string& s) void LLMD5::update(const std::string& s)
@ -183,19 +169,19 @@ void LLMD5::update(const std::string& s)
// MD5 finalization. Ends an MD5 message-digest operation, writing the // MD5 finalization. Ends an MD5 message-digest operation, writing the
// the message digest and zeroizing the context. // the message digest and zeroizing the context.
void LLMD5::finalize()
{
void LLMD5::finalize (){
unsigned char bits[8]; /* Flawfinder: ignore */ unsigned char bits[8]; /* Flawfinder: ignore */
size_t index, padLen; size_t index, padLen;
static uint8_t PADDING[64]={ static uint8_t PADDING[64] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}; };
if (finalized){ if (finalized)
{
std::cerr << "LLMD5::finalize: Already finalized this digest!" << std::endl; std::cerr << "LLMD5::finalize: Already finalized this digest!" << std::endl;
return; return;
} }
@ -216,27 +202,20 @@ void LLMD5::finalize (){
encode(digest, state, 16); encode(digest, state, 16);
// Zeroize sensitive information // Zeroize sensitive information
memset (buffer, 0, sizeof(*buffer)); memset(buffer, 0, sizeof(buffer));
finalized=1;
finalized = true;
} }
LLMD5::LLMD5(FILE* file)
{
LLMD5::LLMD5(FILE *file){
init(); // must be called be all constructors init(); // must be called be all constructors
update(file); update(file);
finalize(); finalize();
} }
LLMD5::LLMD5(std::istream& stream)
{
LLMD5::LLMD5(std::istream& stream){
init(); // must called by all constructors init(); // must called by all constructors
update(stream); update(stream);
finalize(); finalize();
@ -267,46 +246,34 @@ void LLMD5::raw_digest(unsigned char *s) const
{ {
if (!finalized) if (!finalized)
{ {
std::cerr << "LLMD5::raw_digest: Can't get digest if you haven't "<< std::cerr << "LLMD5::raw_digest: Can't get digest if you haven't "
"finalized the digest!" << std::endl; << "finalized the digest!" << std::endl;
s[0] = '\0'; s[0] = '\0';
return; return;
} }
memcpy(s, digest, 16); /* Flawfinder: ignore */ memcpy(s, digest, 16); /* Flawfinder: ignore */
return;
} }
void LLMD5::hex_digest(char* s) const void LLMD5::hex_digest(char* s) const
{ {
int i;
if (!finalized) if (!finalized)
{ {
std::cerr << "LLMD5::hex_digest: Can't get digest if you haven't "<< std::cerr << "LLMD5::hex_digest: Can't get digest if you haven't "
"finalized the digest!" <<std::endl; << "finalized the digest!" << std::endl;
s[0] = '\0'; s[0] = '\0';
return; return;
} }
for (i=0; i<16; i++) for (int i = 0; i < 16; i++)
{ {
sprintf(s + i * 2, "%02x", digest[i]); /* Flawfinder: ignore */ sprintf(s + i * 2, "%02x", digest[i]); /* Flawfinder: ignore */
} }
s[32] = '\0'; s[32] = '\0';
return;
} }
std::ostream& operator<<(std::ostream& stream, const LLMD5& context)
std::ostream& operator<<(std::ostream &stream, LLMD5 context)
{ {
char s[33]; /* Flawfinder: ignore */ char s[33]; /* Flawfinder: ignore */
context.hex_digest(s); context.hex_digest(s);
@ -332,9 +299,9 @@ bool operator!=(const LLMD5& a, const LLMD5& b)
} }
// PRIVATE METHODS: // PRIVATE METHODS:
void LLMD5::init()
void LLMD5::init(){ {
finalized=0; // we just started! finalized = false; // we just started!
// Nothing counted, so count=0 // Nothing counted, so count=0
count = 0; count = 0;
@ -346,12 +313,9 @@ void LLMD5::init(){
state[3] = 0x10325476; state[3] = 0x10325476;
} }
// Constants for MD5Transform routine. // Constants for MD5Transform routine.
// Although we could use C++ style constants, defines are actually better, // Although we could use C++ style constants, defines are actually better,
// since they let us easily evade scope clashes. // since they let us easily evade scope clashes.
#define S11 7 #define S11 7
#define S12 12 #define S12 12
#define S13 17 #define S13 17
@ -386,32 +350,34 @@ void LLMD5::init(){
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation. Rotation is separate from addition to prevent recomputation.
*/ */
#define FF(a, b, c, d, x, s, ac) { \ #define FF(a, b, c, d, x, s, ac) \
{ \
(a) += F((b), (c), (d)) + (x) + (U32)(ac); \ (a) += F((b), (c), (d)) + (x) + (U32)(ac); \
(a) = ROTATE_LEFT((a), (s)); \ (a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define GG(a, b, c, d, x, s, ac) { \ #define GG(a, b, c, d, x, s, ac) \
{ \
(a) += G((b), (c), (d)) + (x) + (U32)(ac); \ (a) += G((b), (c), (d)) + (x) + (U32)(ac); \
(a) = ROTATE_LEFT((a), (s)); \ (a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define HH(a, b, c, d, x, s, ac) { \ #define HH(a, b, c, d, x, s, ac) \
{ \
(a) += H((b), (c), (d)) + (x) + (U32)(ac); \ (a) += H((b), (c), (d)) + (x) + (U32)(ac); \
(a) = ROTATE_LEFT((a), (s)); \ (a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define II(a, b, c, d, x, s, ac) { \ #define II(a, b, c, d, x, s, ac) \
{ \
(a) += I((b), (c), (d)) + (x) + (U32)(ac); \ (a) += I((b), (c), (d)) + (x) + (U32)(ac); \
(a) = ROTATE_LEFT((a), (s)); \ (a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
// LLMD5 basic transformation. Transforms state based on block. // LLMD5 basic transformation. Transforms state based on block.
void LLMD5::transform (const U8 block[64]){ void LLMD5::transform(const U8 block[64])
{
uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
decode(x, block, 64); decode(x, block, 64);
@ -496,19 +462,15 @@ void LLMD5::transform (const U8 block[64]){
state[3] += d; state[3] += d;
// Zeroize sensitive information. // Zeroize sensitive information.
memset ( (uint8_t *) x, 0, sizeof(x)); memset(x, 0, sizeof(x));
} }
// Encodes input (uint32_t) into output (unsigned char). Assumes len is // Encodes input (uint32_t) into output (unsigned char). Assumes len is
// a multiple of 4. // a multiple of 4.
void LLMD5::encode (uint8_t *output, const uint32_t *input, const size_t len) { void LLMD5::encode(uint8_t* output, const uint32_t* input, const size_t len)
{
size_t i, j; for (size_t i = 0, j = 0; j < len; i++, j += 4)
{
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (uint8_t)(input[i] & 0xff); output[j] = (uint8_t)(input[i] & 0xff);
output[j + 1] = (uint8_t)((input[i] >> 8) & 0xff); output[j + 1] = (uint8_t)((input[i] >> 8) & 0xff);
output[j + 2] = (uint8_t)((input[i] >> 16) & 0xff); output[j + 2] = (uint8_t)((input[i] >> 16) & 0xff);
@ -516,18 +478,11 @@ void LLMD5::encode (uint8_t *output, const uint32_t *input, const size_t len) {
} }
} }
// Decodes input (unsigned char) into output (uint32_t). Assumes len is // Decodes input (unsigned char) into output (uint32_t). Assumes len is
// a multiple of 4. // a multiple of 4.
void LLMD5::decode (uint32_t *output, const uint8_t *input, const size_t len){ void LLMD5::decode(uint32_t* output, const uint8_t* input, const size_t len)
{
size_t i, j; for (size_t i = 0, j = 0; j < len; i++, j += 4)
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) | output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
(((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24); (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
} }

View File

@ -76,7 +76,8 @@ const int MD5RAW_BYTES = 16;
const int MD5HEX_STR_SIZE = 33; // char hex[MD5HEX_STR_SIZE]; with null const int MD5HEX_STR_SIZE = 33; // char hex[MD5HEX_STR_SIZE]; with null
const int MD5HEX_STR_BYTES = 32; // message system fixed size const int MD5HEX_STR_BYTES = 32; // message system fixed size
class LL_COMMON_API LLMD5 { class LL_COMMON_API LLMD5
{
// how many bytes to grab at a time when checking files // how many bytes to grab at a time when checking files
static const int BLOCK_LEN; static const int BLOCK_LEN;
@ -100,17 +101,15 @@ public:
void raw_digest(unsigned char* array) const; // provide 16-byte array for binary data void raw_digest(unsigned char* array) const; // provide 16-byte array for binary data
void hex_digest(char* string) const; // provide 33-byte array for ascii-hex string void hex_digest(char* string) const; // provide 33-byte array for ascii-hex string
friend LL_COMMON_API std::ostream& operator<< (std::ostream&, LLMD5 context); friend LL_COMMON_API std::ostream& operator<<(std::ostream&, const LLMD5& context);
private: private:
// next, the private data: // next, the private data:
uint32_t state[4]; uint32_t state[4];
uint64_t count; // number of *bits*, mod 2^64 uint64_t count; // number of *bits*, mod 2^64
uint8_t buffer[64]; // input buffer uint8_t buffer[64]; // input buffer
uint8_t digest[16]; uint8_t digest[16];
uint8_t finalized; bool finalized;
// last, the private methods, mostly static: // last, the private methods, mostly static:
void init(); // called by all constructors void init(); // called by all constructors
@ -119,7 +118,6 @@ private:
static void encode(uint8_t* dest, const uint32_t* src, const size_t length); static void encode(uint8_t* dest, const uint32_t* src, const size_t length);
static void decode(uint32_t* dest, const uint8_t* src, const size_t length); static void decode(uint32_t* dest, const uint8_t* src, const size_t length);
}; };
LL_COMMON_API bool operator==(const LLMD5& a, const LLMD5& b); LL_COMMON_API bool operator==(const LLMD5& a, const LLMD5& b);