xref: /titanic_50/usr/src/common/net/wanboot/crypt/sha1_test.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *  sha1_test.c
31  *
32  *  Description:
33  *      This file will exercise the SHA-1 code performing the three
34  *      tests documented in FIPS PUB 180-1 plus one which calls
35  *      SHA1Input with an exact multiple of 512 bits, plus a few
36  *      error test checks.
37  *
38  *  Portability Issues:
39  *      None.
40  *
41  */
42 
43 #include <stdio.h>
44 #include <strings.h>
45 
46 #include <sys/sha1.h>
47 #include "sha1_test.h"
48 #include "cmn_test.h"
49 
50 /*
51  *  Define patterns for testing
52  */
53 #define	TEST1	"abc"
54 #define	TEST2a	"abcdbcdecdefdefgefghfghighijhi"
55 #define	TEST2b	"jkijkljklmklmnlmnomnopnopq"
56 #define	TEST2	TEST2a TEST2b
57 #define	TEST3	"a"
58 #define	TEST4a	"01234567012345670123456701234567"
59 #define	TEST4b	"01234567012345670123456701234567"
60 
61 /* an exact multiple of 512 bits */
62 #define	TEST4	TEST4a TEST4b
63 
64 static char *testarray[4] = {
65 	TEST1,
66 	TEST2,
67 	TEST3,
68 	TEST4
69 };
70 
71 static int repeatcount[4] = { 1, 1, 1000000, 10 };
72 
73 static char *resultarray[4] = {
74 	"A9993E364706816ABA3E25717850C26C9CD0D89D",
75 	"84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
76 	"34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
77 	"DEA356A2CDDD90C7A7ECEDC5EBB563934F460452"
78 };
79 
80 int
81 sha1test(void)
82 {
83 	SHA1_CTX sha;
84 	int fail;
85 	int i;
86 	int j;
87 	uint8_t digest[20];
88 	uint8_t rdigest[20];
89 
90 	/*
91 	 * Perform SHA-1 tests
92 	 */
93 	for (j = 0; j < 4; ++j) {
94 		fail = 0;
95 		(void) printf("Test #%d ", j+1);
96 
97 		SHA1Init(&sha);
98 
99 		for (i = 0; i < repeatcount[j]; ++i) {
100 			SHA1Update(&sha, (unsigned char *)testarray[j],
101 			    strlen(testarray[j]));
102 		}
103 
104 		SHA1Final(digest, &sha);
105 
106 		getxdata(rdigest, resultarray[j], 20);
107 		if (bcmp(digest, rdigest, 20) != 0) {
108 			(void) printf("FAILED\n");
109 			fail++;
110 		} else {
111 			(void) printf("PASSED\n");
112 		}
113 	}
114 
115 	return (fail);
116 }
117