|
1 /* vim:set ts=2 sw=2 et cindent: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * "clean room" MD4 implementation (see RFC 1320) |
|
8 */ |
|
9 |
|
10 #include <string.h> |
|
11 #include "md4.h" |
|
12 |
|
13 typedef uint32_t Uint32; |
|
14 typedef uint8_t Uint8; |
|
15 |
|
16 /* the "conditional" function */ |
|
17 #define F(x,y,z) (((x) & (y)) | (~(x) & (z))) |
|
18 |
|
19 /* the "majority" function */ |
|
20 #define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
|
21 |
|
22 /* the "parity" function */ |
|
23 #define H(x,y,z) ((x) ^ (y) ^ (z)) |
|
24 |
|
25 /* rotate n-bits to the left */ |
|
26 #define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n))) |
|
27 |
|
28 /* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */ |
|
29 #define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s) |
|
30 |
|
31 /* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */ |
|
32 #define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s) |
|
33 |
|
34 /* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */ |
|
35 #define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s) |
|
36 |
|
37 /* converts from word array to byte array, len is number of bytes */ |
|
38 static void w2b(Uint8 *out, const Uint32 *in, Uint32 len) |
|
39 { |
|
40 Uint8 *bp; const Uint32 *wp, *wpend; |
|
41 |
|
42 bp = out; |
|
43 wp = in; |
|
44 wpend = wp + (len >> 2); |
|
45 |
|
46 for (; wp != wpend; ++wp, bp += 4) |
|
47 { |
|
48 bp[0] = (Uint8) ((*wp ) & 0xFF); |
|
49 bp[1] = (Uint8) ((*wp >> 8) & 0xFF); |
|
50 bp[2] = (Uint8) ((*wp >> 16) & 0xFF); |
|
51 bp[3] = (Uint8) ((*wp >> 24) & 0xFF); |
|
52 } |
|
53 } |
|
54 |
|
55 /* converts from byte array to word array, len is number of bytes */ |
|
56 static void b2w(Uint32 *out, const Uint8 *in, Uint32 len) |
|
57 { |
|
58 Uint32 *wp; const Uint8 *bp, *bpend; |
|
59 |
|
60 wp = out; |
|
61 bp = in; |
|
62 bpend = in + len; |
|
63 |
|
64 for (; bp != bpend; bp += 4, ++wp) |
|
65 { |
|
66 *wp = (Uint32) (bp[0] ) | |
|
67 (Uint32) (bp[1] << 8) | |
|
68 (Uint32) (bp[2] << 16) | |
|
69 (Uint32) (bp[3] << 24); |
|
70 } |
|
71 } |
|
72 |
|
73 /* update state: data is 64 bytes in length */ |
|
74 static void md4step(Uint32 state[4], const Uint8 *data) |
|
75 { |
|
76 Uint32 A, B, C, D, X[16]; |
|
77 |
|
78 b2w(X, data, 64); |
|
79 |
|
80 A = state[0]; |
|
81 B = state[1]; |
|
82 C = state[2]; |
|
83 D = state[3]; |
|
84 |
|
85 RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19); |
|
86 RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19); |
|
87 RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19); |
|
88 RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19); |
|
89 |
|
90 RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13); |
|
91 RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13); |
|
92 RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13); |
|
93 RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13); |
|
94 |
|
95 RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15); |
|
96 RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15); |
|
97 RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15); |
|
98 RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15); |
|
99 |
|
100 state[0] += A; |
|
101 state[1] += B; |
|
102 state[2] += C; |
|
103 state[3] += D; |
|
104 } |
|
105 |
|
106 void md4sum(const Uint8 *input, Uint32 inputLen, Uint8 *result) |
|
107 { |
|
108 Uint8 final[128]; |
|
109 Uint32 i, n, m, state[4]; |
|
110 |
|
111 /* magic initial states */ |
|
112 state[0] = 0x67452301; |
|
113 state[1] = 0xEFCDAB89; |
|
114 state[2] = 0x98BADCFE; |
|
115 state[3] = 0x10325476; |
|
116 |
|
117 /* compute number of complete 64-byte segments contained in input */ |
|
118 m = inputLen >> 6; |
|
119 |
|
120 /* digest first m segments */ |
|
121 for (i=0; i<m; ++i) |
|
122 md4step(state, (input + (i << 6))); |
|
123 |
|
124 /* build final buffer */ |
|
125 n = inputLen % 64; |
|
126 memcpy(final, input + (m << 6), n); |
|
127 final[n] = 0x80; |
|
128 memset(final + n + 1, 0, 120 - (n + 1)); |
|
129 |
|
130 inputLen = inputLen << 3; |
|
131 w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4); |
|
132 |
|
133 md4step(state, final); |
|
134 if (n >= 56) |
|
135 md4step(state, final + 64); |
|
136 |
|
137 /* copy state to result */ |
|
138 w2b(result, state, 16); |
|
139 } |