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