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