MD4 算法描述
我们首先假设输入是一条 b 位的消息, 并且我们希望求出它的消息摘要. 这里 b 是任意非负整数; b 可以为零, 不必是 8 的倍数, 并且可以任意大. 我们设想把消息的各个位写下来如下:
m_0 m_1 ... m_{b-1} .
为计算该消息的消息摘要, 要执行以下五个步骤.
第 1 步. 追加填充位
消息被 "填充" (扩展), 使其长度 (以位计) 模 512 与 448 同余. 也就是说, 消息被扩展为比 512 位的整数倍少 64 位. 填充总是要执行, 即使消息的长度模 512 已经与 448 同余 (此时要添加 512 位的填充).
填充按如下方式进行: 在消息后追加一个 "1" 位, 然后追加足够多的零位, 使得填充后消息的位数模 512 与 448 同余.
第 2 步. 追加长度
把 b (添加填充位之前消息的长度) 的 64 位表示追加到前一步结果之后. 在 b 大于 2^64 的不太可能的情况下, 只使用 b 的低 64 位. (这些位作为两个 32 位字追加, 并按照前述约定先追加低位字.)
此时得到的消息 (在用位填充并用 b 填充之后) 的长度恰好是 512 位的整数倍. 等价地, 该消息的长度恰好是 16 个 (32 位) 字的整数倍. 令 M[0 ... N-1] 表示所得消息的各个字, 其中 N 是 16 的倍数.
第 3 步. 初始化 MD 缓冲区
用一个 4 字缓冲区 (A,B,C,D) 来计算消息摘要. 这里 A,B,C,D 中的每一个都是 32 位寄存器. 这些寄存器按十六进制 (低位字节在前) 初始化为以下值):
word A: 01 23 45 67
word B: 89 ab cd ef
word C: fe dc ba 98
word D: 76 54 32 10
第 4 步. 以 16 字块处理消息
我们首先定义三个辅助函数, 每个函数都以三个 32 位字作为输入, 并产生一个 32 位字作为输出.
f(X,Y,Z) = XY v not(X)Z
g(X,Y,Z) = XY v XZ v YZ
h(X,Y,Z) = X xor Y xor Z
在每个位位置上, f 起条件选择的作用: 若 x 则 y, 否则 z. (函数 f 本可以用 + 而不是 v 来定义, 因为 XY 与 not(X)Z 永远不会在同一位置上同时为 1.) 在每个位位置上, g 起多数函数的作用: 若 x, y, z 中至少有两个为 1, 则 g 在该位位置上为 1, 否则 g 为零. 有趣的是, 若 X, Y, Z 的各位相互独立且无偏, 则 f(X,Y,Z) 的每一位都将独立且无偏, 类似地, g(X,Y,Z) 的每一位也都将独立且无偏. 函数 h 是按位的 "xor" 或 "奇偶校验" 函数; 它具有与 f 和 g 类似的性质.
执行以下操作:
For i = 0 to N/16-1 do /* process each 16-word block */
For j = 0 to 15 do: /* copy block i into X */
Set X[j] to M[i*16+j].
end /* of loop on j */
Save A as AA, B as BB, C as CC, and D as DD.
[Round 1]
Let [A B C D i s] denote the operation
A = (A + f(B,C,D) + X[i]) <<< s .
Do the following 16 operations:
[A B C D 0 3]
[D A B C 1 7]
[C D A B 2 11]
[B C D A 3 19]
[A B C D 4 3]
[D A B C 5 7]
[C D A B 6 11]
[B C D A 7 19]
[A B C D 8 3]
[D A B C 9 7]
[C D A B 10 11]
[B C D A 11 19]
[A B C D 12 3]
[D A B C 13 7]
[C D A B 14 11]
[B C D A 15 19]
[Round 2]
Let [A B C D i s] denote the operation
A = (A + g(B,C,D) + X[i] + 5A827999) <<< s .
(The value 5A..99 is a hexadecimal 32-bit
constant, written with the high-order digit
first. This constant represents the square
root of 2. The octal value of this constant
is 013240474631. See Knuth, The Art of
Programming, Volume 2 (Seminumerical
Algorithms), Second Edition (1981),
Addison-Wesley. Table 2, page 660.)
Do the following 16 operations:
[A B C D 0 3]
[D A B C 4 5]
[C D A B 8 9]
[B C D A 12 13]
[A B C D 1 3]
[D A B C 5 5]
[C D A B 9 9]
[B C D A 13 13]
[A B C D 2 3]
[D A B C 6 5]
[C D A B 10 9]
[B C D A 14 13]
[A B C D 3 3]
[D A B C 7 5]
[C D A B 11 9]
[B C D A 15 13]
[Round 3]
Let [A B C D i s] denote the operation
A = (A + h(B,C,D) + X[i] + 6ED9EBA1) <<< s .
(The value 6E..A1 is a hexadecimal 32-bit
constant, written with the high-order digit
first. This constant represents the square
root of 3. The octal value of this constant
is 015666365641. See Knuth, The Art of
Programming, Volume 2 (Seminumerical
Algorithms), Second Edition (1981),
Addison-Wesley. Table 2, page 660.)
Do the following 16 operations:
[A B C D 0 3]
[D A B C 8 9]
[C D A B 4 11]
[B C D A 12 15]
[A B C D 2 3]
[D A B C 10 9]
[C D A B 6 11]
[B C D A 14 15]
[A B C D 1 3]
[D A B C 9 9]
[C D A B 5 11]
[B C D A 13 15]
[A B C D 3 3]
[D A B C 11 9]
[C D A B 7 11]
[B C D A 15 15]
Then perform the following additions:
A = A + AA
B = B + BB
C = C + CC
D = D + DD
(That is, each of the four registers is incremented by
the value it had before this block was started.)
end /* of loop on i */
第 5 步. 输出
作为输出产生的消息摘要是 A,B,C,D. 也就是说, 我们从 A 的低位字节开始, 到 D 的高位字节结束.
MD4 的描述到此结束. 附录中给出了一个 C 语言参考实现.