1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | * All rights reserved. |
3 | * |
4 | * This package is an SSL implementation written |
5 | * by Eric Young (eay@cryptsoft.com). |
6 | * The implementation was written so as to conform with Netscapes SSL. |
7 | * |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions |
10 | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | * included with this distribution is covered by the same copyright terms |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | * |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. |
17 | * If this package is used in a product, Eric Young should be given attribution |
18 | * as the author of the parts of the library used. |
19 | * This can be in the form of a textual message at program startup or |
20 | * in documentation (online or textual) provided with the package. |
21 | * |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions |
24 | * are met: |
25 | * 1. Redistributions of source code must retain the copyright |
26 | * notice, this list of conditions and the following disclaimer. |
27 | * 2. Redistributions in binary form must reproduce the above copyright |
28 | * notice, this list of conditions and the following disclaimer in the |
29 | * documentation and/or other materials provided with the distribution. |
30 | * 3. All advertising materials mentioning features or use of this software |
31 | * must display the following acknowledgement: |
32 | * "This product includes cryptographic software written by |
33 | * Eric Young (eay@cryptsoft.com)" |
34 | * The word 'cryptographic' can be left out if the rouines from the library |
35 | * being used are not cryptographic related :-). |
36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | * |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | * SUCH DAMAGE. |
51 | * |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | * copied and put under another distribution licence |
55 | * [including the GNU Public Licence.] */ |
56 | |
57 | #include <openssl/md4.h> |
58 | |
59 | #include <stdlib.h> |
60 | #include <string.h> |
61 | |
62 | #include "../../internal.h" |
63 | |
64 | |
65 | uint8_t *MD4(const uint8_t *data, size_t len, uint8_t out[MD4_DIGEST_LENGTH]) { |
66 | MD4_CTX ctx; |
67 | MD4_Init(&ctx); |
68 | MD4_Update(&ctx, data, len); |
69 | MD4_Final(out, &ctx); |
70 | |
71 | return out; |
72 | } |
73 | |
74 | // Implemented from RFC1186 The MD4 Message-Digest Algorithm. |
75 | |
76 | int MD4_Init(MD4_CTX *md4) { |
77 | OPENSSL_memset(md4, 0, sizeof(MD4_CTX)); |
78 | md4->h[0] = 0x67452301UL; |
79 | md4->h[1] = 0xefcdab89UL; |
80 | md4->h[2] = 0x98badcfeUL; |
81 | md4->h[3] = 0x10325476UL; |
82 | return 1; |
83 | } |
84 | |
85 | void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num); |
86 | |
87 | #define DATA_ORDER_IS_LITTLE_ENDIAN |
88 | |
89 | #define HASH_CTX MD4_CTX |
90 | #define HASH_CBLOCK 64 |
91 | #define HASH_DIGEST_LENGTH 16 |
92 | #define HASH_UPDATE MD4_Update |
93 | #define HASH_TRANSFORM MD4_Transform |
94 | #define HASH_FINAL MD4_Final |
95 | #define HASH_MAKE_STRING(c, s) \ |
96 | do { \ |
97 | uint32_t ll; \ |
98 | ll = (c)->h[0]; \ |
99 | HOST_l2c(ll, (s)); \ |
100 | ll = (c)->h[1]; \ |
101 | HOST_l2c(ll, (s)); \ |
102 | ll = (c)->h[2]; \ |
103 | HOST_l2c(ll, (s)); \ |
104 | ll = (c)->h[3]; \ |
105 | HOST_l2c(ll, (s)); \ |
106 | } while (0) |
107 | #define HASH_BLOCK_DATA_ORDER md4_block_data_order |
108 | |
109 | #include "../digest/md32_common.h" |
110 | |
111 | // As pointed out by Wei Dai <weidai@eskimo.com>, the above can be |
112 | // simplified to the code below. Wei attributes these optimizations |
113 | // to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. |
114 | #define F(b, c, d) ((((c) ^ (d)) & (b)) ^ (d)) |
115 | #define G(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) |
116 | #define H(b, c, d) ((b) ^ (c) ^ (d)) |
117 | |
118 | #define ROTATE(a, n) (((a) << (n)) | ((a) >> (32 - (n)))) |
119 | |
120 | #define R0(a, b, c, d, k, s, t) \ |
121 | do { \ |
122 | (a) += ((k) + (t) + F((b), (c), (d))); \ |
123 | (a) = ROTATE(a, s); \ |
124 | } while (0) |
125 | |
126 | #define R1(a, b, c, d, k, s, t) \ |
127 | do { \ |
128 | (a) += ((k) + (t) + G((b), (c), (d))); \ |
129 | (a) = ROTATE(a, s); \ |
130 | } while (0) |
131 | |
132 | #define R2(a, b, c, d, k, s, t) \ |
133 | do { \ |
134 | (a) += ((k) + (t) + H((b), (c), (d))); \ |
135 | (a) = ROTATE(a, s); \ |
136 | } while (0) |
137 | |
138 | void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num) { |
139 | uint32_t A, B, C, D, l; |
140 | uint32_t X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15; |
141 | |
142 | A = state[0]; |
143 | B = state[1]; |
144 | C = state[2]; |
145 | D = state[3]; |
146 | |
147 | for (; num--;) { |
148 | HOST_c2l(data, l); |
149 | X0 = l; |
150 | HOST_c2l(data, l); |
151 | X1 = l; |
152 | // Round 0 |
153 | R0(A, B, C, D, X0, 3, 0); |
154 | HOST_c2l(data, l); |
155 | X2 = l; |
156 | R0(D, A, B, C, X1, 7, 0); |
157 | HOST_c2l(data, l); |
158 | X3 = l; |
159 | R0(C, D, A, B, X2, 11, 0); |
160 | HOST_c2l(data, l); |
161 | X4 = l; |
162 | R0(B, C, D, A, X3, 19, 0); |
163 | HOST_c2l(data, l); |
164 | X5 = l; |
165 | R0(A, B, C, D, X4, 3, 0); |
166 | HOST_c2l(data, l); |
167 | X6 = l; |
168 | R0(D, A, B, C, X5, 7, 0); |
169 | HOST_c2l(data, l); |
170 | X7 = l; |
171 | R0(C, D, A, B, X6, 11, 0); |
172 | HOST_c2l(data, l); |
173 | X8 = l; |
174 | R0(B, C, D, A, X7, 19, 0); |
175 | HOST_c2l(data, l); |
176 | X9 = l; |
177 | R0(A, B, C, D, X8, 3, 0); |
178 | HOST_c2l(data, l); |
179 | X10 = l; |
180 | R0(D, A, B, C, X9, 7, 0); |
181 | HOST_c2l(data, l); |
182 | X11 = l; |
183 | R0(C, D, A, B, X10, 11, 0); |
184 | HOST_c2l(data, l); |
185 | X12 = l; |
186 | R0(B, C, D, A, X11, 19, 0); |
187 | HOST_c2l(data, l); |
188 | X13 = l; |
189 | R0(A, B, C, D, X12, 3, 0); |
190 | HOST_c2l(data, l); |
191 | X14 = l; |
192 | R0(D, A, B, C, X13, 7, 0); |
193 | HOST_c2l(data, l); |
194 | X15 = l; |
195 | R0(C, D, A, B, X14, 11, 0); |
196 | R0(B, C, D, A, X15, 19, 0); |
197 | // Round 1 |
198 | R1(A, B, C, D, X0, 3, 0x5A827999L); |
199 | R1(D, A, B, C, X4, 5, 0x5A827999L); |
200 | R1(C, D, A, B, X8, 9, 0x5A827999L); |
201 | R1(B, C, D, A, X12, 13, 0x5A827999L); |
202 | R1(A, B, C, D, X1, 3, 0x5A827999L); |
203 | R1(D, A, B, C, X5, 5, 0x5A827999L); |
204 | R1(C, D, A, B, X9, 9, 0x5A827999L); |
205 | R1(B, C, D, A, X13, 13, 0x5A827999L); |
206 | R1(A, B, C, D, X2, 3, 0x5A827999L); |
207 | R1(D, A, B, C, X6, 5, 0x5A827999L); |
208 | R1(C, D, A, B, X10, 9, 0x5A827999L); |
209 | R1(B, C, D, A, X14, 13, 0x5A827999L); |
210 | R1(A, B, C, D, X3, 3, 0x5A827999L); |
211 | R1(D, A, B, C, X7, 5, 0x5A827999L); |
212 | R1(C, D, A, B, X11, 9, 0x5A827999L); |
213 | R1(B, C, D, A, X15, 13, 0x5A827999L); |
214 | // Round 2 |
215 | R2(A, B, C, D, X0, 3, 0x6ED9EBA1L); |
216 | R2(D, A, B, C, X8, 9, 0x6ED9EBA1L); |
217 | R2(C, D, A, B, X4, 11, 0x6ED9EBA1L); |
218 | R2(B, C, D, A, X12, 15, 0x6ED9EBA1L); |
219 | R2(A, B, C, D, X2, 3, 0x6ED9EBA1L); |
220 | R2(D, A, B, C, X10, 9, 0x6ED9EBA1L); |
221 | R2(C, D, A, B, X6, 11, 0x6ED9EBA1L); |
222 | R2(B, C, D, A, X14, 15, 0x6ED9EBA1L); |
223 | R2(A, B, C, D, X1, 3, 0x6ED9EBA1L); |
224 | R2(D, A, B, C, X9, 9, 0x6ED9EBA1L); |
225 | R2(C, D, A, B, X5, 11, 0x6ED9EBA1L); |
226 | R2(B, C, D, A, X13, 15, 0x6ED9EBA1L); |
227 | R2(A, B, C, D, X3, 3, 0x6ED9EBA1L); |
228 | R2(D, A, B, C, X11, 9, 0x6ED9EBA1L); |
229 | R2(C, D, A, B, X7, 11, 0x6ED9EBA1L); |
230 | R2(B, C, D, A, X15, 15, 0x6ED9EBA1L); |
231 | |
232 | A = state[0] += A; |
233 | B = state[1] += B; |
234 | C = state[2] += C; |
235 | D = state[3] += D; |
236 | } |
237 | } |
238 | |
239 | #undef DATA_ORDER_IS_LITTLE_ENDIAN |
240 | #undef HASH_CTX |
241 | #undef HASH_CBLOCK |
242 | #undef HASH_DIGEST_LENGTH |
243 | #undef HASH_UPDATE |
244 | #undef HASH_TRANSFORM |
245 | #undef HASH_FINAL |
246 | #undef HASH_MAKE_STRING |
247 | #undef HASH_BLOCK_DATA_ORDER |
248 | #undef F |
249 | #undef G |
250 | #undef H |
251 | #undef ROTATE |
252 | #undef R0 |
253 | #undef R1 |
254 | #undef R2 |
255 | #undef HOST_c2l |
256 | #undef HOST_l2c |
257 | |