masqmail-0.2

diff src/md5/md5.c @ 162:52c82d755215

replaced the MD5 implementation with the one of Solar Designer Until now, the sample code of RFC 1321 was used. It had an ugly license. Now we use the implementation of Solar Designer, which is in the Public Domain. http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
author meillo@marmaro.de
date Sun, 18 Jul 2010 22:01:04 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/md5/md5.c	Sun Jul 18 22:01:04 2010 +0200
     1.3 @@ -0,0 +1,275 @@
     1.4 +/*
     1.5 + * This is an OpenSSL-compatible implementation of the RSA Data Security,
     1.6 + * Inc. MD5 Message-Digest Algorithm (RFC 1321).
     1.7 + *
     1.8 + * Written by Solar Designer <solar at openwall.com> in 2001, and placed
     1.9 + * in the public domain.  There's absolutely no warranty.
    1.10 + *
    1.11 + * This differs from Colin Plumb's older public domain implementation in
    1.12 + * that no 32-bit integer data type is required, there's no compile-time
    1.13 + * endianness configuration, and the function prototypes match OpenSSL's.
    1.14 + * The primary goals are portability and ease of use.
    1.15 + *
    1.16 + * This implementation is meant to be fast, but not as fast as possible.
    1.17 + * Some known optimizations are not included to reduce source code size
    1.18 + * and avoid compile-time configuration.
    1.19 + */
    1.20 +
    1.21 +#ifndef HAVE_OPENSSL
    1.22 +
    1.23 +#include <string.h>
    1.24 +
    1.25 +#include "md5.h"
    1.26 +
    1.27 +/*
    1.28 + * The basic MD5 functions.
    1.29 + *
    1.30 + * F and G are optimized compared to their RFC 1321 definitions for
    1.31 + * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
    1.32 + * implementation.
    1.33 + */
    1.34 +#define F(x, y, z)			((z) ^ ((x) & ((y) ^ (z))))
    1.35 +#define G(x, y, z)			((y) ^ ((z) & ((x) ^ (y))))
    1.36 +#define H(x, y, z)			((x) ^ (y) ^ (z))
    1.37 +#define I(x, y, z)			((y) ^ ((x) | ~(z)))
    1.38 +
    1.39 +/*
    1.40 + * The MD5 transformation for all four rounds.
    1.41 + */
    1.42 +#define STEP(f, a, b, c, d, x, t, s) \
    1.43 +	(a) += f((b), (c), (d)) + (x) + (t); \
    1.44 +	(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
    1.45 +	(a) += (b);
    1.46 +
    1.47 +/*
    1.48 + * SET reads 4 input bytes in little-endian byte order and stores them
    1.49 + * in a properly aligned word in host byte order.
    1.50 + *
    1.51 + * The check for little-endian architectures that tolerate unaligned
    1.52 + * memory accesses is just an optimization.  Nothing will break if it
    1.53 + * doesn't work.
    1.54 + */
    1.55 +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
    1.56 +#define SET(n) \
    1.57 +	(*(MD5_u32plus *)&ptr[(n) * 4])
    1.58 +#define GET(n) \
    1.59 +	SET(n)
    1.60 +#else
    1.61 +#define SET(n) \
    1.62 +	(ctx->block[(n)] = \
    1.63 +	(MD5_u32plus)ptr[(n) * 4] | \
    1.64 +	((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
    1.65 +	((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
    1.66 +	((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
    1.67 +#define GET(n) \
    1.68 +	(ctx->block[(n)])
    1.69 +#endif
    1.70 +
    1.71 +/*
    1.72 + * This processes one or more 64-byte data blocks, but does NOT update
    1.73 + * the bit counters.  There are no alignment requirements.
    1.74 + */
    1.75 +static void *body(MD5_CTX *ctx, void *data, unsigned long size)
    1.76 +{
    1.77 +	unsigned char *ptr;
    1.78 +	MD5_u32plus a, b, c, d;
    1.79 +	MD5_u32plus saved_a, saved_b, saved_c, saved_d;
    1.80 +
    1.81 +	ptr = data;
    1.82 +
    1.83 +	a = ctx->a;
    1.84 +	b = ctx->b;
    1.85 +	c = ctx->c;
    1.86 +	d = ctx->d;
    1.87 +
    1.88 +	do {
    1.89 +		saved_a = a;
    1.90 +		saved_b = b;
    1.91 +		saved_c = c;
    1.92 +		saved_d = d;
    1.93 +
    1.94 +/* Round 1 */
    1.95 +		STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
    1.96 +		STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
    1.97 +		STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
    1.98 +		STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
    1.99 +		STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
   1.100 +		STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
   1.101 +		STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
   1.102 +		STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
   1.103 +		STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
   1.104 +		STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
   1.105 +		STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
   1.106 +		STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
   1.107 +		STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
   1.108 +		STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
   1.109 +		STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
   1.110 +		STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
   1.111 +
   1.112 +/* Round 2 */
   1.113 +		STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
   1.114 +		STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
   1.115 +		STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
   1.116 +		STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
   1.117 +		STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
   1.118 +		STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
   1.119 +		STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
   1.120 +		STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
   1.121 +		STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
   1.122 +		STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
   1.123 +		STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
   1.124 +		STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
   1.125 +		STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
   1.126 +		STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
   1.127 +		STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
   1.128 +		STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
   1.129 +
   1.130 +/* Round 3 */
   1.131 +		STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
   1.132 +		STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
   1.133 +		STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
   1.134 +		STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
   1.135 +		STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
   1.136 +		STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
   1.137 +		STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
   1.138 +		STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
   1.139 +		STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
   1.140 +		STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
   1.141 +		STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
   1.142 +		STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
   1.143 +		STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
   1.144 +		STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
   1.145 +		STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
   1.146 +		STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
   1.147 +
   1.148 +/* Round 4 */
   1.149 +		STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
   1.150 +		STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
   1.151 +		STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
   1.152 +		STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
   1.153 +		STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
   1.154 +		STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
   1.155 +		STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
   1.156 +		STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
   1.157 +		STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
   1.158 +		STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
   1.159 +		STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
   1.160 +		STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
   1.161 +		STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
   1.162 +		STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
   1.163 +		STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
   1.164 +		STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
   1.165 +
   1.166 +		a += saved_a;
   1.167 +		b += saved_b;
   1.168 +		c += saved_c;
   1.169 +		d += saved_d;
   1.170 +
   1.171 +		ptr += 64;
   1.172 +	} while (size -= 64);
   1.173 +
   1.174 +	ctx->a = a;
   1.175 +	ctx->b = b;
   1.176 +	ctx->c = c;
   1.177 +	ctx->d = d;
   1.178 +
   1.179 +	return ptr;
   1.180 +}
   1.181 +
   1.182 +void MD5_Init(MD5_CTX *ctx)
   1.183 +{
   1.184 +	ctx->a = 0x67452301;
   1.185 +	ctx->b = 0xefcdab89;
   1.186 +	ctx->c = 0x98badcfe;
   1.187 +	ctx->d = 0x10325476;
   1.188 +
   1.189 +	ctx->lo = 0;
   1.190 +	ctx->hi = 0;
   1.191 +}
   1.192 +
   1.193 +void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
   1.194 +{
   1.195 +	MD5_u32plus saved_lo;
   1.196 +	unsigned long used, free;
   1.197 +
   1.198 +	saved_lo = ctx->lo;
   1.199 +	if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
   1.200 +		ctx->hi++;
   1.201 +	ctx->hi += size >> 29;
   1.202 +
   1.203 +	used = saved_lo & 0x3f;
   1.204 +
   1.205 +	if (used) {
   1.206 +		free = 64 - used;
   1.207 +
   1.208 +		if (size < free) {
   1.209 +			memcpy(&ctx->buffer[used], data, size);
   1.210 +			return;
   1.211 +		}
   1.212 +
   1.213 +		memcpy(&ctx->buffer[used], data, free);
   1.214 +		data = (unsigned char *)data + free;
   1.215 +		size -= free;
   1.216 +		body(ctx, ctx->buffer, 64);
   1.217 +	}
   1.218 +
   1.219 +	if (size >= 64) {
   1.220 +		data = body(ctx, data, size & ~(unsigned long)0x3f);
   1.221 +		size &= 0x3f;
   1.222 +	}
   1.223 +
   1.224 +	memcpy(ctx->buffer, data, size);
   1.225 +}
   1.226 +
   1.227 +void MD5_Final(unsigned char *result, MD5_CTX *ctx)
   1.228 +{
   1.229 +	unsigned long used, free;
   1.230 +
   1.231 +	used = ctx->lo & 0x3f;
   1.232 +
   1.233 +	ctx->buffer[used++] = 0x80;
   1.234 +
   1.235 +	free = 64 - used;
   1.236 +
   1.237 +	if (free < 8) {
   1.238 +		memset(&ctx->buffer[used], 0, free);
   1.239 +		body(ctx, ctx->buffer, 64);
   1.240 +		used = 0;
   1.241 +		free = 64;
   1.242 +	}
   1.243 +
   1.244 +	memset(&ctx->buffer[used], 0, free - 8);
   1.245 +
   1.246 +	ctx->lo <<= 3;
   1.247 +	ctx->buffer[56] = ctx->lo;
   1.248 +	ctx->buffer[57] = ctx->lo >> 8;
   1.249 +	ctx->buffer[58] = ctx->lo >> 16;
   1.250 +	ctx->buffer[59] = ctx->lo >> 24;
   1.251 +	ctx->buffer[60] = ctx->hi;
   1.252 +	ctx->buffer[61] = ctx->hi >> 8;
   1.253 +	ctx->buffer[62] = ctx->hi >> 16;
   1.254 +	ctx->buffer[63] = ctx->hi >> 24;
   1.255 +
   1.256 +	body(ctx, ctx->buffer, 64);
   1.257 +
   1.258 +	result[0] = ctx->a;
   1.259 +	result[1] = ctx->a >> 8;
   1.260 +	result[2] = ctx->a >> 16;
   1.261 +	result[3] = ctx->a >> 24;
   1.262 +	result[4] = ctx->b;
   1.263 +	result[5] = ctx->b >> 8;
   1.264 +	result[6] = ctx->b >> 16;
   1.265 +	result[7] = ctx->b >> 24;
   1.266 +	result[8] = ctx->c;
   1.267 +	result[9] = ctx->c >> 8;
   1.268 +	result[10] = ctx->c >> 16;
   1.269 +	result[11] = ctx->c >> 24;
   1.270 +	result[12] = ctx->d;
   1.271 +	result[13] = ctx->d >> 8;
   1.272 +	result[14] = ctx->d >> 16;
   1.273 +	result[15] = ctx->d >> 24;
   1.274 +
   1.275 +	memset(ctx, 0, sizeof(*ctx));
   1.276 +}
   1.277 +
   1.278 +#endif