1*d3273b5bSchristos /* $NetBSD: test_prf.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2007 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of KTH nor the names of its contributors may be
20ca1c9b0cSelric * used to endorse or promote products derived from this software without
21ca1c9b0cSelric * specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ca1c9b0cSelric * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ca1c9b0cSelric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ca1c9b0cSelric * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ca1c9b0cSelric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ca1c9b0cSelric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ca1c9b0cSelric * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ca1c9b0cSelric * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ca1c9b0cSelric * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ca1c9b0cSelric * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric
38ca1c9b0cSelric #include <krb5/hex.h>
39ca1c9b0cSelric #include <err.h>
40ca1c9b0cSelric
41ca1c9b0cSelric /*
42ca1c9b0cSelric * key: string2key(aes256, "testkey", "testkey", default_params)
43ca1c9b0cSelric * input: unhex(1122334455667788)
44ca1c9b0cSelric * output: 58b594b8a61df6e9439b7baa991ff5c1
45ca1c9b0cSelric *
46ca1c9b0cSelric * key: string2key(aes128, "testkey", "testkey", default_params)
47ca1c9b0cSelric * input: unhex(1122334455667788)
48ca1c9b0cSelric * output: ffa2f823aa7f83a8ce3c5fb730587129
49ca1c9b0cSelric */
50ca1c9b0cSelric
51ca1c9b0cSelric int
main(int argc,char ** argv)52ca1c9b0cSelric main(int argc, char **argv)
53ca1c9b0cSelric {
54ca1c9b0cSelric krb5_context context;
55ca1c9b0cSelric krb5_error_code ret;
56ca1c9b0cSelric krb5_keyblock key;
57ca1c9b0cSelric krb5_crypto crypto;
58ca1c9b0cSelric size_t length;
59ca1c9b0cSelric krb5_data input, output, output2;
60ca1c9b0cSelric krb5_enctype etype = ETYPE_AES256_CTS_HMAC_SHA1_96;
61ca1c9b0cSelric
62ca1c9b0cSelric ret = krb5_init_context(&context);
63ca1c9b0cSelric if (ret)
64ca1c9b0cSelric errx(1, "krb5_init_context %d", ret);
65ca1c9b0cSelric
66ca1c9b0cSelric ret = krb5_generate_random_keyblock(context, etype, &key);
67ca1c9b0cSelric if (ret)
68ca1c9b0cSelric krb5_err(context, 1, ret, "krb5_generate_random_keyblock");
69ca1c9b0cSelric
70ca1c9b0cSelric ret = krb5_crypto_prf_length(context, etype, &length);
71ca1c9b0cSelric if (ret)
72ca1c9b0cSelric krb5_err(context, 1, ret, "krb5_crypto_prf_length");
73ca1c9b0cSelric
74ca1c9b0cSelric ret = krb5_crypto_init(context, &key, 0, &crypto);
75ca1c9b0cSelric if (ret)
76ca1c9b0cSelric krb5_err(context, 1, ret, "krb5_crypto_init");
77ca1c9b0cSelric
78ca1c9b0cSelric input.data = rk_UNCONST("foo");
79ca1c9b0cSelric input.length = 3;
80ca1c9b0cSelric
81ca1c9b0cSelric ret = krb5_crypto_prf(context, crypto, &input, &output);
82ca1c9b0cSelric if (ret)
83ca1c9b0cSelric krb5_err(context, 1, ret, "krb5_crypto_prf");
84ca1c9b0cSelric
85ca1c9b0cSelric ret = krb5_crypto_prf(context, crypto, &input, &output2);
86ca1c9b0cSelric if (ret)
87ca1c9b0cSelric krb5_err(context, 1, ret, "krb5_crypto_prf");
88ca1c9b0cSelric
89ca1c9b0cSelric if (krb5_data_cmp(&output, &output2) != 0)
90ca1c9b0cSelric krb5_errx(context, 1, "krb5_data_cmp");
91ca1c9b0cSelric
92ca1c9b0cSelric krb5_data_free(&output);
93ca1c9b0cSelric krb5_data_free(&output2);
94ca1c9b0cSelric
95ca1c9b0cSelric krb5_crypto_destroy(context, crypto);
96ca1c9b0cSelric
97ca1c9b0cSelric krb5_free_keyblock_contents(context, &key);
98ca1c9b0cSelric
99ca1c9b0cSelric krb5_free_context(context);
100ca1c9b0cSelric
101ca1c9b0cSelric return 0;
102ca1c9b0cSelric }
103