Coverage Report

Created: 2026-06-07 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/freebl/tlsprfalg.c
Line
Count
Source
1
/* tlsprfalg.c - TLS Pseudo Random Function (PRF) implementation
2
 *
3
 * This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifdef FREEBL_NO_DEPEND
8
#include "stubs.h"
9
#endif
10
11
#include "blapi.h"
12
#include "hasht.h"
13
#include "alghmac.h"
14
15
#define PHASH_STATE_MAX_LEN HASH_LENGTH_MAX
16
17
/* TLS P_hash function */
18
SECStatus
19
TLS_P_hash(HASH_HashType hashType, const SECItem *secret, const char *label,
20
           SECItem *seed, SECItem *result, PRBool isFIPS)
21
665k
{
22
665k
    unsigned char state[PHASH_STATE_MAX_LEN];
23
665k
    unsigned char outbuf[PHASH_STATE_MAX_LEN];
24
665k
    unsigned int state_len = 0, label_len = 0, outbuf_len = 0, chunk_size;
25
665k
    unsigned int remaining;
26
665k
    unsigned char *res;
27
665k
    SECStatus status;
28
665k
    HMACContext *cx;
29
665k
    SECStatus rv = SECFailure;
30
665k
    const SECHashObject *hashObj = HASH_GetRawHashObject(hashType);
31
32
665k
    PORT_Assert((secret != NULL) && (secret->data != NULL || !secret->len));
33
665k
    PORT_Assert((seed != NULL) && (seed->data != NULL));
34
665k
    PORT_Assert((result != NULL) && (result->data != NULL));
35
36
665k
    remaining = result->len;
37
665k
    res = result->data;
38
39
665k
    if (label != NULL)
40
374k
        label_len = PORT_Strlen(label);
41
42
665k
    cx = HMAC_Create(hashObj, secret->data, secret->len, isFIPS);
43
665k
    if (cx == NULL)
44
0
        goto loser;
45
46
    /* initialize the state = A(1) = HMAC_hash(secret, seed) */
47
665k
    HMAC_Begin(cx);
48
665k
    HMAC_Update(cx, (unsigned char *)label, label_len);
49
665k
    HMAC_Update(cx, seed->data, seed->len);
50
665k
    status = HMAC_Finish(cx, state, &state_len, sizeof(state));
51
665k
    if (status != SECSuccess)
52
0
        goto loser;
53
54
    /* generate a block at a time until we're done */
55
2.32M
    while (remaining > 0) {
56
57
1.65M
        HMAC_Begin(cx);
58
1.65M
        HMAC_Update(cx, state, state_len);
59
1.65M
        if (label_len)
60
1.36M
            HMAC_Update(cx, (unsigned char *)label, label_len);
61
1.65M
        HMAC_Update(cx, seed->data, seed->len);
62
1.65M
        status = HMAC_Finish(cx, outbuf, &outbuf_len, sizeof(outbuf));
63
1.65M
        if (status != SECSuccess)
64
0
            goto loser;
65
66
        /* Update the state = A(i) = HMAC_hash(secret, A(i-1)) */
67
1.65M
        HMAC_Begin(cx);
68
1.65M
        HMAC_Update(cx, state, state_len);
69
1.65M
        status = HMAC_Finish(cx, state, &state_len, sizeof(state));
70
1.65M
        if (status != SECSuccess)
71
0
            goto loser;
72
73
1.65M
        chunk_size = PR_MIN(outbuf_len, remaining);
74
1.65M
        PORT_Memcpy(res, &outbuf, chunk_size);
75
1.65M
        res += chunk_size;
76
1.65M
        remaining -= chunk_size;
77
1.65M
    }
78
79
665k
    rv = SECSuccess;
80
81
665k
loser:
82
    /* clear out state so it's not left on the stack */
83
665k
    if (cx)
84
665k
        HMAC_Destroy(cx, PR_TRUE);
85
665k
    PORT_SafeZero(state, sizeof(state));
86
665k
    PORT_SafeZero(outbuf, sizeof(outbuf));
87
665k
    return rv;
88
665k
}
89
90
SECStatus
91
TLS_PRF(const SECItem *secret, const char *label, SECItem *seed,
92
        SECItem *result, PRBool isFIPS)
93
175k
{
94
175k
    SECStatus rv = SECFailure, status;
95
175k
    unsigned int i;
96
175k
    SECItem tmp = { siBuffer, NULL, 0 };
97
175k
    SECItem S1;
98
175k
    SECItem S2;
99
100
175k
    PORT_Assert((secret != NULL) && (secret->data != NULL || !secret->len));
101
175k
    PORT_Assert((seed != NULL) && (seed->data != NULL));
102
175k
    PORT_Assert((result != NULL) && (result->data != NULL));
103
104
175k
    S1.type = siBuffer;
105
175k
    S1.len = (secret->len / 2) + (secret->len & 1);
106
175k
    S1.data = secret->data;
107
108
175k
    S2.type = siBuffer;
109
175k
    S2.len = S1.len;
110
175k
    S2.data = secret->data + (secret->len - S2.len);
111
112
175k
    tmp.data = (unsigned char *)PORT_Alloc(result->len);
113
175k
    if (tmp.data == NULL)
114
0
        goto loser;
115
175k
    tmp.len = result->len;
116
117
175k
    status = TLS_P_hash(HASH_AlgMD5, &S1, label, seed, result, isFIPS);
118
175k
    if (status != SECSuccess)
119
0
        goto loser;
120
121
175k
    status = TLS_P_hash(HASH_AlgSHA1, &S2, label, seed, &tmp, isFIPS);
122
175k
    if (status != SECSuccess)
123
0
        goto loser;
124
125
8.69M
    for (i = 0; i < result->len; i++)
126
8.51M
        result->data[i] ^= tmp.data[i];
127
128
175k
    rv = SECSuccess;
129
130
175k
loser:
131
175k
    if (tmp.data != NULL)
132
175k
        PORT_ZFree(tmp.data, tmp.len);
133
175k
    return rv;
134
175k
}