xref: /openbsd-src/regress/lib/libssl/unit/tls_prf.c (revision 8a74ca12807eb6d70b4a0615a3628019b9364c1e)
1 /* $OpenBSD: tls_prf.c,v 1.11 2024/07/16 14:38:59 jsing Exp $ */
2 /*
3  * Copyright (c) 2017 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <err.h>
19 
20 #include "ssl_local.h"
21 
22 int tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
23     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
24     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
25     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len);
26 
27 #define TLS_PRF_OUT_LEN 128
28 
29 struct tls_prf_test {
30 	const unsigned char *desc;
31 	const SSL_METHOD *(*ssl_method)(void);
32 	const uint16_t cipher_value;
33 	const unsigned char out[TLS_PRF_OUT_LEN];
34 };
35 
36 static const struct tls_prf_test tls_prf_tests[] = {
37 	{
38 		.desc = "SHA256",
39 		.ssl_method = TLSv1_2_method,
40 		.cipher_value = 0x0033,
41 		.out = {
42 			 0x37, 0xa7, 0x06, 0x71, 0x6e, 0x19, 0x19, 0xda,
43 			 0x23, 0x8c, 0xcc, 0xb4, 0x2f, 0x31, 0x64, 0x9d,
44 			 0x05, 0x29, 0x1c, 0x33, 0x7e, 0x09, 0x1b, 0x0c,
45 			 0x0e, 0x23, 0xc1, 0xb0, 0x40, 0xcc, 0x31, 0xf7,
46 			 0x55, 0x66, 0x68, 0xd9, 0xa8, 0xae, 0x74, 0x75,
47 			 0xf3, 0x46, 0xe9, 0x3a, 0x54, 0x9d, 0xe0, 0x8b,
48 			 0x7e, 0x6c, 0x63, 0x1c, 0xfa, 0x2f, 0xfd, 0xc9,
49 			 0xd3, 0xf1, 0xd3, 0xfe, 0x7b, 0x9e, 0x14, 0x95,
50 			 0xb5, 0xd0, 0xad, 0x9b, 0xee, 0x78, 0x8c, 0x83,
51 			 0x18, 0x58, 0x7e, 0xa2, 0x23, 0xc1, 0x8b, 0x62,
52 			 0x94, 0x12, 0xcb, 0xb6, 0x60, 0x69, 0x32, 0xfe,
53 			 0x98, 0x0e, 0x93, 0xb0, 0x8e, 0x5c, 0xfb, 0x6e,
54 			 0xdb, 0x9a, 0xc2, 0x9f, 0x8c, 0x5c, 0x43, 0x19,
55 			 0xeb, 0x4a, 0x52, 0xad, 0x62, 0x2b, 0xdd, 0x9f,
56 			 0xa3, 0x74, 0xa6, 0x96, 0x61, 0x4d, 0x98, 0x40,
57 			 0x63, 0xa6, 0xd4, 0xbb, 0x17, 0x11, 0x75, 0xed,
58 		},
59 	},
60 	{
61 		.desc = "SHA384",
62 		.ssl_method = TLSv1_2_method,
63 		.cipher_value = 0x009d,
64 		.out = {
65 			 0x00, 0x93, 0xc3, 0xfd, 0xa7, 0xbb, 0xdc, 0x5b,
66 			 0x13, 0x3a, 0xe6, 0x8b, 0x1b, 0xac, 0xf3, 0xfb,
67 			 0x3c, 0x9a, 0x78, 0xf6, 0x19, 0xf0, 0x13, 0x0f,
68 			 0x0d, 0x01, 0x9d, 0xdf, 0x0a, 0x28, 0x38, 0xce,
69 			 0x1a, 0x9b, 0x43, 0xbe, 0x56, 0x12, 0xa7, 0x16,
70 			 0x58, 0xe1, 0x8a, 0xe4, 0xc5, 0xbb, 0x10, 0x4c,
71 			 0x3a, 0xf3, 0x7f, 0xd3, 0xdb, 0xe4, 0xe0, 0x3d,
72 			 0xcc, 0x83, 0xca, 0xf0, 0xf9, 0x69, 0xcc, 0x70,
73 			 0x83, 0x32, 0xf6, 0xfc, 0x81, 0x80, 0x02, 0xe8,
74 			 0x31, 0x1e, 0x7c, 0x3b, 0x34, 0xf7, 0x34, 0xd1,
75 			 0xcf, 0x2a, 0xc4, 0x36, 0x2f, 0xe9, 0xaa, 0x7f,
76 			 0x6d, 0x1f, 0x5e, 0x0e, 0x39, 0x05, 0x15, 0xe1,
77 			 0xa2, 0x9a, 0x4d, 0x97, 0x8c, 0x62, 0x46, 0xf1,
78 			 0x87, 0x65, 0xd8, 0xe9, 0x14, 0x11, 0xa6, 0x48,
79 			 0xd7, 0x0e, 0x6e, 0x70, 0xad, 0xfb, 0x3f, 0x36,
80 			 0x05, 0x76, 0x4b, 0xe4, 0x28, 0x50, 0x4a, 0xf2,
81 		},
82 	},
83 };
84 
85 #define N_TLS_PRF_TESTS \
86     (sizeof(tls_prf_tests) / sizeof(*tls_prf_tests))
87 
88 #define TLS_PRF_SEED1	"tls prf seed 1"
89 #define TLS_PRF_SEED2	"tls prf seed 2"
90 #define TLS_PRF_SEED3	"tls prf seed 3"
91 #define TLS_PRF_SEED4	"tls prf seed 4"
92 #define TLS_PRF_SEED5	"tls prf seed 5"
93 #define TLS_PRF_SECRET	"tls prf secretz"
94 
95 static void
96 hexdump(const unsigned char *buf, size_t len)
97 {
98 	size_t i;
99 
100 	for (i = 1; i <= len; i++)
101 		fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
102 
103 	fprintf(stderr, "\n");
104 }
105 
106 static int
107 do_tls_prf_test(int test_no, const struct tls_prf_test *tpt)
108 {
109 	unsigned char *out = NULL;
110 	const SSL_CIPHER *cipher;
111 	SSL_CTX *ssl_ctx = NULL;
112 	SSL *ssl = NULL;
113 	int failure = 1;
114 	int len;
115 
116 	fprintf(stderr, "Test %d - %s\n", test_no, tpt->desc);
117 
118 	if ((out = malloc(TLS_PRF_OUT_LEN)) == NULL)
119 		errx(1, "failed to allocate out");
120 
121 	if ((ssl_ctx = SSL_CTX_new(tpt->ssl_method())) == NULL)
122 		errx(1, "failed to create SSL context");
123 	if ((ssl = SSL_new(ssl_ctx)) == NULL)
124 		errx(1, "failed to create SSL context");
125 
126 	if ((cipher = ssl3_get_cipher_by_value(tpt->cipher_value)) == NULL) {
127 		fprintf(stderr, "FAIL: no cipher %hx\n", tpt->cipher_value);
128 		goto failure;
129 	}
130 
131 	ssl->s3->hs.cipher = cipher;
132 
133 	for (len = 1; len <= TLS_PRF_OUT_LEN; len++) {
134 		memset(out, 'A', TLS_PRF_OUT_LEN);
135 
136 		if (tls1_PRF(ssl, TLS_PRF_SECRET, sizeof(TLS_PRF_SECRET),
137 		    TLS_PRF_SEED1, sizeof(TLS_PRF_SEED1), TLS_PRF_SEED2,
138 		    sizeof(TLS_PRF_SEED2), TLS_PRF_SEED3, sizeof(TLS_PRF_SEED3),
139 		    TLS_PRF_SEED4, sizeof(TLS_PRF_SEED4), TLS_PRF_SEED5,
140 		    sizeof(TLS_PRF_SEED5), out, len) != 1) {
141 			fprintf(stderr, "FAIL: tls_PRF failed for len %d\n",
142 			    len);
143 			goto failure;
144 		}
145 
146 		if (memcmp(out, tpt->out, len) != 0) {
147 			fprintf(stderr, "FAIL: tls_PRF output differs for "
148 			    "len %d\n", len);
149 			fprintf(stderr, "output:\n");
150 			hexdump(out, TLS_PRF_OUT_LEN);
151 			fprintf(stderr, "test data:\n");
152 			hexdump(tpt->out, TLS_PRF_OUT_LEN);
153 			fprintf(stderr, "\n");
154 			goto failure;
155 		}
156 	}
157 
158 	failure = 0;
159 
160  failure:
161 	SSL_free(ssl);
162 	SSL_CTX_free(ssl_ctx);
163 
164 	free(out);
165 
166 	return failure;
167 }
168 
169 int
170 main(int argc, char **argv)
171 {
172 	int failed = 0;
173 	size_t i;
174 
175 	SSL_library_init();
176 	SSL_load_error_strings();
177 
178 	for (i = 0; i < N_TLS_PRF_TESTS; i++)
179 		failed |= do_tls_prf_test(i, &tls_prf_tests[i]);
180 
181 	return failed;
182 }
183