Debian Codegraph @

test/cryptounittest/test-digest-md5.c

raw
1 /**
2 * \file test-digest-md5.c
3 *
4 * \brief The MD5 message digest unit-tests
5 *
6 * \warning MD5 is considered a weak message digest and its use constitutes a
7 * security risk! We recommend considering stronger message digests
8 * instead!
9 *
10 * \copyright 2022 Joseph Benden <[email protected]>
11 *
12 * \license GPL-2.0-OR-LATER
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <stdlib.h>
24
25 #include "aircrack-ng/crypto/crypto.h"
26 #include "tests.h"
27
28 // clang-format off
29 /*
30 * RFC 1321 test vectors
31 */
32 static const uint8_t md5_test_buf[7][81] =
33 {
34 { "" },
35 { "a" },
36 { "abc" },
37 { "message digest" },
38 { "abcdefghijklmnopqrstuvwxyz" },
39 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
40 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }
41 };
42
43 static const size_t md5_test_buflen[7] =
44 {
45 0, 1, 3, 14, 26, 62, 80
46 };
47
48 static const uint8_t md5_test_sum[7][16] =
49 {
50 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
51 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
52 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
53 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
54 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
55 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
56 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
57 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
58 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
59 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
60 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
61 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
62 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
63 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
64 };
65 // clang-format on
66
67 int main(int argc, char ** argv)
68 {
69 int i, error = 0;
70 uint8_t md5sum[DIGEST_MD5_MAC_LEN];
71
72 (void) argc;
73
74 for (i = 0; i < 7; i++)
75 {
76 error |= Digest_MD5(md5_test_buf[i], md5_test_buflen[i], md5sum);
77 error |= test(md5sum, md5_test_sum[i], DIGEST_MD5_MAC_LEN, argv[0]);
78 }
79
80 for (i = 0; i < 7; i++)
81 {
82 error |= Digest_MD5_Vector(
83 1,
84 (const uint8_t * []){(uint8_t *) &md5_test_buf[i]},
85 &md5_test_buflen[i],
86 md5sum);
87 error |= test(md5sum, md5_test_sum[i], DIGEST_MD5_MAC_LEN, argv[0]);
88 }
89
90 return error;
91 }