Coverage Report

Created: 2024-11-21 07:03

/src/nss-nspr/nss/lib/freebl/tlsprfalg.c
Line
Count
Source (jump to first uncovered line)
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
18
{
22
18
    unsigned char state[PHASH_STATE_MAX_LEN];
23
18
    unsigned char outbuf[PHASH_STATE_MAX_LEN];
24
18
    unsigned int state_len = 0, label_len = 0, outbuf_len = 0, chunk_size;
25
18
    unsigned int remaining;
26
18
    unsigned char *res;
27
18
    SECStatus status;
28
18
    HMACContext *cx;
29
18
    SECStatus rv = SECFailure;
30
18
    const SECHashObject *hashObj = HASH_GetRawHashObject(hashType);
31
32
18
    PORT_Assert((secret != NULL) && (secret->data != NULL || !secret->len));
33
18
    PORT_Assert((seed != NULL) && (seed->data != NULL));
34
18
    PORT_Assert((result != NULL) && (result->data != NULL));
35
36
18
    remaining = result->len;
37
18
    res = result->data;
38
39
18
    if (label != NULL)
40
18
        label_len = PORT_Strlen(label);
41
42
18
    cx = HMAC_Create(hashObj, secret->data, secret->len, isFIPS);
43
18
    if (cx == NULL)
44
0
        goto loser;
45
46
    /* initialize the state = A(1) = HMAC_hash(secret, seed) */
47
18
    HMAC_Begin(cx);
48
18
    HMAC_Update(cx, (unsigned char *)label, label_len);
49
18
    HMAC_Update(cx, seed->data, seed->len);
50
18
    status = HMAC_Finish(cx, state, &state_len, sizeof(state));
51
18
    if (status != SECSuccess)
52
0
        goto loser;
53
54
    /* generate a block at a time until we're done */
55
424
    while (remaining > 0) {
56
57
406
        HMAC_Begin(cx);
58
406
        HMAC_Update(cx, state, state_len);
59
406
        if (label_len)
60
0
            HMAC_Update(cx, (unsigned char *)label, label_len);
61
406
        HMAC_Update(cx, seed->data, seed->len);
62
406
        status = HMAC_Finish(cx, outbuf, &outbuf_len, sizeof(outbuf));
63
406
        if (status != SECSuccess)
64
0
            goto loser;
65
66
        /* Update the state = A(i) = HMAC_hash(secret, A(i-1)) */
67
406
        HMAC_Begin(cx);
68
406
        HMAC_Update(cx, state, state_len);
69
406
        status = HMAC_Finish(cx, state, &state_len, sizeof(state));
70
406
        if (status != SECSuccess)
71
0
            goto loser;
72
73
406
        chunk_size = PR_MIN(outbuf_len, remaining);
74
406
        PORT_Memcpy(res, &outbuf, chunk_size);
75
406
        res += chunk_size;
76
406
        remaining -= chunk_size;
77
406
    }
78
79
18
    rv = SECSuccess;
80
81
18
loser:
82
    /* clear out state so it's not left on the stack */
83
18
    if (cx)
84
18
        HMAC_Destroy(cx, PR_TRUE);
85
18
    PORT_Memset(state, 0, sizeof(state));
86
18
    PORT_Memset(outbuf, 0, sizeof(outbuf));
87
18
    return rv;
88
18
}
89
90
SECStatus
91
TLS_PRF(const SECItem *secret, const char *label, SECItem *seed,
92
        SECItem *result, PRBool isFIPS)
93
9
{
94
9
    SECStatus rv = SECFailure, status;
95
9
    unsigned int i;
96
9
    SECItem tmp = { siBuffer, NULL, 0 };
97
9
    SECItem S1;
98
9
    SECItem S2;
99
100
9
    PORT_Assert((secret != NULL) && (secret->data != NULL || !secret->len));
101
9
    PORT_Assert((seed != NULL) && (seed->data != NULL));
102
9
    PORT_Assert((result != NULL) && (result->data != NULL));
103
104
9
    S1.type = siBuffer;
105
9
    S1.len = (secret->len / 2) + (secret->len & 1);
106
9
    S1.data = secret->data;
107
108
9
    S2.type = siBuffer;
109
9
    S2.len = S1.len;
110
9
    S2.data = secret->data + (secret->len - S2.len);
111
112
9
    tmp.data = (unsigned char *)PORT_Alloc(result->len);
113
9
    if (tmp.data == NULL)
114
0
        goto loser;
115
9
    tmp.len = result->len;
116
117
9
    status = TLS_P_hash(HASH_AlgMD5, &S1, label, seed, result, isFIPS);
118
9
    if (status != SECSuccess)
119
0
        goto loser;
120
121
9
    status = TLS_P_hash(HASH_AlgSHA1, &S2, label, seed, &tmp, isFIPS);
122
9
    if (status != SECSuccess)
123
0
        goto loser;
124
125
3.54k
    for (i = 0; i < result->len; i++)
126
3.53k
        result->data[i] ^= tmp.data[i];
127
128
9
    rv = SECSuccess;
129
130
9
loser:
131
9
    if (tmp.data != NULL)
132
9
        PORT_ZFree(tmp.data, tmp.len);
133
9
    return rv;
134
9
}