3 The MD4 hash function, described in RFC 1320.
5 Copyright (C) 2003 Niels Möller, Marcus Comstedt
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
34 /* Based on the public domain md5 code, and modified by Marcus
47 #include "nettle-write.h"
49 /* A block, treated as a sequence of 32-bit words. */
50 #define MD4_DATA_LENGTH 16
53 md4_transform(uint32_t *digest, const uint32_t *data);
56 md4_compress(struct md4_ctx *ctx, const uint8_t *block);
58 /* FIXME: Could be an alias for md5_init */
60 md4_init(struct md4_ctx *ctx)
62 /* Same constants as for md5. */
63 const uint32_t iv[_MD4_DIGEST_LENGTH] =
70 memcpy(ctx->state, iv, sizeof(ctx->state));
77 md4_update(struct md4_ctx *ctx,
81 MD_UPDATE(ctx, length, data, md4_compress, ctx->count++);
85 md4_digest(struct md4_ctx *ctx,
90 uint32_t data[MD4_DATA_LENGTH];
93 assert(length <= MD4_DIGEST_SIZE);
95 MD_PAD(ctx, 8, md4_compress);
96 for (i = 0; i < MD4_DATA_LENGTH - 2; i++)
97 data[i] = LE_READ_UINT32(ctx->block + 4*i);
99 /* There are 512 = 2^9 bits in one block
100 * Little-endian order => Least significant word first */
101 bit_count = (ctx->count << 9) | (ctx->index << 3);
102 data[MD4_DATA_LENGTH-2] = bit_count;
103 data[MD4_DATA_LENGTH-1] = bit_count >> 32;
104 md4_transform(ctx->state, data);
106 _nettle_write_le32(length, digest, ctx->state);
111 #define F(x, y, z) (((y) & (x)) | ((z) & ~(x)))
112 #define G(x, y, z) (((y) & (x)) | ((z) & (x)) | ((y) & (z)))
113 #define H(x, y, z) ((x) ^ (y) ^ (z))
115 #define ROUND(f, w, x, y, z, data, s) \
116 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s) )
118 /* Perform the MD4 transformation on one full block of 16 32-bit words. */
121 md4_transform(uint32_t *digest, const uint32_t *data)
129 ROUND(F, a, b, c, d, data[ 0], 3);
130 ROUND(F, d, a, b, c, data[ 1], 7);
131 ROUND(F, c, d, a, b, data[ 2], 11);
132 ROUND(F, b, c, d, a, data[ 3], 19);
133 ROUND(F, a, b, c, d, data[ 4], 3);
134 ROUND(F, d, a, b, c, data[ 5], 7);
135 ROUND(F, c, d, a, b, data[ 6], 11);
136 ROUND(F, b, c, d, a, data[ 7], 19);
137 ROUND(F, a, b, c, d, data[ 8], 3);
138 ROUND(F, d, a, b, c, data[ 9], 7);
139 ROUND(F, c, d, a, b, data[10], 11);
140 ROUND(F, b, c, d, a, data[11], 19);
141 ROUND(F, a, b, c, d, data[12], 3);
142 ROUND(F, d, a, b, c, data[13], 7);
143 ROUND(F, c, d, a, b, data[14], 11);
144 ROUND(F, b, c, d, a, data[15], 19);
146 ROUND(G, a, b, c, d, data[ 0] + 0x5a827999, 3);
147 ROUND(G, d, a, b, c, data[ 4] + 0x5a827999, 5);
148 ROUND(G, c, d, a, b, data[ 8] + 0x5a827999, 9);
149 ROUND(G, b, c, d, a, data[12] + 0x5a827999, 13);
150 ROUND(G, a, b, c, d, data[ 1] + 0x5a827999, 3);
151 ROUND(G, d, a, b, c, data[ 5] + 0x5a827999, 5);
152 ROUND(G, c, d, a, b, data[ 9] + 0x5a827999, 9);
153 ROUND(G, b, c, d, a, data[13] + 0x5a827999, 13);
154 ROUND(G, a, b, c, d, data[ 2] + 0x5a827999, 3);
155 ROUND(G, d, a, b, c, data[ 6] + 0x5a827999, 5);
156 ROUND(G, c, d, a, b, data[10] + 0x5a827999, 9);
157 ROUND(G, b, c, d, a, data[14] + 0x5a827999, 13);
158 ROUND(G, a, b, c, d, data[ 3] + 0x5a827999, 3);
159 ROUND(G, d, a, b, c, data[ 7] + 0x5a827999, 5);
160 ROUND(G, c, d, a, b, data[11] + 0x5a827999, 9);
161 ROUND(G, b, c, d, a, data[15] + 0x5a827999, 13);
163 ROUND(H, a, b, c, d, data[ 0] + 0x6ed9eba1, 3);
164 ROUND(H, d, a, b, c, data[ 8] + 0x6ed9eba1, 9);
165 ROUND(H, c, d, a, b, data[ 4] + 0x6ed9eba1, 11);
166 ROUND(H, b, c, d, a, data[12] + 0x6ed9eba1, 15);
167 ROUND(H, a, b, c, d, data[ 2] + 0x6ed9eba1, 3);
168 ROUND(H, d, a, b, c, data[10] + 0x6ed9eba1, 9);
169 ROUND(H, c, d, a, b, data[ 6] + 0x6ed9eba1, 11);
170 ROUND(H, b, c, d, a, data[14] + 0x6ed9eba1, 15);
171 ROUND(H, a, b, c, d, data[ 1] + 0x6ed9eba1, 3);
172 ROUND(H, d, a, b, c, data[ 9] + 0x6ed9eba1, 9);
173 ROUND(H, c, d, a, b, data[ 5] + 0x6ed9eba1, 11);
174 ROUND(H, b, c, d, a, data[13] + 0x6ed9eba1, 15);
175 ROUND(H, a, b, c, d, data[ 3] + 0x6ed9eba1, 3);
176 ROUND(H, d, a, b, c, data[11] + 0x6ed9eba1, 9);
177 ROUND(H, c, d, a, b, data[ 7] + 0x6ed9eba1, 11);
178 ROUND(H, b, c, d, a, data[15] + 0x6ed9eba1, 15);
187 md4_compress(struct md4_ctx *ctx, const uint8_t *block)
189 uint32_t data[MD4_DATA_LENGTH];
192 /* Endian independent conversion */
193 for (i = 0; i<16; i++, block += 4)
194 data[i] = LE_READ_UINT32(block);
196 md4_transform(ctx->state, data);