xref: /onnv-gate/usr/src/common/net/wanboot/crypt/hmac_test.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * HMAC SHA-1 test cases as defined by RFC 2202.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * The test uses predefined keys, data and digests. The data and keys
33*0Sstevel@tonic-gate  * are used by the HMAC SHA-1 implemention to produce a hash digest and
34*0Sstevel@tonic-gate  * the the result is compared against the expected digest.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "hmac_sha1.h"
42*0Sstevel@tonic-gate #include "hmac_test.h"
43*0Sstevel@tonic-gate #include "cmn_test.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate typedef struct test_data {
46*0Sstevel@tonic-gate 	unsigned char key[80];
47*0Sstevel@tonic-gate 	int keylen;
48*0Sstevel@tonic-gate 	unsigned char data[80];
49*0Sstevel@tonic-gate 	int datalen;
50*0Sstevel@tonic-gate 	unsigned char digest[20];
51*0Sstevel@tonic-gate } test_data_t;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate int
hmactest(void)54*0Sstevel@tonic-gate hmactest(void)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	test_data_t td[7];
57*0Sstevel@tonic-gate 	SHA1_CTX sha;
58*0Sstevel@tonic-gate 	uchar_t digest[20];
59*0Sstevel@tonic-gate 	int fail;
60*0Sstevel@tonic-gate 	int num;
61*0Sstevel@tonic-gate 	int i;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	td[0].keylen = 20;
64*0Sstevel@tonic-gate 	getxdata(td[0].key, "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
65*0Sstevel@tonic-gate 	    td[0].keylen);
66*0Sstevel@tonic-gate 	td[0].datalen = 8;
67*0Sstevel@tonic-gate 	(void) strcpy((char *)td[0].data, "Hi There");
68*0Sstevel@tonic-gate 	getxdata(td[0].digest, "b617318655057264e28bc0b6fb378c8ef146be00", 20);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	td[1].keylen = 4;
71*0Sstevel@tonic-gate 	(void) strcpy((char *)td[1].key, "Jefe");
72*0Sstevel@tonic-gate 	td[1].datalen =  28;
73*0Sstevel@tonic-gate 	(void) strcpy((char *)td[1].data, "what do ya want for nothing?");
74*0Sstevel@tonic-gate 	getxdata(td[1].digest, "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", 20);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	td[2].keylen = 20;
77*0Sstevel@tonic-gate 	getxdata(td[2].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
78*0Sstevel@tonic-gate 	    td[2].keylen);
79*0Sstevel@tonic-gate 	td[2].datalen = 50;
80*0Sstevel@tonic-gate 	getxdata(td[2].data, "ddddddddddddddddddddddddddddddddddddddddddddd"
81*0Sstevel@tonic-gate 	    "ddddddddddddddddddddddddddddddddddddddddddddddddddddddd", 50);
82*0Sstevel@tonic-gate 	getxdata(td[2].digest, "125d7342b9ac11cd91a39af48aa17b4f63f175d3", 20);
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	td[3].keylen = 25;
85*0Sstevel@tonic-gate 	getxdata(td[3].key, "0102030405060708090a0b0c0d0e0f1011121314151617"
86*0Sstevel@tonic-gate 	    "1819", td[3].keylen);
87*0Sstevel@tonic-gate 	td[3].datalen = 50;
88*0Sstevel@tonic-gate 	getxdata(td[3].data, "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
89*0Sstevel@tonic-gate 	    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
90*0Sstevel@tonic-gate 	    td[3].datalen);
91*0Sstevel@tonic-gate 	getxdata(td[3].digest, "4c9007f4026250c6bc8414f9bf50c86c2d7235da", 20);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	td[4].keylen = 20;
94*0Sstevel@tonic-gate 	getxdata(td[4].key, "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
95*0Sstevel@tonic-gate 	    td[4].keylen);
96*0Sstevel@tonic-gate 	td[4].datalen = 20;
97*0Sstevel@tonic-gate 	(void) strcpy((char *)td[4].data, "Test With Truncation");
98*0Sstevel@tonic-gate 	getxdata(td[4].digest, "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04", 20);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	td[5].keylen = 80;
101*0Sstevel@tonic-gate 	getxdata(td[5].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
102*0Sstevel@tonic-gate 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
103*0Sstevel@tonic-gate 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
104*0Sstevel@tonic-gate 	    td[5].keylen);
105*0Sstevel@tonic-gate 	td[5].datalen = 54;
106*0Sstevel@tonic-gate 	(void) strcpy((char *)td[5].data,
107*0Sstevel@tonic-gate 	    "Test Using Larger Than Block-Size Key - Hash Key First");
108*0Sstevel@tonic-gate 	getxdata(td[5].digest, "aa4ae5e15272d00e95705637ce8a3b55ed402112", 20);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	td[6].keylen = 80;
111*0Sstevel@tonic-gate 	getxdata(td[6].key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
112*0Sstevel@tonic-gate 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
113*0Sstevel@tonic-gate 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
114*0Sstevel@tonic-gate 	    td[6].keylen);
115*0Sstevel@tonic-gate 	td[6].datalen = 73;
116*0Sstevel@tonic-gate 	(void) strcpy((char *)td[6].data,
117*0Sstevel@tonic-gate 	    "Test Using Larger Than Block-Size Key and Larger Than One "
118*0Sstevel@tonic-gate 	    "Block-Size Data");
119*0Sstevel@tonic-gate 	getxdata(td[6].digest, "e8e99d0f45237d786d6bbaa7965c7808bbff1a91", 20);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	num = sizeof (td) / sizeof (test_data_t);
122*0Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
123*0Sstevel@tonic-gate 		fail = 0;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		(void) printf("Test #%d ", i);
126*0Sstevel@tonic-gate 		HMACInit(&sha, td[i].key, td[i].keylen);
127*0Sstevel@tonic-gate 		HMACUpdate(&sha, td[i].data, td[i].datalen);
128*0Sstevel@tonic-gate 		HMACFinal(&sha, td[i].key, td[i].keylen, digest);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		if (bcmp(digest, td[i].digest, 20) != 0) {
131*0Sstevel@tonic-gate 			(void) printf("FAILED\n");
132*0Sstevel@tonic-gate 			fail++;
133*0Sstevel@tonic-gate 		} else {
134*0Sstevel@tonic-gate 			(void) printf("PASSED\n");
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 	return (fail);
138*0Sstevel@tonic-gate }
139