1 /*
2   Copyright (C) 2002-2007 Artifex Software, Inc.
3   All rights reserved.
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 
21   L. Peter Deutsch
22   ghost@aladdin.com
23 
24  */
25 /* $Id: md5main.c 8032 2007-06-08 18:02:05Z giles $ */
26 /*
27   Independent implementation of MD5 (RFC 1321).
28 
29   This code implements the MD5 Algorithm defined in RFC 1321, whose
30   text is available at
31 	http://www.ietf.org/rfc/rfc1321.txt
32   The code is derived from the text of the RFC, including the test suite
33   (section A.5) but excluding the rest of Appendix A.  It does not include
34   any code or documentation that is identified in the RFC as being
35   copyrighted.
36 
37   The original and principal author of md5.c is L. Peter Deutsch
38   <ghost@aladdin.com>.  Other authors are noted in the change history
39   that follows (in reverse chronological order):
40 
41   2007-06-08 RG  Namespaced the api calls to avoid conflict with other
42 	implementations when linking gs as a library.
43   2002-04-13 lpd Splits off main program into a separate file, md5main.c.
44  */
45 
46 #include "md5.h"
47 #include <math.h>
48 #include <stdio.h>
49 #include <string.h>
50 
51 /*
52  * This file builds an executable that performs various functions related
53  * to the MD5 library.  Typical compilation:
54  *	gcc -o md5main -lm md5main.c md5.c
55  */
56 static const char *const usage = "\
57 Usage:\n\
58     md5main --test		# run the self-test (A.5 of RFC 1321)\n\
59     md5main --t-values		# print the T values for the library\n\
60     md5main --version		# print the version of the package\n\
61 ";
62 static const char *const version = "2002-04-13";
63 
64 /* Run the self-test. */
65 static int
do_test(void)66 do_test(void)
67 {
68     static const char *const test[7*2] = {
69 	"", "d41d8cd98f00b204e9800998ecf8427e",
70 	"a", "0cc175b9c0f1b6a831c399e269772661",
71 	"abc", "900150983cd24fb0d6963f7d28e17f72",
72 	"message digest", "f96b697d7cb7938d525a2f31aaf161d0",
73 	"abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
74 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
75 				"d174ab98d277d9f5a5611c2c9f419d9f",
76 	"12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"
77     };
78     int i;
79     int status = 0;
80 
81     for (i = 0; i < 7*2; i += 2) {
82 	gs_md5_state_t state;
83 	gs_md5_byte_t digest[16];
84 	char hex_output[16*2 + 1];
85 	int di;
86 
87 	gs_md5_init(&state);
88 	gs_md5_append(&state, (const gs_md5_byte_t *)test[i], strlen(test[i]));
89 	gs_md5_finish(&state, digest);
90 	for (di = 0; di < 16; ++di)
91 	    sprintf(hex_output + di * 2, "%02x", digest[di]);
92 	if (strcmp(hex_output, test[i + 1])) {
93 	    printf("MD5 (\"%s\") = ", test[i]);
94 	    puts(hex_output);
95 	    printf("**** ERROR, should be: %s\n", test[i + 1]);
96 	    status = 1;
97 	}
98     }
99     if (status == 0)
100 	puts("md5 self-test completed successfully.");
101     return status;
102 }
103 
104 /* Print the T values. */
105 static int
do_t_values(void)106 do_t_values(void)
107 {
108     int i;
109     for (i = 1; i <= 64; ++i) {
110 	unsigned long v = (unsigned long)(4294967296.0 * fabs(sin((double)i)));
111 
112 	/*
113 	 * The following nonsense is only to avoid compiler warnings about
114 	 * "integer constant is unsigned in ANSI C, signed with -traditional".
115 	 */
116 	if (v >> 31) {
117 	    printf("#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n", i,
118 		   v, (unsigned long)(unsigned int)(~v));
119 	} else {
120 	    printf("#define T%d    0x%08lx\n", i, v);
121 	}
122     }
123     return 0;
124 }
125 
126 /* Main program */
127 int
main(int argc,char * argv[])128 main(int argc, char *argv[])
129 {
130     if (argc == 2) {
131 	if (!strcmp(argv[1], "--test"))
132 	    return do_test();
133 	if (!strcmp(argv[1], "--t-values"))
134 	    return do_t_values();
135 	if (!strcmp(argv[1], "--version")) {
136 	    puts(version);
137 	    return 0;
138 	}
139     }
140     puts(usage);
141     return 0;
142 }
143