Apache HTTPD
framework
httpd-2.4.62
srclib
apr-util
crypto
apr_md4.c
Go to the documentation of this file.
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
* This is derived from material copyright RSA Data Security, Inc.
17
* Their notice is reproduced below in its entirety.
18
*
19
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
20
* rights reserved.
21
*
22
* License to copy and use this software is granted provided that it
23
* is identified as the "RSA Data Security, Inc. MD4 Message-Digest
24
* Algorithm" in all material mentioning or referencing this software
25
* or this function.
26
*
27
* License is also granted to make and use derivative works provided
28
* that such works are identified as "derived from the RSA Data
29
* Security, Inc. MD4 Message-Digest Algorithm" in all material
30
* mentioning or referencing the derived work.
31
*
32
* RSA Data Security, Inc. makes no representations concerning either
33
* the merchantability of this software or the suitability of this
34
* software for any particular purpose. It is provided "as is"
35
* without express or implied warranty of any kind.
36
*
37
* These notices must be retained in any copies of any part of this
38
* documentation and/or software.
39
*/
40
41
#include "
apr_strings.h
"
42
#include "
apr_md4.h
"
43
#include "
apr_lib.h
"
44
#include "
apr_crypto.h
"
/* for apr_crypto_memzero, if available */
45
46
#if APR_HAVE_STRING_H
47
#include <string.h>
48
#endif
49
#if APR_HAVE_UNISTD_H
50
#include <unistd.h>
51
#endif
52
53
/* Constants for MD4Transform routine.
54
*/
55
56
#define S11 3
57
#define S12 7
58
#define S13 11
59
#define S14 19
60
#define S21 3
61
#define S22 5
62
#define S23 9
63
#define S24 13
64
#define S31 3
65
#define S32 9
66
#define S33 11
67
#define S34 15
68
69
static
void
MD4Transform
(
apr_uint32_t
state[4],
const
unsigned
char
block
[64]);
70
static
void
Encode
(
unsigned
char
*output,
const
apr_uint32_t
*
input
,
71
unsigned
int
len
);
72
static
void
Decode
(
apr_uint32_t
*output,
const
unsigned
char
*
input
,
73
unsigned
int
len
);
74
75
static
unsigned
char
PADDING
[64] =
76
{
77
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
80
};
81
82
#if APR_CHARSET_EBCDIC
83
static
apr_xlate_t
*
xlate_ebcdic_to_ascii
;
/* used in apr_md4_encode() */
84
#endif
85
86
/* F, G and I are basic MD4 functions.
87
*/
88
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
89
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
90
#define H(x, y, z) ((x) ^ (y) ^ (z))
91
92
/* ROTATE_LEFT rotates x left n bits.
93
*/
94
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
95
96
/* FF, GG and HH are transformations for rounds 1, 2 and 3 */
97
/* Rotation is separate from addition to prevent recomputation */
98
99
#define FF(a, b, c, d, x, s) { \
100
(a) += F ((b), (c), (d)) + (x); \
101
(a) = ROTATE_LEFT ((a), (s)); \
102
}
103
#define GG(a, b, c, d, x, s) { \
104
(a) += G ((b), (c), (d)) + (x) + (apr_uint32_t)0x5a827999; \
105
(a) = ROTATE_LEFT ((a), (s)); \
106
}
107
#define HH(a, b, c, d, x, s) { \
108
(a) += H ((b), (c), (d)) + (x) + (apr_uint32_t)0x6ed9eba1; \
109
(a) = ROTATE_LEFT ((a), (s)); \
110
}
111
112
/* MD4 initialization. Begins an MD4 operation, writing a new context.
113
*/
114
APU_DECLARE
(
apr_status_t
)
apr_md4_init
(
apr_md4_ctx_t
*
context
)
115
{
116
context
->
count
[0] =
context
->
count
[1] = 0;
117
118
/* Load magic initialization constants. */
119
context
->
state
[0] = 0x67452301;
120
context
->
state
[1] = 0xefcdab89;
121
context
->
state
[2] = 0x98badcfe;
122
context
->
state
[3] = 0x10325476;
123
124
#if APR_HAS_XLATE
125
context
->
xlate
=
NULL
;
126
#endif
127
128
return
APR_SUCCESS
;
129
}
130
131
#if APR_HAS_XLATE
132
/* MD4 translation setup. Provides the APR translation handle
133
* to be used for translating the content before calculating the
134
* digest.
135
*/
136
APU_DECLARE
(
apr_status_t
)
apr_md4_set_xlate
(
apr_md4_ctx_t
*
context
,
137
apr_xlate_t
*
xlate
)
138
{
139
apr_status_t
rv;
140
int
is_sb;
141
142
/* TODO: remove the single-byte-only restriction from this code
143
*/
144
rv =
apr_xlate_sb_get
(
xlate
, &is_sb);
145
if
(rv !=
APR_SUCCESS
) {
146
return
rv;
147
}
148
if
(!is_sb) {
149
return
APR_EINVAL
;
150
}
151
context
->
xlate
=
xlate
;
152
return
APR_SUCCESS
;
153
}
154
#endif
/* APR_HAS_XLATE */
155
156
/* MD4 block update operation. Continues an MD4 message-digest
157
* operation, processing another message block, and updating the
158
* context.
159
*/
160
APU_DECLARE
(
apr_status_t
)
apr_md4_update
(
apr_md4_ctx_t
*
context
,
161
const
unsigned
char
*
input
,
162
apr_size_t
inputLen
)
163
{
164
unsigned
int
i
,
idx
,
partLen
;
165
#if APR_HAS_XLATE
166
apr_size_t
inbytes_left
,
outbytes_left
;
167
#endif
168
169
/* Compute number of bytes mod 64 */
170
idx
= (
unsigned
int
)((
context
->
count
[0] >> 3) & 0x3F);
171
172
/* Update number of bits */
173
if
((
context
->
count
[0] += ((
apr_uint32_t
)
inputLen
<< 3))
174
< ((
apr_uint32_t
)
inputLen
<< 3))
175
context
->
count
[1]++;
176
context
->
count
[1] += (
apr_uint32_t
)
inputLen
>> 29;
177
178
partLen
= 64 -
idx
;
179
180
/* Transform as many times as possible. */
181
#if !APR_HAS_XLATE
182
if
(
inputLen
>=
partLen
) {
183
memcpy
(&
context
->
buffer
[
idx
],
input
,
partLen
);
184
MD4Transform
(
context
->
state
,
context
->
buffer
);
185
186
for
(
i
=
partLen
;
i
+ 63 <
inputLen
;
i
+= 64)
187
MD4Transform
(
context
->
state
, &
input
[
i
]);
188
189
idx
= 0;
190
}
191
else
192
i
= 0;
193
194
/* Buffer remaining input */
195
memcpy
(&
context
->
buffer
[
idx
], &
input
[
i
],
inputLen
-
i
);
196
#else
/*APR_HAS_XLATE*/
197
if
(
inputLen
>=
partLen
) {
198
if
(
context
->
xlate
) {
199
inbytes_left
=
outbytes_left
=
partLen
;
200
apr_xlate_conv_buffer
(
context
->
xlate
, (
const
char
*)
input
,
201
&
inbytes_left
,
202
(
char
*)&
context
->
buffer
[
idx
],
203
&
outbytes_left
);
204
}
205
else
{
206
memcpy
(&
context
->
buffer
[
idx
],
input
,
partLen
);
207
}
208
MD4Transform
(
context
->
state
,
context
->
buffer
);
209
210
for
(
i
=
partLen
;
i
+ 63 <
inputLen
;
i
+= 64) {
211
if
(
context
->
xlate
) {
212
unsigned
char
inp_tmp
[64];
213
inbytes_left
=
outbytes_left
= 64;
214
apr_xlate_conv_buffer
(
context
->
xlate
, (
const
char
*)&
input
[
i
],
215
&
inbytes_left
,
216
(
char
*)
inp_tmp
, &
outbytes_left
);
217
MD4Transform
(
context
->
state
,
inp_tmp
);
218
}
219
else
{
220
MD4Transform
(
context
->
state
, &
input
[
i
]);
221
}
222
}
223
224
idx
= 0;
225
}
226
else
227
i
= 0;
228
229
/* Buffer remaining input */
230
if
(
context
->
xlate
) {
231
inbytes_left
=
outbytes_left
=
inputLen
-
i
;
232
apr_xlate_conv_buffer
(
context
->
xlate
, (
const
char
*)&
input
[
i
],
233
&
inbytes_left
, (
char
*)&
context
->
buffer
[
idx
],
234
&
outbytes_left
);
235
}
236
else
{
237
memcpy
(&
context
->
buffer
[
idx
], &
input
[
i
],
inputLen
-
i
);
238
}
239
#endif
/*APR_HAS_XLATE*/
240
return
APR_SUCCESS
;
241
}
242
243
/* MD4 finalization. Ends an MD4 message-digest operation, writing the
244
* the message digest and zeroizing the context.
245
*/
246
APU_DECLARE
(
apr_status_t
)
apr_md4_final
(
247
unsigned
char
digest
[
APR_MD4_DIGESTSIZE
],
248
apr_md4_ctx_t
*
context
)
249
{
250
unsigned
char
bits[8];
251
unsigned
int
idx
,
padLen
;
252
253
/* Save number of bits */
254
Encode
(bits,
context
->
count
, 8);
255
256
#if APR_HAS_XLATE
257
/* apr_md4_update() should not translate for this final round. */
258
context
->
xlate
=
NULL
;
259
#endif
/*APR_HAS_XLATE*/
260
261
/* Pad out to 56 mod 64. */
262
idx
= (
unsigned
int
) ((
context
->
count
[0] >> 3) & 0x3f);
263
padLen
= (
idx
< 56) ? (56 -
idx
) : (120 -
idx
);
264
apr_md4_update
(
context
,
PADDING
,
padLen
);
265
266
/* Append length (before padding) */
267
apr_md4_update
(
context
, bits, 8);
268
269
/* Store state in digest */
270
Encode
(
digest
,
context
->
state
,
APR_MD4_DIGESTSIZE
);
271
272
/* Zeroize sensitive information. */
273
memset
(
context
, 0,
sizeof
(*
context
));
274
275
return
APR_SUCCESS
;
276
}
277
278
/* MD4 computation in one step (init, update, final)
279
*/
280
APU_DECLARE
(
apr_status_t
)
apr_md4
(
unsigned
char
digest
[
APR_MD4_DIGESTSIZE
],
281
const
unsigned
char
*
input
,
282
apr_size_t
inputLen
)
283
{
284
apr_md4_ctx_t
ctx
;
285
apr_status_t
rv;
286
287
apr_md4_init
(&
ctx
);
288
289
if
((rv =
apr_md4_update
(&
ctx
,
input
,
inputLen
)) !=
APR_SUCCESS
)
290
return
rv;
291
292
return
apr_md4_final
(
digest
, &
ctx
);
293
}
294
295
/* MD4 basic transformation. Transforms state based on block. */
296
static
void
MD4Transform
(
apr_uint32_t
state[4],
const
unsigned
char
block
[64])
297
{
298
apr_uint32_t
a
= state[0],
b
= state[1],
c
= state[2],
d
= state[3],
299
x[
APR_MD4_DIGESTSIZE
];
300
301
Decode
(x,
block
, 64);
302
303
/* Round 1 */
304
FF
(
a
,
b
,
c
,
d
, x[ 0],
S11
);
/* 1 */
305
FF
(
d
,
a
,
b
,
c
, x[ 1],
S12
);
/* 2 */
306
FF
(
c
,
d
,
a
,
b
, x[ 2],
S13
);
/* 3 */
307
FF
(
b
,
c
,
d
,
a
, x[ 3],
S14
);
/* 4 */
308
FF
(
a
,
b
,
c
,
d
, x[ 4],
S11
);
/* 5 */
309
FF
(
d
,
a
,
b
,
c
, x[ 5],
S12
);
/* 6 */
310
FF
(
c
,
d
,
a
,
b
, x[ 6],
S13
);
/* 7 */
311
FF
(
b
,
c
,
d
,
a
, x[ 7],
S14
);
/* 8 */
312
FF
(
a
,
b
,
c
,
d
, x[ 8],
S11
);
/* 9 */
313
FF
(
d
,
a
,
b
,
c
, x[ 9],
S12
);
/* 10 */
314
FF
(
c
,
d
,
a
,
b
, x[10],
S13
);
/* 11 */
315
FF
(
b
,
c
,
d
,
a
, x[11],
S14
);
/* 12 */
316
FF
(
a
,
b
,
c
,
d
, x[12],
S11
);
/* 13 */
317
FF
(
d
,
a
,
b
,
c
, x[13],
S12
);
/* 14 */
318
FF
(
c
,
d
,
a
,
b
, x[14],
S13
);
/* 15 */
319
FF
(
b
,
c
,
d
,
a
, x[15],
S14
);
/* 16 */
320
321
/* Round 2 */
322
GG
(
a
,
b
,
c
,
d
, x[ 0],
S21
);
/* 17 */
323
GG
(
d
,
a
,
b
,
c
, x[ 4],
S22
);
/* 18 */
324
GG
(
c
,
d
,
a
,
b
, x[ 8],
S23
);
/* 19 */
325
GG
(
b
,
c
,
d
,
a
, x[12],
S24
);
/* 20 */
326
GG
(
a
,
b
,
c
,
d
, x[ 1],
S21
);
/* 21 */
327
GG
(
d
,
a
,
b
,
c
, x[ 5],
S22
);
/* 22 */
328
GG
(
c
,
d
,
a
,
b
, x[ 9],
S23
);
/* 23 */
329
GG
(
b
,
c
,
d
,
a
, x[13],
S24
);
/* 24 */
330
GG
(
a
,
b
,
c
,
d
, x[ 2],
S21
);
/* 25 */
331
GG
(
d
,
a
,
b
,
c
, x[ 6],
S22
);
/* 26 */
332
GG
(
c
,
d
,
a
,
b
, x[10],
S23
);
/* 27 */
333
GG
(
b
,
c
,
d
,
a
, x[14],
S24
);
/* 28 */
334
GG
(
a
,
b
,
c
,
d
, x[ 3],
S21
);
/* 29 */
335
GG
(
d
,
a
,
b
,
c
, x[ 7],
S22
);
/* 30 */
336
GG
(
c
,
d
,
a
,
b
, x[11],
S23
);
/* 31 */
337
GG
(
b
,
c
,
d
,
a
, x[15],
S24
);
/* 32 */
338
339
/* Round 3 */
340
HH
(
a
,
b
,
c
,
d
, x[ 0],
S31
);
/* 33 */
341
HH
(
d
,
a
,
b
,
c
, x[ 8],
S32
);
/* 34 */
342
HH
(
c
,
d
,
a
,
b
, x[ 4],
S33
);
/* 35 */
343
HH
(
b
,
c
,
d
,
a
, x[12],
S34
);
/* 36 */
344
HH
(
a
,
b
,
c
,
d
, x[ 2],
S31
);
/* 37 */
345
HH
(
d
,
a
,
b
,
c
, x[10],
S32
);
/* 38 */
346
HH
(
c
,
d
,
a
,
b
, x[ 6],
S33
);
/* 39 */
347
HH
(
b
,
c
,
d
,
a
, x[14],
S34
);
/* 40 */
348
HH
(
a
,
b
,
c
,
d
, x[ 1],
S31
);
/* 41 */
349
HH
(
d
,
a
,
b
,
c
, x[ 9],
S32
);
/* 42 */
350
HH
(
c
,
d
,
a
,
b
, x[ 5],
S33
);
/* 43 */
351
HH
(
b
,
c
,
d
,
a
, x[13],
S34
);
/* 44 */
352
HH
(
a
,
b
,
c
,
d
, x[ 3],
S31
);
/* 45 */
353
HH
(
d
,
a
,
b
,
c
, x[11],
S32
);
/* 46 */
354
HH
(
c
,
d
,
a
,
b
, x[ 7],
S33
);
/* 47 */
355
HH
(
b
,
c
,
d
,
a
, x[15],
S34
);
/* 48 */
356
357
state[0] +=
a
;
358
state[1] +=
b
;
359
state[2] +=
c
;
360
state[3] +=
d
;
361
362
/* Zeroize sensitive information. */
363
#if APU_HAVE_CRYPTO
364
apr_crypto_memzero
(x,
sizeof
(x));
365
#else
366
memset
(x, 0,
sizeof
(x));
367
#endif
368
}
369
370
/* Encodes input (apr_uint32_t) into output (unsigned char). Assumes len is
371
* a multiple of 4.
372
*/
373
static
void
Encode
(
unsigned
char
*output,
const
apr_uint32_t
*
input
,
374
unsigned
int
len
)
375
{
376
unsigned
int
i
, j;
377
apr_uint32_t
k;
378
379
for
(
i
= 0, j = 0; j <
len
;
i
++, j += 4) {
380
k =
input
[
i
];
381
output[j] = (
unsigned
char
)(k & 0xff);
382
output[j + 1] = (
unsigned
char
)((k >> 8) & 0xff);
383
output[j + 2] = (
unsigned
char
)((k >> 16) & 0xff);
384
output[j + 3] = (
unsigned
char
)((k >> 24) & 0xff);
385
}
386
}
387
388
/* Decodes input (unsigned char) into output (apr_uint32_t). Assumes len is
389
* a multiple of 4.
390
*/
391
static
void
Decode
(
apr_uint32_t
*output,
const
unsigned
char
*
input
,
392
unsigned
int
len
)
393
{
394
unsigned
int
i
, j;
395
396
for
(
i
= 0, j = 0; j <
len
;
i
++, j += 4)
397
output[
i
] = ((
apr_uint32_t
)
input
[j]) |
398
(((
apr_uint32_t
)
input
[j + 1]) << 8) |
399
(((
apr_uint32_t
)
input
[j + 2]) << 16) |
400
(((
apr_uint32_t
)
input
[j + 3]) << 24);
401
}
402
403
#if APR_CHARSET_EBCDIC
404
APU_DECLARE
(
apr_status_t
)
apr_MD4InitEBCDIC
(
apr_xlate_t
*
xlate
)
405
{
406
xlate_ebcdic_to_ascii
=
xlate
;
407
return
APR_SUCCESS
;
408
}
409
#endif
len
const char apr_size_t len
Definition
ap_regex.h:187
apr_crypto.h
APR-UTIL Crypto library.
apr_lib.h
APR general purpose library routines.
Decode
static void Decode(apr_uint32_t *output, const unsigned char *input, unsigned int len)
Definition
apr_md4.c:391
S24
#define S24
Definition
apr_md4.c:63
S33
#define S33
Definition
apr_md4.c:66
S32
#define S32
Definition
apr_md4.c:65
S12
#define S12
Definition
apr_md4.c:57
HH
#define HH(a, b, c, d, x, s)
Definition
apr_md4.c:107
S11
#define S11
Definition
apr_md4.c:56
MD4Transform
static void MD4Transform(apr_uint32_t state[4], const unsigned char block[64])
Definition
apr_md4.c:296
GG
#define GG(a, b, c, d, x, s)
Definition
apr_md4.c:103
S23
#define S23
Definition
apr_md4.c:62
S14
#define S14
Definition
apr_md4.c:59
Encode
static void Encode(unsigned char *output, const apr_uint32_t *input, unsigned int len)
Definition
apr_md4.c:373
S13
#define S13
Definition
apr_md4.c:58
S21
#define S21
Definition
apr_md4.c:60
FF
#define FF(a, b, c, d, x, s)
Definition
apr_md4.c:99
S22
#define S22
Definition
apr_md4.c:61
PADDING
static unsigned char PADDING[64]
Definition
apr_md4.c:75
S31
#define S31
Definition
apr_md4.c:64
S34
#define S34
Definition
apr_md4.c:67
apr_md4.h
APR-UTIL MD4 Library.
APU_DECLARE
APU_DECLARE(void)
Computes SipHash-2-4, producing a 64bit (APR_SIPHASH_DSIZE) hash from a message and a 128bit (APR_SIP...
Definition
apr_sha1.c:206
d
apr_size_t const unsigned char unsigned int unsigned int d
Definition
apr_siphash.h:72
apr_strings.h
APR Strings library.
context
apr_md5_ctx_t * context
Definition
util_md5.h:58
APR_EINVAL
#define APR_EINVAL
Definition
apr_errno.h:711
xlate
apr_xlate_t * xlate
Definition
apr_md5.h:100
ctx
apr_brigade_flush void * ctx
Definition
apr_buckets.h:802
a
apr_bucket apr_bucket_brigade * a
Definition
apr_buckets.h:700
apr_md4_set_xlate
#define apr_md4_set_xlate(context, xlate)
Definition
apr_md4.h:96
inputLen
const unsigned char apr_size_t inputLen
Definition
apr_md4.h:108
APR_MD4_DIGESTSIZE
#define APR_MD4_DIGESTSIZE
Definition
apr_md4.h:61
apr_xlate_t
struct apr_xlate_t apr_xlate_t
Definition
apr_xlate.h:39
inbytes_left
const char apr_size_t * inbytes_left
Definition
apr_xlate.h:119
outbytes_left
const char apr_size_t char apr_size_t * outbytes_left
Definition
apr_xlate.h:121
size
apr_size_t size
Definition
apr_allocator.h:115
input
const char * input
Definition
apr_cstr.h:93
APR_SUCCESS
#define APR_SUCCESS
Definition
apr_errno.h:225
apr_status_t
int apr_status_t
Definition
apr_errno.h:44
c
apr_vformatter_buff_t * c
Definition
apr_lib.h:175
b
apr_pool_t * b
Definition
apr_pools.h:529
NULL
return NULL
Definition
mod_so.c:359
i
int i
Definition
mod_so.c:347
apr_md4_ctx_t
Definition
apr_md4.h:67
apr_md5_ctx_t::count
apr_uint32_t count[2]
Definition
apr_md5.h:78
apr_md5_ctx_t::xlate
apr_xlate_t * xlate
Definition
apr_md5.h:84
apr_md5_ctx_t::state
apr_uint32_t state[4]
Definition
apr_md5.h:76
apr_md5_ctx_t::buffer
unsigned char buffer[64]
Definition
apr_md5.h:80
block
Definition
xmlparse.c:344
digest
const char * digest
Definition
testmd5.c:30
int
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray
Generated by
1.9.8