huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
/*
 * TLS PRF P_SHA256
 * Copyright (c) 2011, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */
 
#include "includes.h"
 
#include "common.h"
#include "sha256.h"
 
 
/**
 * tls_prf_sha256 - Pseudo-Random Function for TLS v1.2 (P_SHA256, RFC 5246)
 * @secret: Key for PRF
 * @secret_len: Length of the key in bytes
 * @label: A unique label for each purpose of the PRF
 * @seed: Seed value to bind into the key
 * @seed_len: Length of the seed
 * @out: Buffer for the generated pseudo-random key
 * @outlen: Number of bytes of key to generate
 * Returns: 0 on success, -1 on failure.
 *
 * This function is used to derive new, cryptographically separate keys from a
 * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
 */
void 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)
{
   size_t clen;
   u8 A[SHA256_MAC_LEN];
   u8 P[SHA256_MAC_LEN];
   size_t pos;
   const unsigned char *addr[3];
   size_t len[3];
 
   addr[0] = A;
   len[0] = SHA256_MAC_LEN;
   addr[1] = (unsigned char *) label;
   len[1] = os_strlen(label);
   addr[2] = seed;
   len[2] = seed_len;
 
   /*
    * RFC 5246, Chapter 5
    * A(0) = seed, A(i) = HMAC(secret, A(i-1))
    * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
    * PRF(secret, label, seed) = P_SHA256(secret, label + seed)
    */
 
   hmac_sha256_vector(secret, secret_len, 2, &addr[1], &len[1], A);
 
   pos = 0;
   while (pos < outlen) {
       hmac_sha256_vector(secret, secret_len, 3, addr, len, P);
       hmac_sha256(secret, secret_len, A, SHA256_MAC_LEN, A);
 
       clen = outlen - pos;
       if (clen > SHA256_MAC_LEN)
           clen = SHA256_MAC_LEN;
       os_memcpy(out + pos, P, clen);
       pos += clen;
   }
}