]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/heimdal/lib/krb5/aes-test.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / heimdal / lib / krb5 / aes-test.c
1 /*
2  * Copyright (c) 2003 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 <hex.h>
35 #include <err.h>
36
37 #ifdef HAVE_OPENSSL
38 #include <openssl/evp.h>
39 #endif
40
41 RCSID("$Id: aes-test.c 18301 2006-10-07 13:50:34Z lha $");
42
43 static int verbose = 0;
44
45 static void
46 hex_dump_data(const void *data, size_t length)
47 {
48     char *p;
49
50     hex_encode(data, length, &p);
51     printf("%s\n", p);
52     free(p);
53 }
54
55 struct {
56     char *password;
57     char *salt;
58     int saltlen;
59     int iterations;
60     krb5_enctype enctype;
61     size_t keylen;
62     char *pbkdf2;
63     char *key;
64 } keys[] = {
65     { 
66         "password", "ATHENA.MIT.EDUraeburn", -1,
67         1, 
68         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
69         "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15",
70         "\x42\x26\x3c\x6e\x89\xf4\xfc\x28\xb8\xdf\x68\xee\x09\x79\x9f\x15"
71     },
72     {
73         "password", "ATHENA.MIT.EDUraeburn", -1,
74         1, 
75         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
76         "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15"
77         "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3\x33\xec\xc0\xe2\xe1\xf7\x08\x37",
78         "\xfe\x69\x7b\x52\xbc\x0d\x3c\xe1\x44\x32\xba\x03\x6a\x92\xe6\x5b"
79         "\xbb\x52\x28\x09\x90\xa2\xfa\x27\x88\x39\x98\xd7\x2a\xf3\x01\x61"
80     },
81     {
82         "password", "ATHENA.MIT.EDUraeburn", -1,
83         2,
84         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
85         "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d",
86         "\xc6\x51\xbf\x29\xe2\x30\x0a\xc2\x7f\xa4\x69\xd6\x93\xbd\xda\x13"
87     },
88     {
89         "password", "ATHENA.MIT.EDUraeburn", -1,
90         2, 
91         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
92         "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
93         "\xa0\x53\x78\xb9\x32\x44\xec\x8f\x48\xa9\x9e\x61\xad\x79\x9d\x86",
94         "\xa2\xe1\x6d\x16\xb3\x60\x69\xc1\x35\xd5\xe9\xd2\xe2\x5f\x89\x61"
95         "\x02\x68\x56\x18\xb9\x59\x14\xb4\x67\xc6\x76\x22\x22\x58\x24\xff"
96     },
97     {
98         "password", "ATHENA.MIT.EDUraeburn", -1,
99         1200, 
100         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
101         "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b",
102         "\x4c\x01\xcd\x46\xd6\x32\xd0\x1e\x6d\xbe\x23\x0a\x01\xed\x64\x2a"
103     },
104     {
105         "password", "ATHENA.MIT.EDUraeburn", -1,
106         1200, 
107         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
108         "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
109         "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f\x70\x8a\x31\xe2\xe6\x2b\x1e\x13",
110         "\x55\xa6\xac\x74\x0a\xd1\x7b\x48\x46\x94\x10\x51\xe1\xe8\xb0\xa7"
111         "\x54\x8d\x93\xb0\xab\x30\xa8\xbc\x3f\xf1\x62\x80\x38\x2b\x8c\x2a"
112     },
113     {
114         "password", "\x12\x34\x56\x78\x78\x56\x34\x12", 8,
115         5,
116         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
117         "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49",
118         "\xe9\xb2\x3d\x52\x27\x37\x47\xdd\x5c\x35\xcb\x55\xbe\x61\x9d\x8e"
119     },
120     {
121         "password", "\x12\x34\x56\x78\x78\x56\x34\x12", 8,
122         5,
123         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
124         "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
125         "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6\xad\xf4\xfa\x57\x4b\x6e\x64\xee",
126         "\x97\xa4\xe7\x86\xbe\x20\xd8\x1a\x38\x2d\x5e\xbc\x96\xd5\x90\x9c"
127         "\xab\xcd\xad\xc8\x7c\xa4\x8f\x57\x45\x04\x15\x9f\x16\xc3\x6e\x31"
128     },
129     {
130         "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
131         "pass phrase equals block size", -1,
132         1200,
133         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
134         "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9",
135         "\x59\xd1\xbb\x78\x9a\x82\x8b\x1a\xa5\x4e\xf9\xc2\x88\x3f\x69\xed"
136     },
137     {
138         "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
139         "pass phrase equals block size", -1,
140         1200,
141         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
142         "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
143         "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1",
144         "\x89\xad\xee\x36\x08\xdb\x8b\xc7\x1f\x1b\xfb\xfe\x45\x94\x86\xb0"
145         "\x56\x18\xb7\x0c\xba\xe2\x20\x92\x53\x4e\x56\xc5\x53\xba\x4b\x34"
146     },
147     {
148         "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
149         "pass phrase exceeds block size", -1,
150         1200,
151         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
152         "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61",
153         "\xcb\x80\x05\xdc\x5f\x90\x17\x9a\x7f\x02\x10\x4c\x00\x18\x75\x1d"
154     },
155     {
156         "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
157         "pass phrase exceeds block size", -1,
158         1200,
159         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
160         "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
161         "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b\x36\xbe\x92\x46\x91\x5e\xc8\x2a",
162         "\xd7\x8c\x5c\x9c\xb8\x72\xa8\xc9\xda\xd4\x69\x7f\x0b\xb5\xb2\xd2"
163         "\x14\x96\xc8\x2b\xeb\x2c\xae\xda\x21\x12\xfc\xee\xa0\x57\x40\x1b"
164
165     },
166     {
167         "\xf0\x9d\x84\x9e" /* g-clef */, "EXAMPLE.COMpianist", -1,
168         50,
169         ETYPE_AES128_CTS_HMAC_SHA1_96, 16,
170         "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39",
171         "\xf1\x49\xc1\xf2\xe1\x54\xa7\x34\x52\xd4\x3e\x7f\xe6\x2a\x56\xe5"
172     },
173     {
174         "\xf0\x9d\x84\x9e" /* g-clef */, "EXAMPLE.COMpianist", -1,
175         50,
176         ETYPE_AES256_CTS_HMAC_SHA1_96, 32,
177         "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
178         "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2\x81\xff\x30\x69\xe1\xe9\x4f\x52",
179         "\x4b\x6d\x98\x39\xf8\x44\x06\xdf\x1f\x09\xcc\x16\x6d\xb4\xb8\x3c"
180         "\x57\x18\x48\xb7\x84\xa3\xd6\xbd\xc3\x46\x58\x9a\x3e\x39\x3f\x9e"
181     },
182     {
183         "foo", "", -1, 
184         0,
185         ETYPE_ARCFOUR_HMAC_MD5, 16,
186         NULL,
187         "\xac\x8e\x65\x7f\x83\xdf\x82\xbe\xea\x5d\x43\xbd\xaf\x78\x00\xcc"
188     },
189     {
190         "test", "", -1, 
191         0,
192         ETYPE_ARCFOUR_HMAC_MD5, 16,
193         NULL,
194         "\x0c\xb6\x94\x88\x05\xf7\x97\xbf\x2a\x82\x80\x79\x73\xb8\x95\x37"
195     }
196 };
197
198 static int
199 string_to_key_test(krb5_context context)
200 {
201     krb5_data password, opaque;
202     krb5_error_code ret;
203     krb5_salt salt;
204     int i, val = 0;
205     char iter[4];
206
207     for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
208
209         password.data = keys[i].password;
210         password.length = strlen(password.data);
211
212         salt.salttype = KRB5_PW_SALT;
213         salt.saltvalue.data = keys[i].salt;
214         if (keys[i].saltlen == -1)
215             salt.saltvalue.length = strlen(salt.saltvalue.data);
216         else
217             salt.saltvalue.length = keys[i].saltlen;
218     
219         opaque.data = iter;
220         opaque.length = sizeof(iter);
221         _krb5_put_int(iter, keys[i].iterations, 4);
222         
223         if (keys[i].pbkdf2) {
224             unsigned char keyout[32];
225
226             if (keys[i].keylen > sizeof(keyout))
227                 abort();
228
229             PKCS5_PBKDF2_HMAC_SHA1(password.data, password.length,
230                                    salt.saltvalue.data, salt.saltvalue.length,
231                                    keys[i].iterations, 
232                                    keys[i].keylen, keyout);
233             
234             if (memcmp(keyout, keys[i].pbkdf2, keys[i].keylen) != 0) {
235                 krb5_warnx(context, "%d: pbkdf2", i);
236                 val = 1;
237                 continue;
238             }
239             
240             if (verbose) {
241                 printf("PBKDF2:\n");
242                 hex_dump_data(keyout, keys[i].keylen);
243             }
244         }
245
246         {
247             krb5_keyblock key;
248
249             ret = krb5_string_to_key_data_salt_opaque (context,
250                                                        keys[i].enctype,
251                                                        password, 
252                                                        salt, 
253                                                        opaque, 
254                                                        &key);
255             if (ret) {
256                 krb5_warn(context, ret, "%d: string_to_key_data_salt_opaque", 
257                           i);
258                 val = 1;
259                 continue;
260             }
261             
262             if (key.keyvalue.length != keys[i].keylen) {
263                 krb5_warnx(context, "%d: key wrong length (%lu/%lu)",
264                            i, (unsigned long)key.keyvalue.length, 
265                            (unsigned long)keys[i].keylen);
266                 val = 1;
267                 continue;
268             }
269             
270             if (memcmp(key.keyvalue.data, keys[i].key, keys[i].keylen) != 0) {
271                 krb5_warnx(context, "%d: key wrong", i);
272                 val = 1;
273                 continue;
274             }
275             
276             if (verbose) {
277                 printf("key:\n");
278                 hex_dump_data(key.keyvalue.data, key.keyvalue.length);
279             }
280             krb5_free_keyblock_contents(context, &key);
281         }
282     }
283     return val;
284 }
285
286 struct enc_test {
287     size_t len;
288     char *input;
289     char *output;
290     char *nextiv;
291 };
292
293 struct enc_test encs1[] = {
294     {
295         17,
296         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
297         "\x20",
298         "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
299         "\x97",
300         "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
301     },
302     {
303         31,
304         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
305         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
306         "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
307         "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
308         "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
309     },
310     {
311         32,
312         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
313         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
314         "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
315         "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
316         "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
317     },
318     {
319         47,
320         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
321         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
322         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
323         "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
324         "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
325         "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
326         "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
327     },
328     {
329         48,
330         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
331         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
332         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
333         "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
334         "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
335         "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
336         "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
337     },
338     {
339         64,
340         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
341         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
342         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
343         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
344         "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
345         "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
346         "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
347         "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
348         "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
349     }
350 };
351         
352
353 struct enc_test encs2[] = {
354     {
355         17,
356         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
357         "\x20",
358         "\x5c\x13\x26\x27\xc4\xcb\xca\x04\x14\x43\x8a\xb5\x97\x97\x7c\x10"
359         "\x16"
360     },
361     {
362         31,
363         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
364         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
365         "\x16\xb3\xd8\xe5\xcd\x93\xe6\x2c\x28\x70\xa0\x36\x6e\x9a\xb9\x74"
366         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53"
367     },
368     {
369         32,
370         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
371         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
372         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
373         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
374     },
375     {
376         47,
377         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
378         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
379         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
380         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
381         "\xe5\x56\xb4\x88\x41\xb9\xde\x27\xf0\x07\xa1\x6e\x89\x94\x47\xf1"
382         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff"
383     },
384     {
385         48,
386         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
387         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
388         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
389         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
390         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
391         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
392     },
393     {
394         64,
395         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
396         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
397         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
398         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
399         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
400         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
401         "\x70\x29\xf2\x6f\x7c\x79\xc1\x77\x91\xad\x94\xb0\x78\x62\x27\x67"
402         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
403     },
404     {
405         78,
406         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
407         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
408         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
409         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e"
410         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41",
411         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
412         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
413         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
414         "\x73\xfb\x2c\x36\x76\xaf\xcf\x31\xff\xe3\x8a\x89\x0c\x7e\x99\x3f"
415         "\x70\x29\xf2\x6f\x7c\x79\xc1\x77\x91\xad\x94\xb0\x78\x62"
416     },
417     {
418         83,
419         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
420         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
421         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
422         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e"
423         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41"
424         "\x41\x41\x41",
425         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
426         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
427         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
428         "\x70\x29\xf2\x6f\x7c\x79\xc1\x77\x91\xad\x94\xb0\x78\x62\x27\x67"
429         "\x65\x39\x3a\xdb\x92\x05\x4d\x4f\x08\xa1\xfa\x59\xda\x56\x58\x0e"
430         "\x3b\xac\x12"
431     },
432     {
433         92,
434         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
435         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
436         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
437         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e"
438         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41"
439         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41",
440         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
441         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
442         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
443         "\x70\x29\xf2\x6f\x7c\x79\xc1\x77\x91\xad\x94\xb0\x78\x62\x27\x67"
444         "\x0c\xff\xd7\x63\x50\xf8\x4e\xf9\xec\x56\x1c\x79\xc5\xc8\xfe\x50"
445         "\x3b\xac\x12\x6e\xd3\x2d\x02\xc4\xe5\x06\x43\x5f"
446     },
447     {
448         96,
449         "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
450         "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
451         "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
452         "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e"
453         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41"
454         "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41",
455         "\x16\xc1\xee\xdf\x39\xc8\x3f\xfb\xc5\xf6\x72\xe9\xc1\x6e\x53\x0c"
456         "\x69\xde\xce\x59\x83\x6a\x82\xe1\xcd\x21\x93\xd0\x9e\x2a\xff\xc8"
457         "\xfd\x68\xd1\x56\x32\x23\x7b\xfa\xb0\x09\x86\x3b\x17\x53\xfa\x30"
458         "\x70\x29\xf2\x6f\x7c\x79\xc1\x77\x91\xad\x94\xb0\x78\x62\x27\x67"
459         "\x08\x28\x49\xad\xfc\x2d\x8e\x86\xae\x69\xa5\xa8\xd9\x29\x9e\xe4"
460         "\x3b\xac\x12\x6e\xd3\x2d\x02\xc4\xe5\x06\x43\x5f\x4c\x41\xd1\xb8"
461     }
462 };
463
464
465
466 char *aes_key1 = 
467         "\x63\x68\x69\x63\x6b\x65\x6e\x20\x74\x65\x72\x69\x79\x61\x6b\x69";
468
469 char *aes_key2 =
470         "\x63\x68\x69\x63\x6b\x65\x6e\x20\x74\x65\x72\x69\x79\x61\x6b\x69"
471         "\x2c\x20\x79\x75\x6d\x6d\x79\x20\x79\x75\x6d\x6d\x79\x21\x21\x21";
472
473
474 static int
475 samep(int testn, char *type, const void *pp1, const void *pp2, size_t len)
476 {
477     const unsigned char *p1 = pp1, *p2 = pp2;
478     size_t i;
479     int val = 1;
480
481     for (i = 0; i < len; i++) {
482         if (p1[i] != p2[i]) {
483             if (verbose)
484                 printf("M");
485             val = 0;
486         } else {
487             if (verbose)
488                 printf(".");
489         }
490     }
491     if (verbose)
492         printf("\n");
493     return val;
494 }
495
496 static int
497 encryption_test(krb5_context context, const void *key, size_t keylen,
498                 struct enc_test *enc, int numenc)
499 {
500     unsigned char iv[AES_BLOCK_SIZE];
501     int i, val, failed = 0;
502     AES_KEY ekey, dkey;
503     unsigned char *p;
504
505     AES_set_encrypt_key(key, keylen, &ekey);
506     AES_set_decrypt_key(key, keylen, &dkey);
507
508     for (i = 0; i < numenc; i++) {
509         val = 0;
510
511         if (verbose)
512             printf("test: %d\n", i);
513         memset(iv, 0, sizeof(iv));
514
515         p = malloc(enc[i].len + 1);
516         if (p == NULL)
517             krb5_errx(context, 1, "malloc");
518
519         p[enc[i].len] = '\0';
520
521         memcpy(p, enc[i].input, enc[i].len);
522
523         _krb5_aes_cts_encrypt(p, p, enc[i].len,
524                               &ekey, iv, AES_ENCRYPT);
525
526         if (p[enc[i].len] != '\0') {
527             krb5_warnx(context, "%d: encrypt modified off end", i);
528             val = 1;
529         }
530
531         if (!samep(i, "cipher", p, enc[i].output, enc[i].len)) {
532             krb5_warnx(context, "%d: cipher", i);
533             val = 1;
534         }
535
536         if (enc[i].nextiv && !samep(i, "iv", iv, enc[i].nextiv, 16)){ /*XXX*/ 
537             krb5_warnx(context, "%d: iv", i);
538             val = 1;
539         }
540
541         memset(iv, 0, sizeof(iv));
542
543         _krb5_aes_cts_encrypt(p, p, enc[i].len,
544                               &dkey, iv, AES_DECRYPT);
545
546         if (p[enc[i].len] != '\0') {
547             krb5_warnx(context, "%d: decrypt modified off end", i);
548             val = 1;
549         }
550
551         if (!samep(i, "clear", p, enc[i].input, enc[i].len))
552             val = 1;
553
554         if (enc[i].nextiv && !samep(i, "iv", iv, enc[i].nextiv, 16)){ /*XXX*/ 
555             krb5_warnx(context, "%d: iv", i);
556             val = 1;
557         }
558
559         free(p);
560
561         if (val) {
562             printf("test %d failed\n", i);
563             failed = 1;
564         }
565         val = 0;
566     }
567     return failed;
568 }
569
570 static int
571 krb_enc(krb5_context context,
572         krb5_crypto crypto, 
573         unsigned usage,
574         krb5_data *cipher, 
575         krb5_data *clear)
576 {
577     krb5_data decrypt;
578     krb5_error_code ret;
579
580     krb5_data_zero(&decrypt);
581
582     ret = krb5_decrypt(context,
583                        crypto,
584                        usage,
585                        cipher->data,
586                        cipher->length,
587                        &decrypt);
588
589     if (ret) {
590         krb5_warn(context, ret, "krb5_decrypt");
591         return ret;
592     }
593
594     if (decrypt.length != clear->length ||
595         memcmp(decrypt.data, clear->data, decrypt.length) != 0) {
596         krb5_warnx(context, "clear text not same");
597         return EINVAL;
598     }
599
600     krb5_data_free(&decrypt);
601
602     return 0;
603 }
604
605 static int
606 krb_enc_mit(krb5_context context,
607             krb5_enctype enctype,
608             krb5_keyblock *key,
609             unsigned usage,
610             krb5_data *cipher, 
611             krb5_data *clear)
612 {
613     krb5_error_code ret;
614     krb5_enc_data e;
615     krb5_data decrypt;
616     size_t len;
617
618     e.kvno = 0;
619     e.enctype = enctype;
620     e.ciphertext = *cipher;
621
622     ret = krb5_c_decrypt(context, *key, usage, NULL, &e, &decrypt);
623     if (ret)
624         return ret;
625
626     if (decrypt.length != clear->length ||
627         memcmp(decrypt.data, clear->data, decrypt.length) != 0) {
628         krb5_warnx(context, "clear text not same");
629         return EINVAL;
630     }
631
632     krb5_data_free(&decrypt);
633
634     ret = krb5_c_encrypt_length(context, enctype, clear->length, &len);
635     if (ret)
636         return ret;
637
638     if (len != cipher->length) {
639         krb5_warnx(context, "c_encrypt_length wrong %lu != %lu",
640                    (unsigned long)len, (unsigned long)cipher->length);
641         return EINVAL;
642     }
643
644     return 0;
645 }
646
647
648 struct {
649     krb5_enctype enctype;
650     unsigned usage;
651     size_t keylen;
652     void *key;
653     size_t elen;
654     void* edata;
655     size_t plen;
656     void *pdata;
657 } krbencs[] =  {
658     { 
659         ETYPE_AES256_CTS_HMAC_SHA1_96,
660         7,
661         32,   
662         "\x47\x75\x69\x64\x65\x6c\x69\x6e\x65\x73\x20\x74\x6f\x20\x41\x75"
663         "\x74\x68\x6f\x72\x73\x20\x6f\x66\x20\x49\x6e\x74\x65\x72\x6e\x65",
664         44,
665         "\xcf\x79\x8f\x0d\x76\xf3\xe0\xbe\x8e\x66\x94\x70\xfa\xcc\x9e\x91"
666         "\xa9\xec\x1c\x5c\x21\xfb\x6e\xef\x1a\x7a\xc8\xc1\xcc\x5a\x95\x24"
667         "\x6f\x9f\xf4\xd5\xbe\x5d\x59\x97\x44\xd8\x47\xcd",
668         16,
669         "\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x2e\x0a"
670     }
671 };
672
673
674 static int
675 krb_enc_test(krb5_context context)
676 {
677     krb5_error_code ret;
678     krb5_crypto crypto;
679     krb5_keyblock kb;
680     krb5_data cipher, plain;
681     int i, failed = 0;
682
683     for (i = 0; i < sizeof(krbencs)/sizeof(krbencs[0]); i++) {
684
685         kb.keytype = krbencs[i].enctype;
686         kb.keyvalue.length = krbencs[i].keylen;
687         kb.keyvalue.data = krbencs[i].key;
688
689         ret = krb5_crypto_init(context, &kb, krbencs[i].enctype, &crypto);
690
691         cipher.length = krbencs[i].elen;
692         cipher.data = krbencs[i].edata;
693         plain.length = krbencs[i].plen;
694         plain.data = krbencs[i].pdata;
695         
696         ret = krb_enc(context, crypto, krbencs[i].usage, &cipher, &plain);
697                        
698         if (ret) {
699             failed = 1;
700             printf("krb_enc failed with %d\n", ret);
701         }
702         krb5_crypto_destroy(context, crypto);
703
704         ret = krb_enc_mit(context, krbencs[i].enctype, &kb, 
705                           krbencs[i].usage, &cipher, &plain);
706         if (ret) {
707             failed = 1;
708             printf("krb_enc_mit failed with %d\n", ret);
709         }
710
711     }
712
713     return failed;
714 }
715
716
717 static int
718 random_to_key(krb5_context context)
719 {
720     krb5_error_code ret;
721     krb5_keyblock key;
722
723     ret = krb5_random_to_key(context,
724                              ETYPE_DES3_CBC_SHA1,
725                              "\x21\x39\x04\x58\x6A\xBD\x7F"
726                              "\x21\x39\x04\x58\x6A\xBD\x7F"
727                              "\x21\x39\x04\x58\x6A\xBD\x7F",
728                              21,
729                              &key);
730     if (ret){
731         krb5_warn(context, ret, "random_to_key");
732         return 1;
733     }
734     if (key.keyvalue.length != 24)
735         return 1;
736
737     if (memcmp(key.keyvalue.data,
738                "\x20\x38\x04\x58\x6b\xbc\x7f\xc7"
739                "\x20\x38\x04\x58\x6b\xbc\x7f\xc7"
740                "\x20\x38\x04\x58\x6b\xbc\x7f\xc7",
741                24) != 0)
742         return 1;
743
744     krb5_free_keyblock_contents(context, &key);
745
746     return 0;
747 }
748
749
750 int
751 main(int argc, char **argv)
752 {
753     krb5_error_code ret;
754     krb5_context context;
755     int val = 0;
756     
757     ret = krb5_init_context (&context);
758     if (ret)
759         errx (1, "krb5_init_context failed: %d", ret);
760
761     val |= string_to_key_test(context);
762
763     val |= encryption_test(context, aes_key1, 128,
764                            encs1, sizeof(encs1)/sizeof(encs1[0]));
765     val |= encryption_test(context, aes_key2, 256, 
766                            encs2, sizeof(encs2)/sizeof(encs2[0]));
767     val |= krb_enc_test(context);
768     val |= random_to_key(context);
769
770     if (verbose && val == 0)
771         printf("all ok\n");
772     if (val)
773         printf("tests failed\n");
774
775     krb5_free_context(context);
776
777     return val;
778 }