3 * The MD5 hash function, described in RFC 1321.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 /* Based on public domain code hacked by Colin Plumb, Andrew Kuchling, and
41 md5_final(struct md5_ctx *ctx);
44 md5_init(struct md5_ctx *ctx)
46 ctx->digest[0] = 0x67452301;
47 ctx->digest[1] = 0xefcdab89;
48 ctx->digest[2] = 0x98badcfe;
49 ctx->digest[3] = 0x10325476;
51 ctx->count_l = ctx->count_h = 0;
55 #define MD5_INCR(ctx) ((ctx)->count_h += !++(ctx)->count_l)
58 md5_update(struct md5_ctx *ctx,
64 /* Try to fill partial block */
65 unsigned left = MD5_DATA_SIZE - ctx->index;
68 memcpy(ctx->block + ctx->index, data, length);
70 return; /* Finished */
74 memcpy(ctx->block + ctx->index, data, left);
76 _nettle_md5_compress(ctx->digest, ctx->block);
83 while (length >= MD5_DATA_SIZE)
85 _nettle_md5_compress(ctx->digest, data);
88 data += MD5_DATA_SIZE;
89 length -= MD5_DATA_SIZE;
91 if ((ctx->index = length)) /* This assignment is intended */
92 /* Buffer leftovers */
93 memcpy(ctx->block, data, length);
97 md5_digest(struct md5_ctx *ctx,
105 assert(length <= MD5_DIGEST_SIZE);
110 leftover = length % 4;
112 /* Little endian order */
113 for (i = 0; i < words; i++, digest += 4)
114 LE_WRITE_UINT32(digest, ctx->digest[i]);
121 assert(i < _MD5_DIGEST_LENGTH);
123 /* Still least significant byte first. */
124 for (word = ctx->digest[i], j = 0; j < leftover;
126 digest[j] = word & 0xff;
131 /* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
132 * pattern 1 0* (64-bit count of bits processed, LSB-first) */
135 md5_final(struct md5_ctx *ctx)
137 uint32_t bitcount_high;
138 uint32_t bitcount_low;
143 /* Set the first char of padding to 0x80. This is safe since there
144 * is always at least one byte free */
145 assert(i < MD5_DATA_SIZE);
146 ctx->block[i++] = 0x80;
148 if (i > (MD5_DATA_SIZE - 8))
150 /* No room for length in this block. Process it and
151 pad with another one */
152 memset(ctx->block + i, 0, MD5_DATA_SIZE - i);
154 _nettle_md5_compress(ctx->digest, ctx->block);
157 if (i < (MD5_DATA_SIZE - 8))
158 memset(ctx->block + i, 0, (MD5_DATA_SIZE - 8) - i);
160 /* There are 512 = 2^9 bits in one block
161 * Little-endian order => Least significant word first */
163 bitcount_low = (ctx->count_l << 9) | (ctx->index << 3);
164 bitcount_high = (ctx->count_h << 9) | (ctx->count_l >> 23);
165 LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), bitcount_low);
166 LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), bitcount_high);
168 _nettle_md5_compress(ctx->digest, ctx->block);