xref: /freebsd/crypto/heimdal/lib/krb5/string-to-key-test.c (revision 5f1c68f7489c12bce03cdbe6af4faa34f7031683)
1 /*
2  * Copyright (c) 1999 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 
35 RCSID("$Id: string-to-key-test.c,v 1.2 1999/10/28 23:10:38 assar Exp $");
36 
37 enum { MAXSIZE = 24 };
38 
39 static struct testcase {
40     const char *principal_name;
41     const char *password;
42     krb5_enctype enctype;
43     unsigned char res[MAXSIZE];
44 } tests[] = {
45     {"@", "", ETYPE_DES_CBC_MD5,
46      {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}},
47     {"nisse@FOO.SE", "hej", ETYPE_DES_CBC_MD5,
48      {0xfe, 0x67, 0xbf, 0x9e, 0x57, 0x6b, 0xfe, 0x52}},
49     {"assar/liten@FOO.SE", "hemligt", ETYPE_DES_CBC_MD5,
50      {0x5b, 0x9b, 0xcb, 0xf2, 0x97, 0x43, 0xc8, 0x40}},
51     {"@", "", ETYPE_DES3_CBC_SHA1,
52      {0xce, 0xa2, 0x2f, 0x9b, 0x52, 0x2c, 0xb0, 0x15, 0x6e, 0x6b, 0x64,
53       0x73, 0x62, 0x64, 0x73, 0x4f, 0x6e, 0x73, 0xce, 0xa2, 0x2f, 0x9b,
54       0x52, 0x57}},
55     {"nisse@FOO.SE", "hej", ETYPE_DES3_CBC_SHA1,
56      {0x0e, 0xbc, 0x23, 0x9d, 0x68, 0x46, 0xf2, 0xd5, 0x51, 0x98, 0x5b,
57       0x57, 0xc1, 0x57, 0x01, 0x79, 0x04, 0xc4, 0xe9, 0xfe, 0xc1, 0x0e,
58       0x13, 0xd0}},
59     {"assar/liten@FOO.SE", "hemligt", ETYPE_DES3_CBC_SHA1,
60      {0x7f, 0x40, 0x67, 0xb9, 0xbc, 0xc4, 0x40, 0xfb, 0x43, 0x73, 0xd9,
61       0xd3, 0xcd, 0x7c, 0xc7, 0x67, 0xe6, 0x79, 0x94, 0xd0, 0xa8, 0x34,
62       0xdf, 0x62}},
63     {NULL}
64 };
65 
66 int
67 main(int argc, char **argv)
68 {
69     struct testcase *t;
70     krb5_context context;
71     krb5_error_code ret;
72     int val = 0;
73 
74     krb5_init_context (&context);
75 
76     for (t = tests; t->principal_name; ++t) {
77 	krb5_keyblock key;
78 	krb5_principal principal;
79 	int i;
80 
81 	ret = krb5_parse_name (context, t->principal_name, &principal);
82 	if (ret)
83 	    krb5_err (context, 1, ret, "krb5_parse_name %s",
84 		      t->principal_name);
85 	ret = krb5_string_to_key (context, t->enctype, t->password,
86 				  principal, &key);
87 	if (ret)
88 	    krb5_err (context, 1, ret, "krb5_string_to_key");
89 	krb5_free_principal (context, principal);
90 	if (memcmp (key.keyvalue.data, t->res, key.keyvalue.length) != 0) {
91 	    const unsigned char *p = key.keyvalue.data;
92 
93 	    printf ("string_to_key(%s, %s) failed\n",
94 		    t->principal_name, t->password);
95 	    printf ("should be: ");
96 	    for (i = 0; i < key.keyvalue.length; ++i)
97 		printf ("%02x", t->res[i]);
98 	    printf ("\nresult was: ");
99 	    for (i = 0; i < key.keyvalue.length; ++i)
100 		printf ("%02x", p[i]);
101 	    printf ("\n");
102 	    val = 1;
103 	}
104     }
105     return val;
106 }
107