1*0a6a1f1dSLionel Sambuc /*	$NetBSD: validate.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2010 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include <sys/types.h>
39ebfedea0SLionel Sambuc #include <limits.h>
40ebfedea0SLionel Sambuc #include <stdio.h>
41ebfedea0SLionel Sambuc #include <stdlib.h>
42ebfedea0SLionel Sambuc #include <string.h>
43ebfedea0SLionel Sambuc #include <krb5/roken.h>
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc #include <evp.h>
46ebfedea0SLionel Sambuc #include <hmac.h>
47ebfedea0SLionel Sambuc #include <err.h>
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc struct tests {
50ebfedea0SLionel Sambuc     const EVP_CIPHER *(*cipher)(void);
51ebfedea0SLionel Sambuc     const char *name;
52ebfedea0SLionel Sambuc     void *key;
53ebfedea0SLionel Sambuc     size_t keysize;
54ebfedea0SLionel Sambuc     void *iv;
55ebfedea0SLionel Sambuc     size_t datasize;
56ebfedea0SLionel Sambuc     void *indata;
57ebfedea0SLionel Sambuc     void *outdata;
58ebfedea0SLionel Sambuc     void *outiv;
59ebfedea0SLionel Sambuc };
60ebfedea0SLionel Sambuc 
61*0a6a1f1dSLionel Sambuc static struct tests hc_tests[] = {
62ebfedea0SLionel Sambuc     {
63ebfedea0SLionel Sambuc 	EVP_aes_256_cbc,
64ebfedea0SLionel Sambuc 	"aes-256",
65ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
67ebfedea0SLionel Sambuc 	32,
68ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
69ebfedea0SLionel Sambuc 	16,
70ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
71ebfedea0SLionel Sambuc 	"\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
72ebfedea0SLionel Sambuc     },
73ebfedea0SLionel Sambuc #if 0
74ebfedea0SLionel Sambuc     {
75ebfedea0SLionel Sambuc 	EVP_aes_128_cfb8,
76ebfedea0SLionel Sambuc 	"aes-cfb8-128",
77ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
78ebfedea0SLionel Sambuc 	16,
79ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
80ebfedea0SLionel Sambuc 	16,
81ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
82ebfedea0SLionel Sambuc 	"\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b\x88\x4c\xfa\x59\xca\x34\x2b\x2e"
83ebfedea0SLionel Sambuc     },
84ebfedea0SLionel Sambuc #endif
85ebfedea0SLionel Sambuc     {
86ebfedea0SLionel Sambuc 	EVP_des_ede3_cbc,
87ebfedea0SLionel Sambuc 	"des-ede3",
88ebfedea0SLionel Sambuc 	"\x19\x17\xff\xe6\xbb\x77\x2e\xfc"
89ebfedea0SLionel Sambuc 	"\x29\x76\x43\xbc\x63\x56\x7e\x9a"
90ebfedea0SLionel Sambuc 	"\x00\x2e\x4d\x43\x1d\x5f\xfd\x58",
91ebfedea0SLionel Sambuc 	24,
92ebfedea0SLionel Sambuc 	"\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
93ebfedea0SLionel Sambuc 	16,
94ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
95ebfedea0SLionel Sambuc 	"\x55\x95\x97\x76\xa9\x6c\x66\x40\x64\xc7\xf4\x1c\x21\xb7\x14\x1b"
96ebfedea0SLionel Sambuc     },
97ebfedea0SLionel Sambuc #if 0
98ebfedea0SLionel Sambuc     {
99ebfedea0SLionel Sambuc 	EVP_camellia_128_cbc,
100ebfedea0SLionel Sambuc 	"camellia128",
101ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
102ebfedea0SLionel Sambuc 	16,
103ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
104ebfedea0SLionel Sambuc 	16,
105ebfedea0SLionel Sambuc 	"\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106ebfedea0SLionel Sambuc 	"\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C",
107ebfedea0SLionel Sambuc 	NULL
108ebfedea0SLionel Sambuc     },
109ebfedea0SLionel Sambuc #endif
110ebfedea0SLionel Sambuc     {
111ebfedea0SLionel Sambuc 	EVP_rc4,
112ebfedea0SLionel Sambuc 	"rc4 8",
113ebfedea0SLionel Sambuc 	"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
114ebfedea0SLionel Sambuc 	8,
115ebfedea0SLionel Sambuc 	NULL,
116ebfedea0SLionel Sambuc 	8,
117ebfedea0SLionel Sambuc 	"\x00\x00\x00\x00\x00\x00\x00\x00",
118ebfedea0SLionel Sambuc 	"\x74\x94\xC2\xE7\x10\x4B\x08\x79",
119ebfedea0SLionel Sambuc 	NULL
120ebfedea0SLionel Sambuc     },
121ebfedea0SLionel Sambuc     {
122ebfedea0SLionel Sambuc 	EVP_rc4,
123ebfedea0SLionel Sambuc 	"rc4 5",
124ebfedea0SLionel Sambuc 	"\x61\x8a\x63\xd2\xfb",
125ebfedea0SLionel Sambuc 	5,
126ebfedea0SLionel Sambuc 	NULL,
127ebfedea0SLionel Sambuc 	5,
128ebfedea0SLionel Sambuc 	"\xdc\xee\x4c\xf9\x2c",
129ebfedea0SLionel Sambuc 	"\xf1\x38\x29\xc9\xde",
130ebfedea0SLionel Sambuc 	NULL
131ebfedea0SLionel Sambuc     },
132ebfedea0SLionel Sambuc     {
133ebfedea0SLionel Sambuc 	EVP_rc4,
134ebfedea0SLionel Sambuc 	"rc4 309",
135ebfedea0SLionel Sambuc 	"\x29\x04\x19\x72\xfb\x42\xba\x5f\xc7\x12\x77\x12\xf1\x38\x29\xc9",
136ebfedea0SLionel Sambuc 	16,
137ebfedea0SLionel Sambuc 	NULL,
138ebfedea0SLionel Sambuc 	309,
139ebfedea0SLionel Sambuc 	"\x52\x75\x69\x73\x6c\x69\x6e\x6e"
140ebfedea0SLionel Sambuc 	"\x75\x6e\x20\x6c\x61\x75\x6c\x75"
141ebfedea0SLionel Sambuc 	"\x20\x6b\x6f\x72\x76\x69\x73\x73"
142ebfedea0SLionel Sambuc 	"\x73\x61\x6e\x69\x2c\x20\x74\xe4"
143ebfedea0SLionel Sambuc 	"\x68\x6b\xe4\x70\xe4\x69\x64\x65"
144ebfedea0SLionel Sambuc 	"\x6e\x20\x70\xe4\xe4\x6c\x6c\xe4"
145ebfedea0SLionel Sambuc 	"\x20\x74\xe4\x79\x73\x69\x6b\x75"
146ebfedea0SLionel Sambuc 	"\x75\x2e\x20\x4b\x65\x73\xe4\x79"
147ebfedea0SLionel Sambuc 	"\xf6\x6e\x20\x6f\x6e\x20\x6f\x6e"
148ebfedea0SLionel Sambuc 	"\x6e\x69\x20\x6f\x6d\x61\x6e\x61"
149ebfedea0SLionel Sambuc 	"\x6e\x69\x2c\x20\x6b\x61\x73\x6b"
150ebfedea0SLionel Sambuc 	"\x69\x73\x61\x76\x75\x75\x6e\x20"
151ebfedea0SLionel Sambuc 	"\x6c\x61\x61\x6b\x73\x6f\x74\x20"
152ebfedea0SLionel Sambuc 	"\x76\x65\x72\x68\x6f\x75\x75\x2e"
153ebfedea0SLionel Sambuc 	"\x20\x45\x6e\x20\x6d\x61\x20\x69"
154ebfedea0SLionel Sambuc 	"\x6c\x6f\x69\x74\x73\x65\x2c\x20"
155ebfedea0SLionel Sambuc 	"\x73\x75\x72\x65\x20\x68\x75\x6f"
156ebfedea0SLionel Sambuc 	"\x6b\x61\x61\x2c\x20\x6d\x75\x74"
157ebfedea0SLionel Sambuc 	"\x74\x61\x20\x6d\x65\x74\x73\xe4"
158ebfedea0SLionel Sambuc 	"\x6e\x20\x74\x75\x6d\x6d\x75\x75"
159ebfedea0SLionel Sambuc 	"\x73\x20\x6d\x75\x6c\x6c\x65\x20"
160ebfedea0SLionel Sambuc 	"\x74\x75\x6f\x6b\x61\x61\x2e\x20"
161ebfedea0SLionel Sambuc 	"\x50\x75\x75\x6e\x74\x6f\x20\x70"
162ebfedea0SLionel Sambuc 	"\x69\x6c\x76\x65\x6e\x2c\x20\x6d"
163ebfedea0SLionel Sambuc 	"\x69\x20\x68\x75\x6b\x6b\x75\x75"
164ebfedea0SLionel Sambuc 	"\x2c\x20\x73\x69\x69\x6e\x74\x6f"
165ebfedea0SLionel Sambuc 	"\x20\x76\x61\x72\x61\x6e\x20\x74"
166ebfedea0SLionel Sambuc 	"\x75\x75\x6c\x69\x73\x65\x6e\x2c"
167ebfedea0SLionel Sambuc 	"\x20\x6d\x69\x20\x6e\x75\x6b\x6b"
168ebfedea0SLionel Sambuc 	"\x75\x75\x2e\x20\x54\x75\x6f\x6b"
169ebfedea0SLionel Sambuc 	"\x73\x75\x74\x20\x76\x61\x6e\x61"
170ebfedea0SLionel Sambuc 	"\x6d\x6f\x6e\x20\x6a\x61\x20\x76"
171ebfedea0SLionel Sambuc 	"\x61\x72\x6a\x6f\x74\x20\x76\x65"
172ebfedea0SLionel Sambuc 	"\x65\x6e\x2c\x20\x6e\x69\x69\x73"
173ebfedea0SLionel Sambuc 	"\x74\xe4\x20\x73\x79\x64\xe4\x6d"
174ebfedea0SLionel Sambuc 	"\x65\x6e\x69\x20\x6c\x61\x75\x6c"
175ebfedea0SLionel Sambuc 	"\x75\x6e\x20\x74\x65\x65\x6e\x2e"
176ebfedea0SLionel Sambuc 	"\x20\x2d\x20\x45\x69\x6e\x6f\x20"
177ebfedea0SLionel Sambuc 	"\x4c\x65\x69\x6e\x6f",
178ebfedea0SLionel Sambuc 	"\x35\x81\x86\x99\x90\x01\xe6\xb5"
179ebfedea0SLionel Sambuc 	"\xda\xf0\x5e\xce\xeb\x7e\xee\x21"
180ebfedea0SLionel Sambuc 	"\xe0\x68\x9c\x1f\x00\xee\xa8\x1f"
181ebfedea0SLionel Sambuc 	"\x7d\xd2\xca\xae\xe1\xd2\x76\x3e"
182ebfedea0SLionel Sambuc 	"\x68\xaf\x0e\xad\x33\xd6\x6c\x26"
183ebfedea0SLionel Sambuc 	"\x8b\xc9\x46\xc4\x84\xfb\xe9\x4c"
184ebfedea0SLionel Sambuc 	"\x5f\x5e\x0b\x86\xa5\x92\x79\xe4"
185ebfedea0SLionel Sambuc 	"\xf8\x24\xe7\xa6\x40\xbd\x22\x32"
186ebfedea0SLionel Sambuc 	"\x10\xb0\xa6\x11\x60\xb7\xbc\xe9"
187ebfedea0SLionel Sambuc 	"\x86\xea\x65\x68\x80\x03\x59\x6b"
188ebfedea0SLionel Sambuc 	"\x63\x0a\x6b\x90\xf8\xe0\xca\xf6"
189ebfedea0SLionel Sambuc 	"\x91\x2a\x98\xeb\x87\x21\x76\xe8"
190ebfedea0SLionel Sambuc 	"\x3c\x20\x2c\xaa\x64\x16\x6d\x2c"
191ebfedea0SLionel Sambuc 	"\xce\x57\xff\x1b\xca\x57\xb2\x13"
192ebfedea0SLionel Sambuc 	"\xf0\xed\x1a\xa7\x2f\xb8\xea\x52"
193ebfedea0SLionel Sambuc 	"\xb0\xbe\x01\xcd\x1e\x41\x28\x67"
194ebfedea0SLionel Sambuc 	"\x72\x0b\x32\x6e\xb3\x89\xd0\x11"
195ebfedea0SLionel Sambuc 	"\xbd\x70\xd8\xaf\x03\x5f\xb0\xd8"
196ebfedea0SLionel Sambuc 	"\x58\x9d\xbc\xe3\xc6\x66\xf5\xea"
197ebfedea0SLionel Sambuc 	"\x8d\x4c\x79\x54\xc5\x0c\x3f\x34"
198ebfedea0SLionel Sambuc 	"\x0b\x04\x67\xf8\x1b\x42\x59\x61"
199ebfedea0SLionel Sambuc 	"\xc1\x18\x43\x07\x4d\xf6\x20\xf2"
200ebfedea0SLionel Sambuc 	"\x08\x40\x4b\x39\x4c\xf9\xd3\x7f"
201ebfedea0SLionel Sambuc 	"\xf5\x4b\x5f\x1a\xd8\xf6\xea\x7d"
202ebfedea0SLionel Sambuc 	"\xa3\xc5\x61\xdf\xa7\x28\x1f\x96"
203ebfedea0SLionel Sambuc 	"\x44\x63\xd2\xcc\x35\xa4\xd1\xb0"
204ebfedea0SLionel Sambuc 	"\x34\x90\xde\xc5\x1b\x07\x11\xfb"
205ebfedea0SLionel Sambuc 	"\xd6\xf5\x5f\x79\x23\x4d\x5b\x7c"
206ebfedea0SLionel Sambuc 	"\x76\x66\x22\xa6\x6d\xe9\x2b\xe9"
207ebfedea0SLionel Sambuc 	"\x96\x46\x1d\x5e\x4d\xc8\x78\xef"
208ebfedea0SLionel Sambuc 	"\x9b\xca\x03\x05\x21\xe8\x35\x1e"
209ebfedea0SLionel Sambuc 	"\x4b\xae\xd2\xfd\x04\xf9\x46\x73"
210ebfedea0SLionel Sambuc 	"\x68\xc4\xad\x6a\xc1\x86\xd0\x82"
211ebfedea0SLionel Sambuc 	"\x45\xb2\x63\xa2\x66\x6d\x1f\x6c"
212ebfedea0SLionel Sambuc 	"\x54\x20\xf1\x59\x9d\xfd\x9f\x43"
213ebfedea0SLionel Sambuc 	"\x89\x21\xc2\xf5\xa4\x63\x93\x8c"
214ebfedea0SLionel Sambuc 	"\xe0\x98\x22\x65\xee\xf7\x01\x79"
215ebfedea0SLionel Sambuc 	"\xbc\x55\x3f\x33\x9e\xb1\xa4\xc1"
216ebfedea0SLionel Sambuc 	"\xaf\x5f\x6a\x54\x7f"
217ebfedea0SLionel Sambuc     }
218ebfedea0SLionel Sambuc };
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc static int
test_cipher(struct tests * t)221ebfedea0SLionel Sambuc test_cipher(struct tests *t)
222ebfedea0SLionel Sambuc {
223ebfedea0SLionel Sambuc     const EVP_CIPHER *c = t->cipher();
224ebfedea0SLionel Sambuc     EVP_CIPHER_CTX ectx;
225ebfedea0SLionel Sambuc     EVP_CIPHER_CTX dctx;
226ebfedea0SLionel Sambuc     void *d;
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_init(&ectx);
229ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_init(&dctx);
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc     if (EVP_CipherInit_ex(&ectx, c, NULL, NULL, NULL, 1) != 1)
232ebfedea0SLionel Sambuc 	errx(1, "%s: EVP_CipherInit_ex einit", t->name);
233ebfedea0SLionel Sambuc     if (EVP_CipherInit_ex(&dctx, c, NULL, NULL, NULL, 0) != 1)
234ebfedea0SLionel Sambuc 	errx(1, "%s: EVP_CipherInit_ex dinit", t->name);
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_set_key_length(&ectx, t->keysize);
237ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_set_key_length(&dctx, t->keysize);
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc     if (EVP_CipherInit_ex(&ectx, NULL, NULL, t->key, t->iv, 1) != 1)
240ebfedea0SLionel Sambuc 	errx(1, "%s: EVP_CipherInit_ex encrypt", t->name);
241ebfedea0SLionel Sambuc     if (EVP_CipherInit_ex(&dctx, NULL, NULL, t->key, t->iv, 0) != 1)
242ebfedea0SLionel Sambuc 	errx(1, "%s: EVP_CipherInit_ex decrypt", t->name);
243ebfedea0SLionel Sambuc 
244ebfedea0SLionel Sambuc     d = emalloc(t->datasize);
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc     if (!EVP_Cipher(&ectx, d, t->indata, t->datasize))
247ebfedea0SLionel Sambuc 	return 1;
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc     if (memcmp(d, t->outdata, t->datasize) != 0)
250ebfedea0SLionel Sambuc 	errx(1, "%s: encrypt not the same", t->name);
251ebfedea0SLionel Sambuc 
252ebfedea0SLionel Sambuc     if (!EVP_Cipher(&dctx, d, d, t->datasize))
253ebfedea0SLionel Sambuc 	return 1;
254ebfedea0SLionel Sambuc 
255ebfedea0SLionel Sambuc     if (memcmp(d, t->indata, t->datasize) != 0)
256ebfedea0SLionel Sambuc 	errx(1, "%s: decrypt not the same", t->name);
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc     if (t->outiv)
259ebfedea0SLionel Sambuc 	/* XXXX check  */;
260ebfedea0SLionel Sambuc 
261ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_cleanup(&ectx);
262ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_cleanup(&dctx);
263ebfedea0SLionel Sambuc     free(d);
264ebfedea0SLionel Sambuc 
265ebfedea0SLionel Sambuc     return 0;
266ebfedea0SLionel Sambuc }
267ebfedea0SLionel Sambuc 
268ebfedea0SLionel Sambuc static void
check_hmac(void)269ebfedea0SLionel Sambuc check_hmac(void)
270ebfedea0SLionel Sambuc {
271ebfedea0SLionel Sambuc     unsigned char buf[4] = { 0, 0, 0, 0 };
272ebfedea0SLionel Sambuc     char hmackey[] = "hello-world";
273ebfedea0SLionel Sambuc     size_t hmackey_size = sizeof(hmackey);
274ebfedea0SLionel Sambuc     unsigned int hmaclen;
275ebfedea0SLionel Sambuc     unsigned char hmac[EVP_MAX_MD_SIZE];
276ebfedea0SLionel Sambuc     HMAC_CTX c;
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc     char answer[20] = "\x2c\xfa\x32\xb7\x2b\x8a\xf6\xdf\xcf\xda"
279ebfedea0SLionel Sambuc 	              "\x6f\xd1\x52\x4d\x54\x58\x73\x0f\xf3\x24";
280ebfedea0SLionel Sambuc 
281ebfedea0SLionel Sambuc     HMAC_CTX_init(&c);
282ebfedea0SLionel Sambuc     HMAC_Init_ex(&c, hmackey, hmackey_size, EVP_sha1(), NULL);
283ebfedea0SLionel Sambuc     HMAC_Update(&c, buf, sizeof(buf));
284ebfedea0SLionel Sambuc     HMAC_Final(&c, hmac, &hmaclen);
285ebfedea0SLionel Sambuc     HMAC_CTX_cleanup(&c);
286ebfedea0SLionel Sambuc 
287ebfedea0SLionel Sambuc     if (hmaclen != 20)
288ebfedea0SLionel Sambuc 	errx(1, "hmaclen = %d\n", (int)hmaclen);
289ebfedea0SLionel Sambuc 
290ebfedea0SLionel Sambuc     if (ct_memcmp(hmac, answer, hmaclen) != 0)
291ebfedea0SLionel Sambuc 	errx(1, "wrong answer\n");
292ebfedea0SLionel Sambuc }
293ebfedea0SLionel Sambuc 
294ebfedea0SLionel Sambuc void
hcrypto_validate(void)295ebfedea0SLionel Sambuc hcrypto_validate(void)
296ebfedea0SLionel Sambuc {
297ebfedea0SLionel Sambuc     static int validated = 0;
298ebfedea0SLionel Sambuc     unsigned int i;
299ebfedea0SLionel Sambuc 
300ebfedea0SLionel Sambuc     /* its ok to run this twice, do don't check for races */
301ebfedea0SLionel Sambuc     if (validated)
302ebfedea0SLionel Sambuc 	return;
303ebfedea0SLionel Sambuc     validated++;
304ebfedea0SLionel Sambuc 
305*0a6a1f1dSLionel Sambuc     for (i = 0; i < sizeof(hc_tests) / sizeof(hc_tests[0]); i++)
306*0a6a1f1dSLionel Sambuc 	test_cipher(&hc_tests[i]);
307ebfedea0SLionel Sambuc 
308ebfedea0SLionel Sambuc     check_hmac();
309ebfedea0SLionel Sambuc }
310