sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

md5.c (3857B)


      1 /* public domain md5 implementation based on rfc1321 and libtomcrypt */
      2 #include <stdint.h>
      3 #include <string.h>
      4 
      5 #include "../md5.h"
      6 
      7 static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
      8 #define F(x,y,z) (z ^ (x & (y ^ z)))
      9 #define G(x,y,z) (y ^ (z & (y ^ x)))
     10 #define H(x,y,z) (x ^ y ^ z)
     11 #define I(x,y,z) (y ^ (x | ~z))
     12 #define FF(a,b,c,d,w,s,t) a += F(b,c,d) + w + t; a = rol(a,s) + b
     13 #define GG(a,b,c,d,w,s,t) a += G(b,c,d) + w + t; a = rol(a,s) + b
     14 #define HH(a,b,c,d,w,s,t) a += H(b,c,d) + w + t; a = rol(a,s) + b
     15 #define II(a,b,c,d,w,s,t) a += I(b,c,d) + w + t; a = rol(a,s) + b
     16 
     17 static const uint32_t tab[64] = {
     18 	0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
     19 	0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
     20 	0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
     21 	0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
     22 	0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
     23 	0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
     24 	0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
     25 	0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
     26 };
     27 
     28 static void
     29 processblock(struct md5 *s, const uint8_t *buf)
     30 {
     31 	uint32_t i, W[16], a, b, c, d;
     32 
     33 	for (i = 0; i < 16; i++) {
     34 		W[i] = buf[4*i];
     35 		W[i] |= (uint32_t)buf[4*i+1]<<8;
     36 		W[i] |= (uint32_t)buf[4*i+2]<<16;
     37 		W[i] |= (uint32_t)buf[4*i+3]<<24;
     38 	}
     39 
     40 	a = s->h[0];
     41 	b = s->h[1];
     42 	c = s->h[2];
     43 	d = s->h[3];
     44 
     45 	i = 0;
     46 	while (i < 16) {
     47 		FF(a,b,c,d, W[i],  7, tab[i]); i++;
     48 		FF(d,a,b,c, W[i], 12, tab[i]); i++;
     49 		FF(c,d,a,b, W[i], 17, tab[i]); i++;
     50 		FF(b,c,d,a, W[i], 22, tab[i]); i++;
     51 	}
     52 	while (i < 32) {
     53 		GG(a,b,c,d, W[(5*i+1)%16],  5, tab[i]); i++;
     54 		GG(d,a,b,c, W[(5*i+1)%16],  9, tab[i]); i++;
     55 		GG(c,d,a,b, W[(5*i+1)%16], 14, tab[i]); i++;
     56 		GG(b,c,d,a, W[(5*i+1)%16], 20, tab[i]); i++;
     57 	}
     58 	while (i < 48) {
     59 		HH(a,b,c,d, W[(3*i+5)%16],  4, tab[i]); i++;
     60 		HH(d,a,b,c, W[(3*i+5)%16], 11, tab[i]); i++;
     61 		HH(c,d,a,b, W[(3*i+5)%16], 16, tab[i]); i++;
     62 		HH(b,c,d,a, W[(3*i+5)%16], 23, tab[i]); i++;
     63 	}
     64 	while (i < 64) {
     65 		II(a,b,c,d, W[7*i%16],  6, tab[i]); i++;
     66 		II(d,a,b,c, W[7*i%16], 10, tab[i]); i++;
     67 		II(c,d,a,b, W[7*i%16], 15, tab[i]); i++;
     68 		II(b,c,d,a, W[7*i%16], 21, tab[i]); i++;
     69 	}
     70 
     71 	s->h[0] += a;
     72 	s->h[1] += b;
     73 	s->h[2] += c;
     74 	s->h[3] += d;
     75 }
     76 
     77 static void
     78 pad(struct md5 *s)
     79 {
     80 	unsigned r = s->len % 64;
     81 
     82 	s->buf[r++] = 0x80;
     83 	if (r > 56) {
     84 		memset(s->buf + r, 0, 64 - r);
     85 		r = 0;
     86 		processblock(s, s->buf);
     87 	}
     88 	memset(s->buf + r, 0, 56 - r);
     89 	s->len *= 8;
     90 	s->buf[56] = s->len;
     91 	s->buf[57] = s->len >> 8;
     92 	s->buf[58] = s->len >> 16;
     93 	s->buf[59] = s->len >> 24;
     94 	s->buf[60] = s->len >> 32;
     95 	s->buf[61] = s->len >> 40;
     96 	s->buf[62] = s->len >> 48;
     97 	s->buf[63] = s->len >> 56;
     98 	processblock(s, s->buf);
     99 }
    100 
    101 void
    102 md5_init(void *ctx)
    103 {
    104 	struct md5 *s = ctx;
    105 	s->len = 0;
    106 	s->h[0] = 0x67452301;
    107 	s->h[1] = 0xefcdab89;
    108 	s->h[2] = 0x98badcfe;
    109 	s->h[3] = 0x10325476;
    110 }
    111 
    112 void
    113 md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH])
    114 {
    115 	struct md5 *s = ctx;
    116 	int i;
    117 
    118 	pad(s);
    119 	for (i = 0; i < 4; i++) {
    120 		md[4*i] = s->h[i];
    121 		md[4*i+1] = s->h[i] >> 8;
    122 		md[4*i+2] = s->h[i] >> 16;
    123 		md[4*i+3] = s->h[i] >> 24;
    124 	}
    125 }
    126 
    127 void
    128 md5_update(void *ctx, const void *m, unsigned long len)
    129 {
    130 	struct md5 *s = ctx;
    131 	const uint8_t *p = m;
    132 	unsigned r = s->len % 64;
    133 
    134 	s->len += len;
    135 	if (r) {
    136 		if (len < 64 - r) {
    137 			memcpy(s->buf + r, p, len);
    138 			return;
    139 		}
    140 		memcpy(s->buf + r, p, 64 - r);
    141 		len -= 64 - r;
    142 		p += 64 - r;
    143 		processblock(s, s->buf);
    144 	}
    145 	for (; len >= 64; len -= 64, p += 64)
    146 		processblock(s, p);
    147 	memcpy(s->buf, p, len);
    148 }