Coverage Report

Created: 2025-04-22 06:18

/src/nss/lib/freebl/ecl/ecp_25519.c
Line
Count
Source (jump to first uncovered line)
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
76.8k
{
25
76.8k
    PRUint8 *p;
26
76.8k
    PRUint64 i;
27
76.8k
    PRUint8 forbiddenValues[12][32] = {
28
76.8k
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
32
76.8k
        { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35
76.8k
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
36
76.8k
        { 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae,
37
76.8k
          0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a,
38
76.8k
          0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd,
39
76.8k
          0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00 },
40
76.8k
        { 0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24,
41
76.8k
          0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b,
42
76.8k
          0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86,
43
76.8k
          0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57 },
44
76.8k
        { 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
48
76.8k
        { 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
50
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
52
76.8k
        { 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f },
56
76.8k
        { 0xcd, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae,
57
76.8k
          0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a,
58
76.8k
          0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd,
59
76.8k
          0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x80 },
60
76.8k
        { 0x4c, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24,
61
76.8k
          0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b,
62
76.8k
          0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86,
63
76.8k
          0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0xd7 },
64
76.8k
        { 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
68
76.8k
        { 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
72
76.8k
        { 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75
76.8k
          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
76
76.8k
    };
77
78
76.8k
    if (px->len == 32) {
79
76.7k
        p = px->data;
80
76.7k
    } else {
81
28
        return SECFailure;
82
28
    }
83
84
998k
    for (i = 0; i < PR_ARRAY_SIZE(forbiddenValues); ++i) {
85
921k
        if (NSS_SecureMemcmp(p, forbiddenValues[i], px->len) == 0) {
86
15
            return SECFailure;
87
15
        }
88
921k
    }
89
90
76.7k
    return SECSuccess;
91
76.7k
}
92
93
/*
94
 * scalar validation is not necessary.
95
 */
96
SECStatus
97
ec_Curve25519_scalar_validate(const SECItem *scalar)
98
53.9k
{
99
53.9k
    if (!scalar || !scalar->data) {
100
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
101
0
        return SECFailure;
102
0
    }
103
104
53.9k
    if (scalar->len != 32) {
105
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
106
0
        return SECFailure;
107
0
    }
108
53.9k
    return SECSuccess;
109
53.9k
}
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
84.1k
{
119
84.1k
    PRUint8 *px;
120
84.1k
    PRUint8 basePoint[32] = { 9 };
121
122
84.1k
    if (!P) {
123
61.2k
        px = basePoint;
124
61.2k
    } else {
125
22.8k
        PORT_Assert(P->len == 32);
126
22.8k
        if (P->len != 32) {
127
0
            return SECFailure;
128
0
        }
129
22.8k
        px = P->data;
130
22.8k
    }
131
84.1k
    if (k->len != 32) {
132
0
        return SECFailure;
133
0
    }
134
135
84.1k
    SECStatus rv = ec_Curve25519_mul(X->data, k->data, px);
136
84.1k
    if (NSS_SecureMemcmpZero(X->data, X->len) == 0) {
137
8
        return SECFailure;
138
8
    }
139
84.1k
    return rv;
140
84.1k
}