Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rnp/src/lib/crypto/ec.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2017, [Ribose Inc](https://www.ribose.com).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
 * POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include <botan/ffi.h>
28
#include <string.h>
29
#include <cassert>
30
#include "ec.h"
31
#include "types.h"
32
#include "utils.h"
33
#include "mem.h"
34
#include "botan_utils.hpp"
35
#include "botan/ec_group.h"
36
#include "botan/ecdh.h"
37
#if defined(ENABLE_CRYPTO_REFRESH) || defined(ENABLE_PQC)
38
#include "x25519_x448.h"
39
#include "ed25519_ed448.h"
40
#endif
41
#include <cassert>
42
43
namespace pgp {
44
namespace ec {
45
46
static id_str_pair ec_algo_to_botan[] = {
47
  {PGP_PKA_ECDH, "ECDH"},
48
  {PGP_PKA_ECDSA, "ECDSA"},
49
  {PGP_PKA_SM2, "SM2_Sig"},
50
  {0, NULL},
51
};
52
53
rnp_result_t
54
Key::generate_x25519(rnp::RNG &rng)
55
0
{
56
0
    rnp::botan::Privkey pr_key;
57
0
    if (botan_privkey_create(&pr_key.get(), "Curve25519", "", rng.handle())) {
58
0
        return RNP_ERROR_KEY_GENERATION;
59
0
    }
60
61
0
    rnp::botan::Pubkey pu_key;
62
0
    if (botan_privkey_export_pubkey(&pu_key.get(), pr_key.get())) {
63
0
        return RNP_ERROR_KEY_GENERATION;
64
0
    }
65
66
    /* botan returns key in little-endian, while mpi is big-endian */
67
0
    rnp::secure_array<uint8_t, 32> keyle;
68
0
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(3, 6, 0)
69
0
    rnp_botan_view_buf keyle_vb{keyle.data(), keyle.size()};
70
0
    if (botan_privkey_view_raw(pr_key.get(), &keyle_vb, rnp_botan_view_bin)) {
71
0
        return RNP_ERROR_KEY_GENERATION;
72
0
    }
73
#else
74
    if (botan_privkey_x25519_get_privkey(pr_key.get(), keyle.data())) {
75
        return RNP_ERROR_KEY_GENERATION;
76
    }
77
#endif
78
0
    x.resize(32);
79
0
    for (int i = 0; i < 32; i++) {
80
0
        x[31 - i] = keyle[i];
81
0
    }
82
    /* botan doesn't tweak secret key bits, so we should do that here */
83
0
    if (!x25519_tweak_bits(*this)) {
84
0
        return RNP_ERROR_KEY_GENERATION;
85
0
    }
86
87
0
    p.resize(33);
88
0
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(3, 6, 0)
89
0
    rnp_botan_view_buf pub_vb{&p[1], 32};
90
0
    if (botan_pubkey_view_raw(pu_key.get(), &pub_vb, rnp_botan_view_bin)) {
91
0
        return RNP_ERROR_KEY_GENERATION;
92
0
    }
93
#else
94
    if (botan_pubkey_x25519_get_pubkey(pu_key.get(), &p[1])) {
95
        return RNP_ERROR_KEY_GENERATION;
96
    }
97
#endif
98
0
    p[0] = 0x40;
99
0
    return RNP_SUCCESS;
100
0
}
101
102
rnp_result_t
103
Key::generate(rnp::RNG &rng, const pgp_pubkey_alg_t alg_id, const pgp_curve_t curve)
104
0
{
105
    /**
106
     * Keeps "0x04 || x || y"
107
     * \see 13.2.  ECDSA, ECDH, SM2 Conversion Primitives
108
     *
109
     * P-521 is biggest supported curve
110
     */
111
0
    if (!Curve::alg_allows(alg_id, curve)) {
112
0
        return RNP_ERROR_BAD_PARAMETERS;
113
0
    }
114
115
0
    const char *ec_algo = id_str_pair::lookup(ec_algo_to_botan, alg_id, NULL);
116
0
    assert(ec_algo);
117
0
    auto ec_desc = Curve::get(curve);
118
0
    if (!ec_desc) {
119
0
        return RNP_ERROR_BAD_PARAMETERS;
120
0
    }
121
122
    // at this point it must succeed
123
0
    rnp::botan::Privkey pr_key;
124
0
    if (botan_privkey_create(&pr_key.get(), ec_algo, ec_desc->botan_name, rng.handle())) {
125
0
        return RNP_ERROR_KEY_GENERATION;
126
0
    }
127
128
0
    rnp::botan::Pubkey pu_key;
129
0
    if (botan_privkey_export_pubkey(&pu_key.get(), pr_key.get())) {
130
0
        return RNP_ERROR_KEY_GENERATION;
131
0
    }
132
133
0
    rnp::bn px;
134
0
    rnp::bn py;
135
0
    rnp::bn bx;
136
137
0
    if (!px || !py || !bx) {
138
0
        RNP_LOG("Allocation failed");
139
0
        return RNP_ERROR_OUT_OF_MEMORY;
140
0
    }
141
142
0
    if (botan_pubkey_get_field(px.get(), pu_key.get(), "public_x") ||
143
0
        botan_pubkey_get_field(py.get(), pu_key.get(), "public_y") ||
144
0
        botan_privkey_get_field(bx.get(), pr_key.get(), "x")) {
145
0
        return RNP_ERROR_KEY_GENERATION;
146
0
    }
147
148
    // Safety check
149
0
    size_t field_size = ec_desc->bytes();
150
0
    if ((px.bytes() > field_size) || (py.bytes() > field_size)) {
151
0
        RNP_LOG("Key generation failed");
152
0
        return RNP_ERROR_BAD_PARAMETERS;
153
0
    }
154
155
    /*
156
     * Convert coordinates to MPI stored as
157
     * "0x04 || x || y"
158
     *
159
     *  \see 13.2.  ECDSA and ECDH Conversion Primitives
160
     *
161
     * Note: Generated pk/sk may not always have exact number of bytes
162
     *       which is important when converting to octet-string
163
     */
164
0
    p.resize(2 * field_size + 1);
165
0
    p[0] = 0x04;
166
0
    px.bin(&p[1 + field_size - px.bytes()]);
167
0
    py.bin(&p[1 + 2 * field_size - py.bytes()]);
168
    /* secret key value */
169
0
    bx.mpi(x);
170
0
    return RNP_SUCCESS;
171
0
}
172
173
} // namespace ec
174
} // namespace pgp
175
176
#if defined(ENABLE_CRYPTO_REFRESH) || defined(ENABLE_PQC)
177
static bool
178
is_generic_prime_curve(pgp_curve_t curve)
179
0
{
180
0
    switch (curve) {
181
0
    case PGP_CURVE_NIST_P_256:
182
0
        FALLTHROUGH_STATEMENT;
183
0
    case PGP_CURVE_NIST_P_384:
184
0
        FALLTHROUGH_STATEMENT;
185
0
    case PGP_CURVE_NIST_P_521:
186
0
        FALLTHROUGH_STATEMENT;
187
0
    case PGP_CURVE_BP256:
188
0
        FALLTHROUGH_STATEMENT;
189
0
    case PGP_CURVE_BP384:
190
0
        FALLTHROUGH_STATEMENT;
191
0
    case PGP_CURVE_BP512:
192
0
        FALLTHROUGH_STATEMENT;
193
0
    case PGP_CURVE_P256K1:
194
0
        return true;
195
0
    default:
196
0
        return false;
197
0
    }
198
0
}
199
200
static rnp_result_t
201
ec_generate_generic_native(rnp::RNG *            rng,
202
                           std::vector<uint8_t> &privkey,
203
                           std::vector<uint8_t> &pubkey,
204
                           pgp_curve_t           curve)
205
0
{
206
0
    if (!is_generic_prime_curve(curve)) {
207
0
        RNP_LOG("expected generic prime curve");
208
0
        return RNP_ERROR_BAD_PARAMETERS;
209
0
    }
210
211
0
    auto ec_desc = pgp::ec::Curve::get(curve);
212
0
    if (!ec_desc) {
213
0
        return RNP_ERROR_BAD_PARAMETERS;
214
0
    }
215
0
    const size_t field_size = ec_desc->bytes();
216
217
0
    rnp::botan::Privkey pr_key;
218
0
    if (botan_privkey_create(&pr_key.get(), "ECDH", ec_desc->botan_name, rng->handle())) {
219
0
        return RNP_ERROR_KEY_GENERATION;
220
0
    }
221
0
    rnp::botan::Pubkey pu_key;
222
0
    if (botan_privkey_export_pubkey(&pu_key.get(), pr_key.get())) {
223
0
        return RNP_ERROR_KEY_GENERATION;
224
0
    }
225
226
0
    rnp::bn px;
227
0
    rnp::bn py;
228
0
    rnp::bn bx;
229
0
    if (!px || !py || !bx) {
230
0
        RNP_LOG("Allocation failed");
231
0
        return RNP_ERROR_OUT_OF_MEMORY;
232
0
    }
233
0
    if (botan_pubkey_get_field(px.get(), pu_key.get(), "public_x") ||
234
0
        botan_pubkey_get_field(py.get(), pu_key.get(), "public_y") ||
235
0
        botan_privkey_get_field(bx.get(), pr_key.get(), "x")) {
236
0
        return RNP_ERROR_KEY_GENERATION;
237
0
    }
238
0
    if ((px.bytes() > field_size) || (py.bytes() > field_size)) {
239
0
        RNP_LOG("Key generation failed");
240
0
        return RNP_ERROR_BAD_PARAMETERS;
241
0
    }
242
243
    /* pubkey: 0x04 || X || Y, coordinates zero-padded to the field size */
244
0
    pubkey.resize(2 * field_size + 1);
245
0
    pubkey[0] = 0x04;
246
0
    px.bin(&pubkey[1 + field_size - px.bytes()]);
247
0
    py.bin(&pubkey[1 + 2 * field_size - py.bytes()]);
248
    /* privkey: secret scalar, zero-padded to the field size */
249
0
    privkey.assign(field_size, 0);
250
0
    bx.bin(&privkey[field_size - bx.bytes()]);
251
0
    return RNP_SUCCESS;
252
0
}
253
254
rnp_result_t
255
ec_generate_native(rnp::RNG *            rng,
256
                   std::vector<uint8_t> &privkey,
257
                   std::vector<uint8_t> &pubkey,
258
                   pgp_curve_t           curve)
259
0
{
260
0
    switch (curve) {
261
0
    case PGP_CURVE_25519:
262
0
        return generate_x25519_native(rng, privkey, pubkey);
263
0
    case PGP_CURVE_ED25519:
264
0
        return generate_ed25519_native(rng, privkey, pubkey);
265
0
#if defined(ENABLE_CRYPTO_REFRESH)
266
0
    case PGP_CURVE_448:
267
0
        return generate_x448_native(rng, privkey, pubkey);
268
0
    case PGP_CURVE_ED448:
269
0
        return generate_ed448_native(rng, privkey, pubkey);
270
0
#endif
271
0
    default:
272
0
        break;
273
0
    }
274
0
    return ec_generate_generic_native(rng, privkey, pubkey, curve);
275
0
}
276
#endif