1a8f6409peter/* Licensed to the Apache Software Foundation (ASF) under one or more
2a8f6409peter * contributor license agreements.  See the NOTICE file distributed with
3a8f6409peter * this work for additional information regarding copyright ownership.
4a8f6409peter * The ASF licenses this file to You under the Apache License, Version 2.0
5a8f6409peter * (the "License"); you may not use this file except in compliance with
6a8f6409peter * the License.  You may obtain a copy of the License at
7a8f6409peter *
8a8f6409peter *     http://www.apache.org/licenses/LICENSE-2.0
9a8f6409peter *
10a8f6409peter * Unless required by applicable law or agreed to in writing, software
11a8f6409peter * distributed under the License is distributed on an "AS IS" BASIS,
12a8f6409peter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8f6409peter * See the License for the specific language governing permissions and
14a8f6409peter * limitations under the License.
15a8f6409peter */
16a8f6409peter/* This is derived from material copyright RSA Data Security, Inc.
17a8f6409peter * Their notice is reproduced below in its entirety.
18a8f6409peter *
19a8f6409peter * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
20a8f6409peter * rights reserved.
21a8f6409peter *
22a8f6409peter * RSA Data Security, Inc. makes no representations concerning either
23a8f6409peter * the merchantability of this software or the suitability of this
24a8f6409peter * software for any particular purpose. It is provided "as is"
25a8f6409peter * without express or implied warranty of any kind.
26a8f6409peter *
27a8f6409peter * These notices must be retained in any copies of any part of this
28a8f6409peter * documentation and/or software.
29a8f6409peter */
30a8f6409peter
31a8f6409peter#include <assert.h>
32a8f6409peter#include <stdio.h>
33a8f6409peter#include <stdlib.h>
34a8f6409peter
35a8f6409peter#include "apr_errno.h"
36a8f6409peter#include "apr_md4.h"
37a8f6409peter#include "apr_file_io.h"
38a8f6409peter
39a8f6409peter#include "abts.h"
40a8f6409peter#include "testutil.h"
41a8f6409peter
42a8f6409peterstatic struct {
43a8f6409peter        const char *string;
44a8f6409peter        const char *md4sum;
45a8f6409peter} md4sums[] =
46a8f6409peter{
47a8f6409peter/*
48a8f6409peter* Taken from the old md4 test suite.
49a8f6409peter* MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
50a8f6409peter* MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
51a8f6409peter* MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
52a8f6409peter* MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
53a8f6409peter* MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
54a8f6409peter* MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
55a8f6409peter* MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
56a8f6409peter*
57a8f6409peter*/
58a8f6409peter        {"",
59a8f6409peter         "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"},
60a8f6409peter        {"a",
61a8f6409peter         "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"},
62a8f6409peter        {"abc",
63a8f6409peter         "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"},
64a8f6409peter        {"message digest",
65a8f6409peter         "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b"},
66a8f6409peter        {"abcdefghijklmnopqrstuvwxyz",
67a8f6409peter         "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d\xa9"},
68a8f6409peter        {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
69a8f6409peter         "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"},
70a8f6409peter        {"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
71a8f6409peter         "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05\x36"}
72a8f6409peter};
73a8f6409peter
74a8f6409peterstatic int num_sums = sizeof(md4sums) / sizeof(md4sums[0]);
75a8f6409peterstatic int count;
76a8f6409peter
77a8f6409peter#if 0
78a8f6409peterstatic int MDStringComp(const void *string, const void *sum)
79a8f6409peter{
80a8f6409peter        apr_md4_ctx_t context;
81a8f6409peter        unsigned char digest[APR_MD4_DIGESTSIZE];
82a8f6409peter        unsigned int len = strlen(string);
83a8f6409peter
84a8f6409peter        apr_md4_init(&context);
85a8f6409peter        apr_md4_update(&context, (unsigned char *)string, len);
86a8f6409peter        apr_md4_final(digest, &context);
87a8f6409peter        return (memcmp(digest, sum, APR_MD4_DIGESTSIZE));
88a8f6409peter
89a8f6409peter}
90a8f6409peter#endif
91a8f6409peter
92a8f6409peterstatic void test_md4sum(abts_case *tc, void *data)
93a8f6409peter{
94a8f6409peter        apr_md4_ctx_t context;
95a8f6409peter        unsigned char digest[APR_MD4_DIGESTSIZE];
96a8f6409peter        const void *string = md4sums[count].string;
97a8f6409peter        const void *sum = md4sums[count].md4sum;
98a8f6409peter        unsigned int len = strlen(string);
99a8f6409peter
100a8f6409peter        ABTS_ASSERT(tc, "apr_md4_init", (apr_md4_init(&context) == 0));
101a8f6409peter        ABTS_ASSERT(tc, "apr_md4_update",
102a8f6409peter                    (apr_md4_update(&context,
103a8f6409peter                                    (unsigned char *)string, len) == 0));
104a8f6409peter
105a8f6409peter        ABTS_ASSERT(tc, "apr_md4_final", (apr_md4_final(digest, &context) ==0));
106a8f6409peter        ABTS_ASSERT(tc, "check for correct md4 digest",
107a8f6409peter                    (memcmp(digest, sum, APR_MD4_DIGESTSIZE) == 0));
108a8f6409peter}
109a8f6409peter
110a8f6409peterabts_suite *testmd4(abts_suite *suite)
111a8f6409peter{
112a8f6409peter        suite = ADD_SUITE(suite);
113a8f6409peter
114a8f6409peter        for (count=0; count < num_sums; count++) {
115a8f6409peter            abts_run_test(suite, test_md4sum, NULL);
116a8f6409peter        }
117a8f6409peter
118a8f6409peter        return suite;
119a8f6409peter}
120