Update MD5 code with proper coding style/documentation for this project.

This commit is contained in:
Michael R Sweet 2025-02-15 13:35:54 -05:00
parent fceb5a807d
commit 77117ac789
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244

View File

@ -1,7 +1,7 @@
// //
// MD5 functions for PDFio. // MD5 functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2025 by Michael R Sweet.
// Copyright © 1999 Aladdin Enterprises. All rights reserved. // Copyright © 1999 Aladdin Enterprises. All rights reserved.
// //
// This software is provided 'as-is', without any express or implied // This software is provided 'as-is', without any express or implied
@ -108,231 +108,285 @@
#define T63 0x2ad7d2bb #define T63 0x2ad7d2bb
#define T64 0xeb86d391 #define T64 0xeb86d391
//
// Use the unoptimized (big-endian) implementation if we don't know the
// endian-ness of the platform.
//
#ifdef __BYTE_ORDER__
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define ARCH_IS_BIG_ENDIAN 0 // Use little endian optimized version
# else
# define ARCH_IS_BIG_ENDIAN 1 // Use generic version
# endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#elif !defined(ARCH_IS_BIG_ENDIAN)
# define ARCH_IS_BIG_ENDIAN 1 // Use generic version
#endif // !ARCH_IS_BIG_ENDIAN
//
// 'md5_process()' - Hash a block of data.
//
static void static void
md5_process(_pdfio_md5_t *pms, const uint8_t *data /*[64]*/) md5_process(_pdfio_md5_t *pms, // I - MD5 state
const uint8_t *data/*[64]*/)// I - Data
{ {
uint32_t uint32_t a = pms->abcd[0], // First word of state
a = pms->abcd[0], b = pms->abcd[1], b = pms->abcd[1], // Second word of state
c = pms->abcd[2], d = pms->abcd[3]; c = pms->abcd[2], // Third word of state
uint32_t t; d = pms->abcd[3]; // Fourth word of state
uint32_t t; // Temporary state
#ifndef ARCH_IS_BIG_ENDIAN
# define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */
#endif
#if ARCH_IS_BIG_ENDIAN #if ARCH_IS_BIG_ENDIAN
// On big-endian machines, we must arrange the bytes in the right
// order. (This also works on machines of unknown byte order.)
uint32_t X[16]; // Little-endian representation
const uint8_t *xp; // Pointer into data
int i; // Looping var
/* for (i = 0, xp = data; i < 16; i ++, xp += 4)
* On big-endian machines, we must arrange the bytes in the right X[i] = xp[0] + (unsigned)(xp[1] << 8) + (unsigned)(xp[2] << 16) + (unsigned)(xp[3] << 24);
* order. (This also works on machines of unknown byte order.)
*/
uint32_t X[16];
const uint8_t *xp = data;
int i;
for (i = 0; i < 16; ++i, xp += 4)
X[i] = xp[0] + (unsigned)(xp[1] << 8) + (unsigned)(xp[2] << 16) + (unsigned)(xp[3] << 24);
#else /* !ARCH_IS_BIG_ENDIAN */ #else /* !ARCH_IS_BIG_ENDIAN */
// On little-endian machines, we can process properly aligned data without copying it.
uint32_t xbuf[16]; // Aligned buffer
const uint32_t *X; // Pointer to little-endian representation
/* if (!((data - (const uint8_t *)0) & 3))
* On little-endian machines, we can process properly aligned data {
* without copying it. // data is properly aligned, use it directly...
*/ X = (const uint32_t *)data;
uint32_t xbuf[16]; }
const uint32_t *X; else
{
if (!((data - (const uint8_t *)0) & 3)) { // data is not aligned, copy to the aligned buffer...
/* data are properly aligned */ memcpy(xbuf, data, 64);
X = (const uint32_t *)data; X = xbuf;
} else { }
/* not aligned */ #endif // ARCH_IS_BIG_ENDIAN
memcpy(xbuf, data, 64);
X = xbuf;
}
#endif
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
/* Round 1. */ // Round 1.
/* Let [abcd k s i] denote the operation // Let [abcd k s i] denote the operation
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ // a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s).
#define F(x, y, z) (((x) & (y)) | (~(x) & (z))) #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET(a, b, c, d, k, s, Ti)\ #define SET(a, b, c, d, k, s, Ti) t = a + F(b,c,d) + X[k] + Ti; a = ROTATE_LEFT(t, s) + b
t = a + F(b,c,d) + X[k] + Ti;\
a = ROTATE_LEFT(t, s) + b // Do the following 16 operations.
/* Do the following 16 operations. */ SET(a, b, c, d, 0, 7, T1);
SET(a, b, c, d, 0, 7, T1); SET(d, a, b, c, 1, 12, T2);
SET(d, a, b, c, 1, 12, T2); SET(c, d, a, b, 2, 17, T3);
SET(c, d, a, b, 2, 17, T3); SET(b, c, d, a, 3, 22, T4);
SET(b, c, d, a, 3, 22, T4); SET(a, b, c, d, 4, 7, T5);
SET(a, b, c, d, 4, 7, T5); SET(d, a, b, c, 5, 12, T6);
SET(d, a, b, c, 5, 12, T6); SET(c, d, a, b, 6, 17, T7);
SET(c, d, a, b, 6, 17, T7); SET(b, c, d, a, 7, 22, T8);
SET(b, c, d, a, 7, 22, T8); SET(a, b, c, d, 8, 7, T9);
SET(a, b, c, d, 8, 7, T9); SET(d, a, b, c, 9, 12, T10);
SET(d, a, b, c, 9, 12, T10); SET(c, d, a, b, 10, 17, T11);
SET(c, d, a, b, 10, 17, T11); SET(b, c, d, a, 11, 22, T12);
SET(b, c, d, a, 11, 22, T12); SET(a, b, c, d, 12, 7, T13);
SET(a, b, c, d, 12, 7, T13); SET(d, a, b, c, 13, 12, T14);
SET(d, a, b, c, 13, 12, T14); SET(c, d, a, b, 14, 17, T15);
SET(c, d, a, b, 14, 17, T15); SET(b, c, d, a, 15, 22, T16);
SET(b, c, d, a, 15, 22, T16);
#undef SET #undef SET
/* Round 2. */ // Round 2.
/* Let [abcd k s i] denote the operation // Let [abcd k s i] denote the operation
a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ // a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s).
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET(a, b, c, d, k, s, Ti)\ #define SET(a, b, c, d, k, s, Ti) t = a + G(b,c,d) + X[k] + Ti; a = ROTATE_LEFT(t, s) + b
t = a + G(b,c,d) + X[k] + Ti;\
a = ROTATE_LEFT(t, s) + b // Do the following 16 operations.
/* Do the following 16 operations. */ SET(a, b, c, d, 1, 5, T17);
SET(a, b, c, d, 1, 5, T17); SET(d, a, b, c, 6, 9, T18);
SET(d, a, b, c, 6, 9, T18); SET(c, d, a, b, 11, 14, T19);
SET(c, d, a, b, 11, 14, T19); SET(b, c, d, a, 0, 20, T20);
SET(b, c, d, a, 0, 20, T20); SET(a, b, c, d, 5, 5, T21);
SET(a, b, c, d, 5, 5, T21); SET(d, a, b, c, 10, 9, T22);
SET(d, a, b, c, 10, 9, T22); SET(c, d, a, b, 15, 14, T23);
SET(c, d, a, b, 15, 14, T23); SET(b, c, d, a, 4, 20, T24);
SET(b, c, d, a, 4, 20, T24); SET(a, b, c, d, 9, 5, T25);
SET(a, b, c, d, 9, 5, T25); SET(d, a, b, c, 14, 9, T26);
SET(d, a, b, c, 14, 9, T26); SET(c, d, a, b, 3, 14, T27);
SET(c, d, a, b, 3, 14, T27); SET(b, c, d, a, 8, 20, T28);
SET(b, c, d, a, 8, 20, T28); SET(a, b, c, d, 13, 5, T29);
SET(a, b, c, d, 13, 5, T29); SET(d, a, b, c, 2, 9, T30);
SET(d, a, b, c, 2, 9, T30); SET(c, d, a, b, 7, 14, T31);
SET(c, d, a, b, 7, 14, T31); SET(b, c, d, a, 12, 20, T32);
SET(b, c, d, a, 12, 20, T32);
#undef SET #undef SET
/* Round 3. */ // Round 3.
/* Let [abcd k s t] denote the operation // Let [abcd k s t] denote the operation
a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ // a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s).
#define H(x, y, z) ((x) ^ (y) ^ (z)) #define H(x, y, z) ((x) ^ (y) ^ (z))
#define SET(a, b, c, d, k, s, Ti)\ #define SET(a, b, c, d, k, s, Ti) t = a + H(b,c,d) + X[k] + Ti; a = ROTATE_LEFT(t, s) + b
t = a + H(b,c,d) + X[k] + Ti;\
a = ROTATE_LEFT(t, s) + b // Do the following 16 operations.
/* Do the following 16 operations. */ SET(a, b, c, d, 5, 4, T33);
SET(a, b, c, d, 5, 4, T33); SET(d, a, b, c, 8, 11, T34);
SET(d, a, b, c, 8, 11, T34); SET(c, d, a, b, 11, 16, T35);
SET(c, d, a, b, 11, 16, T35); SET(b, c, d, a, 14, 23, T36);
SET(b, c, d, a, 14, 23, T36); SET(a, b, c, d, 1, 4, T37);
SET(a, b, c, d, 1, 4, T37); SET(d, a, b, c, 4, 11, T38);
SET(d, a, b, c, 4, 11, T38); SET(c, d, a, b, 7, 16, T39);
SET(c, d, a, b, 7, 16, T39); SET(b, c, d, a, 10, 23, T40);
SET(b, c, d, a, 10, 23, T40); SET(a, b, c, d, 13, 4, T41);
SET(a, b, c, d, 13, 4, T41); SET(d, a, b, c, 0, 11, T42);
SET(d, a, b, c, 0, 11, T42); SET(c, d, a, b, 3, 16, T43);
SET(c, d, a, b, 3, 16, T43); SET(b, c, d, a, 6, 23, T44);
SET(b, c, d, a, 6, 23, T44); SET(a, b, c, d, 9, 4, T45);
SET(a, b, c, d, 9, 4, T45); SET(d, a, b, c, 12, 11, T46);
SET(d, a, b, c, 12, 11, T46); SET(c, d, a, b, 15, 16, T47);
SET(c, d, a, b, 15, 16, T47); SET(b, c, d, a, 2, 23, T48);
SET(b, c, d, a, 2, 23, T48);
#undef SET #undef SET
/* Round 4. */ // Round 4.
/* Let [abcd k s t] denote the operation // Let [abcd k s t] denote the operation
a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ // a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s).
#define I(x, y, z) ((y) ^ ((x) | ~(z))) #define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define SET(a, b, c, d, k, s, Ti)\ #define SET(a, b, c, d, k, s, Ti) t = a + I(b,c,d) + X[k] + Ti; a = ROTATE_LEFT(t, s) + b
t = a + I(b,c,d) + X[k] + Ti;\
a = ROTATE_LEFT(t, s) + b // Do the following 16 operations.
/* Do the following 16 operations. */ SET(a, b, c, d, 0, 6, T49);
SET(a, b, c, d, 0, 6, T49); SET(d, a, b, c, 7, 10, T50);
SET(d, a, b, c, 7, 10, T50); SET(c, d, a, b, 14, 15, T51);
SET(c, d, a, b, 14, 15, T51); SET(b, c, d, a, 5, 21, T52);
SET(b, c, d, a, 5, 21, T52); SET(a, b, c, d, 12, 6, T53);
SET(a, b, c, d, 12, 6, T53); SET(d, a, b, c, 3, 10, T54);
SET(d, a, b, c, 3, 10, T54); SET(c, d, a, b, 10, 15, T55);
SET(c, d, a, b, 10, 15, T55); SET(b, c, d, a, 1, 21, T56);
SET(b, c, d, a, 1, 21, T56); SET(a, b, c, d, 8, 6, T57);
SET(a, b, c, d, 8, 6, T57); SET(d, a, b, c, 15, 10, T58);
SET(d, a, b, c, 15, 10, T58); SET(c, d, a, b, 6, 15, T59);
SET(c, d, a, b, 6, 15, T59); SET(b, c, d, a, 13, 21, T60);
SET(b, c, d, a, 13, 21, T60); SET(a, b, c, d, 4, 6, T61);
SET(a, b, c, d, 4, 6, T61); SET(d, a, b, c, 11, 10, T62);
SET(d, a, b, c, 11, 10, T62); SET(c, d, a, b, 2, 15, T63);
SET(c, d, a, b, 2, 15, T63); SET(b, c, d, a, 9, 21, T64);
SET(b, c, d, a, 9, 21, T64);
#undef SET #undef SET
/* Then perform the following additions. (That is increment each // Then perform the following additions. (That is increment each of the four
of the four registers by the value it had before this block // registers by the value it had before this block was started.)
was started.) */ pms->abcd[0] += a;
pms->abcd[0] += a; pms->abcd[1] += b;
pms->abcd[1] += b; pms->abcd[2] += c;
pms->abcd[2] += c; pms->abcd[3] += d;
pms->abcd[3] += d;
} }
//
// '_pdfioCryptoMD5Init()' - Initialize an MD5 hash.
//
void void
_pdfioCryptoMD5Init(_pdfio_md5_t *pms) _pdfioCryptoMD5Init(_pdfio_md5_t *pms) // I - MD5 state
{ {
pms->count[0] = pms->count[1] = 0; pms->count[0] = pms->count[1] = 0;
pms->abcd[0] = 0x67452301; pms->abcd[0] = 0x67452301;
pms->abcd[1] = 0xefcdab89; pms->abcd[1] = 0xefcdab89;
pms->abcd[2] = 0x98badcfe; pms->abcd[2] = 0x98badcfe;
pms->abcd[3] = 0x10325476; pms->abcd[3] = 0x10325476;
} }
//
// '_pdfioCryptoMD5Append()' - Append bytes to the MD5 hash.
//
void void
_pdfioCryptoMD5Append(_pdfio_md5_t *pms, const uint8_t *data, size_t nbytes) _pdfioCryptoMD5Append(
_pdfio_md5_t *pms, // I - MD5 state
const uint8_t *data, // I - Data to add
size_t nbytes) // I - Number of bytes
{ {
const uint8_t *p = data; const uint8_t *p = data; // Pointer into data
size_t left = nbytes; size_t left = nbytes; // Remaining bytes
size_t offset = (pms->count[0] >> 3) & 63; size_t offset = (pms->count[0] >> 3) & 63;
uint32_t nbits = (uint32_t)(nbytes << 3); // Offset into state
uint32_t nbits = (uint32_t)(nbytes << 3);
// Number of bits to add
if (nbytes == 0)
return;
/* Update the message length. */ if (nbytes == 0)
pms->count[1] += (unsigned)(nbytes >> 29); return;
pms->count[0] += nbits;
if (pms->count[0] < nbits)
pms->count[1]++;
/* Process an initial partial block. */ // Update the message length.
if (offset) { pms->count[1] += (unsigned)(nbytes >> 29);
size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); pms->count[0] += nbits;
if (pms->count[0] < nbits)
pms->count[1] ++;
memcpy(pms->buf + offset, p, copy); // Process an initial partial block.
if (offset + copy < 64) if (offset)
return; {
p += copy; size_t copy = ((offset + nbytes) > 64 ? 64 - offset : nbytes);
left -= copy; // Number of bytes to copy
md5_process(pms, pms->buf);
}
/* Process full blocks. */ memcpy(pms->buf + offset, p, copy);
for (; left >= 64; p += 64, left -= 64)
md5_process(pms, p);
/* Process a final partial block. */ if ((offset + copy) < 64)
if (left) return;
memcpy(pms->buf, p, left);
p += copy;
left -= copy;
md5_process(pms, pms->buf);
}
// Process full blocks.
for (; left >= 64; p += 64, left -= 64)
md5_process(pms, p);
// Copy a final partial block.
if (left)
memcpy(pms->buf, p, left);
} }
//
// '_pdfioCryptoMD5Finish()' - Finalize the MD5 hash.
//
void void
_pdfioCryptoMD5Finish(_pdfio_md5_t *pms, uint8_t digest[16]) _pdfioCryptoMD5Finish(
_pdfio_md5_t *pms, // I - MD5 state
uint8_t digest[16]) // O - Digest value
{ {
static const uint8_t pad[64] = { int i; // Looping var
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t data[8]; // Digest length data
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, static const uint8_t pad[64] = // Padding bytes
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 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
uint8_t data[8]; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
int i; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Save the length before padding. */
for (i = 0; i < 8; ++i) // Save the length before padding.
data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3)); for (i = 0; i < 8; ++i)
/* Pad to 56 bytes mod 64. */ data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3));
_pdfioCryptoMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
/* Append the length. */ // Pad to 56 bytes mod 64.
_pdfioCryptoMD5Append(pms, data, 8); _pdfioCryptoMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
for (i = 0; i < 16; ++i)
digest[i] = (uint8_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); // Append the length.
_pdfioCryptoMD5Append(pms, data, 8);
// Copy the digest from the state...
for (i = 0; i < 16; ++i)
digest[i] = (uint8_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
} }