summaryrefslogtreecommitdiff
path: root/src/libtls/tls_prf.c
blob: 918de1e50bef1aa231b604925337cdc4602ba423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "tls_prf.h"

typedef struct private_tls_prf12_t private_tls_prf12_t;

/**
 * Private data of an tls_prf_t object.
 */
struct private_tls_prf12_t {

	/**
	 * Public tls_prf_t interface.
	 */
	tls_prf_t public;

	/**
	 * Underlying primitive PRF
	 */
	prf_t *prf;
};

METHOD(tls_prf_t, set_key12, bool,
	private_tls_prf12_t *this, chunk_t key)
{
	return this->prf->set_key(this->prf, key);
}

/**
 * The P_hash function as in TLS 1.0/1.2
 */
static bool p_hash(prf_t *prf, char *label, chunk_t seed, size_t block_size,
				   size_t bytes, char *out)
{
	char buf[block_size], abuf[block_size];
	chunk_t a;

	/* seed = label + seed */
	seed = chunk_cata("cc", chunk_create(label, strlen(label)), seed);
	/* A(0) = seed */
	a = seed;

	while (TRUE)
	{
		/* A(i) = HMAC_hash(secret, A(i-1)) */
		if (!prf->get_bytes(prf, a, abuf))
		{
			return FALSE;
		}
		a = chunk_from_thing(abuf);
		/* HMAC_hash(secret, A(i) + seed) */
		if (!prf->get_bytes(prf, a, NULL) ||
			!prf->get_bytes(prf, seed, buf))
		{
			return FALSE;
		}

		if (bytes <= block_size)
		{
			memcpy(out, buf, bytes);
			break;
		}
		memcpy(out, buf, block_size);
		out += block_size;
		bytes -= block_size;
	}
	return TRUE;
}

METHOD(tls_prf_t, get_bytes12, bool,
	private_tls_prf12_t *this, char *label, chunk_t seed,
	size_t bytes, char *out)
{
	return p_hash(this->prf, label, seed, this->prf->get_block_size(this->prf),
				  bytes, out);
}

METHOD(tls_prf_t, destroy12, void,
	private_tls_prf12_t *this)
{
	this->prf->destroy(this->prf);
	free(this);
}

/**
 * See header
 */
tls_prf_t *tls_prf_create_12(pseudo_random_function_t prf)
{
	private_tls_prf12_t *this;

	INIT(this,
		.public = {
			.set_key = _set_key12,
			.get_bytes = _get_bytes12,
			.destroy = _destroy12,
		},
		.prf = lib->crypto->create_prf(lib->crypto, prf),
	);
	if (!this->prf)
	{
		free(this);
		return NULL;
	}
	return &this->public;
}


typedef struct private_tls_prf10_t private_tls_prf10_t;

/**
 * Private data of an tls_prf_t object.
 */
struct private_tls_prf10_t {

	/**
	 * Public tls_prf_t interface.
	 */
	tls_prf_t public;

	/**
	 * Underlying MD5 PRF
	 */
	prf_t *md5;

	/**
	 * Underlying SHA1 PRF
	 */
	prf_t *sha1;
};

METHOD(tls_prf_t, set_key10, bool,
	private_tls_prf10_t *this, chunk_t key)
{
	size_t len = key.len / 2 + key.len % 2;

	return this->md5->set_key(this->md5, chunk_create(key.ptr, len)) &&
		   this->sha1->set_key(this->sha1, chunk_create(key.ptr + key.len - len,
														len));
}

METHOD(tls_prf_t, get_bytes10, bool,
	private_tls_prf10_t *this, char *label, chunk_t seed,
	size_t bytes, char *out)
{
	char buf[bytes];

	if (!p_hash(this->md5, label, seed, this->md5->get_block_size(this->md5),
				bytes, out) ||
		!p_hash(this->sha1, label, seed, this->sha1->get_block_size(this->sha1),
				bytes, buf))
	{
		return FALSE;
	}
	memxor(out, buf, bytes);
	return TRUE;
}

METHOD(tls_prf_t, destroy10, void,
	private_tls_prf10_t *this)
{
	DESTROY_IF(this->md5);
	DESTROY_IF(this->sha1);
	free(this);
}

/**
 * See header
 */
tls_prf_t *tls_prf_create_10(pseudo_random_function_t prf)
{
	private_tls_prf10_t *this;

	INIT(this,
		.public = {
			.set_key = _set_key10,
			.get_bytes = _get_bytes10,
			.destroy = _destroy10,
		},
		.md5 = lib->crypto->create_prf(lib->crypto, PRF_HMAC_MD5),
		.sha1 = lib->crypto->create_prf(lib->crypto, PRF_HMAC_SHA1),
	);
	if (!this->md5 || !this->sha1)
	{
		destroy10(this);
		return NULL;
	}
	return &this->public;
}