Coverage Report

Created: 2026-05-19 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/freebl/ecl/ecp_25519.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/* curve 25519 https://www.rfc-editor.org/rfc/rfc7748.txt */
6
7
#ifdef FREEBL_NO_DEPEND
8
#include "../stubs.h"
9
#endif
10
11
#include "ecl-priv.h"
12
#include "secitem.h"
13
#include "secerr.h"
14
#include "secport.h"
15
#include <stdlib.h>
16
#include <stdio.h>
17
18
/*
19
 * point validation is not necessary in general. But this checks a point (px)
20
 * against some known bad values.
21
 */
22
SECStatus
23
ec_Curve25519_pt_validate(const SECItem *px)
24
24.6k
{
25
24.6k
    PRUint8 *p;
26
24.6k
    PRUint64 i;
27
24.6k
    PRUint8 forbiddenValues[12][32] = {
28
24.6k
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
32
24.6k
        { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35
24.6k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
36
24.6k
        { 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae,
37
24.6k
          0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a,
38
24.6k
          0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd,
39
24.6k
          0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00 },
40
24.6k
        { 0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24,
41
24.6k
          0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b,
42
24.6k
          0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86,
43
24.6k
          0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57 },
44
24.6k
        { 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
48
24.6k
        { 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
50
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
52
24.6k
        { 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
56
24.6k
        { 0xcd, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae,
57
24.6k
          0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a,
58
24.6k
          0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd,
59
24.6k
          0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x80 },
60
24.6k
        { 0x4c, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24,
61
24.6k
          0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b,
62
24.6k
          0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86,
63
24.6k
          0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0xd7 },
64
24.6k
        { 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
68
24.6k
        { 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
72
24.6k
        { 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75
24.6k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
76
24.6k
    };
77
78
24.6k
    if (px->len == 32) {
79
24.6k
        p = px->data;
80
24.6k
    } else {
81
11
        return SECFailure;
82
11
    }
83
84
320k
    for (i = 0; i < PR_ARRAY_SIZE(forbiddenValues); ++i) {
85
296k
        if (NSS_SecureMemcmp(p, forbiddenValues[i], px->len) == 0) {
86
10
            return SECFailure;
87
10
        }
88
296k
    }
89
90
24.6k
    return SECSuccess;
91
24.6k
}
92
93
/*
94
 * scalar validation is not necessary.
95
 */
96
SECStatus
97
ec_Curve25519_scalar_validate(const SECItem *scalar)
98
19.5k
{
99
19.5k
    if (!scalar || !scalar->data) {
100
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
101
0
        return SECFailure;
102
0
    }
103
104
19.5k
    if (scalar->len != 32) {
105
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
106
0
        return SECFailure;
107
0
    }
108
19.5k
    return SECSuccess;
109
19.5k
}
110
111
/*
112
 * Scalar multiplication for Curve25519.
113
 * If P == NULL, the base point is used.
114
 * Returns X = k*P
115
 */
116
SECStatus
117
ec_Curve25519_pt_mul(SECItem *X, SECItem *k, SECItem *P)
118
28.0k
{
119
28.0k
    PRUint8 *px;
120
28.0k
    PRUint8 basePoint[32] = { 9 };
121
122
28.0k
    if (!P) {
123
22.5k
        px = basePoint;
124
22.5k
    } else {
125
5.46k
        PORT_Assert(P->len == 32);
126
5.46k
        if (P->len != 32) {
127
0
            return SECFailure;
128
0
        }
129
5.46k
        px = P->data;
130
5.46k
    }
131
28.0k
    if (k->len != 32) {
132
7
        return SECFailure;
133
7
    }
134
28.0k
#ifndef UNSAFE_FUZZER_MODE
135
28.0k
    SECStatus rv = ec_Curve25519_mul(X->data, k->data, px);
136
28.0k
    if (NSS_SecureMemcmpZero(X->data, X->len) == 0) {
137
5
        return SECFailure;
138
5
    }
139
28.0k
    return rv;
140
#else
141
    return px != NULL;
142
#endif
143
28.0k
}