1 /*
2  * Copyright (c) 2005 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 
33 #include "krb5_locl.h"
34 #include <err.h>
35 #include <getarg.h>
36 
37 RCSID("$Id: test_pkinit_dh2key.c 18809 2006-10-22 07:11:43Z lha $");
38 
39 static void
40 test_dh2key(int i,
41 	    krb5_context context,
42 	    const heim_octet_string *dh,
43 	    const heim_octet_string *c_n,
44 	    const heim_octet_string *k_n,
45 	    krb5_enctype etype,
46 	    const heim_octet_string *result)
47 {
48     krb5_error_code ret;
49     krb5_keyblock key;
50 
51     ret = _krb5_pk_octetstring2key(context,
52 				   etype,
53 				   dh->data, dh->length,
54 				   c_n,
55 				   k_n,
56 				   &key);
57     if (ret != 0)
58 	krb5_err(context, 1, ret, "_krb5_pk_octetstring2key: %d", i);
59 
60     if (key.keyvalue.length != result->length ||
61 	memcmp(key.keyvalue.data, result->data, result->length) != 0)
62 	krb5_errx(context, 1, "resulting key wrong: %d", i);
63 
64     krb5_free_keyblock_contents(context, &key);
65 }
66 
67 
68 struct {
69     krb5_enctype type;
70     krb5_data X;
71     krb5_data key;
72 } tests[] = {
73     /* 0 */
74     {
75 	ETYPE_AES256_CTS_HMAC_SHA1_96,
76 	{
77 	    256,
78 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
79 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
80 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
81 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
82 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
83 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
85 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
86 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
87 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
88 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
89 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
90 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
91 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
92 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
93 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
94 	},
95 	{
96 	    32,
97 	    "\x5e\xe5\x0d\x67\x5c\x80\x9f\xe5\x9e\x4a\x77\x62\xc5\x4b\x65\x83"
98 	    "\x75\x47\xea\xfb\x15\x9b\xd8\xcd\xc7\x5f\xfc\xa5\x91\x1e\x4c\x41"
99 	}
100     },
101     /* 1 */
102     {
103 	ETYPE_AES256_CTS_HMAC_SHA1_96,
104 	{
105 	    128,
106 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
107 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
108 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
109 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
110 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
111 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
112 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
113 	    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
114 	},
115 	{
116 	    32,
117 	    "\xac\xf7\x70\x7c\x08\x97\x3d\xdf\xdb\x27\xcd\x36\x14\x42\xcc\xfb"
118 	    "\xa3\x55\xc8\x88\x4c\xb4\x72\xf3\x7d\xa6\x36\xd0\x7d\x56\x78\x7e"
119 	}
120     },
121     /* 2 */
122     {
123 	ETYPE_AES256_CTS_HMAC_SHA1_96,
124 	{
125 	    128,
126 	    "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
127 	    "\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
128 	    "\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d"
129 	    "\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c"
130 	    "\x0d\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"
131 	    "\x0c\x0d\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
132 	    "\x0b\x0c\x0d\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
133 	    "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08"
134 	},
135 	{
136 	    32,
137 	    "\xc4\x42\xda\x58\x5f\xcb\x80\xe4\x3b\x47\x94\x6f\x25\x40\x93\xe3"
138 	    "\x73\x29\xd9\x90\x01\x38\x0d\xb7\x83\x71\xdb\x3a\xcf\x5c\x79\x7e"
139 	}
140     },
141     /* 3 */
142     {
143 	ETYPE_AES256_CTS_HMAC_SHA1_96,
144 	{
145 	    77,
146 	    "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
147 	    "\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
148 	    "\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d"
149 	    "\x0e\x0f\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c"
150 	    "\x0d\x0e\x0f\x10\x00\x01\x02\x03"
151 	    "\x04\x05\x06\x07\x08"
152 	},
153 	{
154 	    32,
155 	    "\x00\x53\x95\x3b\x84\xc8\x96\xf4\xeb\x38\x5c\x3f\x2e\x75\x1c\x4a"
156 	    "\x59\x0e\xd6\xff\xad\xca\x6f\xf6\x4f\x47\xeb\xeb\x8d\x78\x0f\xfc"
157 	}
158     }
159 };
160 
161 
162 static int version_flag = 0;
163 static int help_flag	= 0;
164 
165 static struct getargs args[] = {
166     {"version",	0,	arg_flag,	&version_flag,
167      "print version", NULL },
168     {"help",	0,	arg_flag,	&help_flag,
169      NULL, NULL }
170 };
171 
172 static void
173 usage (int ret)
174 {
175     arg_printusage (args,
176 		    sizeof(args)/sizeof(*args),
177 		    NULL,
178 		    "");
179     exit (ret);
180 }
181 
182 
183 int
184 main(int argc, char **argv)
185 {
186     krb5_context context;
187     krb5_error_code ret;
188     int i, optidx = 0;
189 
190     setprogname(argv[0]);
191 
192     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
193 	usage(1);
194 
195     if (help_flag)
196 	usage (0);
197 
198     if(version_flag){
199 	print_version(NULL);
200 	exit(0);
201     }
202 
203     argc -= optidx;
204     argv += optidx;
205 
206     ret = krb5_init_context(&context);
207     if (ret)
208 	errx (1, "krb5_init_context failed: %d", ret);
209 
210     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
211 	test_dh2key(i, context, &tests[i].X, NULL, NULL,
212 		    tests[i].type, &tests[i].key);
213     }
214 
215     krb5_free_context(context);
216 
217     return 0;
218 }
219