2 * Author: John Tsiombikas <nuclear@member.fsf.org>
4 * This software is public domain. Feel free to use it any way you like.
6 * If public domain is not applicable in your part of the world, you may use
7 * this under the terms of the Creative Commons CC-0 license:
8 * http://creativecommons.org/publicdomain/zero/1.0/
16 static const int shifttab[] = {
17 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
18 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
19 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
20 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
23 static const uint32_t sintab[] = {
24 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
25 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
26 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
27 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
28 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
29 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
30 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
31 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
32 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
33 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
34 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
35 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
36 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
37 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
38 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
39 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
42 static void sum_block(uint32_t *blk, uint32_t *sum);
43 static uint32_t rol(uint32_t x, int s);
45 void md5_begin(struct md5_state *md)
47 md->sum[0] = 0x67452301;
48 md->sum[1] = 0xefcdab89;
49 md->sum[2] = 0x98badcfe;
50 md->sum[3] = 0x10325476;
55 void md5_msg(struct md5_state *md, void *msg, int msg_size)
60 pending = BLOCKSZ - md->bblen;
61 sz = msg_size > pending ? pending : msg_size;
63 memcpy(md->blockbuf + md->bblen, msg, sz);
64 msg = (char*)msg + sz;
68 if(md->bblen >= BLOCKSZ) {
69 sum_block((uint32_t*)md->blockbuf, md->sum);
71 md->len += BLOCKSZ * 8;
76 static void sum_block(uint32_t *blk, uint32_t *sum)
87 f = d ^ (b & (c ^ d));
92 f = c ^ (d & (b ^ c));
93 g = (i * 5 + 1) & 0xf;
98 g = (i * 3 + 5) & 0xf;
106 f += a + sintab[i] + blk[g];
110 b += rol(f, shifttab[i]);
119 static uint32_t rol(uint32_t x, int s)
121 return (x << s) | (x >> (32 - s));
124 void md5_end(struct md5_state *md)
126 md->len += md->bblen * 8;
127 md->blockbuf[md->bblen++] = 0x80; /* append 1-bit plus 8 zeros padding */
129 if(md->bblen > BLOCKSZ - 8) {
130 /* pad to BLOCKSZ, sum, then continue with further padding */
131 memset(md->blockbuf + md->bblen, 0, BLOCKSZ - md->bblen);
132 sum_block((uint32_t*)md->blockbuf, md->sum);
136 /* pad to BLOCKSZ - 8 */
137 memset(md->blockbuf + md->bblen, 0, BLOCKSZ - 8 - md->bblen);
140 *(uint64_t*)(md->blockbuf + BLOCKSZ - 8) = md->len;
142 /* then sum for the last time */
143 sum_block((uint32_t*)md->blockbuf, md->sum);
146 const char *md5_sumstr(struct md5_state *md)
149 unsigned char *sum = (unsigned char*)md->sum;
150 char *s = (char*)md->blockbuf;
152 for(i=0; i<16; i++) {
153 s += sprintf(s, "%02x", sum[i]);
155 return (char*)md->blockbuf;