Skip to content

Commit 28ce850

Browse files
author
Stefan Fritsch
committed
Merge r1460244:
speed-up md5 by avoiding some memcopies PR: 49011 Submitted by: Stefan Fritsch, Stefan Fuhrmann <stefanfuhrmann alice-dsl de> git-svn-id: https://svn.apache.org/repos/asf/apr/apr-util/branches/1.5.x@1460281 13f79535-47bb-0310-9956-ffa450edef68
1 parent b563a31 commit 28ce850

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-*- coding: utf-8 -*-
22
Changes with APR-util 1.5.2
33

4+
*) Speedup md5 calculation by avoiding some copying on little endian
5+
architectures. PR 49011. [Stefan Fritsch, Stefan Fuhrmann
6+
<stefanfuhrmann alice-dsl de>]
7+
48
*) Use heap memory for crypt in apr_password_validate(), to reduce stack
59
usage. PR 54572. [Stefan Fritsch]
610

crypto/apr_md5.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,18 @@ APU_DECLARE(apr_status_t) apr_md5(unsigned char digest[APR_MD5_DIGESTSIZE],
338338
static void MD5Transform(apr_uint32_t state[4], const unsigned char block[64])
339339
{
340340
apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3],
341-
x[APR_MD5_DIGESTSIZE];
341+
tmpbuf[APR_MD5_DIGESTSIZE];
342+
const apr_uint32_t *x;
342343

343-
Decode(x, block, 64);
344+
#if !APR_IS_BIGENDIAN
345+
if ((apr_uintptr_t)block % sizeof(apr_uint32_t) == 0) {
346+
x = (apr_uint32_t *)block;
347+
} else
348+
#endif
349+
{
350+
Decode(tmpbuf, block, 64);
351+
x = tmpbuf;
352+
}
344353

345354
/* Round 1 */
346355
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
@@ -419,8 +428,13 @@ static void MD5Transform(apr_uint32_t state[4], const unsigned char block[64])
419428
state[2] += c;
420429
state[3] += d;
421430

422-
/* Zeroize sensitive information. */
423-
memset(x, 0, sizeof(x));
431+
#if !APR_IS_BIGENDIAN
432+
if (x == tmpbuf)
433+
#endif
434+
{
435+
/* Zeroize sensitive information. */
436+
memset(tmpbuf, 0, sizeof(tmpbuf));
437+
}
424438
}
425439

426440
/* Encodes input (apr_uint32_t) into output (unsigned char). Assumes len is

test/testmd5.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,38 @@ static void test_md5sum(abts_case *tc, void *data)
6666
(memcmp(digest, sum, APR_MD5_DIGESTSIZE) == 0));
6767
}
6868

69+
static void test_md5sum_unaligned(abts_case *tc, void *data)
70+
{
71+
apr_md5_ctx_t context;
72+
const char *string = "abcdefghijklmnopqrstuvwxyz01234"
73+
"abcdefghijklmnopqrstuvwxyz01234"
74+
"abcdefghijklmnopqrstuvwxyz01234"
75+
"abcdefghijklmnopqrstuvwxyz01234_";
76+
const char *sum =
77+
"\x93\x17\x22\x78\xee\x30\x82\xb3\xeb\x95\x33\xec\xea\x78\xb7\x89";
78+
unsigned char digest[APR_MD5_DIGESTSIZE];
79+
unsigned int i;
80+
81+
ABTS_ASSERT(tc, "apr_md5_init", (apr_md5_init(&context) == 0));
82+
for (i = 0; i < 10; i++) {
83+
ABTS_ASSERT(tc, "apr_md5_update",
84+
(apr_md5_update(&context, string, strlen(string)) == 0));
85+
string++;
86+
}
87+
ABTS_ASSERT(tc, "apr_md5_final", (apr_md5_final(digest, &context)
88+
== 0));
89+
ABTS_ASSERT(tc, "check for correct md5 digest of unaligned data",
90+
(memcmp(digest, sum, APR_MD5_DIGESTSIZE) == 0));
91+
}
92+
6993
abts_suite *testmd5(abts_suite *suite)
7094
{
7195
suite = ADD_SUITE(suite);
7296

7397
for (count=0; count < num_sums; count++) {
7498
abts_run_test(suite, test_md5sum, NULL);
7599
}
100+
abts_run_test(suite, test_md5sum_unaligned, NULL);
76101

77102
return suite;
78103
}

0 commit comments

Comments
 (0)