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,59 +108,75 @@
#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
* 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); 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...
*/
uint32_t xbuf[16];
const uint32_t *X;
if (!((data - (const uint8_t *)0) & 3)) {
/* data are properly aligned */
X = (const uint32_t *)data; X = (const uint32_t *)data;
} else { }
/* not aligned */ else
{
// data is not aligned, copy to the aligned buffer...
memcpy(xbuf, data, 64); memcpy(xbuf, data, 64);
X = xbuf; X = xbuf;
} }
#endif #endif // ARCH_IS_BIG_ENDIAN
#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);
@ -177,16 +193,16 @@ md5_process(_pdfio_md5_t *pms, const uint8_t *data /*[64]*/)
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);
@ -203,16 +219,16 @@ md5_process(_pdfio_md5_t *pms, const uint8_t *data /*[64]*/)
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);
@ -229,16 +245,16 @@ md5_process(_pdfio_md5_t *pms, const uint8_t *data /*[64]*/)
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);
@ -255,19 +271,24 @@ md5_process(_pdfio_md5_t *pms, const uint8_t *data /*[64]*/)
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;
@ -276,63 +297,96 @@ _pdfioCryptoMD5Init(_pdfio_md5_t *pms)
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;
// Offset into state
uint32_t nbits = (uint32_t)(nbytes << 3); uint32_t nbits = (uint32_t)(nbytes << 3);
// Number of bits to add
if (nbytes == 0) if (nbytes == 0)
return; return;
/* Update the message length. */ // Update the message length.
pms->count[1] += (unsigned)(nbytes >> 29); pms->count[1] += (unsigned)(nbytes >> 29);
pms->count[0] += nbits; pms->count[0] += nbits;
if (pms->count[0] < nbits) if (pms->count[0] < nbits)
pms->count[1]++; pms->count[1] ++;
/* Process an initial partial block. */ // Process an initial partial block.
if (offset) { if (offset)
size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); {
size_t copy = ((offset + nbytes) > 64 ? 64 - offset : nbytes);
// Number of bytes to copy
memcpy(pms->buf + offset, p, copy); memcpy(pms->buf + offset, p, copy);
if (offset + copy < 64)
if ((offset + copy) < 64)
return; return;
p += copy; p += copy;
left -= copy; left -= copy;
md5_process(pms, pms->buf); md5_process(pms, pms->buf);
} }
/* Process full blocks. */ // Process full blocks.
for (; left >= 64; p += 64, left -= 64) for (; left >= 64; p += 64, left -= 64)
md5_process(pms, p); md5_process(pms, p);
/* Process a final partial block. */ // Copy a final partial block.
if (left) if (left)
memcpy(pms->buf, p, left); memcpy(pms->buf, p, left);
} }
void
_pdfioCryptoMD5Finish(_pdfio_md5_t *pms, uint8_t digest[16])
{
static const uint8_t pad[64] = {
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
};
uint8_t data[8];
int i;
/* Save the length before padding. */ //
// '_pdfioCryptoMD5Finish()' - Finalize the MD5 hash.
//
void
_pdfioCryptoMD5Finish(
_pdfio_md5_t *pms, // I - MD5 state
uint8_t digest[16]) // O - Digest value
{
int i; // Looping var
uint8_t data[8]; // Digest length data
static const uint8_t pad[64] = // Padding bytes
{
0x80, 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, 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) for (i = 0; i < 8; ++i)
data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3)); data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3));
/* Pad to 56 bytes mod 64. */
// Pad to 56 bytes mod 64.
_pdfioCryptoMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); _pdfioCryptoMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
/* Append the length. */
// Append the length.
_pdfioCryptoMD5Append(pms, data, 8); _pdfioCryptoMD5Append(pms, data, 8);
// Copy the digest from the state...
for (i = 0; i < 16; ++i) for (i = 0; i < 16; ++i)
digest[i] = (uint8_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); digest[i] = (uint8_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
} }