Debian Codegraph @

lib/alg-md4.c

raw symbols SCIP
1 /*
2 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 * MD4 Message-Digest Algorithm (RFC 1320).
4 *
5 * Homepage:
6 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
7 *
8 * Author:
9 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
10 *
11 * This software was written by Alexander Peslyak in 2001. No copyright is
12 * claimed, and the software is hereby placed in the public domain.
13 * In case this attempt to disclaim copyright and place the software in the
14 * public domain is deemed null and void, then the software is
15 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 * general public under the following terms:
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted.
20 *
21 * There's ABSOLUTELY NO WARRANTY, express or implied.
22 *
23 * (This is a heavily cut-down "BSD license".)
24 *
25 * This differs from Colin Plumb's older public domain implementation in that
26 * no exactly 32-bit integer data type is required (any 32-bit or wider
27 * unsigned integer data type will do), there's no compile-time endianness
28 * configuration, and the function prototypes match OpenSSL's. No code from
29 * Colin Plumb's implementation has been reused; this comment merely compares
30 * the properties of the two independent implementations.
31 *
32 * The primary goals of this implementation are portability and ease of use.
33 * It is meant to be fast, but not as fast as possible. Some known
34 * optimizations are not included to reduce source code size and avoid
35 * compile-time configuration.
36 */
37
38 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
39
40 #if INCLUDE_ntcxx . . $ `crypt-hashes.h:15:9`!
41
42 #include "alg-md4.h"cxx . . $ `<file>/lib/alg-md4.h`/
43
44 /*
45 * The basic MD4 functions.
46 *
47 * F and G are optimized compared to their RFC 1320 definitions, with the
48 * optimization for F borrowed from Colin Plumb's MD5 implementation.
49 */
50 #define Fcxx . . $ `lib/alg-md4.c:50:9`!(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
51 #define Gcxx . . $ `lib/alg-md4.c:51:9`!(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
52 #define Hcxx . . $ `lib/alg-md4.c:52:9`!(x, y, z) ((x) ^ (y) ^ (z))
53
54 /*
55 * The MD4 transformation for all three rounds.
56 */
57 #define STEPcxx . . $ `lib/alg-md4.c:57:9`!(f, a, b, c, d, x, s) \
58 (a) += f((b), (c), (d)) + (x); \
59 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
60
61 /*
62 * SET reads 4 input bytes in little-endian byte order and stores them in a
63 * properly aligned word in host byte order.
64 *
65 * The check for little-endian architectures that tolerate unaligned memory
66 * accesses is just an optimization. Nothing will break if it fails to detect
67 * a suitable architecture.
68 *
69 * Unfortunately, this optimization may be a C strict aliasing rules violation
70 * if the caller's data buffer has effective type that cannot be aliased by
71 * MD4_u32plus. In practice, this problem may occur if these MD4 routines are
72 * inlined into a calling function, or with future and dangerously advanced
73 * link-time optimizations. For the time being, keeping these MD4 routines in
74 * their own translation unit avoids the problem.
75 */
76 #if 0 /* defined(__i386__) || defined(__x86_64__) || defined(__vax__) */
77 #define SET(n) \
78 (*(const MD4_u32plus *)&ptr[(n) * 4])
79 #define GET(n) \
80 SET(n)
81 #else
82 #define SETcxx . . $ `lib/alg-md4.c:82:9`!(n) \
83 (ctx->block[(n)] = \
84 (MD4_u32plus)ptr[(n) * 4] | \
85 ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
86 ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
87 ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
88 #define GETcxx . . $ `lib/alg-md4.c:88:9`!(n) \
89 (ctx->block[(n)])
90 #endif
91
92 /*
93 * This processes one or more 64-byte data blocks, but does NOT update the bit
94 * counters. There are no alignment requirements.
95 */
96 static const void *bodycxx . . $ body(13be03513c63f0ff).(MD4_CTXcxx . . $ MD4_CTX# *ctx, const void *data, unsigned long size)
97 {
98 const unsigned char *ptr;
99 MD4_u32pluscxx . . $ MD4_u32plus# a, b, c, d;
100 MD4_u32pluscxx . . $ MD4_u32plus# saved_a, saved_b, saved_c, saved_d;
101 const MD4_u32pluscxx . . $ MD4_u32plus# ac1 = 0x5a827999, ac2 = 0x6ed9eba1;
102
103 ptr = (const unsigned char *)data;
104
105 a = ctx->acxx . . $ $anonymous_type_80da953f977fe66d_0#a.;
106 b = ctx->bcxx . . $ $anonymous_type_80da953f977fe66d_0#b.;
107 c = ctx->ccxx . . $ $anonymous_type_80da953f977fe66d_0#c.;
108 d = ctx->dcxx . . $ $anonymous_type_80da953f977fe66d_0#d.;
109
110 do {
111 saved_a = a;
112 saved_b = b;
113 saved_c = c;
114 saved_d = d;
115
116 /* Round 1 */
117 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, a, b, c, d, SETcxx . . $ `lib/alg-md4.c:82:9`!(0), 3)
118 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, d, a, b, c, SETcxx . . $ `lib/alg-md4.c:82:9`!(1), 7)
119 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, c, d, a, b, SETcxx . . $ `lib/alg-md4.c:82:9`!(2), 11)
120 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, b, c, d, a, SETcxx . . $ `lib/alg-md4.c:82:9`!(3), 19)
121 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, a, b, c, d, SETcxx . . $ `lib/alg-md4.c:82:9`!(4), 3)
122 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, d, a, b, c, SETcxx . . $ `lib/alg-md4.c:82:9`!(5), 7)
123 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, c, d, a, b, SETcxx . . $ `lib/alg-md4.c:82:9`!(6), 11)
124 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, b, c, d, a, SETcxx . . $ `lib/alg-md4.c:82:9`!(7), 19)
125 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, a, b, c, d, SETcxx . . $ `lib/alg-md4.c:82:9`!(8), 3)
126 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, d, a, b, c, SETcxx . . $ `lib/alg-md4.c:82:9`!(9), 7)
127 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, c, d, a, b, SETcxx . . $ `lib/alg-md4.c:82:9`!(10), 11)
128 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, b, c, d, a, SETcxx . . $ `lib/alg-md4.c:82:9`!(11), 19)
129 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, a, b, c, d, SETcxx . . $ `lib/alg-md4.c:82:9`!(12), 3)
130 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, d, a, b, c, SETcxx . . $ `lib/alg-md4.c:82:9`!(13), 7)
131 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, c, d, a, b, SETcxx . . $ `lib/alg-md4.c:82:9`!(14), 11)
132 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Fcxx . . $ `lib/alg-md4.c:50:9`!, b, c, d, a, SETcxx . . $ `lib/alg-md4.c:82:9`!(15), 19)
133
134 /* Round 2 */
135 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(0) + ac1, 3)
136 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(4) + ac1, 5)
137 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(8) + ac1, 9)
138 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(12) + ac1, 13)
139 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(1) + ac1, 3)
140 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(5) + ac1, 5)
141 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(9) + ac1, 9)
142 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(13) + ac1, 13)
143 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(2) + ac1, 3)
144 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(6) + ac1, 5)
145 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(10) + ac1, 9)
146 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(14) + ac1, 13)
147 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(3) + ac1, 3)
148 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(7) + ac1, 5)
149 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(11) + ac1, 9)
150 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Gcxx . . $ `lib/alg-md4.c:51:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(15) + ac1, 13)
151
152 /* Round 3 */
153 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(0) + ac2, 3)
154 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(8) + ac2, 9)
155 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(4) + ac2, 11)
156 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(12) + ac2, 15)
157 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(2) + ac2, 3)
158 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(10) + ac2, 9)
159 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(6) + ac2, 11)
160 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(14) + ac2, 15)
161 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(1) + ac2, 3)
162 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(9) + ac2, 9)
163 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(5) + ac2, 11)
164 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(13) + ac2, 15)
165 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, a, b, c, d, GETcxx . . $ `lib/alg-md4.c:88:9`!(3) + ac2, 3)
166 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, d, a, b, c, GETcxx . . $ `lib/alg-md4.c:88:9`!(11) + ac2, 9)
167 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, c, d, a, b, GETcxx . . $ `lib/alg-md4.c:88:9`!(7) + ac2, 11)
168 STEPcxx . . $ `lib/alg-md4.c:57:9`!(Hcxx . . $ `lib/alg-md4.c:52:9`!, b, c, d, a, GETcxx . . $ `lib/alg-md4.c:88:9`!(15) + ac2, 15)
169
170 a += saved_a;
171 b += saved_b;
172 c += saved_c;
173 d += saved_d;
174
175 ptr += 64;
176 } while (size -= 64);
177
178 ctx->acxx . . $ $anonymous_type_80da953f977fe66d_0#a. = a;
179 ctx->bcxx . . $ $anonymous_type_80da953f977fe66d_0#b. = b;
180 ctx->ccxx . . $ $anonymous_type_80da953f977fe66d_0#c. = c;
181 ctx->dcxx . . $ $anonymous_type_80da953f977fe66d_0#d. = d;
182
183 return ptr;
184 }
185
186 void MD4_Initcxx . . $ `lib/crypt-port.h:344:9`!(MD4_CTXcxx . . $ MD4_CTX# *ctx)
187 {
188 ctx->acxx . . $ $anonymous_type_80da953f977fe66d_0#a. = 0x67452301;
189 ctx->bcxx . . $ $anonymous_type_80da953f977fe66d_0#b. = 0xefcdab89;
190 ctx->ccxx . . $ $anonymous_type_80da953f977fe66d_0#c. = 0x98badcfe;
191 ctx->dcxx . . $ $anonymous_type_80da953f977fe66d_0#d. = 0x10325476;
192
193 ctx->locxx . . $ $anonymous_type_80da953f977fe66d_0#lo. = 0;
194 ctx->hicxx . . $ $anonymous_type_80da953f977fe66d_0#hi. = 0;
195 }
196
197 void MD4_Updatecxx . . $ `lib/crypt-port.h:345:9`!(MD4_CTXcxx . . $ MD4_CTX# *ctx, const void *data, size_t size)
198 {
199 MD4_u32pluscxx . . $ MD4_u32plus# saved_lo;
200 unsigned long used, available;
201
202 saved_lo = ctx->locxx . . $ $anonymous_type_80da953f977fe66d_0#lo.;
203 if ((ctx->locxx . . $ $anonymous_type_80da953f977fe66d_0#lo. = (saved_lo + size) & 0x1fffffff) < saved_lo)
204 ctx->hicxx . . $ $anonymous_type_80da953f977fe66d_0#hi.++;
205 ctx->hicxx . . $ $anonymous_type_80da953f977fe66d_0#hi. += (MD4_u32pluscxx . . $ MD4_u32plus#) size >> 29;
206
207 used = saved_lo & 0x3f;
208
209 if (used) {
210 available = 64 - used;
211
212 if (size < available) {
213 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer.[used], data, size);
214 return;
215 }
216
217 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer.[used], data, available);
218 data = (const unsigned char *)data + available;
219 size -= available;
220 bodycxx . . $ body(13be03513c63f0ff).(ctx, ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer., 64);
221 }
222
223 if (size >= 64) {
224 data = bodycxx . . $ body(13be03513c63f0ff).(ctx, data, size & ~(unsigned long)0x3f);
225 size &= 0x3f;
226 }
227
228 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer., data, size);
229 }
230
231 #define OUTcxx . . $ `lib/alg-md4.c:231:9`!(dst, src) \
232 (dst)[0] = (unsigned char)(src); \
233 (dst)[1] = (unsigned char)((src) >> 8); \
234 (dst)[2] = (unsigned char)((src) >> 16); \
235 (dst)[3] = (unsigned char)((src) >> 24);
236
237 void MD4_Finalcxx . . $ `lib/crypt-port.h:346:9`!(uint8_tcxx . . $ uint8_t# result[16], MD4_CTXcxx . . $ MD4_CTX# *ctx)
238 {
239 unsigned long used, available;
240
241 used = ctx->locxx . . $ $anonymous_type_80da953f977fe66d_0#lo. & 0x3f;
242
243 ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer.[used++] = 0x80;
244
245 available = 64 - used;
246
247 if (available < 8) {
248 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer.[used], 0, available);
249 bodycxx . . $ body(13be03513c63f0ff).(ctx, ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer., 64);
250 used = 0;
251 available = 64;
252 }
253
254 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer.[used], 0, available - 8);
255
256 ctx->locxx . . $ $anonymous_type_80da953f977fe66d_0#lo. <<= 3;
257 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&ctx->buffer[56], ctx->lo)
258 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&ctx->buffer[60], ctx->hi)
259
260 bodycxx . . $ body(13be03513c63f0ff).(ctx, ctx->buffercxx . . $ $anonymous_type_80da953f977fe66d_0#buffer., 64);
261
262 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&result[0], ctx->a)
263 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&result[4], ctx->b)
264 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&result[8], ctx->c)
265 OUTcxx . . $ `lib/alg-md4.c:231:9`!(&result[12], ctx->d)
266
267 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(ctx, sizeof(*ctx));
268 }
269
270 #endif