Source:NetHack 5.0.0/src/nhmd4.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to nhmd4.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/nhmd4.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	nhmd4.c	$NHDT-Date: 1708811387 2024/02/24 21:49:47 $	$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.0 $ */
2. /*-Copyright (c) Kenneth Lorber, Kensington, Maryland, 2024 */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. /*
6.  * Usage is to try to match traceback data with the instance of the
7.  * program which produced that, not for security related purposes.
8.  *
9.  * Derived from:
10.  */
11. /*
12.  * MD4 (RFC-1320) message digest.
13.  * Modified from MD5 code by Andrey Panin <pazke@donpac.ru>
14.  *
15.  * Written by Solar Designer <solar@openwall.com> in 2001, and placed in
16.  * the public domain.  There's absolutely no warranty.
17.  *
18.  * This differs from Colin Plumb's older public domain implementation in
19.  * that no 32-bit integer data type is required, there's no compile-time
20.  * endianness configuration, and the function prototypes match OpenSSL's.
21.  * The primary goals are portability and ease of use.
22.  *
23.  * This implementation is meant to be fast, but not as fast as possible.
24.  * Some known optimizations are not included to reduce source code size
25.  * and avoid compile-time configuration.
26.  */
27. #include "hack.h"
28. #ifdef CRASHREPORT
29. 
30. #include "nhmd4.h"
31. 
32. staticfn const unsigned char *nhmd4_body(struct nhmd4_context *,
33.                                  const unsigned char *, size_t);
34. 
35. /* Avoid a conflict from a Lua header */
36. #ifdef G
37. #undef G
38. #endif
39. 
40. /*
41.  * The basic MD4 functions.
42.  */
43. #define F(x, y, z)      ((z) ^ ((x) & ((y) ^ (z))))
44. #define G(x, y, z)      (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
45. #define H(x, y, z)      ((x) ^ (y) ^ (z))
46. 
47. /*
48.  * STEP: the MD4 transformation used for all four rounds.
49.  * (Joining two expressions with the comma operator provides a sequence
50.  * point.  C89/C90 and later guarantee that the first will be fully complete
51.  * before the second starts, making both assignments to 'a' be well defined.)
52.  */
53. #define STEP(f, a, b, c, d, x, s) \
54.     (((a) += f((b), (c), (d)) + (x)),                   \
55.      ((a) = ((a) << (s)) | ((a) >> (32 - (s)))))
56. 
57. /*
58.  * SET reads 4 input bytes in little-endian byte order and stores them
59.  * in a properly aligned word in host byte order.
60.  *
61.  * The check for little-endian architectures which tolerate unaligned
62.  * memory accesses is just an optimization.  Nothing will break if it
63.  * doesn't work.
64.  */
65. #if defined(__i386__) || defined(__x86_64__)
66. #define SET(n) (*(const quint32 *) &ptr[(n) * 4])
67. #define GET(n) SET(n)
68. #else
69. #define SET(n) \
70.     (ctx->block[(n)] = \
71.      ((quint32) ptr[(n) * 4]                          \
72.       | ((quint32) ptr[(n) * 4 + 1] << 8)             \
73.       | ((quint32) ptr[(n) * 4 + 2] << 16)            \
74.       | ((quint32) ptr[(n) * 4 + 3] << 24)))
75. #define GET(n) (ctx->block[(n)])
76. #endif
77. 
78. /*
79.  * This processes one or more 64-byte data blocks, but does NOT update
80.  * the bit counters.  There're no alignment requirements.
81.  */
82. staticfn const unsigned char *
83. nhmd4_body(
84.     struct nhmd4_context *ctx,
85.     const unsigned char *data,
86.     size_t size)
87. {
88.     const unsigned char *ptr;
89.     quint32 a, b, c, d;
90.     quint32 saved_a, saved_b, saved_c, saved_d;
91. 
92.     ptr = data;
93. 
94.     a = ctx->a;
95.     b = ctx->b;
96.     c = ctx->c;
97.     d = ctx->d;
98. 
99.     do {
100.         saved_a = a;
101.         saved_b = b;
102.         saved_c = c;
103.         saved_d = d;
104. 
105. /* Round 1 */
106.         STEP(F, a, b, c, d, SET( 0),  3);
107.         STEP(F, d, a, b, c, SET( 1),  7);
108.         STEP(F, c, d, a, b, SET( 2), 11);
109.         STEP(F, b, c, d, a, SET( 3), 19);
110. 
111.         STEP(F, a, b, c, d, SET( 4),  3);
112.         STEP(F, d, a, b, c, SET( 5),  7);
113.         STEP(F, c, d, a, b, SET( 6), 11);
114.         STEP(F, b, c, d, a, SET( 7), 19);
115. 
116.         STEP(F, a, b, c, d, SET( 8),  3);
117.         STEP(F, d, a, b, c, SET( 9),  7);
118.         STEP(F, c, d, a, b, SET(10), 11);
119.         STEP(F, b, c, d, a, SET(11), 19);
120. 
121.         STEP(F, a, b, c, d, SET(12),  3);
122.         STEP(F, d, a, b, c, SET(13),  7);
123.         STEP(F, c, d, a, b, SET(14), 11);
124.         STEP(F, b, c, d, a, SET(15), 19);
125. /* Round 2 */
126.         STEP(G, a, b, c, d, GET( 0) + 0x5A827999,  3);
127.         STEP(G, d, a, b, c, GET( 4) + 0x5A827999,  5);
128.         STEP(G, c, d, a, b, GET( 8) + 0x5A827999,  9);
129.         STEP(G, b, c, d, a, GET(12) + 0x5A827999, 13);
130. 
131.         STEP(G, a, b, c, d, GET( 1) + 0x5A827999,  3);
132.         STEP(G, d, a, b, c, GET( 5) + 0x5A827999,  5);
133.         STEP(G, c, d, a, b, GET( 9) + 0x5A827999,  9);
134.         STEP(G, b, c, d, a, GET(13) + 0x5A827999, 13);
135. 
136.         STEP(G, a, b, c, d, GET( 2) + 0x5A827999,  3);
137.         STEP(G, d, a, b, c, GET( 6) + 0x5A827999,  5);
138.         STEP(G, c, d, a, b, GET(10) + 0x5A827999,  9);
139.         STEP(G, b, c, d, a, GET(14) + 0x5A827999, 13);
140. 
141.         STEP(G, a, b, c, d, GET( 3) + 0x5A827999,  3);
142.         STEP(G, d, a, b, c, GET( 7) + 0x5A827999,  5);
143.         STEP(G, c, d, a, b, GET(11) + 0x5A827999,  9);
144.         STEP(G, b, c, d, a, GET(15) + 0x5A827999, 13);
145. /* Round 3 */
146.         STEP(H, a, b, c, d, GET( 0) + 0x6ED9EBA1,  3);
147.         STEP(H, d, a, b, c, GET( 8) + 0x6ED9EBA1,  9);
148.         STEP(H, c, d, a, b, GET( 4) + 0x6ED9EBA1, 11);
149.         STEP(H, b, c, d, a, GET(12) + 0x6ED9EBA1, 15);
150. 
151.         STEP(H, a, b, c, d, GET( 2) + 0x6ED9EBA1,  3);
152.         STEP(H, d, a, b, c, GET(10) + 0x6ED9EBA1,  9);
153.         STEP(H, c, d, a, b, GET( 6) + 0x6ED9EBA1, 11);
154.         STEP(H, b, c, d, a, GET(14) + 0x6ED9EBA1, 15);
155. 
156.         STEP(H, a, b, c, d, GET( 1) + 0x6ED9EBA1,  3);
157.         STEP(H, d, a, b, c, GET( 9) + 0x6ED9EBA1,  9);
158.         STEP(H, c, d, a, b, GET( 5) + 0x6ED9EBA1, 11);
159.         STEP(H, b, c, d, a, GET(13) + 0x6ED9EBA1, 15);
160. 
161.         STEP(H, a, b, c, d, GET( 3) + 0x6ED9EBA1,  3);
162.         STEP(H, d, a, b, c, GET(11) + 0x6ED9EBA1,  9);
163.         STEP(H, c, d, a, b, GET( 7) + 0x6ED9EBA1, 11);
164.         STEP(H, b, c, d, a, GET(15) + 0x6ED9EBA1, 15);
165. 
166.         a += saved_a;
167.         b += saved_b;
168.         c += saved_c;
169.         d += saved_d;
170. 
171.         ptr += 64;
172.     } while (size -= 64);
173. 
174.     ctx->a = a;
175.     ctx->b = b;
176.     ctx->c = c;
177.     ctx->d = d;
178. 
179.     return ptr;
180. }
181. 
182. void
183. nhmd4_init(
184.     struct nhmd4_context *ctx)
185. {
186.     ctx->a = 0x67452301;
187.     ctx->b = 0xefcdab89;
188.     ctx->c = 0x98badcfe;
189.     ctx->d = 0x10325476;
190. 
191.     ctx->lo = 0;
192.     ctx->hi = 0;
193. }
194. 
195. void
196. nhmd4_update(
197.     struct nhmd4_context *ctx,
198.     const unsigned char *data,
199.     size_t size)
200. {
201.     /* @UNSAFE */
202.     quint32 saved_lo;
203.     unsigned long used, free;
204. 
205.     saved_lo = ctx->lo;
206.     if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
207.         ctx->hi++;
208.     ctx->hi += (quint32)(size >> 29);
209. 
210.     used = saved_lo & 0x3f;
211. 
212.     if (used) {
213.         free = 64 - used;
214. 
215.         if (size < free) {
216.             memcpy(&ctx->buffer[used], data, size);
217.             return;
218.         }
219. 
220.         memcpy(&ctx->buffer[used], data, free);
221.         data = (const unsigned char *) data + free;
222.         size -= free;
223.         nhmd4_body(ctx, ctx->buffer, 64);
224.     }
225. 
226.     if (size >= 64) {
227.         data = nhmd4_body(ctx, data, size & ~0x3fUL);
228.         size &= 0x3fUL;
229.     }
230. 
231.     memcpy(ctx->buffer, data, size);
232. }
233. 
234. void
235. nhmd4_final(
236.     struct nhmd4_context *ctx,
237.     unsigned char result[NHMD4_RESULTLEN])
238. {
239.     /* @UNSAFE */
240.     unsigned long used, free;
241. 
242.     used = ctx->lo & 0x3fUL;
243. 
244.     ctx->buffer[used++] = 0x80;
245. 
246.     free = 64 - used;
247. 
248.     if (free < 8) {
249.         memset(&ctx->buffer[used], 0, free);
250.         nhmd4_body(ctx, ctx->buffer, 64);
251.         used = 0;
252.         free = 64;
253.     }
254. 
255.     memset(&ctx->buffer[used], 0, free - 8);
256. 
257.     ctx->lo <<= 3;
258.     ctx->buffer[56] = ctx->lo;
259.     ctx->buffer[57] = ctx->lo >> 8;
260.     ctx->buffer[58] = ctx->lo >> 16;
261.     ctx->buffer[59] = ctx->lo >> 24;
262.     ctx->buffer[60] = ctx->hi;
263.     ctx->buffer[61] = ctx->hi >> 8;
264.     ctx->buffer[62] = ctx->hi >> 16;
265.     ctx->buffer[63] = ctx->hi >> 24;
266. 
267.     nhmd4_body(ctx, ctx->buffer, 64);
268. 
269.     result[0] = ctx->a;
270.     result[1] = ctx->a >> 8;
271.     result[2] = ctx->a >> 16;
272.     result[3] = ctx->a >> 24;
273.     result[4] = ctx->b;
274.     result[5] = ctx->b >> 8;
275.     result[6] = ctx->b >> 16;
276.     result[7] = ctx->b >> 24;
277.     result[8] = ctx->c;
278.     result[9] = ctx->c >> 8;
279.     result[10] = ctx->c >> 16;
280.     result[11] = ctx->c >> 24;
281.     result[12] = ctx->d;
282.     result[13] = ctx->d >> 8;
283.     result[14] = ctx->d >> 16;
284.     result[15] = ctx->d >> 24;
285. 
286.     memset(ctx, 0, sizeof *ctx);
287. }
288. 
289. #undef F
290. #undef G
291. #undef H
292. #undef STEP
293. #undef SET
294. #undef GET
295. 
296. #endif /* CRASHREPORT */
297. 
298. /*nhmd4.c*/