1*ebfedea0SLionel Sambuc /* $NetBSD: derived-key-test.c,v 1.1.1.1 2011/04/13 18:15:32 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 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 krb5_enctype enctype;
42*ebfedea0SLionel Sambuc unsigned char constant[MAXSIZE];
43*ebfedea0SLionel Sambuc size_t constant_len;
44*ebfedea0SLionel Sambuc unsigned char key[MAXSIZE];
45*ebfedea0SLionel Sambuc unsigned char res[MAXSIZE];
46*ebfedea0SLionel Sambuc } tests[] = {
47*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0x55}, 5,
48*ebfedea0SLionel Sambuc {0xdc, 0xe0, 0x6b, 0x1f, 0x64, 0xc8, 0x57, 0xa1, 0x1c, 0x3d, 0xb5, 0x7c, 0x51, 0x89, 0x9b, 0x2c, 0xc1, 0x79, 0x10, 0x08, 0xce, 0x97, 0x3b, 0x92},
49*ebfedea0SLionel Sambuc {0x92, 0x51, 0x79, 0xd0, 0x45, 0x91, 0xa7, 0x9b, 0x5d, 0x31, 0x92, 0xc4, 0xa7, 0xe9, 0xc2, 0x89, 0xb0, 0x49, 0xc7, 0x1f, 0x6e, 0xe6, 0x04, 0xcd}},
50*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0xaa}, 5,
51*ebfedea0SLionel Sambuc {0x5e, 0x13, 0xd3, 0x1c, 0x70, 0xef, 0x76, 0x57, 0x46, 0x57, 0x85, 0x31, 0xcb, 0x51, 0xc1, 0x5b, 0xf1, 0x1c, 0xa8, 0x2c, 0x97, 0xce, 0xe9, 0xf2},
52*ebfedea0SLionel Sambuc {0x9e, 0x58, 0xe5, 0xa1, 0x46, 0xd9, 0x94, 0x2a, 0x10, 0x1c, 0x46, 0x98, 0x45, 0xd6, 0x7a, 0x20, 0xe3, 0xc4, 0x25, 0x9e, 0xd9, 0x13, 0xf2, 0x07}},
53*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0x55}, 5,
54*ebfedea0SLionel Sambuc {0x98, 0xe6, 0xfd, 0x8a, 0x04, 0xa4, 0xb6, 0x85, 0x9b, 0x75, 0xa1, 0x76, 0x54, 0x0b, 0x97, 0x52, 0xba, 0xd3, 0xec, 0xd6, 0x10, 0xa2, 0x52, 0xbc},
55*ebfedea0SLionel Sambuc {0x13, 0xfe, 0xf8, 0x0d, 0x76, 0x3e, 0x94, 0xec, 0x6d, 0x13, 0xfd, 0x2c, 0xa1, 0xd0, 0x85, 0x07, 0x02, 0x49, 0xda, 0xd3, 0x98, 0x08, 0xea, 0xbf}},
56*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0xaa}, 5,
57*ebfedea0SLionel Sambuc {0x62, 0x2a, 0xec, 0x25, 0xa2, 0xfe, 0x2c, 0xad, 0x70, 0x94, 0x68, 0x0b, 0x7c, 0x64, 0x94, 0x02, 0x80, 0x08, 0x4c, 0x1a, 0x7c, 0xec, 0x92, 0xb5},
58*ebfedea0SLionel Sambuc {0xf8, 0xdf, 0xbf, 0x04, 0xb0, 0x97, 0xe6, 0xd9, 0xdc, 0x07, 0x02, 0x68, 0x6b, 0xcb, 0x34, 0x89, 0xd9, 0x1f, 0xd9, 0xa4, 0x51, 0x6b, 0x70, 0x3e}},
59*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73}, 8,
60*ebfedea0SLionel Sambuc {0xd3, 0xf8, 0x29, 0x8c, 0xcb, 0x16, 0x64, 0x38, 0xdc, 0xb9, 0xb9, 0x3e, 0xe5, 0xa7, 0x62, 0x92, 0x86, 0xa4, 0x91, 0xf8, 0x38, 0xf8, 0x02, 0xfb},
61*ebfedea0SLionel Sambuc {0x23, 0x70, 0xda, 0x57, 0x5d, 0x2a, 0x3d, 0xa8, 0x64, 0xce, 0xbf, 0xdc, 0x52, 0x04, 0xd5, 0x6d, 0xf7, 0x79, 0xa7, 0xdf, 0x43, 0xd9, 0xda, 0x43}},
62*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x65}, 7,
63*ebfedea0SLionel Sambuc {0xb5, 0x5e, 0x98, 0x34, 0x67, 0xe5, 0x51, 0xb3, 0xe5, 0xd0, 0xe5, 0xb6, 0xc8, 0x0d, 0x45, 0x76, 0x94, 0x23, 0xa8, 0x73, 0xdc, 0x62, 0xb3, 0x0e},
64*ebfedea0SLionel Sambuc {0x01, 0x26, 0x38, 0x8a, 0xad, 0xc8, 0x1a, 0x1f, 0x2a, 0x62, 0xbc, 0x45, 0xf8, 0xd5, 0xc1, 0x91, 0x51, 0xba, 0xcd, 0xd5, 0xcb, 0x79, 0x8a, 0x3e}},
65*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0x55}, 5,
66*ebfedea0SLionel Sambuc {0xc1, 0x08, 0x16, 0x49, 0xad, 0xa7, 0x43, 0x62, 0xe6, 0xa1, 0x45, 0x9d, 0x01, 0xdf, 0xd3, 0x0d, 0x67, 0xc2, 0x23, 0x4c, 0x94, 0x07, 0x04, 0xda},
67*ebfedea0SLionel Sambuc {0x34, 0x80, 0x57, 0xec, 0x98, 0xfd, 0xc4, 0x80, 0x16, 0x16, 0x1c, 0x2a, 0x4c, 0x7a, 0x94, 0x3e, 0x92, 0xae, 0x49, 0x2c, 0x98, 0x91, 0x75, 0xf7}},
68*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0xaa}, 5,
69*ebfedea0SLionel Sambuc {0x5d, 0x15, 0x4a, 0xf2, 0x38, 0xf4, 0x67, 0x13, 0x15, 0x57, 0x19, 0xd5, 0x5e, 0x2f, 0x1f, 0x79, 0x0d, 0xd6, 0x61, 0xf2, 0x79, 0xa7, 0x91, 0x7c},
70*ebfedea0SLionel Sambuc {0xa8, 0x80, 0x8a, 0xc2, 0x67, 0xda, 0xda, 0x3d, 0xcb, 0xe9, 0xa7, 0xc8, 0x46, 0x26, 0xfb, 0xc7, 0x61, 0xc2, 0x94, 0xb0, 0x13, 0x15, 0xe5, 0xc1}},
71*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0x55}, 5,
72*ebfedea0SLionel Sambuc {0x79, 0x85, 0x62, 0xe0, 0x49, 0x85, 0x2f, 0x57, 0xdc, 0x8c, 0x34, 0x3b, 0xa1, 0x7f, 0x2c, 0xa1, 0xd9, 0x73, 0x94, 0xef, 0xc8, 0xad, 0xc4, 0x43},
73*ebfedea0SLionel Sambuc {0xc8, 0x13, 0xf8, 0x8a, 0x3b, 0xe3, 0xb3, 0x34, 0xf7, 0x54, 0x25, 0xce, 0x91, 0x75, 0xfb, 0xe3, 0xc8, 0x49, 0x3b, 0x89, 0xc8, 0x70, 0x3b, 0x49}},
74*ebfedea0SLionel Sambuc {ETYPE_DES3_CBC_SHA1, {0x00, 0x00, 0x00, 0x01, 0xaa}, 5,
75*ebfedea0SLionel Sambuc {0x26, 0xdc, 0xe3, 0x34, 0xb5, 0x45, 0x29, 0x2f, 0x2f, 0xea, 0xb9, 0xa8, 0x70, 0x1a, 0x89, 0xa4, 0xb9, 0x9e, 0xb9, 0x94, 0x2c, 0xec, 0xd0, 0x16},
76*ebfedea0SLionel Sambuc {0xf4, 0x8f, 0xfd, 0x6e, 0x83, 0xf8, 0x3e, 0x73, 0x54, 0xe6, 0x94, 0xfd, 0x25, 0x2c, 0xf8, 0x3b, 0xfe, 0x58, 0xf7, 0xd5, 0xba, 0x37, 0xec, 0x5d}},
77*ebfedea0SLionel Sambuc {0}
78*ebfedea0SLionel Sambuc };
79*ebfedea0SLionel Sambuc
80*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)81*ebfedea0SLionel Sambuc main(int argc, char **argv)
82*ebfedea0SLionel Sambuc {
83*ebfedea0SLionel Sambuc struct testcase *t;
84*ebfedea0SLionel Sambuc krb5_context context;
85*ebfedea0SLionel Sambuc krb5_error_code ret;
86*ebfedea0SLionel Sambuc int val = 0;
87*ebfedea0SLionel Sambuc
88*ebfedea0SLionel Sambuc ret = krb5_init_context (&context);
89*ebfedea0SLionel Sambuc if (ret)
90*ebfedea0SLionel Sambuc errx (1, "krb5_init_context failed: %d", ret);
91*ebfedea0SLionel Sambuc
92*ebfedea0SLionel Sambuc for (t = tests; t->enctype != 0; ++t) {
93*ebfedea0SLionel Sambuc krb5_keyblock key;
94*ebfedea0SLionel Sambuc krb5_keyblock *dkey;
95*ebfedea0SLionel Sambuc
96*ebfedea0SLionel Sambuc key.keytype = KEYTYPE_DES3;
97*ebfedea0SLionel Sambuc key.keyvalue.length = MAXSIZE;
98*ebfedea0SLionel Sambuc key.keyvalue.data = t->key;
99*ebfedea0SLionel Sambuc
100*ebfedea0SLionel Sambuc ret = krb5_derive_key(context, &key, t->enctype, t->constant,
101*ebfedea0SLionel Sambuc t->constant_len, &dkey);
102*ebfedea0SLionel Sambuc if (ret)
103*ebfedea0SLionel Sambuc krb5_err (context, 1, ret, "krb5_derive_key");
104*ebfedea0SLionel Sambuc if (memcmp (dkey->keyvalue.data, t->res, dkey->keyvalue.length) != 0) {
105*ebfedea0SLionel Sambuc const unsigned char *p = dkey->keyvalue.data;
106*ebfedea0SLionel Sambuc int i;
107*ebfedea0SLionel Sambuc
108*ebfedea0SLionel Sambuc printf ("derive_key failed\n");
109*ebfedea0SLionel Sambuc printf ("should be: ");
110*ebfedea0SLionel Sambuc for (i = 0; i < dkey->keyvalue.length; ++i)
111*ebfedea0SLionel Sambuc printf ("%02x", t->res[i]);
112*ebfedea0SLionel Sambuc printf ("\nresult was: ");
113*ebfedea0SLionel Sambuc for (i = 0; i < dkey->keyvalue.length; ++i)
114*ebfedea0SLionel Sambuc printf ("%02x", p[i]);
115*ebfedea0SLionel Sambuc printf ("\n");
116*ebfedea0SLionel Sambuc val = 1;
117*ebfedea0SLionel Sambuc }
118*ebfedea0SLionel Sambuc krb5_free_keyblock(context, dkey);
119*ebfedea0SLionel Sambuc }
120*ebfedea0SLionel Sambuc krb5_free_context(context);
121*ebfedea0SLionel Sambuc
122*ebfedea0SLionel Sambuc return val;
123*ebfedea0SLionel Sambuc }
124