Coverage Report

Created: 2026-07-25 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mbedtls/3rdparty/everest/library/x25519.c
Line
Count
Source
1
/*
2
 *  ECDH with curve-optimized implementation multiplexing
3
 *
4
 *  Copyright 2016-2018 INRIA and Microsoft Corporation
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 *  not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 *
19
 *  This file is part of Mbed TLS (https://tls.mbed.org)
20
 */
21
22
#include "common.h"
23
24
#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
25
26
#include <mbedtls/ecdh.h>
27
28
#if !(defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16))
29
#define KRML_VERIFIED_UINT128
30
#endif
31
32
#include <Hacl_Curve25519.h>
33
#include <mbedtls/platform_util.h>
34
35
#include "x25519.h"
36
37
#include <string.h>
38
39
/*
40
 * Initialize context
41
 */
42
void mbedtls_x25519_init( mbedtls_x25519_context *ctx )
43
0
{
44
0
    mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x25519_context ) );
45
0
}
46
47
/*
48
 * Free context
49
 */
50
void mbedtls_x25519_free( mbedtls_x25519_context *ctx )
51
0
{
52
0
    if( ctx == NULL )
53
0
        return;
54
55
0
    mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
56
0
    mbedtls_platform_zeroize( ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES );
57
0
}
58
59
int mbedtls_x25519_make_params( mbedtls_x25519_context *ctx, size_t *olen,
60
                        unsigned char *buf, size_t blen,
61
                        int( *f_rng )(void *, unsigned char *, size_t),
62
                        void *p_rng )
63
0
{
64
0
    int ret = 0;
65
66
0
    uint8_t base[MBEDTLS_X25519_KEY_SIZE_BYTES] = {0};
67
68
0
    if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 )
69
0
        return ret;
70
71
0
    *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 4;
72
0
    if( blen < *olen )
73
0
        return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
74
75
0
    *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
76
0
    *buf++ = MBEDTLS_ECP_TLS_CURVE25519 >> 8;
77
0
    *buf++ = MBEDTLS_ECP_TLS_CURVE25519 & 0xFF;
78
0
    *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES;
79
80
0
    base[0] = 9;
81
0
    Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base );
82
83
0
    base[0] = 0;
84
0
    if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 )
85
0
        return MBEDTLS_ERR_ECP_RANDOM_FAILED;
86
87
0
    return( 0 );
88
0
}
89
90
int mbedtls_x25519_read_params( mbedtls_x25519_context *ctx,
91
                        const unsigned char **buf, const unsigned char *end )
92
0
{
93
0
    if( end - *buf < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 )
94
0
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
95
96
0
    if( ( *(*buf)++ != MBEDTLS_X25519_KEY_SIZE_BYTES ) )
97
0
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
98
99
0
    memcpy( ctx->peer_point, *buf, MBEDTLS_X25519_KEY_SIZE_BYTES );
100
0
    *buf += MBEDTLS_X25519_KEY_SIZE_BYTES;
101
0
    return( 0 );
102
0
}
103
104
int mbedtls_x25519_get_params( mbedtls_x25519_context *ctx, const mbedtls_ecp_keypair *key,
105
                               mbedtls_x25519_ecdh_side side )
106
0
{
107
0
    size_t olen = 0;
108
109
0
    switch( side ) {
110
0
    case MBEDTLS_X25519_ECDH_THEIRS:
111
0
        return mbedtls_ecp_point_write_binary( &key->grp, &key->Q, MBEDTLS_ECP_PF_COMPRESSED, &olen, ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES );
112
0
    case MBEDTLS_X25519_ECDH_OURS:
113
0
        return mbedtls_mpi_write_binary_le( &key->d, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
114
0
    default:
115
0
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
116
0
    }
117
0
}
118
119
int mbedtls_x25519_calc_secret( mbedtls_x25519_context *ctx, size_t *olen,
120
                        unsigned char *buf, size_t blen,
121
                        int( *f_rng )(void *, unsigned char *, size_t),
122
                        void *p_rng )
123
0
{
124
    /* f_rng and p_rng are not used here because this implementation does not
125
       need blinding since it has constant trace. */
126
0
    (( void )f_rng);
127
0
    (( void )p_rng);
128
129
0
    *olen = MBEDTLS_X25519_KEY_SIZE_BYTES;
130
131
0
    if( blen < *olen )
132
0
        return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
133
134
    /* scalarmult modifies this input, let's make a copy... */
135
0
    unsigned char secret[MBEDTLS_X25519_KEY_SIZE_BYTES];
136
0
    memcpy(secret, ctx->our_secret, sizeof(secret));
137
138
0
    Hacl_Curve25519_crypto_scalarmult( buf, secret, ctx->peer_point);
139
140
    /* Wipe the copy and don't let the peer choose a small subgroup point */
141
0
    mbedtls_platform_zeroize( secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
142
143
0
    if( memcmp( buf, secret, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 )
144
0
        return MBEDTLS_ERR_ECP_RANDOM_FAILED;
145
146
0
    return( 0 );
147
0
}
148
149
int mbedtls_x25519_make_public( mbedtls_x25519_context *ctx, size_t *olen,
150
                        unsigned char *buf, size_t blen,
151
                        int( *f_rng )(void *, unsigned char *, size_t),
152
                        void *p_rng )
153
0
{
154
0
    int ret = 0;
155
0
    unsigned char base[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 };
156
157
0
    if( ctx == NULL )
158
0
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
159
160
0
    if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 )
161
0
        return ret;
162
163
0
    *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 1;
164
0
    if( blen < *olen )
165
0
        return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
166
0
    *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES;
167
168
0
    base[0] = 9;
169
0
    Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base );
170
171
0
    base[0] = 0;
172
0
    if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES ) == 0 )
173
0
        return MBEDTLS_ERR_ECP_RANDOM_FAILED;
174
175
0
    return( ret );
176
0
}
177
178
int mbedtls_x25519_read_public( mbedtls_x25519_context *ctx,
179
                        const unsigned char *buf, size_t blen )
180
0
{
181
0
    if( blen < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 )
182
0
        return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
183
0
    if( (*buf++ != MBEDTLS_X25519_KEY_SIZE_BYTES) )
184
0
        return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
185
0
    memcpy( ctx->peer_point, buf, MBEDTLS_X25519_KEY_SIZE_BYTES );
186
0
    return( 0 );
187
0
}
188
189
190
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */