xref: /freebsd/lib/libmd/tests/shadriver.c (revision e0c51286dd6d1c1ce6d3761933a028cc3dcdd6ca)
1*e0c51286SKyle Evans /* SHADRIVER.C - test driver for SHA-1 (and SHA-2) */
2*e0c51286SKyle Evans 
3*e0c51286SKyle Evans /*-
4*e0c51286SKyle Evans  * SPDX-License-Identifier: RSA-MD
5*e0c51286SKyle Evans  *
6*e0c51286SKyle Evans  * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All rights
7*e0c51286SKyle Evans  * reserved.
8*e0c51286SKyle Evans  *
9*e0c51286SKyle Evans  * RSA Data Security, Inc. makes no representations concerning either the
10*e0c51286SKyle Evans  * merchantability of this software or the suitability of this software for
11*e0c51286SKyle Evans  * any particular purpose. It is provided "as is" without express or implied
12*e0c51286SKyle Evans  * warranty of any kind.
13*e0c51286SKyle Evans  *
14*e0c51286SKyle Evans  * These notices must be retained in any copies of any part of this
15*e0c51286SKyle Evans  * documentation and/or software. */
16*e0c51286SKyle Evans 
17*e0c51286SKyle Evans #include <sys/types.h>
18*e0c51286SKyle Evans 
19*e0c51286SKyle Evans #include <stdio.h>
20*e0c51286SKyle Evans #include <time.h>
21*e0c51286SKyle Evans #include <string.h>
22*e0c51286SKyle Evans 
23*e0c51286SKyle Evans #include "sha.h"
24*e0c51286SKyle Evans #include "sha224.h"
25*e0c51286SKyle Evans #include "sha256.h"
26*e0c51286SKyle Evans #include "sha384.h"
27*e0c51286SKyle Evans #include "sha512.h"
28*e0c51286SKyle Evans #include "sha512t.h"
29*e0c51286SKyle Evans 
30*e0c51286SKyle Evans /* The following makes SHA default to SHA-1 if it has not already been
31*e0c51286SKyle Evans  * defined with C compiler flags. */
32*e0c51286SKyle Evans #ifndef SHA
33*e0c51286SKyle Evans #define SHA 1
34*e0c51286SKyle Evans #endif
35*e0c51286SKyle Evans 
36*e0c51286SKyle Evans #if SHA == 1
37*e0c51286SKyle Evans #undef SHA_Data
38*e0c51286SKyle Evans #define SHA_Data SHA1_Data
39*e0c51286SKyle Evans #elif SHA == 224
40*e0c51286SKyle Evans #undef SHA_Data
41*e0c51286SKyle Evans #define SHA_Data SHA224_Data
42*e0c51286SKyle Evans #elif SHA == 256
43*e0c51286SKyle Evans #undef SHA_Data
44*e0c51286SKyle Evans #define SHA_Data SHA256_Data
45*e0c51286SKyle Evans #elif SHA == 384
46*e0c51286SKyle Evans #undef SHA_Data
47*e0c51286SKyle Evans #define SHA_Data SHA384_Data
48*e0c51286SKyle Evans #elif SHA == 512
49*e0c51286SKyle Evans #undef SHA_Data
50*e0c51286SKyle Evans #define SHA_Data SHA512_Data
51*e0c51286SKyle Evans #elif SHA == 512224
52*e0c51286SKyle Evans #undef SHA_Data
53*e0c51286SKyle Evans #define SHA_Data SHA512_224_Data
54*e0c51286SKyle Evans #elif SHA == 512256
55*e0c51286SKyle Evans #undef SHA_Data
56*e0c51286SKyle Evans #define SHA_Data SHA512_256_Data
57*e0c51286SKyle Evans #endif
58*e0c51286SKyle Evans 
59*e0c51286SKyle Evans /* Digests a string and prints the result. */
60*e0c51286SKyle Evans static void
61*e0c51286SKyle Evans SHAString(char *string)
62*e0c51286SKyle Evans {
63*e0c51286SKyle Evans 	char buf[2*64 + 1];
64*e0c51286SKyle Evans 
65*e0c51286SKyle Evans 	printf("SHA-%d (\"%s\") = %s\n",
66*e0c51286SKyle Evans 	       SHA, string, SHA_Data(string, strlen(string), buf));
67*e0c51286SKyle Evans }
68*e0c51286SKyle Evans 
69*e0c51286SKyle Evans /* Digests a reference suite of strings and prints the results. */
70*e0c51286SKyle Evans int
71*e0c51286SKyle Evans main(void)
72*e0c51286SKyle Evans {
73*e0c51286SKyle Evans 	printf("SHA-%d test suite:\n", SHA);
74*e0c51286SKyle Evans 
75*e0c51286SKyle Evans 	SHAString("");
76*e0c51286SKyle Evans 	SHAString("abc");
77*e0c51286SKyle Evans 	SHAString("message digest");
78*e0c51286SKyle Evans 	SHAString("abcdefghijklmnopqrstuvwxyz");
79*e0c51286SKyle Evans 	SHAString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
80*e0c51286SKyle Evans 		  "abcdefghijklmnopqrstuvwxyz0123456789");
81*e0c51286SKyle Evans 	SHAString("1234567890123456789012345678901234567890"
82*e0c51286SKyle Evans 		  "1234567890123456789012345678901234567890");
83*e0c51286SKyle Evans 
84*e0c51286SKyle Evans 	return 0;
85*e0c51286SKyle Evans }
86