2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
30 #include <ipxe/test.h>
31 #include "digest_test.h"
33 /** An MD5 test vector */
34 struct md5_test_vector {
37 /** Test data length */
39 /** Expected digest */
40 uint8_t digest[MD5_DIGEST_SIZE];
43 /** MD5 test vectors */
44 static struct md5_test_vector md5_test_vectors[] = {
45 /* Test inputs borrowed from SHA-1 tests, with results
46 * calculated using md5sum.
49 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
50 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
52 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
53 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
54 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
55 { 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca,
56 0xaa, 0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a } },
59 /** MD5 test fragment lists */
60 static struct digest_test_fragments md5_test_fragments[] = {
62 { { 1, 1, 1, 1, 1, 1, 1, 1 } },
63 { { 2, 0, 23, 4, 6, 1, 0 } },
67 * Perform MD5 self-test
70 static void md5_test_exec ( void ) {
71 struct digest_algorithm *digest = &md5_algorithm;
72 struct md5_test_vector *test;
77 /* Correctness test */
78 for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
79 sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
80 test = &md5_test_vectors[i];
81 /* Test with a single pass */
82 digest_ok ( digest, NULL, test->data, test->len, test->digest );
83 /* Test with fragment lists */
84 for ( j = 0 ; j < ( sizeof ( md5_test_fragments ) /
85 sizeof ( md5_test_fragments[0] ) ) ; j++ ){
86 digest_ok ( digest, &md5_test_fragments[j],
87 test->data, test->len, test->digest );
92 cost = digest_cost ( digest );
93 DBG ( "MD5 required %ld cycles per byte\n", cost );
97 struct self_test md5_test __self_test = {
99 .exec = md5_test_exec,