xref: /illumos-gate/usr/src/test/crypto-tests/tests/hmac/hmac_sha1.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License (), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019 Joyent, Inc.
14  */
15 
16 #include <sys/crypto/ioctl.h>
17 #include <sys/sha1.h>
18 
19 /*
20  * Test data from RFC2202
21  */
22 
23 static uint8_t KEY0[] = {
24 	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
25 	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
26 	0x0b, 0x0b, 0x0b, 0x0b
27 };
28 
29 /* "Hi There" */
30 static uint8_t DATA0[] = { 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 };
31 
32 static uint8_t HMAC0[] = {
33 	0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
34 	0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
35 	0xf1, 0x46, 0xbe, 0x00
36 };
37 
38 
39 /* "Jefe" */
40 static uint8_t KEY1[] = { 0x4a, 0x65, 0x66, 0x65 };
41 
42 /* "what do ya want for nothing?" */
43 static uint8_t DATA1[] = {
44 	0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20,
45 	0x79, 0x61, 0x20, 0x77, 0x61, 0x6e, 0x74, 0x20,
46 	0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68,
47 	0x69, 0x6e, 0x67, 0x3f
48 };
49 
50 static uint8_t HMAC1[] = {
51 	0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2,
52 	0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c,
53 	0x25, 0x9a, 0x7c, 0x79
54 };
55 
56 
57 static uint8_t KEY2[] = {
58 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
59 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
60 	0xaa, 0xaa, 0xaa, 0xaa
61 };
62 
63 static uint8_t DATA2[] = {
64 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
65 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
66 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
67 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
68 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
69 	0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
70 	0xdd, 0xdd
71 };
72 
73 static uint8_t HMAC2[] = {
74 	0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11, 0xcd,
75 	0x91, 0xa3, 0x9a, 0xf4, 0x8a, 0xa1, 0x7b, 0x4f,
76 	0x63, 0xf1, 0x75, 0xd3
77 };
78 
79 
80 static uint8_t KEY3[] = {
81 	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
82 	0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
83 	0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
84 	0x19
85 };
86 
87 static uint8_t DATA3[] = {
88 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
89 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
90 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
91 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
92 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
93 	0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
94 	0xcd, 0xcd
95 };
96 
97 static uint8_t HMAC3[] = {
98 	0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50, 0xc6,
99 	0xbc, 0x84, 0x14, 0xf9, 0xbf, 0x50, 0xc8, 0x6c,
100 	0x2d, 0x72, 0x35, 0xda
101 };
102 
103 
104 static uint8_t KEY4[] = {
105 	0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
106 	0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
107 	0x0c, 0x0c, 0x0c, 0x0c
108 };
109 
110 /* "Test With Truncation" */
111 static uint8_t DATA4[] = {
112 	0x54, 0x65, 0x73, 0x74, 0x20, 0x57, 0x69, 0x74,
113 	0x68, 0x20, 0x54, 0x72, 0x75, 0x6e, 0x63, 0x61,
114 	0x74, 0x69, 0x6f, 0x6e
115 };
116 
117 static uint8_t HMAC4[] = {
118 	0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f,
119 	0xe7, 0xf2, 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32,
120 	0x4a, 0x9a, 0x5a, 0x04
121 };
122 
123 
124 static uint8_t KEY5[] = {
125 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
126 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
127 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
128 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
129 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
130 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
131 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
132 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
133 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
134 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
135 };
136 
137 /* "Test Using Larger Than Block-Size Key - Hash Key First" */
138 static uint8_t DATA5[] = {
139 	0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69,
140 	0x6e, 0x67, 0x20, 0x4c, 0x61, 0x72, 0x67, 0x65,
141 	0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42,
142 	0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a,
143 	0x65, 0x20, 0x4b, 0x65, 0x79, 0x20, 0x2d, 0x20,
144 	0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79,
145 	0x20, 0x46, 0x69, 0x72, 0x73, 0x74
146 };
147 
148 static uint8_t HMAC5[] = {
149 	0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e,
150 	0x95, 0x70, 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55,
151 	0xed, 0x40, 0x21, 0x12
152 };
153 
154 
155 static uint8_t KEY6[] = {
156 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
157 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
158 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
159 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
160 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
161 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
162 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
163 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
164 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
165 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
166 };
167 
168 /*
169  * "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
170  */
171 static uint8_t DATA6[] = {
172 	0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69,
173 	0x6e, 0x67, 0x20, 0x4c, 0x61, 0x72, 0x67, 0x65,
174 	0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42,
175 	0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a,
176 	0x65, 0x20, 0x4b, 0x65, 0x79, 0x20, 0x61, 0x6e,
177 	0x64, 0x20, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x72,
178 	0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x4f, 0x6e,
179 	0x65, 0x20, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x2d,
180 	0x53, 0x69, 0x7a, 0x65, 0x20, 0x44, 0x61, 0x74,
181 	0x61
182 };
183 static uint8_t HMAC6[] = {
184 	0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78,
185 	0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08,
186 	0xbb, 0xff, 0x1a, 0x91
187 };
188 
189 uint8_t *DATA[] = { DATA0, DATA1, DATA2, DATA3, DATA4, DATA5, DATA6 };
190 
191 size_t DATALEN[] = {
192 	sizeof (DATA0), sizeof (DATA1), sizeof (DATA2), sizeof (DATA3),
193 	sizeof (DATA4), sizeof (DATA5), sizeof (DATA6)
194 };
195 
196 uint8_t *KEY[] = { KEY0, KEY1, KEY2, KEY3, KEY4, KEY5, KEY6 };
197 
198 size_t KEYLEN[] = {
199 	sizeof (KEY0), sizeof (KEY1), sizeof (KEY2), sizeof (KEY3),
200 	sizeof (KEY4), sizeof (KEY5), sizeof (KEY6)
201 };
202 
203 uint8_t *HMAC[] = { HMAC0, HMAC1, HMAC2, HMAC3, HMAC4, HMAC5, HMAC6 };
204 
205 char *mechname = SUN_CKM_SHA1_HMAC;
206 size_t msgcount = 7;
207 size_t hmac_len = SHA1_DIGEST_LENGTH;
208