xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/test_fx.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: test_fx.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2009 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 KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc  *    specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "krb5_locl.h"
36ebfedea0SLionel Sambuc #include <err.h>
37ebfedea0SLionel Sambuc #include <krb5/getarg.h>
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc struct {
40ebfedea0SLionel Sambuc     char *p1;
41ebfedea0SLionel Sambuc     char *pepper1;
42ebfedea0SLionel Sambuc     krb5_enctype e1;
43ebfedea0SLionel Sambuc     char *p2;
44ebfedea0SLionel Sambuc     char *pepper2;
45ebfedea0SLionel Sambuc     krb5_enctype e2;
46ebfedea0SLionel Sambuc     krb5_enctype e3;
47ebfedea0SLionel Sambuc     char *key;
48ebfedea0SLionel Sambuc     size_t len;
49ebfedea0SLionel Sambuc } cf2[] = {
50ebfedea0SLionel Sambuc     {
51ebfedea0SLionel Sambuc 	"key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96,
52ebfedea0SLionel Sambuc 	"key2", "b", ETYPE_AES128_CTS_HMAC_SHA1_96,
53ebfedea0SLionel Sambuc 	ETYPE_AES128_CTS_HMAC_SHA1_96,
54ebfedea0SLionel Sambuc 	"\x97\xdf\x97\xe4\xb7\x98\xb2\x9e\xb3\x1e\xd7\x28\x02\x87\xa9\x2a",
55ebfedea0SLionel Sambuc 	16
56ebfedea0SLionel Sambuc     },
57ebfedea0SLionel Sambuc     {
58ebfedea0SLionel Sambuc 	"key1", "a", ETYPE_AES256_CTS_HMAC_SHA1_96,
59ebfedea0SLionel Sambuc 	"key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96,
60ebfedea0SLionel Sambuc 	ETYPE_AES256_CTS_HMAC_SHA1_96,
61ebfedea0SLionel Sambuc 	"\x4d\x6c\xa4\xe6\x29\x78\x5c\x1f\x01\xba\xf5\x5e\x2e\x54\x85\x66"
62ebfedea0SLionel Sambuc 	"\xb9\x61\x7a\xe3\xa9\x68\x68\xc3\x37\xcb\x93\xb5\xe7\x2b\x1c\x7b",
63ebfedea0SLionel Sambuc 	32
64ebfedea0SLionel Sambuc     },
65ebfedea0SLionel Sambuc     {
66ebfedea0SLionel Sambuc 	"key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96,
67ebfedea0SLionel Sambuc 	"key2", "b", ETYPE_AES128_CTS_HMAC_SHA1_96,
68ebfedea0SLionel Sambuc 	ETYPE_AES256_CTS_HMAC_SHA1_96,
69ebfedea0SLionel Sambuc 	"\x97\xdf\x97\xe4\xb7\x98\xb2\x9e\xb3\x1e\xd7\x28\x2\x87\xa9\x2a"
70ebfedea0SLionel Sambuc 	"\x1\x96\xfa\xf2\x44\xf8\x11\x20\xc2\x1c\x51\x17\xb3\xe6\xeb\x98",
71ebfedea0SLionel Sambuc 	32
72ebfedea0SLionel Sambuc     },
73ebfedea0SLionel Sambuc     {
74ebfedea0SLionel Sambuc 	"key1", "a", ETYPE_AES256_CTS_HMAC_SHA1_96,
75ebfedea0SLionel Sambuc 	"key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96,
76ebfedea0SLionel Sambuc 	ETYPE_AES128_CTS_HMAC_SHA1_96,
77ebfedea0SLionel Sambuc 	"\x4d\x6c\xa4\xe6\x29\x78\x5c\x1f\x01\xba\xf5\x5e\x2e\x54\x85\x66",
78ebfedea0SLionel Sambuc 	16
79ebfedea0SLionel Sambuc     },
80ebfedea0SLionel Sambuc     {
81ebfedea0SLionel Sambuc 	"key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96,
82ebfedea0SLionel Sambuc 	"key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96,
83ebfedea0SLionel Sambuc 	ETYPE_AES256_CTS_HMAC_SHA1_96,
84ebfedea0SLionel Sambuc 	"\x88\xbd\xb2\xa9\xf\x3e\x52\x5a\xb0\x5f\x68\xc5\x43\x9a\x4d\x5e"
85ebfedea0SLionel Sambuc 	"\x9c\x2b\xfd\x2b\x02\x24\xde\x39\xb5\x82\xf4\xbb\x05\xfe\x2\x2e",
86ebfedea0SLionel Sambuc 	32
87ebfedea0SLionel Sambuc     }
88ebfedea0SLionel Sambuc };
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc 
91ebfedea0SLionel Sambuc static void
test_cf2(krb5_context context)92ebfedea0SLionel Sambuc test_cf2(krb5_context context)
93ebfedea0SLionel Sambuc {
94ebfedea0SLionel Sambuc     krb5_error_code ret;
95ebfedea0SLionel Sambuc     krb5_data pw, p1, p2;
96ebfedea0SLionel Sambuc     krb5_salt salt;
97ebfedea0SLionel Sambuc     krb5_keyblock k1, k2, k3;
98ebfedea0SLionel Sambuc     krb5_crypto c1, c2;
99ebfedea0SLionel Sambuc     unsigned int i;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     for (i = 0; i < sizeof(cf2)/sizeof(cf2[0]); i++) {
102ebfedea0SLionel Sambuc 	pw.data = cf2[i].p1;
103ebfedea0SLionel Sambuc 	pw.length = strlen(cf2[i].p1);
104*0a6a1f1dSLionel Sambuc 	salt.salttype = (krb5_salttype)KRB5_PADATA_PW_SALT;
105ebfedea0SLionel Sambuc 	salt.saltvalue.data = cf2[i].p1;
106ebfedea0SLionel Sambuc 	salt.saltvalue.length = strlen(cf2[i].p1);
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc 	ret = krb5_string_to_key_data_salt(context,
109ebfedea0SLionel Sambuc 					   cf2[i].e1,
110ebfedea0SLionel Sambuc 					   pw,
111ebfedea0SLionel Sambuc 					   salt,
112ebfedea0SLionel Sambuc 					   &k1);
113ebfedea0SLionel Sambuc 	if (ret)
114ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "krb5_string_to_key_data_salt");
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc 	ret = krb5_crypto_init(context, &k1, 0, &c1);
117ebfedea0SLionel Sambuc 	if (ret)
118ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "krb5_crypto_init");
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc 	pw.data = cf2[i].p2;
121ebfedea0SLionel Sambuc 	pw.length = strlen(cf2[i].p2);
122ebfedea0SLionel Sambuc 	salt.saltvalue.data = cf2[i].p2;
123ebfedea0SLionel Sambuc 	salt.saltvalue.length = strlen(cf2[i].p2);
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc 	ret = krb5_string_to_key_data_salt(context,
126ebfedea0SLionel Sambuc 					   cf2[i].e2,
127ebfedea0SLionel Sambuc 					   pw,
128ebfedea0SLionel Sambuc 					   salt,
129ebfedea0SLionel Sambuc 					   &k2);
130ebfedea0SLionel Sambuc 	if (ret)
131ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "krb5_string_to_key_data_salt");
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc 	ret = krb5_crypto_init(context, &k2, 0, &c2);
134ebfedea0SLionel Sambuc 	if (ret)
135ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "krb5_crypto_init");
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc 	p1.data = cf2[i].pepper1;
139ebfedea0SLionel Sambuc 	p1.length = strlen(cf2[i].pepper1);
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc 	p2.data = cf2[i].pepper2;
142ebfedea0SLionel Sambuc 	p2.length = strlen(cf2[i].pepper2);
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc 	ret = krb5_crypto_fx_cf2(context, c1, c2, &p1, &p2, cf2[i].e3, &k3);
145ebfedea0SLionel Sambuc 	if (ret)
146ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "krb5_crypto_fx_cf2");
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc 	if (k3.keytype != cf2[i].e3)
149ebfedea0SLionel Sambuc 	    krb5_errx(context, 1, "length not right");
150ebfedea0SLionel Sambuc 	if (k3.keyvalue.length != cf2[i].len ||
151ebfedea0SLionel Sambuc 	    memcmp(k3.keyvalue.data, cf2[i].key, cf2[i].len) != 0)
152ebfedea0SLionel Sambuc 	    krb5_errx(context, 1, "key not same");
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc 	krb5_crypto_destroy(context, c1);
155ebfedea0SLionel Sambuc 	krb5_crypto_destroy(context, c2);
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc 	krb5_free_keyblock_contents(context, &k1);
158ebfedea0SLionel Sambuc 	krb5_free_keyblock_contents(context, &k2);
159ebfedea0SLionel Sambuc 	krb5_free_keyblock_contents(context, &k3);
160ebfedea0SLionel Sambuc     }
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc static int version_flag = 0;
164ebfedea0SLionel Sambuc static int help_flag	= 0;
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc static struct getargs args[] = {
167ebfedea0SLionel Sambuc     {"version",	0,	arg_flag,	&version_flag,
168ebfedea0SLionel Sambuc      "print version", NULL },
169ebfedea0SLionel Sambuc     {"help",	0,	arg_flag,	&help_flag,
170ebfedea0SLionel Sambuc      NULL, NULL }
171ebfedea0SLionel Sambuc };
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc static void
usage(int ret)174ebfedea0SLionel Sambuc usage (int ret)
175ebfedea0SLionel Sambuc {
176ebfedea0SLionel Sambuc     arg_printusage (args,
177ebfedea0SLionel Sambuc 		    sizeof(args)/sizeof(*args),
178ebfedea0SLionel Sambuc 		    NULL,
179ebfedea0SLionel Sambuc 		    "");
180ebfedea0SLionel Sambuc     exit (ret);
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc 
183ebfedea0SLionel Sambuc int
main(int argc,char ** argv)184ebfedea0SLionel Sambuc main(int argc, char **argv)
185ebfedea0SLionel Sambuc {
186ebfedea0SLionel Sambuc     krb5_context context;
187ebfedea0SLionel Sambuc     krb5_error_code ret;
188ebfedea0SLionel Sambuc     int optidx = 0;
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     setprogname(argv[0]);
191ebfedea0SLionel Sambuc 
192ebfedea0SLionel Sambuc     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
193ebfedea0SLionel Sambuc 	usage(1);
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc     if (help_flag)
196ebfedea0SLionel Sambuc 	usage (0);
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc     if(version_flag){
199ebfedea0SLionel Sambuc 	print_version(NULL);
200ebfedea0SLionel Sambuc 	exit(0);
201ebfedea0SLionel Sambuc     }
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
204ebfedea0SLionel Sambuc     if (ret)
205ebfedea0SLionel Sambuc 	errx (1, "krb5_init_context failed: %d", ret);
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc     test_cf2(context);
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     krb5_free_context(context);
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     return 0;
212ebfedea0SLionel Sambuc }
213