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