xref: /netbsd-src/external/bsd/wpa/dist/src/crypto/sha256-tlsprf.c (revision 3d6c0713cbbf51a549dcd172f2c1ea93f7781249)
1e604d861Schristos /*
2e604d861Schristos  * TLS PRF P_SHA256
3e604d861Schristos  * Copyright (c) 2011, Jouni Malinen <j@w1.fi>
4e604d861Schristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
7e604d861Schristos  */
8e604d861Schristos 
9e604d861Schristos #include "includes.h"
10e604d861Schristos 
11e604d861Schristos #include "common.h"
12e604d861Schristos #include "sha256.h"
13e604d861Schristos 
14e604d861Schristos 
15e604d861Schristos /**
16e604d861Schristos  * tls_prf_sha256 - Pseudo-Random Function for TLS v1.2 (P_SHA256, RFC 5246)
17e604d861Schristos  * @secret: Key for PRF
18e604d861Schristos  * @secret_len: Length of the key in bytes
19e604d861Schristos  * @label: A unique label for each purpose of the PRF
20e604d861Schristos  * @seed: Seed value to bind into the key
21e604d861Schristos  * @seed_len: Length of the seed
22e604d861Schristos  * @out: Buffer for the generated pseudo-random key
23e604d861Schristos  * @outlen: Number of bytes of key to generate
24e604d861Schristos  * Returns: 0 on success, -1 on failure.
25e604d861Schristos  *
26e604d861Schristos  * This function is used to derive new, cryptographically separate keys from a
27e604d861Schristos  * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
28e604d861Schristos  */
tls_prf_sha256(const u8 * secret,size_t secret_len,const char * label,const u8 * seed,size_t seed_len,u8 * out,size_t outlen)29*3d6c0713Schristos int tls_prf_sha256(const u8 *secret, size_t secret_len, const char *label,
30e604d861Schristos 		   const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
31e604d861Schristos {
32e604d861Schristos 	size_t clen;
33e604d861Schristos 	u8 A[SHA256_MAC_LEN];
34e604d861Schristos 	u8 P[SHA256_MAC_LEN];
35e604d861Schristos 	size_t pos;
36e604d861Schristos 	const unsigned char *addr[3];
37e604d861Schristos 	size_t len[3];
38e604d861Schristos 
39e604d861Schristos 	addr[0] = A;
40e604d861Schristos 	len[0] = SHA256_MAC_LEN;
41e604d861Schristos 	addr[1] = (unsigned char *) label;
42e604d861Schristos 	len[1] = os_strlen(label);
43e604d861Schristos 	addr[2] = seed;
44e604d861Schristos 	len[2] = seed_len;
45e604d861Schristos 
46e604d861Schristos 	/*
47e604d861Schristos 	 * RFC 5246, Chapter 5
48e604d861Schristos 	 * A(0) = seed, A(i) = HMAC(secret, A(i-1))
49e604d861Schristos 	 * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
50e604d861Schristos 	 * PRF(secret, label, seed) = P_SHA256(secret, label + seed)
51e604d861Schristos 	 */
52e604d861Schristos 
53*3d6c0713Schristos 	if (hmac_sha256_vector(secret, secret_len, 2, &addr[1], &len[1], A) < 0)
54*3d6c0713Schristos 		return -1;
55e604d861Schristos 
56e604d861Schristos 	pos = 0;
57e604d861Schristos 	while (pos < outlen) {
58*3d6c0713Schristos 		if (hmac_sha256_vector(secret, secret_len, 3, addr, len, P) <
59*3d6c0713Schristos 		    0 ||
60*3d6c0713Schristos 		    hmac_sha256(secret, secret_len, A, SHA256_MAC_LEN, A) < 0)
61*3d6c0713Schristos 			return -1;
62e604d861Schristos 
63e604d861Schristos 		clen = outlen - pos;
64e604d861Schristos 		if (clen > SHA256_MAC_LEN)
65e604d861Schristos 			clen = SHA256_MAC_LEN;
66e604d861Schristos 		os_memcpy(out + pos, P, clen);
67e604d861Schristos 		pos += clen;
68e604d861Schristos 	}
69*3d6c0713Schristos 
70*3d6c0713Schristos 	return 0;
71e604d861Schristos }
72