1/*	$NetBSD: pseudo-random-test.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2
3/*
4 * Copyright (c) 2001 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 *    used to endorse or promote products derived from this software without
21 *    specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34
35#include "krb5_locl.h"
36#include <err.h>
37
38enum { MAXSIZE = 48 };
39
40static struct testcase {
41    krb5_enctype enctype;
42    unsigned char constant[MAXSIZE];
43    size_t constant_len;
44    unsigned char key[MAXSIZE];
45    unsigned char res[MAXSIZE];
46} tests[] = {
47    {ETYPE_AES128_CTS_HMAC_SHA256_128, "test", 4,
48     {0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28, 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C},
49     {0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE, 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86,
50      0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B, 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95}},
51    {ETYPE_AES256_CTS_HMAC_SHA384_192, "test", 4,
52     {0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D, 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98,
53      0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0, 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52},
54     {0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6, 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0,
55      0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C, 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D,
56      0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33, 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2}},
57    {0, {0}, 0, {0}, {0}}
58};
59
60int
61main(int argc, char **argv)
62{
63    struct testcase *t;
64    krb5_context context;
65    krb5_error_code ret;
66    int val = 0;
67
68    ret = krb5_init_context (&context);
69    if (ret)
70	errx (1, "krb5_init_context failed: %d", ret);
71
72    for (t = tests; t->enctype != 0; ++t) {
73	krb5_keyblock key;
74	krb5_crypto crypto;
75	krb5_data constant, prf;
76
77	krb5_data_zero(&prf);
78
79	key.keytype = t->enctype;
80	krb5_enctype_keysize(context, t->enctype, &key.keyvalue.length);
81	key.keyvalue.data   = t->key;
82
83	ret = krb5_crypto_init(context, &key, 0, &crypto);
84	if (ret)
85	    krb5_err (context, 1, ret, "krb5_crypto_init");
86
87	constant.data = t->constant;
88	constant.length = t->constant_len;
89
90	ret = krb5_crypto_prf(context, crypto, &constant, &prf);
91	if (ret)
92	    krb5_err (context, 1, ret, "krb5_crypto_prf");
93
94	if (memcmp(prf.data, t->res, prf.length) != 0) {
95	    const unsigned char *p = prf.data;
96	    int i;
97
98	    printf ("PRF failed (enctype %d)\n", t->enctype);
99	    printf ("should be: ");
100	    for (i = 0; i < prf.length; ++i)
101		printf ("%02x", t->res[i]);
102	    printf ("\nresult was: ");
103	    for (i = 0; i < prf.length; ++i)
104		printf ("%02x", p[i]);
105	    printf ("\n");
106	    val = 1;
107	}
108	krb5_data_free(&prf);
109	krb5_crypto_destroy(context, crypto);
110    }
111    krb5_free_context(context);
112
113    return val;
114}
115