xref: /freebsd/crypto/heimdal/lib/krb5/string-to-key-test.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of KTH nor the names of its contributors may be
18b528cefcSMark Murray  *    used to endorse or promote products derived from this software without
19b528cefcSMark Murray  *    specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22b528cefcSMark Murray  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24b528cefcSMark Murray  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25b528cefcSMark Murray  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26b528cefcSMark Murray  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27b528cefcSMark Murray  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28b528cefcSMark Murray  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29b528cefcSMark Murray  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30b528cefcSMark Murray  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31b528cefcSMark Murray  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32b528cefcSMark Murray 
33b528cefcSMark Murray #include "krb5_locl.h"
34c19800e8SDoug Rabson #include <err.h>
35b528cefcSMark Murray 
36b528cefcSMark Murray enum { MAXSIZE = 24 };
37b528cefcSMark Murray 
38b528cefcSMark Murray static struct testcase {
39b528cefcSMark Murray     const char *principal_name;
40b528cefcSMark Murray     const char *password;
41b528cefcSMark Murray     krb5_enctype enctype;
42b528cefcSMark Murray     unsigned char res[MAXSIZE];
43b528cefcSMark Murray } tests[] = {
44*ae771770SStanislav Sedov #ifdef HEIM_WEAK_CRYPTO
45b528cefcSMark Murray     {"@", "", ETYPE_DES_CBC_MD5,
46adb0ddaeSAssar Westerlund      {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xf1}},
47b528cefcSMark Murray     {"nisse@FOO.SE", "hej", ETYPE_DES_CBC_MD5,
48b528cefcSMark Murray      {0xfe, 0x67, 0xbf, 0x9e, 0x57, 0x6b, 0xfe, 0x52}},
49b528cefcSMark Murray     {"assar/liten@FOO.SE", "hemligt", ETYPE_DES_CBC_MD5,
50b528cefcSMark Murray      {0x5b, 0x9b, 0xcb, 0xf2, 0x97, 0x43, 0xc8, 0x40}},
51*ae771770SStanislav Sedov     {"raeburn@ATHENA.MIT.EDU", "password", ETYPE_DES_CBC_MD5,
52*ae771770SStanislav Sedov      {0xcb, 0xc2, 0x2f, 0xae, 0x23, 0x52, 0x98, 0xe3}},
53*ae771770SStanislav Sedov     {"danny@WHITEHOUSE.GOV", "potatoe", ETYPE_DES_CBC_MD5,
54*ae771770SStanislav Sedov      {0xdf, 0x3d, 0x32, 0xa7, 0x4f, 0xd9, 0x2a, 0x01}},
55*ae771770SStanislav Sedov     {"buckaroo@EXAMPLE.COM", "penny", ETYPE_DES_CBC_MD5,
56*ae771770SStanislav Sedov      {0x94, 0x43, 0xa2, 0xe5, 0x32, 0xfd, 0xc4, 0xf1}},
57*ae771770SStanislav Sedov     {"Juri\xc5\xa1i\xc4\x87@ATHENA.MIT.EDU", "\xc3\x9f", ETYPE_DES_CBC_MD5,
58*ae771770SStanislav Sedov      {0x62, 0xc8, 0x1a, 0x52, 0x32, 0xb5, 0xe6, 0x9d}},
59*ae771770SStanislav Sedov     {"AAAAAAAA", "11119999", ETYPE_DES_CBC_MD5,
60*ae771770SStanislav Sedov      {0x98, 0x40, 0x54, 0xd0, 0xf1, 0xa7, 0x3e, 0x31}},
61*ae771770SStanislav Sedov     {"FFFFAAAA", "NNNN6666", ETYPE_DES_CBC_MD5,
62*ae771770SStanislav Sedov      {0xc4, 0xbf, 0x6b, 0x25, 0xad, 0xf7, 0xa4, 0xf8}},
63*ae771770SStanislav Sedov #endif
64c19800e8SDoug Rabson #if 0
65b528cefcSMark Murray     {"@", "", ETYPE_DES3_CBC_SHA1,
66b528cefcSMark Murray      {0xce, 0xa2, 0x2f, 0x9b, 0x52, 0x2c, 0xb0, 0x15, 0x6e, 0x6b, 0x64,
67b528cefcSMark Murray       0x73, 0x62, 0x64, 0x73, 0x4f, 0x6e, 0x73, 0xce, 0xa2, 0x2f, 0x9b,
68b528cefcSMark Murray       0x52, 0x57}},
69c19800e8SDoug Rabson #endif
70b528cefcSMark Murray     {"nisse@FOO.SE", "hej", ETYPE_DES3_CBC_SHA1,
71b528cefcSMark Murray      {0x0e, 0xbc, 0x23, 0x9d, 0x68, 0x46, 0xf2, 0xd5, 0x51, 0x98, 0x5b,
72b528cefcSMark Murray       0x57, 0xc1, 0x57, 0x01, 0x79, 0x04, 0xc4, 0xe9, 0xfe, 0xc1, 0x0e,
73b528cefcSMark Murray       0x13, 0xd0}},
74b528cefcSMark Murray     {"assar/liten@FOO.SE", "hemligt", ETYPE_DES3_CBC_SHA1,
75b528cefcSMark Murray      {0x7f, 0x40, 0x67, 0xb9, 0xbc, 0xc4, 0x40, 0xfb, 0x43, 0x73, 0xd9,
76b528cefcSMark Murray       0xd3, 0xcd, 0x7c, 0xc7, 0x67, 0xe6, 0x79, 0x94, 0xd0, 0xa8, 0x34,
77b528cefcSMark Murray       0xdf, 0x62}},
785e9cd1aeSAssar Westerlund     {"does/not@MATTER", "foo", ETYPE_ARCFOUR_HMAC_MD5,
795e9cd1aeSAssar Westerlund      {0xac, 0x8e, 0x65, 0x7f, 0x83, 0xdf, 0x82, 0xbe,
805e9cd1aeSAssar Westerlund       0xea, 0x5d, 0x43, 0xbd, 0xaf, 0x78, 0x00, 0xcc}},
81adb0ddaeSAssar Westerlund     {"raeburn@ATHENA.MIT.EDU", "password", ETYPE_DES3_CBC_SHA1,
82adb0ddaeSAssar Westerlund      {0x85, 0x0b, 0xb5, 0x13, 0x58, 0x54, 0x8c, 0xd0, 0x5e, 0x86, 0x76, 0x8c, 0x31, 0x3e, 0x3b, 0xfe, 0xf7, 0x51, 0x19, 0x37, 0xdc, 0xf7, 0x2c, 0x3e}},
83adb0ddaeSAssar Westerlund     {"danny@WHITEHOUSE.GOV", "potatoe", ETYPE_DES3_CBC_SHA1,
84adb0ddaeSAssar Westerlund      {0xdf, 0xcd, 0x23, 0x3d, 0xd0, 0xa4, 0x32, 0x04, 0xea, 0x6d, 0xc4, 0x37, 0xfb, 0x15, 0xe0, 0x61, 0xb0, 0x29, 0x79, 0xc1, 0xf7, 0x4f, 0x37, 0x7a}},
85adb0ddaeSAssar Westerlund     {"buckaroo@EXAMPLE.COM", "penny", ETYPE_DES3_CBC_SHA1,
86adb0ddaeSAssar Westerlund      {0x6d, 0x2f, 0xcd, 0xf2, 0xd6, 0xfb, 0xbc, 0x3d, 0xdc, 0xad, 0xb5, 0xda, 0x57, 0x10, 0xa2, 0x34, 0x89, 0xb0, 0xd3, 0xb6, 0x9d, 0x5d, 0x9d, 0x4a}},
87adb0ddaeSAssar Westerlund     {"Juri\xc5\xa1i\xc4\x87@ATHENA.MIT.EDU", "\xc3\x9f", ETYPE_DES3_CBC_SHA1,
88adb0ddaeSAssar Westerlund      {0x16, 0xd5, 0xa4, 0x0e, 0x1c, 0xe3, 0xba, 0xcb, 0x61, 0xb9, 0xdc, 0xe0, 0x04, 0x70, 0x32, 0x4c, 0x83, 0x19, 0x73, 0xa7, 0xb9, 0x52, 0xfe, 0xb0}},
89b528cefcSMark Murray     {NULL}
90b528cefcSMark Murray };
91b528cefcSMark Murray 
92b528cefcSMark Murray int
main(int argc,char ** argv)93b528cefcSMark Murray main(int argc, char **argv)
94b528cefcSMark Murray {
95b528cefcSMark Murray     struct testcase *t;
96b528cefcSMark Murray     krb5_context context;
97b528cefcSMark Murray     krb5_error_code ret;
98b528cefcSMark Murray     int val = 0;
99b528cefcSMark Murray 
1005e9cd1aeSAssar Westerlund     ret = krb5_init_context (&context);
1015e9cd1aeSAssar Westerlund     if (ret)
1025e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
103b528cefcSMark Murray 
104adb0ddaeSAssar Westerlund     /* to enable realm-less principal name above */
105adb0ddaeSAssar Westerlund 
106adb0ddaeSAssar Westerlund     krb5_set_default_realm(context, "");
107adb0ddaeSAssar Westerlund 
108b528cefcSMark Murray     for (t = tests; t->principal_name; ++t) {
109b528cefcSMark Murray 	krb5_keyblock key;
110b528cefcSMark Murray 	krb5_principal principal;
111b528cefcSMark Murray 	int i;
112b528cefcSMark Murray 
113b528cefcSMark Murray 	ret = krb5_parse_name (context, t->principal_name, &principal);
114b528cefcSMark Murray 	if (ret)
115b528cefcSMark Murray 	    krb5_err (context, 1, ret, "krb5_parse_name %s",
116b528cefcSMark Murray 		      t->principal_name);
117b528cefcSMark Murray 	ret = krb5_string_to_key (context, t->enctype, t->password,
118b528cefcSMark Murray 				  principal, &key);
119b528cefcSMark Murray 	if (ret)
120b528cefcSMark Murray 	    krb5_err (context, 1, ret, "krb5_string_to_key");
121b528cefcSMark Murray 	krb5_free_principal (context, principal);
122b528cefcSMark Murray 	if (memcmp (key.keyvalue.data, t->res, key.keyvalue.length) != 0) {
123b528cefcSMark Murray 	    const unsigned char *p = key.keyvalue.data;
124b528cefcSMark Murray 
125b528cefcSMark Murray 	    printf ("string_to_key(%s, %s) failed\n",
126b528cefcSMark Murray 		    t->principal_name, t->password);
127b528cefcSMark Murray 	    printf ("should be: ");
128b528cefcSMark Murray 	    for (i = 0; i < key.keyvalue.length; ++i)
129b528cefcSMark Murray 		printf ("%02x", t->res[i]);
130b528cefcSMark Murray 	    printf ("\nresult was: ");
131b528cefcSMark Murray 	    for (i = 0; i < key.keyvalue.length; ++i)
132b528cefcSMark Murray 		printf ("%02x", p[i]);
133b528cefcSMark Murray 	    printf ("\n");
134b528cefcSMark Murray 	    val = 1;
135b528cefcSMark Murray 	}
136c19800e8SDoug Rabson 	krb5_free_keyblock_contents(context, &key);
137b528cefcSMark Murray     }
138c19800e8SDoug Rabson     krb5_free_context(context);
139b528cefcSMark Murray     return val;
140b528cefcSMark Murray }
141