xref: /titanic_51/usr/src/common/net/wanboot/crypt/hmac_test.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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  * HMAC SHA-1 test cases as defined by RFC 2202.
31  *
32  * The test uses predefined keys, data and digests. The data and keys
33  * are used by the HMAC SHA-1 implemention to produce a hash digest and
34  * the the result is compared against the expected digest.
35  */
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <strings.h>
40 
41 #include "hmac_sha1.h"
42 #include "hmac_test.h"
43 #include "cmn_test.h"
44 
45 typedef struct test_data {
46 	unsigned char key[80];
47 	int keylen;
48 	unsigned char data[80];
49 	int datalen;
50 	unsigned char digest[20];
51 } test_data_t;
52 
53 int
54 hmactest(void)
55 {
56 	test_data_t td[7];
57 	SHA1_CTX sha;
58 	uchar_t digest[20];
59 	int fail;
60 	int num;
61 	int i;
62 
63 	td[0].keylen = 20;
64 	getxdata(td[0].key, "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
65 	    td[0].keylen);
66 	td[0].datalen = 8;
67 	(void) strcpy((char *)td[0].data, "Hi There");
68 	getxdata(td[0].digest, "b617318655057264e28bc0b6fb378c8ef146be00", 20);
69 
70 	td[1].keylen = 4;
71 	(void) strcpy((char *)td[1].key, "Jefe");
72 	td[1].datalen =  28;
73 	(void) strcpy((char *)td[1].data, "what do ya want for nothing?");
74 	getxdata(td[1].digest, "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", 20);
75 
76 	td[2].keylen = 20;
77 	getxdata(td[2].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
78 	    td[2].keylen);
79 	td[2].datalen = 50;
80 	getxdata(td[2].data, "ddddddddddddddddddddddddddddddddddddddddddddd"
81 	    "ddddddddddddddddddddddddddddddddddddddddddddddddddddddd", 50);
82 	getxdata(td[2].digest, "125d7342b9ac11cd91a39af48aa17b4f63f175d3", 20);
83 
84 	td[3].keylen = 25;
85 	getxdata(td[3].key, "0102030405060708090a0b0c0d0e0f1011121314151617"
86 	    "1819", td[3].keylen);
87 	td[3].datalen = 50;
88 	getxdata(td[3].data, "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
89 	    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
90 	    td[3].datalen);
91 	getxdata(td[3].digest, "4c9007f4026250c6bc8414f9bf50c86c2d7235da", 20);
92 
93 	td[4].keylen = 20;
94 	getxdata(td[4].key, "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
95 	    td[4].keylen);
96 	td[4].datalen = 20;
97 	(void) strcpy((char *)td[4].data, "Test With Truncation");
98 	getxdata(td[4].digest, "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04", 20);
99 
100 	td[5].keylen = 80;
101 	getxdata(td[5].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
102 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
103 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
104 	    td[5].keylen);
105 	td[5].datalen = 54;
106 	(void) strcpy((char *)td[5].data,
107 	    "Test Using Larger Than Block-Size Key - Hash Key First");
108 	getxdata(td[5].digest, "aa4ae5e15272d00e95705637ce8a3b55ed402112", 20);
109 
110 	td[6].keylen = 80;
111 	getxdata(td[6].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
112 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
113 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
114 	    td[6].keylen);
115 	td[6].datalen = 73;
116 	(void) strcpy((char *)td[6].data,
117 	    "Test Using Larger Than Block-Size Key and Larger Than One "
118 	    "Block-Size Data");
119 	getxdata(td[6].digest, "e8e99d0f45237d786d6bbaa7965c7808bbff1a91", 20);
120 
121 	num = sizeof (td) / sizeof (test_data_t);
122 	for (i = 0; i < num; i++) {
123 		fail = 0;
124 
125 		(void) printf("Test #%d ", i);
126 		HMACInit(&sha, td[i].key, td[i].keylen);
127 		HMACUpdate(&sha, td[i].data, td[i].datalen);
128 		HMACFinal(&sha, td[i].key, td[i].keylen, digest);
129 
130 		if (bcmp(digest, td[i].digest, 20) != 0) {
131 			(void) printf("FAILED\n");
132 			fail++;
133 		} else {
134 			(void) printf("PASSED\n");
135 		}
136 	}
137 	return (fail);
138 }
139