xref: /freebsd/crypto/krb5/src/lib/crypto/crypto_tests/t_prf.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_prf.c - PRF test cases */
3 /*
4  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "k5-int.h"
34 
35 struct test {
36     krb5_enctype enctype;
37     krb5_data keybits;
38     krb5_data prf_input;
39     krb5_data expected;
40 } tests[] = {
41     {
42         ENCTYPE_AES128_CTS_HMAC_SHA1_96,
43         { KV5M_DATA, 16,
44           "\xAE\x27\x2E\x7C\xDE\xC8\x6A\xC5\x13\x8C\xDB\x19\x6D\x8E\x29\x7D" },
45         { KV5M_DATA, 2, "\x01\x61" },
46         { KV5M_DATA, 16,
47           "\x77\xB3\x9A\x37\xA8\x68\x92\x0F\x2A\x51\xF9\xDD\x15\x0C\x57\x17" }
48     },
49     {
50         ENCTYPE_AES128_CTS_HMAC_SHA1_96,
51         { KV5M_DATA, 16,
52           "\x67\xAB\x1C\xFE\xF3\x5E\x4C\x27\xFF\xDE\xAC\x60\x38\x5A\x3E\x9C" },
53         { KV5M_DATA, 2, "\x01\x62" },
54         { KV5M_DATA, 16,
55           "\xE0\x6C\x0D\xD3\x1F\xF0\x20\x91\x99\x4F\x2E\xF5\x17\x8B\xFE\x3D" }
56     },
57 
58     {
59         ENCTYPE_AES256_CTS_HMAC_SHA1_96,
60         { KV5M_DATA, 32,
61           "\xC0\x1F\x15\x72\x11\xF7\xB7\x7E\xAA\xF4\x57\xC3\xE1\x56\x69\x01"
62           "\x27\xEE\x12\x7D\x81\x0B\xA6\x39\x2E\x97\xBA\xA2\x43\xEB\x06\x16" },
63         { KV5M_DATA, 2, "\x01\x61" },
64         { KV5M_DATA, 16,
65           "\xB2\x62\x8C\x78\x8E\x2E\x9C\x4A\x9B\xB4\x64\x46\x78\xC2\x9F\x2F" }
66     },
67     {
68         ENCTYPE_AES256_CTS_HMAC_SHA1_96,
69         { KV5M_DATA, 32,
70           "\xC0\x1F\x15\x72\x11\xF7\xB7\x7E\xAA\xF4\x57\xC3\xE1\x56\x69\x01"
71           "\x27\xEE\x12\x7D\x81\x0B\xA6\x39\x2E\x97\xBA\xA2\x43\xEB\x06\x16" },
72         { KV5M_DATA, 2, "\x02\x61" },
73         { KV5M_DATA, 16,
74           "\xB4\x06\x37\x33\x50\xCE\xE8\xA6\x12\x6F\x4A\x9B\x65\xA0\xCD\x21" }
75     },
76     {
77         ENCTYPE_AES256_CTS_HMAC_SHA1_96,
78         { KV5M_DATA, 32,
79           "\x9D\x52\x0D\x2D\x98\x0A\xA7\xCB\x6B\x69\x36\x82\xB6\x2D\xA2\x58"
80           "\xB3\x33\x86\x79\x51\x64\x2C\xE6\x47\xAE\x62\xB1\xE5\xE0\xB5\xE9" },
81         { KV5M_DATA, 2, "\x01\x62" },
82         { KV5M_DATA, 16,
83           "\xFF\x0E\x28\x9E\xA7\x56\xC0\x55\x9A\x0E\x91\x18\x56\x96\x1A\x49" }
84     },
85     {
86         ENCTYPE_AES256_CTS_HMAC_SHA1_96,
87         { KV5M_DATA, 32,
88           "\x9D\x52\x0D\x2D\x98\x0A\xA7\xCB\x6B\x69\x36\x82\xB6\x2D\xA2\x58"
89           "\xB3\x33\x86\x79\x51\x64\x2C\xE6\x47\xAE\x62\xB1\xE5\xE0\xB5\xE9" },
90         { KV5M_DATA, 2, "\x02\x62" },
91         { KV5M_DATA, 16,
92           "\x0D\x67\x4D\xD0\xF9\xA6\x80\x65\x25\xA4\xD9\x2E\x82\x8B\xD1\x5A" }
93     },
94 
95     {
96         ENCTYPE_AES128_CTS_HMAC_SHA256_128,
97         { KV5M_DATA, 16,
98           "\x37\x05\xD9\x60\x80\xC1\x77\x28\xA0\xE8\x00\xEA\xB6\xE0\xD2\x3C" },
99         { KV5M_DATA, 4, "test" },
100         { KV5M_DATA, 32,
101           "\x9D\x18\x86\x16\xF6\x38\x52\xFE\x86\x91\x5B\xB8\x40\xB4\xA8\x86"
102           "\xFF\x3E\x6B\xB0\xF8\x19\xB4\x9B\x89\x33\x93\xD3\x93\x85\x42\x95" }
103     },
104 
105     {
106         ENCTYPE_AES256_CTS_HMAC_SHA384_192,
107         { KV5M_DATA, 32,
108           "\x6D\x40\x4D\x37\xFA\xF7\x9F\x9D\xF0\xD3\x35\x68\xD3\x20\x66\x98"
109           "\x00\xEB\x48\x36\x47\x2E\xA8\xA0\x26\xD1\x6B\x71\x82\x46\x0C\x52" },
110         { KV5M_DATA, 4, "test" },
111         { KV5M_DATA, 48,
112           "\x98\x01\xF6\x9A\x36\x8C\x2B\xF6\x75\xE5\x95\x21\xE1\x77\xD9\xA0"
113           "\x7F\x67\xEF\xE1\xCF\xDE\x8D\x3C\x8D\x6F\x6A\x02\x56\xE3\xB1\x7D"
114           "\xB3\xC1\xB6\x2A\xD1\xB8\x55\x33\x60\xD1\x73\x67\xEB\x15\x14\xD2" }
115     },
116 };
117 
118 int
main(void)119 main(void)
120 {
121     krb5_error_code ret;
122     krb5_data output;
123     krb5_keyblock kb;
124     size_t i, prfsz;
125     const struct test *test;
126 
127     for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
128         test = &tests[i];
129         kb.magic = KV5M_KEYBLOCK;
130         kb.enctype = test->enctype;
131         kb.length = test->keybits.length;
132         kb.contents = (uint8_t *)test->keybits.data;
133 
134         ret = krb5_c_prf_length(NULL, test->enctype, &prfsz);
135         assert(!ret);
136         ret = alloc_data(&output, prfsz);
137         assert(!ret);
138         ret = krb5_c_prf(NULL, &kb, &tests[i].prf_input, &output);
139         assert(!ret);
140 
141         if (!data_eq(output, tests[i].expected)) {
142             printf("Test %d failed\n", (int)i);
143             exit(1);
144         }
145         free(output.data);
146     }
147 
148     return 0;
149 }
150