Coverage Report

Created: 2026-10-30 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/nettle/pk.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
3
 * Copyright (C) 2013-2017 Nikos Mavrogiannopoulos
4
 * Copyright (C) 2016-2017 Red Hat, Inc.
5
 *
6
 * Author: Nikos Mavrogiannopoulos
7
 *
8
 * This file is part of GNUTLS.
9
 *
10
 * The GNUTLS library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public License
12
 * as published by the Free Software Foundation; either version 2.1 of
13
 * the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
22
 *
23
 */
24
25
/* This file contains the functions needed for RSA/DSA public key
26
 * encryption and signatures.
27
 */
28
29
#include "gnutls_int.h"
30
#include "mpi.h"
31
#include "pk.h"
32
#include "errors.h"
33
#include "datum.h"
34
#include "global.h"
35
#include "tls-sig.h"
36
#include "num.h"
37
#include "x509/x509_int.h"
38
#include "x509/common.h"
39
#include "random.h"
40
#include "pk.h"
41
#include <nettle/dsa.h>
42
#include "dsa-fips.h"
43
#include "rsa-fips.h"
44
#include <nettle/rsa.h>
45
#include <gnutls/crypto.h>
46
#include <nettle/bignum.h>
47
#include <nettle/ecc.h>
48
#include <nettle/ecdsa.h>
49
#include <nettle/ecc-curve.h>
50
#include <nettle/curve25519.h>
51
#include <nettle/curve448.h>
52
#include <nettle/eddsa.h>
53
#include <nettle/version.h>
54
#if ENABLE_GOST
55
#if NEED_INT_ECC
56
#include "ecc/gostdsa.h"
57
#include "ecc-gost-curve.h"
58
#else
59
#include <nettle/gostdsa.h>
60
0
#define gost_point_mul_g ecc_point_mul_g
61
0
#define gost_point_set ecc_point_set
62
#endif
63
#include "gost/gostdsa2.h"
64
#endif
65
#include "int/ecdsa-compute-k.h"
66
#include "int/dsa-compute-k.h"
67
#include "gnettle.h"
68
#include "fips.h"
69
#include "dh.h"
70
#include "audit.h"
71
#ifdef HAVE_LEANCRYPTO
72
#include <leancrypto.h>
73
#endif
74
#include "attribute.h"
75
76
#define MAX_PRIME_CURVE_COORD_SIZE 66
77
78
static inline const struct ecc_curve *get_supported_nist_curve(int curve);
79
static inline const struct ecc_curve *get_supported_gost_curve(int curve);
80
81
static inline const char *get_supported_nist_curve_order(int curve);
82
static inline const char *get_supported_nist_curve_modulus(int curve);
83
84
/* When these callbacks are used for a nettle operation, the
85
 * caller must check the macro HAVE_LIB_ERROR() after the operation
86
 * is complete. If the macro is true, the operation is to be considered
87
 * failed (meaning the random generation failed).
88
 */
89
static void rnd_key_func(void *_ctx, size_t length, uint8_t *data)
90
0
{
91
0
  if (gnutls_rnd(GNUTLS_RND_KEY, data, length) < 0) {
92
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
93
0
  }
94
0
}
95
96
static void rnd_tmpkey_func(void *_ctx, size_t length, uint8_t *data)
97
0
{
98
0
  if (gnutls_rnd(GNUTLS_RND_RANDOM, data, length) < 0) {
99
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
100
0
  }
101
0
}
102
103
static void rnd_nonce_func(void *_ctx, size_t length, uint8_t *data)
104
0
{
105
0
  if (gnutls_rnd(GNUTLS_RND_NONCE, data, length) < 0) {
106
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
107
0
  }
108
0
}
109
110
static void rnd_datum_func(void *ctx, size_t length, uint8_t *data)
111
0
{
112
0
  gnutls_datum_t *d = ctx;
113
114
0
  if (length > d->size) {
115
0
    memset(data, 0, length - d->size);
116
0
    memcpy(data + (length - d->size), d->data, d->size);
117
0
  } else {
118
0
    memcpy(data, d->data, length);
119
0
  }
120
0
}
121
122
static void rnd_nonce_func_fallback(void *_ctx, size_t length, uint8_t *data)
123
0
{
124
0
  if (unlikely(_gnutls_get_lib_state() != LIB_STATE_SELFTEST)) {
125
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
126
0
  }
127
128
0
  memset(data, 0xAA, length);
129
0
}
130
131
static void ecc_scalar_zclear(struct ecc_scalar *s)
132
0
{
133
0
  zeroize_key(s->p, ecc_size(s->ecc) * sizeof(mp_limb_t));
134
0
  ecc_scalar_clear(s);
135
0
}
136
137
static void ecc_point_zclear(struct ecc_point *p)
138
0
{
139
0
  zeroize_key(p->p, ecc_size_a(p->ecc) * sizeof(mp_limb_t));
140
0
  ecc_point_clear(p);
141
0
}
142
143
static void _dsa_params_get(const gnutls_pk_params_st *pk_params,
144
          struct dsa_params *pub)
145
0
{
146
0
  memcpy(pub->p, pk_params->params[DSA_P], SIZEOF_MPZT);
147
148
0
  if (pk_params->params[DSA_Q])
149
0
    memcpy(&pub->q, pk_params->params[DSA_Q], SIZEOF_MPZT);
150
0
  memcpy(pub->g, pk_params->params[DSA_G], SIZEOF_MPZT);
151
0
}
152
153
static void _rsa_params_to_privkey(const gnutls_pk_params_st *pk_params,
154
           struct rsa_private_key *priv)
155
0
{
156
0
  memcpy(priv->d, pk_params->params[RSA_PRIV], SIZEOF_MPZT);
157
0
  memcpy(priv->p, pk_params->params[RSA_PRIME1], SIZEOF_MPZT);
158
0
  memcpy(priv->q, pk_params->params[RSA_PRIME2], SIZEOF_MPZT);
159
0
  memcpy(priv->c, pk_params->params[RSA_COEF], SIZEOF_MPZT);
160
0
  memcpy(priv->a, pk_params->params[RSA_E1], SIZEOF_MPZT);
161
0
  memcpy(priv->b, pk_params->params[RSA_E2], SIZEOF_MPZT);
162
  /* we do not rsa_private_key_prepare() because it involves a multiplication.
163
   * we call it once when we import the parameters */
164
0
  priv->size = nettle_mpz_sizeinbase_256_u(
165
0
    TOMPZ(pk_params->params[RSA_MODULUS]));
166
0
}
167
168
/* returns a negative value on invalid pubkey */
169
static int _rsa_params_to_pubkey(const gnutls_pk_params_st *pk_params,
170
         struct rsa_public_key *pub)
171
0
{
172
0
  memcpy(pub->n, pk_params->params[RSA_MODULUS], SIZEOF_MPZT);
173
0
  memcpy(pub->e, pk_params->params[RSA_PUB], SIZEOF_MPZT);
174
0
  if (rsa_public_key_prepare(pub) == 0)
175
0
    return gnutls_assert_val(GNUTLS_E_PK_INVALID_PUBKEY);
176
177
0
  return 0;
178
0
}
179
180
static int _ecc_params_to_privkey(const gnutls_pk_params_st *pk_params,
181
          struct ecc_scalar *priv,
182
          const struct ecc_curve *curve)
183
0
{
184
0
  ecc_scalar_init(priv, curve);
185
0
  if (ecc_scalar_set(priv, pk_params->params[ECC_K]) == 0) {
186
0
    ecc_scalar_clear(priv);
187
0
    return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
188
0
  }
189
190
0
  return 0;
191
0
}
192
193
static int _ecc_params_to_pubkey(const gnutls_pk_params_st *pk_params,
194
         struct ecc_point *pub,
195
         const struct ecc_curve *curve)
196
0
{
197
0
  ecc_point_init(pub, curve);
198
0
  if (ecc_point_set(pub, pk_params->params[ECC_X],
199
0
        pk_params->params[ECC_Y]) == 0) {
200
0
    ecc_point_clear(pub);
201
0
    return gnutls_assert_val(GNUTLS_E_PK_INVALID_PUBKEY);
202
0
  }
203
204
0
  return 0;
205
0
}
206
207
#if ENABLE_GOST
208
static int _gost_params_to_privkey(const gnutls_pk_params_st *pk_params,
209
           struct ecc_scalar *priv,
210
           const struct ecc_curve *curve)
211
0
{
212
0
  ecc_scalar_init(priv, curve);
213
0
  if (ecc_scalar_set(priv, pk_params->params[GOST_K]) == 0) {
214
0
    ecc_scalar_clear(priv);
215
0
    return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
216
0
  }
217
218
0
  return 0;
219
0
}
220
221
static int _gost_params_to_pubkey(const gnutls_pk_params_st *pk_params,
222
          struct ecc_point *pub,
223
          const struct ecc_curve *curve)
224
0
{
225
0
  ecc_point_init(pub, curve);
226
0
  if (gost_point_set(pub, pk_params->params[GOST_X],
227
0
         pk_params->params[GOST_Y]) == 0) {
228
0
    ecc_point_clear(pub);
229
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
230
0
  }
231
232
0
  return 0;
233
0
}
234
#endif
235
236
static int ecc_shared_secret(struct ecc_scalar *private_key,
237
           struct ecc_point *public_key, void *out,
238
           unsigned size)
239
0
{
240
0
  struct ecc_point r;
241
0
  mpz_t x, y;
242
0
  int ret = 0;
243
244
0
  mpz_init(x);
245
0
  mpz_init(y);
246
0
  ecc_point_init(&r, public_key->ecc);
247
248
0
  ecc_point_mul(&r, private_key, public_key);
249
250
0
  ecc_point_get(&r, x, y);
251
252
  /* Check if the point is not an identity element.  Note that this cannot
253
   * happen in nettle implementation, because it cannot represent an
254
   * infinity point. */
255
0
  if (mpz_cmp_ui(x, 0) == 0 && mpz_cmp_ui(y, 0) == 0) {
256
0
    ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
257
0
    goto cleanup;
258
0
  }
259
260
0
  nettle_mpz_get_str_256(size, out, x);
261
262
0
cleanup:
263
0
  mpz_clear(x);
264
0
  mpz_clear(y);
265
0
  ecc_point_clear(&r);
266
267
0
  return ret;
268
0
}
269
270
0
#define MAX_DH_BITS DEFAULT_MAX_VERIFY_BITS
271
/* This is used when we have no idea on the structure
272
 * of p-1 used by the peer. It is still a conservative
273
 * choice, but small than what we've been using before.
274
 */
275
#define DH_EXPONENT_SIZE(p_size) (2 * _gnutls_pk_bits_to_subgroup_bits(p_size))
276
277
static inline int edwards_curve_mul(gnutls_pk_algorithm_t algo, uint8_t *q,
278
            const uint8_t *n, const uint8_t *p)
279
0
{
280
0
  switch (algo) {
281
0
  case GNUTLS_PK_ECDH_X25519:
282
0
    curve25519_mul(q, n, p);
283
0
    return 0;
284
0
  case GNUTLS_PK_ECDH_X448:
285
0
    curve448_mul(q, n, p);
286
0
    return 0;
287
0
  default:
288
0
    return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
289
0
  }
290
0
}
291
292
/* This is used for DH or ECDH key derivation. In DH for example
293
 * it is given the peers Y and our x, and calculates Y^x
294
 */
295
static int _wrap_nettle_pk_derive(gnutls_pk_algorithm_t algo,
296
          gnutls_datum_t *out,
297
          const gnutls_pk_params_st *priv,
298
          const gnutls_pk_params_st *pub,
299
          const gnutls_datum_t *nonce,
300
          unsigned int flags)
301
0
{
302
0
  int ret;
303
0
  bool not_approved = false;
304
305
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::derive",
306
0
              "pk::algorithm", CRAU_STRING,
307
0
              gnutls_pk_get_name(algo), NULL);
308
309
0
  switch (algo) {
310
0
  case GNUTLS_PK_DH: {
311
0
    bigint_t f, x, q, prime;
312
0
    bigint_t k = NULL, primesub1 = NULL, r = NULL;
313
0
    unsigned int bits;
314
315
0
    if (nonce != NULL) {
316
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
317
0
      goto cleanup;
318
0
    }
319
320
0
    f = pub->params[DH_Y];
321
0
    x = priv->params[DH_X];
322
0
    q = priv->params[DH_Q];
323
0
    prime = priv->params[DH_P];
324
325
0
    ret = _gnutls_mpi_init_multi(&k, &primesub1, &r, NULL);
326
0
    if (ret < 0) {
327
0
      gnutls_assert();
328
0
      goto cleanup;
329
0
    }
330
331
0
    ret = _gnutls_mpi_sub_ui(primesub1, prime, 1);
332
0
    if (ret < 0) {
333
0
      gnutls_assert();
334
0
      goto dh_cleanup;
335
0
    }
336
337
    /* check if f==0,1, or f >= p-1 */
338
0
    if ((_gnutls_mpi_cmp_ui(f, 1) == 0) ||
339
0
        (_gnutls_mpi_cmp_ui(f, 0) == 0) ||
340
0
        (_gnutls_mpi_cmp(f, primesub1) >= 0)) {
341
0
      gnutls_assert();
342
0
      ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
343
0
      goto dh_cleanup;
344
0
    }
345
346
    /* if we have Q check that y ^ q mod p == 1 */
347
0
    if (q != NULL) {
348
0
      ret = _gnutls_mpi_powm(r, f, q, prime);
349
0
      if (ret < 0) {
350
0
        gnutls_assert();
351
0
        goto dh_cleanup;
352
0
      }
353
0
      ret = _gnutls_mpi_cmp_ui(r, 1);
354
0
      if (ret != 0) {
355
0
        gnutls_assert();
356
0
        ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
357
0
        goto dh_cleanup;
358
0
      }
359
0
    } else if ((flags & PK_DERIVE_TLS13) &&
360
0
         _gnutls_fips_mode_enabled()) {
361
      /* Mandatory in FIPS mode for TLS 1.3 */
362
0
      ret = gnutls_assert_val(
363
0
        GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
364
0
      goto dh_cleanup;
365
0
    }
366
367
    /* prevent denial of service */
368
0
    bits = _gnutls_mpi_get_nbits(prime);
369
0
    if (bits == 0 || bits > MAX_DH_BITS) {
370
0
      gnutls_assert();
371
0
      ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
372
0
      goto dh_cleanup;
373
0
    }
374
375
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
376
377
0
    if (bits < 2048) {
378
0
      not_approved = true;
379
0
    }
380
381
0
    ret = _gnutls_mpi_powm(k, f, x, prime);
382
0
    if (ret < 0) {
383
0
      gnutls_assert();
384
0
      goto dh_cleanup;
385
0
    }
386
387
    /* check if k==0,1, or k = p-1 */
388
0
    if ((_gnutls_mpi_cmp_ui(k, 1) == 0) ||
389
0
        (_gnutls_mpi_cmp_ui(k, 0) == 0) ||
390
0
        (_gnutls_mpi_cmp(k, primesub1) == 0)) {
391
0
      ret = gnutls_assert_val(
392
0
        GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
393
0
      goto dh_cleanup;
394
0
    }
395
396
0
    if (flags & PK_DERIVE_TLS13) {
397
0
      ret = _gnutls_mpi_dprint_size(k, out, (bits + 7) / 8);
398
0
    } else {
399
0
      ret = _gnutls_mpi_dprint(k, out);
400
0
    }
401
402
0
    if (ret < 0) {
403
0
      gnutls_assert();
404
0
      goto dh_cleanup;
405
0
    }
406
407
0
    ret = 0;
408
0
  dh_cleanup:
409
0
    _gnutls_mpi_release(&r);
410
0
    _gnutls_mpi_release(&primesub1);
411
0
    zrelease_mpi_key(&k);
412
0
    if (ret < 0)
413
0
      goto cleanup;
414
415
0
    break;
416
0
  }
417
0
  case GNUTLS_PK_EC: {
418
0
    struct ecc_scalar ecc_priv;
419
0
    struct ecc_point ecc_pub;
420
0
    const struct ecc_curve *curve;
421
0
    struct ecc_scalar n;
422
0
    struct ecc_scalar m;
423
0
    struct ecc_point r;
424
0
    mpz_t x, y, xx, yy, nn, mm;
425
426
0
    out->data = NULL;
427
428
0
    if (nonce != NULL) {
429
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
430
0
      goto cleanup;
431
0
    }
432
433
0
    curve = get_supported_nist_curve(priv->curve);
434
0
    if (curve == NULL) {
435
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
436
0
      goto cleanup;
437
0
    }
438
439
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
440
0
           gnutls_ecc_curve_get_name(priv->curve),
441
0
           NULL);
442
443
    /* P-192 is not supported in FIPS 140-3 */
444
0
    if (priv->curve == GNUTLS_ECC_CURVE_SECP192R1) {
445
0
      not_approved = true;
446
0
    }
447
448
0
    mpz_init(x);
449
0
    mpz_init(y);
450
0
    mpz_init(xx);
451
0
    mpz_init(yy);
452
0
    mpz_init(nn);
453
0
    mpz_init(mm);
454
455
0
    ecc_scalar_init(&n, curve);
456
0
    ecc_scalar_init(&m, curve);
457
0
    ecc_point_init(&r, curve);
458
459
0
    ret = _ecc_params_to_pubkey(pub, &ecc_pub, curve);
460
0
    if (ret < 0) {
461
0
      gnutls_assert();
462
0
      goto ecc_fail_cleanup;
463
0
    }
464
465
0
    ret = _ecc_params_to_privkey(priv, &ecc_priv, curve);
466
0
    if (ret < 0) {
467
0
      ecc_point_clear(&ecc_pub);
468
0
      gnutls_assert();
469
0
      goto ecc_fail_cleanup;
470
0
    }
471
472
0
    out->size = gnutls_ecc_curve_get_size(priv->curve);
473
    /*ecc_size(curve)*sizeof(mp_limb_t); */
474
0
    out->data = gnutls_malloc(out->size);
475
0
    if (out->data == NULL) {
476
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
477
0
      goto ecc_cleanup;
478
0
    }
479
480
    /* Perform ECC Full Public-Key Validation Routine
481
     * according to SP800-56A (revision 3), 5.6.2.3.3.
482
     */
483
484
    /* Step 1: verify that Q is not an identity
485
     * element (an infinity point). Note that this
486
     * cannot happen in the nettle implementation,
487
     * because it cannot represent an infinity point
488
     * on curves. */
489
0
    ret = ecc_shared_secret(&ecc_priv, &ecc_pub, out->data,
490
0
          out->size);
491
0
    if (ret < 0) {
492
0
      gnutls_free(out->data);
493
0
      goto ecc_cleanup;
494
0
    }
495
#ifdef ENABLE_FIPS140
496
    if (_gnutls_fips_mode_enabled()) {
497
      const char *order, *modulus;
498
499
      ecc_point_mul(&r, &ecc_priv, &ecc_pub);
500
      ecc_point_get(&r, x, y);
501
502
      /* Step 2: verify that both coordinates of Q are
503
       * in the range [0, p - 1].
504
       *
505
       * Step 3: verify that Q lie on the curve
506
       *
507
       * Both checks are performed in nettle.  */
508
      if (!ecc_point_set(&r, x, y)) {
509
        ret = gnutls_assert_val(
510
          GNUTLS_E_ILLEGAL_PARAMETER);
511
        goto ecc_cleanup;
512
      }
513
514
      /* Step 4: verify that n * Q, where n is the
515
       * curve order, result in an identity element
516
       *
517
       * Since nettle internally cannot represent an
518
       * identity element on curves, we validate this
519
       * instead:
520
       *
521
       *   (n - 1) * Q = -Q
522
       *
523
       * That effectively means: n * Q = -Q + Q = O
524
       */
525
      order = get_supported_nist_curve_order(priv->curve);
526
      if (unlikely(order == NULL)) {
527
        ret = gnutls_assert_val(
528
          GNUTLS_E_INTERNAL_ERROR);
529
        goto ecc_cleanup;
530
      }
531
532
      ret = mpz_set_str(nn, order, 16);
533
      if (unlikely(ret < 0)) {
534
        ret = gnutls_assert_val(
535
          GNUTLS_E_MPI_SCAN_FAILED);
536
        goto ecc_cleanup;
537
      }
538
539
      modulus = get_supported_nist_curve_modulus(priv->curve);
540
      if (unlikely(modulus == NULL)) {
541
        ret = gnutls_assert_val(
542
          GNUTLS_E_INTERNAL_ERROR);
543
        goto ecc_cleanup;
544
      }
545
546
      ret = mpz_set_str(mm, modulus, 16);
547
      if (unlikely(ret < 0)) {
548
        ret = gnutls_assert_val(
549
          GNUTLS_E_MPI_SCAN_FAILED);
550
        goto ecc_cleanup;
551
      }
552
553
      /* (n - 1) * Q = -Q */
554
      mpz_sub_ui(nn, nn, 1);
555
      ecc_scalar_set(&n, nn);
556
      ecc_point_mul(&r, &n, &r);
557
      ecc_point_get(&r, xx, yy);
558
      mpz_sub(mm, mm, y);
559
560
      if (mpz_cmp(xx, x) != 0 || mpz_cmp(yy, mm) != 0) {
561
        ret = gnutls_assert_val(
562
          GNUTLS_E_ILLEGAL_PARAMETER);
563
        goto ecc_cleanup;
564
      }
565
    } else {
566
      not_approved = true;
567
    }
568
#endif
569
570
0
  ecc_cleanup:
571
0
    ecc_point_clear(&ecc_pub);
572
0
    ecc_scalar_zclear(&ecc_priv);
573
0
  ecc_fail_cleanup:
574
0
    mpz_clear(x);
575
0
    mpz_clear(y);
576
0
    mpz_clear(xx);
577
0
    mpz_clear(yy);
578
0
    mpz_clear(nn);
579
0
    mpz_clear(mm);
580
0
    ecc_point_clear(&r);
581
0
    ecc_scalar_clear(&n);
582
0
    ecc_scalar_clear(&m);
583
0
    if (ret < 0)
584
0
      goto cleanup;
585
0
    break;
586
0
  }
587
0
  case GNUTLS_PK_ECDH_X25519:
588
0
  case GNUTLS_PK_ECDH_X448: {
589
0
    unsigned size = gnutls_ecc_curve_get_size(priv->curve);
590
591
    /* Edwards curves are not approved */
592
0
    not_approved = true;
593
594
0
    if (nonce != NULL) {
595
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
596
0
      goto cleanup;
597
0
    }
598
599
    /* The point is in pub, while the private part (scalar) in priv. */
600
601
0
    if (size == 0 || priv->raw_priv.size != size) {
602
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
603
0
      goto cleanup;
604
0
    }
605
606
0
    out->data = gnutls_malloc(size);
607
0
    if (out->data == NULL) {
608
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
609
0
      goto cleanup;
610
0
    }
611
612
0
    out->size = size;
613
614
0
    ret = edwards_curve_mul(algo, out->data, priv->raw_priv.data,
615
0
          pub->raw_pub.data);
616
0
    if (ret < 0)
617
0
      goto cleanup;
618
619
0
    if (_gnutls_mem_is_zero(out->data, out->size)) {
620
0
      gnutls_free(out->data);
621
0
      gnutls_assert();
622
0
      ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
623
0
      goto cleanup;
624
0
    }
625
0
    break;
626
0
  }
627
0
#if ENABLE_GOST
628
0
  case GNUTLS_PK_GOST_01:
629
0
  case GNUTLS_PK_GOST_12_256:
630
0
  case GNUTLS_PK_GOST_12_512: {
631
0
    struct ecc_scalar ecc_priv;
632
0
    struct ecc_point ecc_pub;
633
0
    const struct ecc_curve *curve;
634
635
    /* GOST curves are not approved */
636
0
    not_approved = true;
637
638
0
    out->data = NULL;
639
640
0
    curve = get_supported_gost_curve(priv->curve);
641
0
    if (curve == NULL) {
642
0
      gnutls_assert();
643
0
      ret = GNUTLS_E_ECC_UNSUPPORTED_CURVE;
644
0
      goto cleanup;
645
0
    }
646
647
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
648
0
           gnutls_ecc_curve_get_name(priv->curve),
649
0
           NULL);
650
651
0
    if (nonce == NULL) {
652
0
      gnutls_assert();
653
0
      ret = GNUTLS_E_INVALID_REQUEST;
654
0
      goto cleanup;
655
0
    }
656
657
0
    ret = _gost_params_to_pubkey(pub, &ecc_pub, curve);
658
0
    if (ret < 0) {
659
0
      gnutls_assert();
660
0
      goto cleanup;
661
0
    }
662
663
0
    ret = _gost_params_to_privkey(priv, &ecc_priv, curve);
664
0
    if (ret < 0) {
665
0
      ecc_point_clear(&ecc_pub);
666
0
      gnutls_assert();
667
0
      goto cleanup;
668
0
    }
669
670
0
    out->size = 2 * gnutls_ecc_curve_get_size(priv->curve);
671
0
    out->data = gnutls_malloc(out->size);
672
0
    if (out->data == NULL) {
673
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
674
0
      goto gost_cleanup;
675
0
    }
676
677
0
    gostdsa_vko(&ecc_priv, &ecc_pub, nonce->size, nonce->data,
678
0
          out->data);
679
680
0
  gost_cleanup:
681
0
    ecc_point_clear(&ecc_pub);
682
0
    ecc_scalar_zclear(&ecc_priv);
683
0
    if (ret < 0)
684
0
      goto cleanup;
685
0
    break;
686
0
  }
687
0
#endif
688
0
  default:
689
0
    gnutls_assert();
690
0
    ret = GNUTLS_E_INTERNAL_ERROR;
691
0
    goto cleanup;
692
0
  }
693
694
0
  ret = 0;
695
696
0
cleanup:
697
0
  if (ret < 0) {
698
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
699
0
  } else if (not_approved) {
700
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
701
0
  } else {
702
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
703
0
  }
704
705
0
  gnutls_audit_pop_context();
706
707
0
  return ret;
708
0
}
709
710
#ifdef HAVE_LEANCRYPTO
711
static enum lc_kyber_type ml_kem_pk_to_lc_kyber_type(gnutls_pk_algorithm_t algo)
712
{
713
  switch (algo) {
714
#ifdef LC_KYBER_768_ENABLED
715
  case GNUTLS_PK_MLKEM768:
716
    return LC_KYBER_768;
717
#endif
718
#ifdef LC_KYBER_1024_ENABLED
719
  case GNUTLS_PK_MLKEM1024:
720
    return LC_KYBER_1024;
721
#endif
722
  default:
723
    return gnutls_assert_val(LC_KYBER_UNKNOWN);
724
  }
725
}
726
727
static int ml_kem_exists(gnutls_pk_algorithm_t algo)
728
{
729
  return ml_kem_pk_to_lc_kyber_type(algo) != LC_KYBER_UNKNOWN;
730
}
731
732
static int ml_kem_encaps(gnutls_pk_algorithm_t algo, gnutls_datum_t *ciphertext,
733
       gnutls_datum_t *shared_secret,
734
       const gnutls_datum_t *pub)
735
{
736
  enum lc_kyber_type type;
737
  struct lc_kyber_ct ct;
738
  struct lc_kyber_ss ss;
739
  struct lc_kyber_pk pk;
740
  gnutls_datum_t tmp_ciphertext = { NULL, 0 };
741
  gnutls_datum_t tmp_shared_secret = { NULL, 0 };
742
  uint8_t *ptr;
743
  size_t len;
744
  int ret;
745
746
  type = ml_kem_pk_to_lc_kyber_type(algo);
747
  if (type == LC_KYBER_UNKNOWN)
748
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
749
750
  ret = lc_kyber_pk_load(&pk, pub->data, pub->size);
751
  if (ret < 0 || lc_kyber_pk_type(&pk) != type) {
752
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
753
    goto cleanup;
754
  }
755
756
  ret = lc_kyber_enc(&ct, &ss, &pk);
757
  if (ret < 0) {
758
    ret = gnutls_assert_val(GNUTLS_E_PK_ENCRYPTION_FAILED);
759
    goto cleanup;
760
  }
761
762
  ret = lc_kyber_ct_ptr(&ptr, &len, &ct);
763
  if (ret < 0) {
764
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
765
    goto cleanup;
766
  }
767
  ret = _gnutls_set_datum(&tmp_ciphertext, ptr, len);
768
  if (ret < 0) {
769
    gnutls_assert();
770
    goto cleanup;
771
  }
772
773
  ret = lc_kyber_ss_ptr(&ptr, &len, &ss);
774
  if (ret < 0) {
775
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
776
    goto cleanup;
777
  }
778
  ret = _gnutls_set_datum(&tmp_shared_secret, ptr, len);
779
  if (ret < 0) {
780
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
781
    goto cleanup;
782
  }
783
784
  *ciphertext = _gnutls_take_datum(&tmp_ciphertext);
785
  *shared_secret = _gnutls_take_datum(&tmp_shared_secret);
786
787
  ret = 0;
788
789
cleanup:
790
  _gnutls_free_datum(&tmp_ciphertext);
791
  _gnutls_free_key_datum(&tmp_shared_secret);
792
  zeroize_key(&pk, sizeof(pk));
793
  return ret;
794
}
795
796
static int ml_kem_decaps(gnutls_pk_algorithm_t algo,
797
       gnutls_datum_t *shared_secret,
798
       const gnutls_datum_t *ciphertext,
799
       const gnutls_datum_t *priv)
800
{
801
  int ret;
802
  enum lc_kyber_type type;
803
  struct lc_kyber_ss ss;
804
  struct lc_kyber_ct ct;
805
  struct lc_kyber_sk sk;
806
  gnutls_datum_t tmp_shared_secret = { NULL, 0 };
807
  uint8_t *ptr;
808
  size_t len;
809
810
  type = ml_kem_pk_to_lc_kyber_type(algo);
811
  if (type == LC_KYBER_UNKNOWN)
812
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
813
814
  ret = lc_kyber_sk_load(&sk, priv->data, priv->size);
815
  if (ret < 0 || lc_kyber_sk_type(&sk) != type) {
816
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
817
    goto cleanup;
818
  }
819
820
  ret = lc_kyber_ct_load(&ct, ciphertext->data, ciphertext->size);
821
  if (ret < 0 || lc_kyber_ct_type(&ct) != type) {
822
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
823
    goto cleanup;
824
  }
825
826
  ret = lc_kyber_dec(&ss, &ct, &sk);
827
  if (ret < 0) {
828
    ret = gnutls_assert_val(GNUTLS_E_PK_DECRYPTION_FAILED);
829
    goto cleanup;
830
  }
831
832
  ret = lc_kyber_ss_ptr(&ptr, &len, &ss);
833
  if (ret < 0) {
834
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
835
    goto cleanup;
836
  }
837
838
  ret = _gnutls_set_datum(&tmp_shared_secret, ptr, len);
839
  if (ret < 0) {
840
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
841
    goto cleanup;
842
  }
843
844
  *shared_secret = _gnutls_take_datum(&tmp_shared_secret);
845
846
  ret = 0;
847
848
cleanup:
849
  _gnutls_free_key_datum(&tmp_shared_secret);
850
  zeroize_key(&ss, sizeof(ss));
851
  zeroize_key(&sk, sizeof(sk));
852
  return ret;
853
}
854
855
static int ml_kem_generate_keypair(gnutls_pk_algorithm_t algo,
856
           gnutls_datum_t *raw_priv,
857
           gnutls_datum_t *raw_pub)
858
{
859
  int ret;
860
  enum lc_kyber_type type;
861
  struct lc_kyber_sk sk;
862
  struct lc_kyber_pk pk;
863
  gnutls_datum_t tmp_raw_priv = { NULL, 0 };
864
  gnutls_datum_t tmp_raw_pub = { NULL, 0 };
865
  uint8_t *ptr;
866
  size_t len;
867
868
  type = ml_kem_pk_to_lc_kyber_type(algo);
869
  if (type == LC_KYBER_UNKNOWN)
870
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
871
872
  ret = lc_kyber_keypair(&pk, &sk, lc_seeded_rng, type);
873
  if (ret < 0) {
874
    ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
875
    goto cleanup;
876
  }
877
878
  ret = lc_kyber_sk_ptr(&ptr, &len, &sk);
879
  if (ret < 0) {
880
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
881
    goto cleanup;
882
  }
883
884
  ret = _gnutls_set_datum(&tmp_raw_priv, ptr, len);
885
  if (ret < 0) {
886
    gnutls_assert();
887
    goto cleanup;
888
  }
889
890
  ret = lc_kyber_pk_ptr(&ptr, &len, &pk);
891
  if (ret < 0) {
892
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
893
    goto cleanup;
894
  }
895
896
  ret = _gnutls_set_datum(&tmp_raw_pub, ptr, len);
897
  if (ret < 0) {
898
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
899
    goto cleanup;
900
  }
901
902
  *raw_priv = _gnutls_take_datum(&tmp_raw_priv);
903
  *raw_pub = _gnutls_take_datum(&tmp_raw_pub);
904
905
  ret = 0;
906
907
cleanup:
908
  _gnutls_free_key_datum(&tmp_raw_priv);
909
  _gnutls_free_key_datum(&tmp_raw_pub);
910
  zeroize_key(&pk, sizeof(pk));
911
  zeroize_key(&sk, sizeof(sk));
912
  return ret;
913
}
914
#else
915
static int ml_kem_exists(gnutls_pk_algorithm_t algo MAYBE_UNUSED)
916
0
{
917
0
  return 0;
918
0
}
919
920
static int ml_kem_encaps(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
921
       gnutls_datum_t *ciphertext MAYBE_UNUSED,
922
       gnutls_datum_t *shared_secret MAYBE_UNUSED,
923
       const gnutls_datum_t *pub MAYBE_UNUSED)
924
0
{
925
0
  return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);
926
0
}
927
928
static int ml_kem_decaps(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
929
       gnutls_datum_t *shared_secret MAYBE_UNUSED,
930
       const gnutls_datum_t *ciphertext MAYBE_UNUSED,
931
       const gnutls_datum_t *priv MAYBE_UNUSED)
932
0
{
933
0
  return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);
934
0
}
935
936
static int ml_kem_generate_keypair(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
937
           gnutls_datum_t *raw_priv MAYBE_UNUSED,
938
           gnutls_datum_t *raw_pub MAYBE_UNUSED)
939
0
{
940
0
  return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);
941
0
}
942
#endif
943
944
static int _wrap_nettle_pk_encaps(gnutls_pk_algorithm_t algo,
945
          gnutls_datum_t *ciphertext,
946
          gnutls_datum_t *shared_secret,
947
          const gnutls_datum_t *pub)
948
0
{
949
0
  int ret;
950
0
  bool not_approved = false;
951
952
0
  switch (algo) {
953
0
  case GNUTLS_PK_MLKEM768:
954
0
  case GNUTLS_PK_MLKEM1024:
955
    /* unapproved until we implement a CAST per IG 10.3.A(14). */
956
0
    not_approved = true;
957
0
    break;
958
0
  default:
959
0
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);
960
0
  }
961
962
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING,
963
0
              "pk::encapsulate", "pk::algorithm",
964
0
              CRAU_STRING,
965
0
              gnutls_pk_get_name(algo), NULL);
966
967
0
  ret = ml_kem_encaps(algo, ciphertext, shared_secret, pub);
968
969
0
  if (ret < 0) {
970
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
971
0
  } else if (not_approved) {
972
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
973
0
  } else {
974
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
975
0
  }
976
977
0
  gnutls_audit_pop_context();
978
979
0
  return ret;
980
0
}
981
982
static int _wrap_nettle_pk_decaps(gnutls_pk_algorithm_t algo,
983
          gnutls_datum_t *shared_secret,
984
          const gnutls_datum_t *ciphertext,
985
          const gnutls_datum_t *priv)
986
0
{
987
0
  int ret;
988
0
  bool not_approved = false;
989
990
0
  switch (algo) {
991
0
  case GNUTLS_PK_MLKEM768:
992
0
  case GNUTLS_PK_MLKEM1024:
993
    /* unapproved until we implement a CAST per IG 10.3.A(14). */
994
0
    not_approved = true;
995
0
    break;
996
0
  default:
997
0
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_ALGORITHM);
998
0
  }
999
1000
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING,
1001
0
              "pk::decapsulate", "pk::algorithm",
1002
0
              CRAU_STRING,
1003
0
              gnutls_pk_get_name(algo), NULL);
1004
1005
0
  ret = ml_kem_decaps(algo, shared_secret, ciphertext, priv);
1006
1007
0
  if (ret < 0) {
1008
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1009
0
  } else if (not_approved) {
1010
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1011
0
  } else {
1012
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1013
0
  }
1014
1015
0
  gnutls_audit_pop_context();
1016
1017
0
  return ret;
1018
0
}
1019
1020
/* This wraps nettle_rsa_encrypt so it returns ciphertext as a byte
1021
 * array instead of a mpz_t value.  Returns 1 on success; 0 otherwise.
1022
 */
1023
static inline int _rsa_encrypt(const struct rsa_public_key *key, void *rnd_ctx,
1024
             nettle_random_func *rnd_func, size_t length,
1025
             const uint8_t *message, uint8_t *ciphertext)
1026
0
{
1027
0
  mpz_t p;
1028
0
  int ret;
1029
1030
0
  mpz_init(p);
1031
1032
0
  ret = rsa_encrypt(key, rnd_ctx, rnd_func, length, message, p);
1033
1034
0
  if (ret == 0) {
1035
0
    gnutls_assert();
1036
0
    goto cleanup;
1037
0
  }
1038
1039
0
  if (_gnutls_mpi_bprint_size(p, ciphertext, key->size) < 0) {
1040
0
    gnutls_assert();
1041
0
    goto cleanup;
1042
0
  }
1043
1044
0
cleanup:
1045
0
  mpz_clear(p);
1046
0
  return ret;
1047
0
}
1048
1049
/* This wraps nettle_rsa_oaep_sha*_encrypt to parametrize the function
1050
 * calls with a DIG argument.  Returns 1 on success; 0 otherwise.
1051
 */
1052
static inline int _rsa_oaep_encrypt(gnutls_digest_algorithm_t dig,
1053
            const struct rsa_public_key *pub,
1054
            void *rnd_ctx, nettle_random_func *rnd_func,
1055
            size_t label_length, const uint8_t *label,
1056
            size_t length, const uint8_t *message,
1057
            uint8_t *ciphertext)
1058
0
{
1059
0
  int (*encrypt_func)(const struct rsa_public_key *, void *,
1060
0
          nettle_random_func *, size_t, const uint8_t *,
1061
0
          size_t, const uint8_t *, uint8_t *);
1062
1063
0
  switch (dig) {
1064
0
  case GNUTLS_DIG_SHA256:
1065
0
    encrypt_func = rsa_oaep_sha256_encrypt;
1066
0
    break;
1067
0
  case GNUTLS_DIG_SHA384:
1068
0
    encrypt_func = rsa_oaep_sha384_encrypt;
1069
0
    break;
1070
0
  case GNUTLS_DIG_SHA512:
1071
0
    encrypt_func = rsa_oaep_sha512_encrypt;
1072
0
    break;
1073
0
  default:
1074
0
    gnutls_assert();
1075
0
    return 0;
1076
0
  }
1077
1078
0
  return encrypt_func(pub, rnd_ctx, rnd_func, label_length, label, length,
1079
0
          message, ciphertext);
1080
0
}
1081
1082
static int _wrap_nettle_pk_encrypt(gnutls_pk_algorithm_t algo,
1083
           gnutls_datum_t *ciphertext,
1084
           const gnutls_datum_t *plaintext,
1085
           const gnutls_pk_params_st *pk_params,
1086
           const gnutls_x509_spki_st *encrypt_params)
1087
0
{
1088
0
  int ret;
1089
0
  bool not_approved = false;
1090
0
  uint8_t *buf = NULL;
1091
1092
0
  FAIL_IF_LIB_ERROR;
1093
1094
0
  if (algo == GNUTLS_PK_RSA && pk_params->spki.pk == GNUTLS_PK_RSA_OAEP) {
1095
0
    algo = GNUTLS_PK_RSA_OAEP;
1096
0
  }
1097
1098
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::encrypt",
1099
0
              "pk::algorithm", CRAU_STRING,
1100
0
              gnutls_pk_get_name(algo), NULL);
1101
1102
0
  switch (algo) {
1103
0
  case GNUTLS_PK_RSA: {
1104
0
    struct rsa_public_key pub;
1105
0
    nettle_random_func *random_func;
1106
0
    size_t bits;
1107
1108
0
    if (!_gnutls_config_is_rsa_pkcs1_encrypt_allowed()) {
1109
0
      ret = gnutls_assert_val(
1110
0
        GNUTLS_E_UNSUPPORTED_ENCRYPTION_ALGORITHM);
1111
0
      goto cleanup;
1112
0
    }
1113
1114
    /* RSA encryption with PKCS#1 v1.5 padding is not approved */
1115
0
    not_approved = true;
1116
1117
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
1118
0
    if (ret < 0) {
1119
0
      gnutls_assert();
1120
0
      goto cleanup;
1121
0
    }
1122
1123
0
    bits = mpz_sizeinbase(pub.n, 2);
1124
1125
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
1126
1127
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
1128
0
      random_func = rnd_nonce_func_fallback;
1129
0
    else
1130
0
      random_func = rnd_nonce_func;
1131
1132
0
    buf = gnutls_malloc(pub.size);
1133
0
    if (!buf) {
1134
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1135
0
      goto cleanup;
1136
0
    }
1137
1138
0
    ret = _rsa_encrypt(&pub, NULL, random_func, plaintext->size,
1139
0
           plaintext->data, buf);
1140
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
1141
0
      ret = gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
1142
0
      goto cleanup;
1143
0
    }
1144
1145
0
    ciphertext->data = buf;
1146
0
    buf = NULL;
1147
0
    ciphertext->size = pub.size;
1148
0
    break;
1149
0
  }
1150
0
  case GNUTLS_PK_RSA_OAEP: {
1151
0
    struct rsa_public_key pub;
1152
0
    nettle_random_func *random_func;
1153
0
    size_t bits;
1154
1155
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
1156
0
    if (ret < 0) {
1157
0
      gnutls_assert();
1158
0
      goto cleanup;
1159
0
    }
1160
1161
#ifdef ENABLE_FIPS140
1162
    if (_gnutls_fips_mode_enabled() &&
1163
        _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
1164
      /* gnutls_rnd (called from rnd_nonce_func)
1165
       * will flip FIPS service indicator; use a
1166
       * temprary context
1167
       */
1168
      gnutls_fips140_context_st context =
1169
        GNUTLS_FIPS140_CONTEXT_INIT;
1170
1171
      ret = gnutls_fips140_push_context(&context);
1172
      if (ret < 0)
1173
        goto cleanup;
1174
1175
      not_approved = !rsa_validate_sp800_56b_r2_public(
1176
        &pub, NULL, rnd_nonce_func);
1177
1178
      ret = gnutls_fips140_pop_context();
1179
      if (ret < 0)
1180
        goto cleanup;
1181
    }
1182
#endif
1183
1184
0
    bits = mpz_sizeinbase(pub.n, 2);
1185
1186
0
    _gnutls_audit_data(
1187
0
      "pk::bits", CRAU_WORD, bits, "pk::hash", CRAU_STRING,
1188
0
      gnutls_digest_get_name(encrypt_params->rsa_oaep_dig),
1189
0
      NULL);
1190
1191
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
1192
0
      random_func = rnd_nonce_func_fallback;
1193
0
    else
1194
0
      random_func = rnd_nonce_func;
1195
1196
0
    buf = gnutls_malloc(pub.size);
1197
0
    if (!buf) {
1198
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1199
0
      goto cleanup;
1200
0
    }
1201
1202
0
    ret = _rsa_oaep_encrypt(encrypt_params->rsa_oaep_dig, &pub,
1203
0
          NULL, random_func,
1204
0
          encrypt_params->rsa_oaep_label.size,
1205
0
          encrypt_params->rsa_oaep_label.data,
1206
0
          plaintext->size, plaintext->data, buf);
1207
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
1208
0
      ret = gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
1209
0
      goto cleanup;
1210
0
    }
1211
0
    ciphertext->data = buf;
1212
0
    buf = NULL;
1213
0
    ciphertext->size = pub.size;
1214
0
    break;
1215
0
  }
1216
0
  default:
1217
0
    gnutls_assert();
1218
0
    ret = GNUTLS_E_INVALID_REQUEST;
1219
0
    goto cleanup;
1220
0
  }
1221
1222
0
  ret = 0;
1223
1224
0
cleanup:
1225
0
  gnutls_free(buf);
1226
0
  if (ret < 0) {
1227
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1228
0
  } else if (not_approved) {
1229
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1230
0
  } else {
1231
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1232
0
  }
1233
1234
0
  gnutls_audit_pop_context();
1235
1236
0
  FAIL_IF_LIB_ERROR;
1237
0
  return ret;
1238
0
}
1239
1240
/* This wraps nettle_rsa_decrypt_tr so it takes ciphertext as a byte
1241
 * array instead of a mpz_t value.  Returns 1 on success; 0 otherwise.
1242
 */
1243
static inline int _rsa_decrypt_tr(const struct rsa_public_key *pub,
1244
          const struct rsa_private_key *key,
1245
          void *rnd_ctx, nettle_random_func *rnd_func,
1246
          size_t *length, uint8_t *message,
1247
          const uint8_t *ciphertext)
1248
0
{
1249
0
  bigint_t c;
1250
0
  int ret;
1251
1252
0
  if (_gnutls_mpi_init_scan_nz(&c, ciphertext, pub->size) < 0) {
1253
0
    gnutls_assert();
1254
0
    return 0;
1255
0
  }
1256
1257
0
  ret = rsa_decrypt_tr(pub, key, rnd_ctx, rnd_func, length, message, c);
1258
1259
0
  _gnutls_mpi_release(&c);
1260
1261
0
  return ret;
1262
0
}
1263
1264
/* This wraps nettle_rsa_oaep_sha*_decrypt to parametrize the function
1265
 * calls with a DIG argument.  Returns 1 on success; 0 otherwise.
1266
 */
1267
static inline int _rsa_oaep_decrypt(gnutls_digest_algorithm_t dig,
1268
            const struct rsa_public_key *pub,
1269
            const struct rsa_private_key *key,
1270
            void *rnd_ctx, nettle_random_func *rnd_func,
1271
            size_t label_length, const uint8_t *label,
1272
            size_t *length, uint8_t *message,
1273
            const uint8_t *ciphertext)
1274
0
{
1275
0
  int (*decrypt_func)(const struct rsa_public_key *,
1276
0
          const struct rsa_private_key *, void *,
1277
0
          nettle_random_func *, size_t, const uint8_t *,
1278
0
          size_t *, uint8_t *, const uint8_t *);
1279
1280
0
  switch (dig) {
1281
0
  case GNUTLS_DIG_SHA256:
1282
0
    decrypt_func = rsa_oaep_sha256_decrypt;
1283
0
    break;
1284
0
  case GNUTLS_DIG_SHA384:
1285
0
    decrypt_func = rsa_oaep_sha384_decrypt;
1286
0
    break;
1287
0
  case GNUTLS_DIG_SHA512:
1288
0
    decrypt_func = rsa_oaep_sha512_decrypt;
1289
0
    break;
1290
0
  default:
1291
0
    gnutls_assert();
1292
0
    return 0;
1293
0
  }
1294
1295
0
  return decrypt_func(pub, key, rnd_ctx, rnd_func, label_length, label,
1296
0
          length, message, ciphertext);
1297
0
}
1298
1299
static int _wrap_nettle_pk_decrypt(gnutls_pk_algorithm_t algo,
1300
           gnutls_datum_t *plaintext,
1301
           const gnutls_datum_t *ciphertext,
1302
           const gnutls_pk_params_st *pk_params,
1303
           const gnutls_x509_spki_st *encrypt_params)
1304
0
{
1305
0
  int ret;
1306
0
  bool not_approved = false;
1307
0
  uint8_t *buf = NULL;
1308
1309
0
  FAIL_IF_LIB_ERROR;
1310
1311
0
  if (algo == GNUTLS_PK_RSA && encrypt_params->pk == GNUTLS_PK_RSA_OAEP) {
1312
0
    algo = GNUTLS_PK_RSA_OAEP;
1313
0
  }
1314
1315
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::decrypt",
1316
0
              "pk::algorithm", CRAU_STRING,
1317
0
              gnutls_pk_get_name(algo), NULL);
1318
1319
0
  switch (algo) {
1320
0
  case GNUTLS_PK_RSA: {
1321
0
    struct rsa_private_key priv;
1322
0
    struct rsa_public_key pub;
1323
0
    size_t length;
1324
0
    nettle_random_func *random_func;
1325
0
    size_t bits;
1326
1327
0
    if (!_gnutls_config_is_rsa_pkcs1_encrypt_allowed()) {
1328
0
      ret = gnutls_assert_val(
1329
0
        GNUTLS_E_UNSUPPORTED_ENCRYPTION_ALGORITHM);
1330
0
      goto cleanup;
1331
0
    }
1332
1333
    /* RSA decryption with PKCS#1 v1.5 padding is not approved */
1334
0
    not_approved = true;
1335
1336
0
    _rsa_params_to_privkey(pk_params, &priv);
1337
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
1338
0
    if (ret < 0) {
1339
0
      gnutls_assert();
1340
0
      goto cleanup;
1341
0
    }
1342
1343
0
    bits = mpz_sizeinbase(pub.n, 2);
1344
1345
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
1346
1347
0
    if (ciphertext->size != pub.size) {
1348
0
      ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1349
0
      goto cleanup;
1350
0
    }
1351
1352
0
    length = pub.size;
1353
0
    buf = gnutls_malloc(length);
1354
0
    if (!buf) {
1355
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1356
0
      goto cleanup;
1357
0
    }
1358
1359
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
1360
0
      random_func = rnd_nonce_func_fallback;
1361
0
    else
1362
0
      random_func = rnd_nonce_func;
1363
0
    ret = _rsa_decrypt_tr(&pub, &priv, NULL, random_func, &length,
1364
0
              buf, ciphertext->data);
1365
1366
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
1367
0
      ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1368
0
      goto cleanup;
1369
0
    }
1370
1371
0
    plaintext->data = buf;
1372
0
    buf = NULL;
1373
0
    plaintext->size = length;
1374
0
    break;
1375
0
  }
1376
0
  case GNUTLS_PK_RSA_OAEP: {
1377
0
    struct rsa_private_key priv;
1378
0
    struct rsa_public_key pub;
1379
0
    size_t length;
1380
0
    nettle_random_func *random_func;
1381
0
    size_t bits;
1382
1383
0
    _rsa_params_to_privkey(pk_params, &priv);
1384
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
1385
0
    if (ret < 0) {
1386
0
      gnutls_assert();
1387
0
      goto cleanup;
1388
0
    }
1389
1390
0
    bits = mpz_sizeinbase(pub.n, 2);
1391
1392
0
    _gnutls_audit_data(
1393
0
      "pk::bits", CRAU_WORD, bits, "pk::hash", CRAU_STRING,
1394
0
      gnutls_digest_get_name(encrypt_params->rsa_oaep_dig),
1395
0
      NULL);
1396
1397
0
    if (ciphertext->size != pub.size) {
1398
0
      ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1399
0
      goto cleanup;
1400
0
    }
1401
1402
0
    length = pub.size;
1403
0
    buf = gnutls_malloc(length);
1404
0
    if (!buf) {
1405
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1406
0
      goto cleanup;
1407
0
    }
1408
1409
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
1410
0
      random_func = rnd_nonce_func_fallback;
1411
0
    else
1412
0
      random_func = rnd_nonce_func;
1413
0
    ret = _rsa_oaep_decrypt(encrypt_params->rsa_oaep_dig, &pub,
1414
0
          &priv, NULL, random_func,
1415
0
          encrypt_params->rsa_oaep_label.size,
1416
0
          encrypt_params->rsa_oaep_label.data,
1417
0
          &length, buf, ciphertext->data);
1418
1419
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
1420
0
      ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1421
0
      goto cleanup;
1422
0
    }
1423
1424
0
    plaintext->data = buf;
1425
0
    buf = NULL;
1426
0
    plaintext->size = length;
1427
0
    break;
1428
0
  }
1429
0
  default:
1430
0
    gnutls_assert();
1431
0
    ret = GNUTLS_E_INTERNAL_ERROR;
1432
0
    goto cleanup;
1433
0
  }
1434
1435
0
  ret = 0;
1436
1437
0
cleanup:
1438
0
  gnutls_free(buf);
1439
0
  if (ret < 0) {
1440
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1441
0
  } else if (not_approved) {
1442
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1443
0
  } else {
1444
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1445
0
  }
1446
1447
0
  gnutls_audit_pop_context();
1448
1449
0
  FAIL_IF_LIB_ERROR;
1450
0
  return ret;
1451
0
}
1452
1453
/* This wraps nettle_rsa_sec_decrypt so it takes ciphertext as a byte
1454
 * array instead of a mpz_t value.  Returns 1 on success; 0 otherwise.
1455
 */
1456
static inline int _rsa_sec_decrypt(const struct rsa_public_key *pub,
1457
           const struct rsa_private_key *key,
1458
           void *rnd_ctx, nettle_random_func *rnd_func,
1459
           size_t length, uint8_t *message,
1460
           const uint8_t *ciphertext)
1461
0
{
1462
0
  bigint_t c;
1463
0
  int ret;
1464
1465
0
  if (_gnutls_mpi_init_scan_nz(&c, ciphertext, pub->size) < 0) {
1466
0
    gnutls_assert();
1467
0
    return 0;
1468
0
  }
1469
1470
0
  ret = rsa_sec_decrypt(pub, key, rnd_ctx, rnd_func, length, message, c);
1471
1472
0
  _gnutls_mpi_release(&c);
1473
1474
0
  return ret;
1475
0
}
1476
1477
/* Note: we do not allocate in this function to avoid asymettric
1478
 * unallocation (which creates a side channel) in case of failure
1479
 * */
1480
static int _wrap_nettle_pk_decrypt2(gnutls_pk_algorithm_t algo,
1481
            const gnutls_datum_t *ciphertext,
1482
            unsigned char *plaintext,
1483
            size_t plaintext_size,
1484
            const gnutls_pk_params_st *pk_params,
1485
            const gnutls_x509_spki_st *encrypt_params)
1486
0
{
1487
0
  struct rsa_private_key priv;
1488
0
  struct rsa_public_key pub;
1489
0
  uint32_t is_err;
1490
0
  int ret;
1491
0
  nettle_random_func *random_func;
1492
0
  bool not_approved = false;
1493
0
  size_t bits;
1494
1495
0
  FAIL_IF_LIB_ERROR;
1496
1497
0
  if ((algo != GNUTLS_PK_RSA && algo != GNUTLS_PK_RSA_OAEP) ||
1498
0
      plaintext == NULL) {
1499
0
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1500
0
    goto fail;
1501
0
  }
1502
1503
0
  if (encrypt_params->pk == GNUTLS_PK_RSA_OAEP) {
1504
0
    algo = GNUTLS_PK_RSA_OAEP;
1505
0
  }
1506
1507
0
  _rsa_params_to_privkey(pk_params, &priv);
1508
0
  ret = _rsa_params_to_pubkey(pk_params, &pub);
1509
0
  if (ret < 0) {
1510
0
    gnutls_assert();
1511
0
    goto fail;
1512
0
  }
1513
1514
0
  bits = mpz_sizeinbase(pub.n, 2);
1515
1516
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::decrypt",
1517
0
              "pk::algorithm", CRAU_STRING,
1518
0
              gnutls_pk_get_name(algo),
1519
0
              "pk::bits", CRAU_WORD, bits, NULL);
1520
1521
0
  if (ciphertext->size != pub.size) {
1522
0
    ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1523
0
    goto fail;
1524
0
  }
1525
1526
0
  if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
1527
0
    random_func = rnd_nonce_func_fallback;
1528
0
  else
1529
0
    random_func = rnd_nonce_func;
1530
1531
0
  switch (algo) {
1532
0
  case GNUTLS_PK_RSA:
1533
0
    if (!_gnutls_config_is_rsa_pkcs1_encrypt_allowed()) {
1534
0
      ret = gnutls_assert_val(
1535
0
        GNUTLS_E_UNSUPPORTED_ENCRYPTION_ALGORITHM);
1536
0
      goto fail;
1537
0
    }
1538
1539
    /* RSA decryption with PKCS#1 v1.5 padding is not approved */
1540
0
    not_approved = true;
1541
1542
0
    ret = _rsa_sec_decrypt(&pub, &priv, NULL, random_func,
1543
0
               plaintext_size, plaintext,
1544
0
               ciphertext->data);
1545
0
    break;
1546
0
  case GNUTLS_PK_RSA_OAEP:
1547
0
    _gnutls_audit_data(
1548
0
      "pk::hash", CRAU_STRING,
1549
0
      gnutls_digest_get_name(encrypt_params->rsa_oaep_dig),
1550
0
      NULL);
1551
1552
0
    ret = _rsa_oaep_decrypt(encrypt_params->rsa_oaep_dig, &pub,
1553
0
          &priv, NULL, random_func,
1554
0
          encrypt_params->rsa_oaep_label.size,
1555
0
          encrypt_params->rsa_oaep_label.data,
1556
0
          &plaintext_size, plaintext,
1557
0
          ciphertext->data);
1558
0
    break;
1559
0
  default:
1560
0
    gnutls_assert();
1561
0
    ret = GNUTLS_E_INTERNAL_ERROR;
1562
0
    goto fail;
1563
0
  }
1564
1565
  /* The decrypt operation is infallible; treat the approved
1566
   * operation as complete at this point, regardless of any
1567
   * decryption failure detected below.
1568
   */
1569
0
  _gnutls_switch_fips_state(not_approved ?
1570
0
            GNUTLS_FIPS140_OP_NOT_APPROVED :
1571
0
            GNUTLS_FIPS140_OP_APPROVED);
1572
1573
  /* after this point, any conditional on failure that cause differences
1574
   * in execution may create a timing or cache access pattern side
1575
   * channel that can be used as an oracle, so thread very carefully */
1576
1577
  /* Here HAVE_LIB_ERROR() should be fine as it doesn't have
1578
   * branches in it and returns a bool */
1579
0
  is_err = HAVE_LIB_ERROR();
1580
  /* if is_err != 0 */
1581
0
  is_err = CONSTCHECK_NOT_EQUAL(is_err, 0);
1582
  /* or ret == 0 */
1583
0
  is_err |= CONSTCHECK_EQUAL(ret, 0);
1584
  /* then return GNUTLS_E_DECRYPTION_FAILED */
1585
0
  return (int)((is_err * UINT_MAX) & GNUTLS_E_DECRYPTION_FAILED);
1586
1587
0
fail:
1588
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1589
1590
0
  gnutls_audit_pop_context();
1591
1592
0
  return ret;
1593
0
}
1594
1595
#define CHECK_INVALID_RSA_PSS_PARAMS(dig_size, salt_size, pub_size, err) \
1596
0
  if (unlikely(dig_size + salt_size + 2 > pub_size))               \
1597
0
  return gnutls_assert_val(err)
1598
1599
static int _rsa_pss_sign_digest_tr(gnutls_digest_algorithm_t dig,
1600
           const struct rsa_public_key *pub,
1601
           const struct rsa_private_key *priv,
1602
           void *rnd_ctx, nettle_random_func *rnd_func,
1603
           size_t salt_size, const uint8_t *digest,
1604
           mpz_t s)
1605
0
{
1606
0
  int (*sign_func)(const struct rsa_public_key *,
1607
0
       const struct rsa_private_key *, void *,
1608
0
       nettle_random_func *, size_t, const uint8_t *,
1609
0
       const uint8_t *, mpz_t);
1610
0
  uint8_t *salt = NULL;
1611
0
  size_t hash_size;
1612
0
  int ret;
1613
1614
0
  switch (dig) {
1615
0
  case GNUTLS_DIG_SHA256:
1616
0
    sign_func = rsa_pss_sha256_sign_digest_tr;
1617
0
    hash_size = 32;
1618
0
    break;
1619
0
  case GNUTLS_DIG_SHA384:
1620
0
    sign_func = rsa_pss_sha384_sign_digest_tr;
1621
0
    hash_size = 48;
1622
0
    break;
1623
0
  case GNUTLS_DIG_SHA512:
1624
0
    sign_func = rsa_pss_sha512_sign_digest_tr;
1625
0
    hash_size = 64;
1626
0
    break;
1627
0
  default:
1628
0
    gnutls_assert();
1629
0
    return GNUTLS_E_UNKNOWN_ALGORITHM;
1630
0
  }
1631
1632
  /* This is also checked in pss_encode_mgf1, but error out earlier.  */
1633
0
  CHECK_INVALID_RSA_PSS_PARAMS(hash_size, salt_size, pub->size,
1634
0
             GNUTLS_E_PK_INVALID_PUBKEY_PARAMS);
1635
1636
0
  if (salt_size > 0) {
1637
0
    salt = gnutls_malloc(salt_size);
1638
0
    if (salt == NULL)
1639
0
      return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1640
1641
0
    rnd_func(NULL, salt_size, salt);
1642
0
  }
1643
1644
0
  ret = sign_func(pub, priv, rnd_ctx, rnd_func, salt_size, salt, digest,
1645
0
      s);
1646
0
  if (ret == 0) {
1647
0
    gnutls_assert();
1648
0
    ret = GNUTLS_E_PK_SIGN_FAILED;
1649
0
  } else
1650
0
    ret = 0;
1651
1652
0
  gnutls_free(salt);
1653
0
  return ret;
1654
0
}
1655
1656
static inline gnutls_ecc_curve_t get_eddsa_curve(gnutls_pk_algorithm_t algo)
1657
0
{
1658
0
  switch (algo) {
1659
0
  case GNUTLS_PK_EDDSA_ED25519:
1660
0
    return GNUTLS_ECC_CURVE_ED25519;
1661
0
  case GNUTLS_PK_EDDSA_ED448:
1662
0
    return GNUTLS_ECC_CURVE_ED448;
1663
0
  default:
1664
0
    return gnutls_assert_val(GNUTLS_ECC_CURVE_INVALID);
1665
0
  }
1666
0
}
1667
1668
static inline gnutls_ecc_curve_t get_ecdh_curve(gnutls_pk_algorithm_t algo)
1669
0
{
1670
0
  switch (algo) {
1671
0
  case GNUTLS_PK_ECDH_X25519:
1672
0
    return GNUTLS_ECC_CURVE_X25519;
1673
0
  case GNUTLS_PK_ECDH_X448:
1674
0
    return GNUTLS_ECC_CURVE_X448;
1675
0
  default:
1676
0
    return gnutls_assert_val(GNUTLS_ECC_CURVE_INVALID);
1677
0
  }
1678
0
}
1679
1680
static inline int eddsa_sign(gnutls_pk_algorithm_t algo, const uint8_t *pub,
1681
           const uint8_t *priv, size_t length,
1682
           const uint8_t *msg, uint8_t *signature)
1683
0
{
1684
0
  switch (algo) {
1685
0
  case GNUTLS_PK_EDDSA_ED25519:
1686
0
    ed25519_sha512_sign(pub, priv, length, msg, signature);
1687
0
    return 0;
1688
0
  case GNUTLS_PK_EDDSA_ED448:
1689
0
    ed448_shake256_sign(pub, priv, length, msg, signature);
1690
0
    return 0;
1691
0
  default:
1692
0
    return gnutls_assert_val(
1693
0
      GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1694
0
  }
1695
0
}
1696
1697
#ifdef HAVE_LEANCRYPTO
1698
static enum lc_dilithium_type
1699
ml_dsa_pk_to_lc_dilithium_type(gnutls_pk_algorithm_t algo)
1700
{
1701
  switch (algo) {
1702
#ifdef LC_DILITHIUM_44_ENABLED
1703
  case GNUTLS_PK_MLDSA44:
1704
    return LC_DILITHIUM_44;
1705
#endif
1706
#ifdef LC_DILITHIUM_65_ENABLED
1707
  case GNUTLS_PK_MLDSA65:
1708
    return LC_DILITHIUM_65;
1709
#endif
1710
#ifdef LC_DILITHIUM_87_ENABLED
1711
  case GNUTLS_PK_MLDSA87:
1712
    return LC_DILITHIUM_87;
1713
#endif
1714
  default:
1715
    return gnutls_assert_val(LC_DILITHIUM_UNKNOWN);
1716
  }
1717
}
1718
1719
static int ml_dsa_exists(gnutls_pk_algorithm_t algo)
1720
{
1721
  return ml_dsa_pk_to_lc_dilithium_type(algo) != LC_DILITHIUM_UNKNOWN;
1722
}
1723
1724
static int ml_dsa_sign(gnutls_pk_algorithm_t algo, gnutls_datum_t *signature,
1725
           const gnutls_datum_t *message,
1726
           const gnutls_datum_t *raw_priv)
1727
{
1728
  int ret;
1729
  enum lc_dilithium_type type;
1730
  struct lc_dilithium_sk sk;
1731
  struct lc_dilithium_sig sig;
1732
  gnutls_datum_t tmp_signature = { NULL, 0 };
1733
  uint8_t *ptr;
1734
  size_t len;
1735
1736
  type = ml_dsa_pk_to_lc_dilithium_type(algo);
1737
  if (type == LC_DILITHIUM_UNKNOWN)
1738
    return gnutls_assert_val(
1739
      GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1740
1741
  ret = lc_dilithium_sk_load(&sk, raw_priv->data, raw_priv->size);
1742
  if (ret < 0 || lc_dilithium_sk_type(&sk) != type) {
1743
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1744
    goto cleanup;
1745
  }
1746
1747
  ret = lc_dilithium_sign(&sig, message->data, message->size, &sk,
1748
        lc_seeded_rng);
1749
  if (ret < 0) {
1750
    ret = gnutls_assert_val(GNUTLS_E_PK_SIGN_FAILED);
1751
    goto cleanup;
1752
  }
1753
1754
  ret = lc_dilithium_sig_ptr(&ptr, &len, &sig);
1755
  if (ret < 0) {
1756
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1757
    goto cleanup;
1758
  }
1759
  ret = _gnutls_set_datum(&tmp_signature, ptr, len);
1760
  if (ret < 0)
1761
    goto cleanup;
1762
1763
  *signature = _gnutls_take_datum(&tmp_signature);
1764
1765
  ret = 0;
1766
1767
cleanup:
1768
  _gnutls_free_datum(&tmp_signature);
1769
  zeroize_key(&sk, sizeof(sk));
1770
  return ret;
1771
}
1772
1773
static int ml_dsa_verify(gnutls_pk_algorithm_t algo,
1774
       const gnutls_datum_t *signature,
1775
       const gnutls_datum_t *message,
1776
       const gnutls_datum_t *raw_pub)
1777
{
1778
  int ret;
1779
  enum lc_dilithium_type type;
1780
  struct lc_dilithium_sig sig;
1781
  struct lc_dilithium_pk pk;
1782
1783
  type = ml_dsa_pk_to_lc_dilithium_type(algo);
1784
  if (type == LC_DILITHIUM_UNKNOWN)
1785
    return gnutls_assert_val(
1786
      GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1787
1788
  ret = lc_dilithium_pk_load(&pk, raw_pub->data, raw_pub->size);
1789
  if (ret < 0 || lc_dilithium_pk_type(&pk) != type) {
1790
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1791
    goto cleanup;
1792
  }
1793
1794
  ret = lc_dilithium_sig_load(&sig, signature->data, signature->size);
1795
  if (ret < 0 || lc_dilithium_sig_type(&sig) != type) {
1796
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1797
    goto cleanup;
1798
  }
1799
1800
  ret = lc_dilithium_verify(&sig, message->data, message->size, &pk);
1801
  if (ret < 0) {
1802
    ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
1803
    goto cleanup;
1804
  }
1805
1806
  ret = 0;
1807
1808
cleanup:
1809
  zeroize_key(&pk, sizeof(pk));
1810
  return ret;
1811
}
1812
1813
static int ml_dsa_generate_keypair(gnutls_pk_algorithm_t algo,
1814
           gnutls_datum_t *raw_priv,
1815
           gnutls_datum_t *raw_pub,
1816
           const gnutls_datum_t *raw_seed)
1817
{
1818
  int ret;
1819
  enum lc_dilithium_type type;
1820
  struct lc_dilithium_sk sk;
1821
  struct lc_dilithium_pk pk;
1822
  gnutls_datum_t tmp_raw_priv = { NULL, 0 };
1823
  gnutls_datum_t tmp_raw_pub = { NULL, 0 };
1824
  uint8_t *ptr;
1825
  size_t len;
1826
1827
  type = ml_dsa_pk_to_lc_dilithium_type(algo);
1828
  if (type == LC_DILITHIUM_UNKNOWN)
1829
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
1830
1831
  ret = lc_dilithium_keypair_from_seed(&pk, &sk, raw_seed->data,
1832
               raw_seed->size, type);
1833
  if (ret < 0) {
1834
    ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
1835
    goto cleanup;
1836
  }
1837
1838
  ret = lc_dilithium_sk_ptr(&ptr, &len, &sk);
1839
  if (ret < 0) {
1840
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1841
    goto cleanup;
1842
  }
1843
1844
  ret = _gnutls_set_datum(&tmp_raw_priv, ptr, len);
1845
  if (ret < 0) {
1846
    gnutls_assert();
1847
    goto cleanup;
1848
  }
1849
1850
  ret = lc_dilithium_pk_ptr(&ptr, &len, &pk);
1851
  if (ret < 0) {
1852
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1853
    goto cleanup;
1854
  }
1855
1856
  ret = _gnutls_set_datum(&tmp_raw_pub, ptr, len);
1857
  if (ret < 0) {
1858
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1859
    goto cleanup;
1860
  }
1861
1862
  *raw_priv = _gnutls_take_datum(&tmp_raw_priv);
1863
  *raw_pub = _gnutls_take_datum(&tmp_raw_pub);
1864
1865
  ret = 0;
1866
1867
cleanup:
1868
  _gnutls_free_key_datum(&tmp_raw_priv);
1869
  _gnutls_free_key_datum(&tmp_raw_pub);
1870
  zeroize_key(&pk, sizeof(pk));
1871
  zeroize_key(&sk, sizeof(sk));
1872
  return ret;
1873
}
1874
1875
#ifdef HAVE_LC_DILITHIUM_PK_FROM_SK
1876
static int ml_dsa_privkey_to_pubkey(gnutls_pk_algorithm_t algo,
1877
            const gnutls_datum_t *raw_priv,
1878
            gnutls_datum_t *raw_pub)
1879
{
1880
  int ret;
1881
  enum lc_dilithium_type type;
1882
  struct lc_dilithium_sk sk;
1883
  struct lc_dilithium_pk pk;
1884
  gnutls_datum_t tmp_raw_pub = { NULL, 0 };
1885
  uint8_t *ptr;
1886
  size_t len;
1887
1888
  type = ml_dsa_pk_to_lc_dilithium_type(algo);
1889
  if (type == LC_DILITHIUM_UNKNOWN)
1890
    return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
1891
1892
  ret = lc_dilithium_sk_load(&sk, raw_priv->data, raw_priv->size);
1893
  if (ret < 0 || lc_dilithium_sk_type(&sk) != type) {
1894
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1895
    goto cleanup;
1896
  }
1897
1898
  ret = lc_dilithium_pk_from_sk(&pk, &sk);
1899
  if (ret < 0) {
1900
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1901
    goto cleanup;
1902
  }
1903
1904
  ret = lc_dilithium_pk_ptr(&ptr, &len, &pk);
1905
  if (ret < 0) {
1906
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1907
    goto cleanup;
1908
  }
1909
1910
  ret = _gnutls_set_datum(&tmp_raw_pub, ptr, len);
1911
  if (ret < 0) {
1912
    ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
1913
    goto cleanup;
1914
  }
1915
1916
  *raw_pub = _gnutls_take_datum(&tmp_raw_pub);
1917
1918
  ret = 0;
1919
1920
cleanup:
1921
  _gnutls_free_key_datum(&tmp_raw_pub);
1922
  zeroize_key(&pk, sizeof(pk));
1923
  zeroize_key(&sk, sizeof(sk));
1924
  return ret;
1925
}
1926
#else /* !HAVE_LC_DILITHIUM_PK_FROM_SK */
1927
static int ml_dsa_privkey_to_pubkey(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
1928
            const gnutls_datum_t *raw_priv MAYBE_UNUSED,
1929
            gnutls_datum_t *raw_pub MAYBE_UNUSED)
1930
{
1931
  return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);
1932
}
1933
#endif
1934
#else /* !HAVE_LEANCRYPTO */
1935
static int ml_dsa_exists(gnutls_pk_algorithm_t algo MAYBE_UNUSED)
1936
0
{
1937
0
  return 0;
1938
0
}
1939
1940
static int ml_dsa_sign(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
1941
           gnutls_datum_t *signature MAYBE_UNUSED,
1942
           const gnutls_datum_t *message MAYBE_UNUSED,
1943
           const gnutls_datum_t *raw_priv MAYBE_UNUSED)
1944
0
{
1945
0
  return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1946
0
}
1947
1948
static int ml_dsa_verify(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
1949
       const gnutls_datum_t *signature MAYBE_UNUSED,
1950
       const gnutls_datum_t *message MAYBE_UNUSED,
1951
       const gnutls_datum_t *raw_pub MAYBE_UNUSED)
1952
0
{
1953
0
  return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1954
0
}
1955
1956
static int ml_dsa_generate_keypair(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
1957
           gnutls_datum_t *raw_priv MAYBE_UNUSED,
1958
           gnutls_datum_t *raw_pub MAYBE_UNUSED,
1959
           const gnutls_datum_t *raw_seed MAYBE_UNUSED)
1960
0
{
1961
0
  return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1962
0
}
1963
1964
static int ml_dsa_privkey_to_pubkey(gnutls_pk_algorithm_t algo MAYBE_UNUSED,
1965
            const gnutls_datum_t *raw_priv MAYBE_UNUSED,
1966
            gnutls_datum_t *raw_pub MAYBE_UNUSED)
1967
0
{
1968
0
  return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
1969
0
}
1970
#endif
1971
1972
/* This is the lower-level part of privkey_sign_raw_data().
1973
 *
1974
 * It accepts data in the appropriate hash form, i.e., DigestInfo
1975
 * for PK_RSA, hash for PK_ECDSA, PK_DSA, PK_RSA_PSS, and raw data
1976
 * for Ed25519 and Ed448.
1977
 *
1978
 * in case of EC/DSA, signed data are encoded into r,s values
1979
 */
1980
static int _wrap_nettle_pk_sign(gnutls_pk_algorithm_t algo,
1981
        gnutls_datum_t *signature,
1982
        const gnutls_datum_t *vdata,
1983
        const gnutls_pk_params_st *pk_params,
1984
        const gnutls_x509_spki_st *sign_params)
1985
0
{
1986
0
  int ret;
1987
0
  unsigned int hash_len;
1988
0
  const mac_entry_st *me;
1989
0
  bool not_approved = false;
1990
1991
0
  FAIL_IF_LIB_ERROR;
1992
1993
  /* check if the curve relates to the algorithm used */
1994
0
  if (IS_EC(algo) && gnutls_ecc_curve_get_pk(pk_params->curve) != algo) {
1995
0
    ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
1996
0
    goto cleanup;
1997
0
  }
1998
1999
  /* deterministic ECDSA/DSA is prohibited under FIPS except in
2000
   * the selftests */
2001
0
  if ((algo == GNUTLS_PK_DSA || algo == GNUTLS_PK_ECDSA) &&
2002
0
      (sign_params->flags & GNUTLS_PK_FLAG_REPRODUCIBLE) &&
2003
0
      _gnutls_fips_mode_enabled() &&
2004
0
      _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
2005
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
2006
0
    goto cleanup;
2007
0
  }
2008
2009
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::sign",
2010
0
              "pk::algorithm", CRAU_STRING,
2011
0
              gnutls_pk_get_name(algo), NULL);
2012
2013
0
  switch (algo) {
2014
0
  case GNUTLS_PK_EDDSA_ED25519: /* we do EdDSA */
2015
0
  case GNUTLS_PK_EDDSA_ED448: {
2016
0
    const gnutls_ecc_curve_entry_st *e;
2017
2018
0
    if (unlikely(get_eddsa_curve(algo) != pk_params->curve)) {
2019
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2020
0
      goto cleanup;
2021
0
    }
2022
2023
0
    e = _gnutls_ecc_curve_get_params(pk_params->curve);
2024
0
    if (e == NULL) {
2025
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
2026
0
      goto cleanup;
2027
0
    }
2028
2029
0
    signature->data = gnutls_malloc(e->sig_size);
2030
0
    if (signature->data == NULL) {
2031
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
2032
0
      goto cleanup;
2033
0
    }
2034
2035
0
    signature->size = e->sig_size;
2036
2037
0
    if (pk_params->raw_pub.size != e->size ||
2038
0
        pk_params->raw_priv.size != e->size) {
2039
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIGN_FAILED);
2040
0
      goto cleanup;
2041
0
    }
2042
2043
0
    ret = eddsa_sign(algo, pk_params->raw_pub.data,
2044
0
         pk_params->raw_priv.data, vdata->size,
2045
0
         vdata->data, signature->data);
2046
0
    if (ret < 0)
2047
0
      goto cleanup;
2048
2049
0
    break;
2050
0
  }
2051
0
#if ENABLE_GOST
2052
0
  case GNUTLS_PK_GOST_01:
2053
0
  case GNUTLS_PK_GOST_12_256:
2054
0
  case GNUTLS_PK_GOST_12_512: {
2055
0
    struct ecc_scalar priv;
2056
0
    struct dsa_signature sig;
2057
0
    const struct ecc_curve *curve;
2058
2059
    /* GOSTDSA is not approved */
2060
0
    not_approved = true;
2061
2062
0
    curve = get_supported_gost_curve(pk_params->curve);
2063
0
    if (curve == NULL) {
2064
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2065
0
      goto cleanup;
2066
0
    }
2067
2068
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
2069
0
           gnutls_ecc_curve_get_name(pk_params->curve),
2070
0
           NULL);
2071
2072
0
    ret = _ecc_params_to_privkey(pk_params, &priv, curve);
2073
0
    if (ret < 0) {
2074
0
      gnutls_assert();
2075
0
      goto cleanup;
2076
0
    }
2077
2078
    /* This call will return a valid MAC entry and
2079
     * getters will check that is not null anyway. */
2080
0
    me = hash_to_entry(_gnutls_gost_digest(pk_params->algo));
2081
0
    if (_gnutls_mac_get_algo_len(me) != vdata->size) {
2082
0
      _gnutls_debug_log(
2083
0
        "Security level of algorithm requires hash %s(%zd)\n",
2084
0
        _gnutls_mac_get_name(me),
2085
0
        _gnutls_mac_get_algo_len(me));
2086
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
2087
0
      goto cleanup;
2088
0
    }
2089
2090
0
    dsa_signature_init(&sig);
2091
2092
0
    gostdsa_sign(&priv, NULL, rnd_tmpkey_func, vdata->size,
2093
0
           vdata->data, &sig);
2094
2095
0
    ret = _gnutls_encode_gost_rs(signature, &sig.r, &sig.s,
2096
0
               (ecc_bit_size(curve) + 7) / 8);
2097
2098
0
    dsa_signature_clear(&sig);
2099
0
    ecc_scalar_zclear(&priv);
2100
2101
0
    if (ret < 0) {
2102
0
      gnutls_assert();
2103
0
      goto cleanup;
2104
0
    }
2105
0
    break;
2106
0
  }
2107
0
#endif
2108
0
  case GNUTLS_PK_ECDSA: /* we do ECDSA */
2109
0
  {
2110
0
    struct ecc_scalar priv;
2111
0
    struct dsa_signature sig;
2112
0
    int curve_id = pk_params->curve;
2113
0
    const struct ecc_curve *curve;
2114
0
    mpz_t q;
2115
    /* 521-bit elliptic curve generator at maximum */
2116
0
    uint8_t buf[(521 + 7) / 8];
2117
0
    gnutls_datum_t k = { NULL, 0 };
2118
0
    void *random_ctx;
2119
0
    nettle_random_func *random_func;
2120
2121
0
    curve = get_supported_nist_curve(curve_id);
2122
0
    if (curve == NULL) {
2123
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2124
0
      goto cleanup;
2125
0
    }
2126
2127
    /* P-192 is not supported in FIPS 140-3 */
2128
0
    if (curve_id == GNUTLS_ECC_CURVE_SECP192R1) {
2129
0
      not_approved = true;
2130
0
    }
2131
2132
0
    ret = _ecc_params_to_privkey(pk_params, &priv, curve);
2133
0
    if (ret < 0) {
2134
0
      gnutls_assert();
2135
0
      goto cleanup;
2136
0
    }
2137
2138
0
    dsa_signature_init(&sig);
2139
2140
0
    me = _gnutls_dsa_q_to_hash(pk_params, &hash_len);
2141
2142
0
    if (hash_len > vdata->size) {
2143
0
      gnutls_assert();
2144
0
      _gnutls_debug_log(
2145
0
        "Security level of algorithm requires hash %s(%d) or better\n",
2146
0
        _gnutls_mac_get_name(me), hash_len);
2147
0
      hash_len = vdata->size;
2148
0
    }
2149
2150
    /* Only SHA-2 is allowed in FIPS 140-3 */
2151
0
    switch (DIG_TO_MAC(sign_params->dsa_dig)) {
2152
0
    case GNUTLS_MAC_SHA256:
2153
0
    case GNUTLS_MAC_SHA384:
2154
0
    case GNUTLS_MAC_SHA512:
2155
0
    case GNUTLS_MAC_SHA224:
2156
0
      break;
2157
0
    default:
2158
0
      not_approved = true;
2159
0
    }
2160
2161
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
2162
0
           gnutls_ecc_curve_get_name(curve_id),
2163
0
           "pk::hash", CRAU_STRING,
2164
0
           _gnutls_mac_get_name(me), NULL);
2165
2166
0
    mpz_init(q);
2167
2168
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST ||
2169
0
        (sign_params->flags & GNUTLS_PK_FLAG_REPRODUCIBLE)) {
2170
0
      mp_limb_t h[DSA_COMPUTE_K_ITCH];
2171
2172
0
      ret = _gnutls_ecc_curve_to_dsa_q(q, curve_id);
2173
0
      if (ret < 0)
2174
0
        goto ecdsa_cleanup;
2175
2176
0
      ret = _gnutls_dsa_compute_k(
2177
0
        h, mpz_limbs_read(q), priv.p,
2178
0
        ecc_size(priv.ecc), ecc_bit_size(priv.ecc),
2179
0
        DIG_TO_MAC(sign_params->dsa_dig), vdata->data,
2180
0
        vdata->size);
2181
0
      if (ret < 0)
2182
0
        goto ecdsa_cleanup;
2183
2184
0
      k.data = buf;
2185
0
      k.size = (ecc_bit_size(priv.ecc) + 7) / 8;
2186
2187
0
      _gnutls_ecdsa_compute_k_finish(k.data, k.size, h,
2188
0
                   ecc_size(priv.ecc));
2189
2190
0
      random_ctx = &k;
2191
0
      random_func = rnd_datum_func;
2192
0
    } else {
2193
0
      random_ctx = NULL;
2194
0
      random_func = rnd_nonce_func;
2195
0
    }
2196
0
    ecdsa_sign(&priv, random_ctx, random_func, hash_len,
2197
0
         vdata->data, &sig);
2198
2199
    /* prevent memory leaks */
2200
0
    if (HAVE_LIB_ERROR()) {
2201
0
      ret = GNUTLS_E_LIB_IN_ERROR_STATE;
2202
0
      goto ecdsa_cleanup;
2203
0
    }
2204
2205
0
    ret = _gnutls_encode_ber_rs(signature, &sig.r, &sig.s);
2206
2207
0
  ecdsa_cleanup:
2208
0
    dsa_signature_clear(&sig);
2209
0
    ecc_scalar_zclear(&priv);
2210
0
    mpz_clear(q);
2211
2212
0
    if (ret < 0) {
2213
0
      gnutls_assert();
2214
0
      goto cleanup;
2215
0
    }
2216
0
    break;
2217
0
  }
2218
0
#ifdef ENABLE_DSA
2219
0
  case GNUTLS_PK_DSA: {
2220
0
    struct dsa_params pub;
2221
0
    bigint_t priv;
2222
0
    struct dsa_signature sig;
2223
    /* 512-bit DSA subgroup at maximum */
2224
0
    uint8_t buf[(512 + 7) / 8];
2225
0
    gnutls_datum_t k = { NULL, 0 };
2226
0
    void *random_ctx;
2227
0
    nettle_random_func *random_func;
2228
0
    size_t bits;
2229
2230
    /* DSA is currently being defined as sunset with the
2231
     * current draft of FIPS 186-5 */
2232
0
    not_approved = true;
2233
2234
0
    memset(&priv, 0, sizeof(priv));
2235
0
    memset(&pub, 0, sizeof(pub));
2236
0
    _dsa_params_get(pk_params, &pub);
2237
2238
0
    priv = pk_params->params[DSA_X];
2239
2240
0
    dsa_signature_init(&sig);
2241
2242
0
    me = _gnutls_dsa_q_to_hash(pk_params, &hash_len);
2243
2244
0
    if (hash_len > vdata->size) {
2245
0
      gnutls_assert();
2246
0
      _gnutls_debug_log(
2247
0
        "Security level of algorithm requires hash %s(%d) or better (have: %d)\n",
2248
0
        _gnutls_mac_get_name(me), hash_len,
2249
0
        (int)vdata->size);
2250
0
      hash_len = vdata->size;
2251
0
    }
2252
2253
0
    bits = mpz_sizeinbase(pub.p, 2);
2254
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
2255
2256
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST ||
2257
0
        (sign_params->flags & GNUTLS_PK_FLAG_REPRODUCIBLE)) {
2258
0
      mp_limb_t h[DSA_COMPUTE_K_ITCH];
2259
2260
0
      ret = _gnutls_dsa_compute_k(
2261
0
        h, mpz_limbs_read(pub.q),
2262
0
        mpz_limbs_read(TOMPZ(priv)), mpz_size(pub.q),
2263
0
        mpz_sizeinbase(pub.q, 2),
2264
0
        DIG_TO_MAC(sign_params->dsa_dig), vdata->data,
2265
0
        vdata->size);
2266
0
      if (ret < 0)
2267
0
        goto dsa_fail;
2268
2269
0
      k.data = buf;
2270
0
      k.size = (mpz_sizeinbase(pub.q, 2) + 7) / 8;
2271
2272
0
      _gnutls_dsa_compute_k_finish(k.data, k.size, h,
2273
0
                 mpz_size(pub.q));
2274
2275
0
      random_ctx = &k;
2276
0
      random_func = rnd_datum_func;
2277
0
    } else {
2278
0
      random_ctx = NULL;
2279
0
      random_func = rnd_nonce_func;
2280
0
    }
2281
0
    ret = dsa_sign(&pub, TOMPZ(priv), random_ctx, random_func,
2282
0
             hash_len, vdata->data, &sig);
2283
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
2284
0
      gnutls_assert();
2285
0
      ret = GNUTLS_E_PK_SIGN_FAILED;
2286
0
      goto dsa_fail;
2287
0
    }
2288
2289
0
    ret = _gnutls_encode_ber_rs(signature, &sig.r, &sig.s);
2290
2291
0
  dsa_fail:
2292
0
    dsa_signature_clear(&sig);
2293
2294
0
    if (ret < 0) {
2295
0
      gnutls_assert();
2296
0
      goto cleanup;
2297
0
    }
2298
0
    break;
2299
0
  }
2300
0
#endif
2301
0
  case GNUTLS_PK_RSA: {
2302
0
    struct rsa_private_key priv;
2303
0
    struct rsa_public_key pub;
2304
0
    nettle_random_func *random_func;
2305
0
    mpz_t s;
2306
0
    size_t bits;
2307
2308
0
    _rsa_params_to_privkey(pk_params, &priv);
2309
2310
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
2311
0
    if (ret < 0) {
2312
0
      gnutls_assert();
2313
0
      goto cleanup;
2314
0
    }
2315
2316
0
    bits = mpz_sizeinbase(pub.n, 2);
2317
2318
    /* RSA modulus size should be 2048-bit or larger in FIPS
2319
     * 140-3.  In addition to this, only SHA-2 is allowed
2320
     * for SigGen; it is checked in pk_prepare_hash lib/pk.c
2321
     */
2322
0
    if (unlikely(bits < 2048)) {
2323
0
      not_approved = true;
2324
0
    }
2325
2326
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
2327
2328
0
    mpz_init(s);
2329
2330
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
2331
0
      random_func = rnd_nonce_func_fallback;
2332
0
    else
2333
0
      random_func = rnd_nonce_func;
2334
0
    ret = rsa_pkcs1_sign_tr(&pub, &priv, NULL, random_func,
2335
0
          vdata->size, vdata->data, s);
2336
0
    if (ret == 0 || HAVE_LIB_ERROR()) {
2337
0
      gnutls_assert();
2338
0
      ret = GNUTLS_E_PK_SIGN_FAILED;
2339
0
      goto rsa_fail;
2340
0
    }
2341
2342
0
    ret = _gnutls_mpi_dprint_size(s, signature, pub.size);
2343
2344
0
  rsa_fail:
2345
0
    mpz_clear(s);
2346
2347
0
    if (ret < 0) {
2348
0
      gnutls_assert();
2349
0
      goto cleanup;
2350
0
    }
2351
2352
0
    break;
2353
0
  }
2354
0
  case GNUTLS_PK_RSA_PSS: {
2355
0
    struct rsa_private_key priv;
2356
0
    struct rsa_public_key pub;
2357
0
    nettle_random_func *random_func;
2358
0
    mpz_t s;
2359
0
    size_t bits;
2360
2361
0
    _rsa_params_to_privkey(pk_params, &priv);
2362
2363
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
2364
0
    if (ret < 0) {
2365
0
      gnutls_assert();
2366
0
      goto cleanup;
2367
0
    }
2368
2369
0
    bits = mpz_sizeinbase(pub.n, 2);
2370
2371
    /* RSA modulus size should be 2048-bit or larger in FIPS
2372
     * 140-3.  In addition to this, only SHA-2 is allowed
2373
     * for SigGen; however, Nettle only support SHA256,
2374
     * SHA384, and SHA512 for RSA-PSS (see
2375
     * _rsa_pss_sign_digest_tr in this file for details).
2376
     */
2377
0
    if (unlikely(bits < 2048)) {
2378
0
      not_approved = true;
2379
0
    }
2380
2381
0
    mpz_init(s);
2382
2383
0
    me = hash_to_entry(sign_params->rsa_pss_dig);
2384
2385
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, "pk::hash",
2386
0
           CRAU_STRING, _gnutls_mac_get_name(me), NULL);
2387
2388
    /* According to FIPS 186-5 5.4, the salt length must be
2389
     * in the range between 0 and the hash length inclusive.
2390
     */
2391
0
    if (sign_params->salt_size > _gnutls_mac_get_algo_len(me)) {
2392
0
      not_approved = true;
2393
0
    }
2394
2395
0
    if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
2396
0
      random_func = rnd_nonce_func_fallback;
2397
0
    else
2398
0
      random_func = rnd_nonce_func;
2399
0
    ret = _rsa_pss_sign_digest_tr(sign_params->rsa_pss_dig, &pub,
2400
0
                &priv, NULL, random_func,
2401
0
                sign_params->salt_size,
2402
0
                vdata->data, s);
2403
0
    if (ret < 0) {
2404
0
      gnutls_assert();
2405
0
      ret = GNUTLS_E_PK_SIGN_FAILED;
2406
0
      goto rsa_pss_fail;
2407
0
    }
2408
2409
0
    ret = _gnutls_mpi_dprint_size(s, signature, pub.size);
2410
2411
0
  rsa_pss_fail:
2412
0
    mpz_clear(s);
2413
2414
0
    if (ret < 0) {
2415
0
      gnutls_assert();
2416
0
      goto cleanup;
2417
0
    }
2418
2419
0
    break;
2420
0
  }
2421
0
  case GNUTLS_PK_MLDSA44:
2422
0
  case GNUTLS_PK_MLDSA65:
2423
0
  case GNUTLS_PK_MLDSA87:
2424
0
    not_approved = true;
2425
0
    ret = ml_dsa_sign(algo, signature, vdata, &pk_params->raw_priv);
2426
0
    if (ret < 0)
2427
0
      goto cleanup;
2428
0
    break;
2429
0
  default:
2430
0
    gnutls_assert();
2431
0
    ret = GNUTLS_E_INTERNAL_ERROR;
2432
0
    goto cleanup;
2433
0
  }
2434
2435
0
  ret = 0;
2436
2437
0
cleanup:
2438
0
  if (ret < 0) {
2439
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2440
0
  } else if (not_approved) {
2441
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2442
0
  } else {
2443
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
2444
0
  }
2445
2446
0
  gnutls_audit_pop_context();
2447
2448
0
  FAIL_IF_LIB_ERROR;
2449
0
  return ret;
2450
0
}
2451
2452
static int _rsa_pss_verify_digest(gnutls_digest_algorithm_t dig,
2453
          const struct rsa_public_key *pub,
2454
          size_t salt_size, const uint8_t *digest,
2455
          size_t digest_size, const mpz_t s)
2456
0
{
2457
0
  int (*verify_func)(const struct rsa_public_key *, size_t,
2458
0
         const uint8_t *, const mpz_t);
2459
0
  size_t hash_size;
2460
2461
0
  switch (dig) {
2462
0
  case GNUTLS_DIG_SHA256:
2463
0
    verify_func = rsa_pss_sha256_verify_digest;
2464
0
    hash_size = 32;
2465
0
    break;
2466
0
  case GNUTLS_DIG_SHA384:
2467
0
    verify_func = rsa_pss_sha384_verify_digest;
2468
0
    hash_size = 48;
2469
0
    break;
2470
0
  case GNUTLS_DIG_SHA512:
2471
0
    verify_func = rsa_pss_sha512_verify_digest;
2472
0
    hash_size = 64;
2473
0
    break;
2474
0
  default:
2475
0
    gnutls_assert();
2476
0
    return 0;
2477
0
  }
2478
2479
0
  if (digest_size != hash_size)
2480
0
    return gnutls_assert_val(0);
2481
2482
0
  CHECK_INVALID_RSA_PSS_PARAMS(hash_size, salt_size, pub->size, 0);
2483
2484
0
  return verify_func(pub, salt_size, digest, s);
2485
0
}
2486
2487
static inline int eddsa_verify(gnutls_pk_algorithm_t algo, const uint8_t *pub,
2488
             size_t length, const uint8_t *msg,
2489
             const uint8_t *signature)
2490
0
{
2491
0
  int ret;
2492
2493
0
  switch (algo) {
2494
0
  case GNUTLS_PK_EDDSA_ED25519:
2495
0
    ret = ed25519_sha512_verify(pub, length, msg, signature);
2496
0
    if (ret == 0)
2497
0
      return gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2498
0
    return 0;
2499
0
  case GNUTLS_PK_EDDSA_ED448:
2500
0
    ret = ed448_shake256_verify(pub, length, msg, signature);
2501
0
    if (ret == 0)
2502
0
      return gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2503
0
    return 0;
2504
0
  default:
2505
0
    return gnutls_assert_val(
2506
0
      GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
2507
0
  }
2508
0
}
2509
2510
static int _wrap_nettle_pk_verify(gnutls_pk_algorithm_t algo,
2511
          const gnutls_datum_t *vdata,
2512
          const gnutls_datum_t *signature,
2513
          const gnutls_pk_params_st *pk_params,
2514
          const gnutls_x509_spki_st *sign_params)
2515
0
{
2516
0
  int ret;
2517
0
  unsigned int hash_len;
2518
0
  bigint_t tmp[2] = { NULL, NULL };
2519
0
  bool not_approved = false;
2520
2521
0
  FAIL_IF_LIB_ERROR;
2522
2523
  /* check if the curve relates to the algorithm used */
2524
0
  if (IS_EC(algo) && gnutls_ecc_curve_get_pk(pk_params->curve) != algo) {
2525
0
    ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2526
0
    goto cleanup;
2527
0
  }
2528
2529
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::verify",
2530
0
              "pk::algorithm", CRAU_STRING,
2531
0
              gnutls_pk_get_name(algo), NULL);
2532
2533
0
  switch (algo) {
2534
0
  case GNUTLS_PK_EDDSA_ED25519: /* we do EdDSA */
2535
0
  case GNUTLS_PK_EDDSA_ED448: {
2536
0
    const gnutls_ecc_curve_entry_st *e;
2537
2538
0
    if (unlikely(get_eddsa_curve(algo) != pk_params->curve)) {
2539
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2540
0
      goto cleanup;
2541
0
    }
2542
2543
0
    e = _gnutls_ecc_curve_get_params(pk_params->curve);
2544
0
    if (e == NULL) {
2545
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2546
0
      goto cleanup;
2547
0
    }
2548
2549
0
    if (signature->size != e->sig_size) {
2550
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2551
0
      goto cleanup;
2552
0
    }
2553
2554
0
    if (pk_params->raw_pub.size != e->size) {
2555
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIGN_FAILED);
2556
0
      goto cleanup;
2557
0
    }
2558
2559
0
    ret = eddsa_verify(algo, pk_params->raw_pub.data, vdata->size,
2560
0
           vdata->data, signature->data);
2561
0
    break;
2562
0
  }
2563
0
#if ENABLE_GOST
2564
0
  case GNUTLS_PK_GOST_01:
2565
0
  case GNUTLS_PK_GOST_12_256:
2566
0
  case GNUTLS_PK_GOST_12_512: {
2567
0
    struct ecc_point pub;
2568
0
    struct dsa_signature sig;
2569
0
    const struct ecc_curve *curve;
2570
0
    const mac_entry_st *me;
2571
2572
    /* GOSTDSA is not approved */
2573
0
    not_approved = true;
2574
2575
0
    curve = get_supported_gost_curve(pk_params->curve);
2576
0
    if (curve == NULL) {
2577
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2578
0
      goto cleanup;
2579
0
    }
2580
2581
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
2582
0
           gnutls_ecc_curve_get_name(pk_params->curve),
2583
0
           NULL);
2584
2585
    /* This call will return a valid MAC entry and
2586
     * getters will check that is not null anyway. */
2587
0
    me = hash_to_entry(_gnutls_gost_digest(pk_params->algo));
2588
0
    if (_gnutls_mac_get_algo_len(me) != vdata->size) {
2589
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2590
0
      goto cleanup;
2591
0
    }
2592
2593
0
    ret = _gnutls_decode_gost_rs(signature, &tmp[0], &tmp[1]);
2594
0
    if (ret < 0) {
2595
0
      gnutls_assert();
2596
0
      goto cleanup;
2597
0
    }
2598
2599
0
    ret = _gost_params_to_pubkey(pk_params, &pub, curve);
2600
0
    if (ret < 0) {
2601
0
      gnutls_assert();
2602
0
      goto cleanup;
2603
0
    }
2604
2605
0
    memcpy(sig.r, tmp[0], SIZEOF_MPZT);
2606
0
    memcpy(sig.s, tmp[1], SIZEOF_MPZT);
2607
2608
0
    ret = gostdsa_verify(&pub, vdata->size, vdata->data, &sig);
2609
0
    if (ret == 0) {
2610
0
      gnutls_assert();
2611
0
      ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
2612
0
    } else
2613
0
      ret = 0;
2614
2615
0
    ecc_point_clear(&pub);
2616
0
    break;
2617
0
  }
2618
0
#endif
2619
0
  case GNUTLS_PK_ECDSA: /* ECDSA */
2620
0
  {
2621
0
    struct ecc_point pub;
2622
0
    struct dsa_signature sig;
2623
0
    int curve_id = pk_params->curve;
2624
0
    const struct ecc_curve *curve;
2625
0
    const mac_entry_st *me;
2626
2627
0
    curve = get_supported_nist_curve(curve_id);
2628
0
    if (curve == NULL) {
2629
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
2630
0
      goto cleanup;
2631
0
    }
2632
2633
    /* P-192 is not supported in FIPS 140-3 */
2634
0
    if (curve_id == GNUTLS_ECC_CURVE_SECP192R1) {
2635
0
      not_approved = true;
2636
0
    }
2637
2638
0
    ret = _gnutls_decode_ber_rs(signature, &tmp[0], &tmp[1]);
2639
0
    if (ret < 0) {
2640
0
      gnutls_assert();
2641
0
      goto cleanup;
2642
0
    }
2643
2644
0
    ret = _ecc_params_to_pubkey(pk_params, &pub, curve);
2645
0
    if (ret < 0) {
2646
0
      gnutls_assert();
2647
0
      goto cleanup;
2648
0
    }
2649
2650
0
    memcpy(sig.r, tmp[0], SIZEOF_MPZT);
2651
0
    memcpy(sig.s, tmp[1], SIZEOF_MPZT);
2652
2653
0
    (void)_gnutls_dsa_q_to_hash(pk_params, &hash_len);
2654
2655
0
    if (hash_len > vdata->size)
2656
0
      hash_len = vdata->size;
2657
2658
0
    switch (DIG_TO_MAC(sign_params->dsa_dig)) {
2659
0
    case GNUTLS_MAC_SHA256:
2660
0
    case GNUTLS_MAC_SHA384:
2661
0
    case GNUTLS_MAC_SHA512:
2662
0
    case GNUTLS_MAC_SHA224:
2663
0
      break;
2664
0
    default:
2665
0
      not_approved = true;
2666
0
    }
2667
2668
0
    me = hash_to_entry(sign_params->dsa_dig);
2669
2670
0
    _gnutls_audit_data("pk::curve", CRAU_STRING,
2671
0
           gnutls_ecc_curve_get_name(curve_id),
2672
0
           "pk::hash", CRAU_STRING,
2673
0
           _gnutls_mac_get_name(me), NULL);
2674
2675
0
    ret = ecdsa_verify(&pub, hash_len, vdata->data, &sig);
2676
0
    if (ret == 0) {
2677
0
      gnutls_assert();
2678
0
      ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
2679
0
    } else
2680
0
      ret = 0;
2681
2682
0
    ecc_point_clear(&pub);
2683
0
    break;
2684
0
  }
2685
0
#ifdef ENABLE_DSA
2686
0
  case GNUTLS_PK_DSA: {
2687
0
    struct dsa_params pub;
2688
0
    struct dsa_signature sig;
2689
0
    bigint_t y;
2690
0
    size_t bits;
2691
2692
    /* DSA is currently being defined as sunset with the
2693
     * current draft of FIPS 186-5 */
2694
0
    not_approved = true;
2695
2696
0
    ret = _gnutls_decode_ber_rs(signature, &tmp[0], &tmp[1]);
2697
0
    if (ret < 0) {
2698
0
      gnutls_assert();
2699
0
      goto cleanup;
2700
0
    }
2701
0
    memset(&pub, 0, sizeof(pub));
2702
0
    _dsa_params_get(pk_params, &pub);
2703
0
    y = pk_params->params[DSA_Y];
2704
2705
0
    memcpy(sig.r, tmp[0], SIZEOF_MPZT);
2706
0
    memcpy(sig.s, tmp[1], SIZEOF_MPZT);
2707
2708
0
    _gnutls_dsa_q_to_hash(pk_params, &hash_len);
2709
2710
0
    if (hash_len > vdata->size)
2711
0
      hash_len = vdata->size;
2712
2713
0
    bits = mpz_sizeinbase(pub.p, 2);
2714
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
2715
2716
0
    ret = dsa_verify(&pub, TOMPZ(y), hash_len, vdata->data, &sig);
2717
0
    if (ret == 0) {
2718
0
      gnutls_assert();
2719
0
      ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
2720
0
    } else
2721
0
      ret = 0;
2722
2723
0
    break;
2724
0
  }
2725
0
#endif
2726
0
  case GNUTLS_PK_RSA: {
2727
0
    struct rsa_public_key pub;
2728
0
    size_t bits;
2729
2730
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
2731
0
    if (ret < 0) {
2732
0
      gnutls_assert();
2733
0
      goto cleanup;
2734
0
    }
2735
2736
0
    bits = mpz_sizeinbase(pub.n, 2);
2737
2738
0
    _gnutls_audit_data("pk::bits", CRAU_WORD, bits, NULL);
2739
2740
    /* In FIPS 140-3, RSA key size should be larger than 2048-bit.
2741
     * In addition to this, only SHA-2 is allowed
2742
     * for SigVer; it is checked in _pkcs1_rsa_verify_sig in
2743
     * lib/pubkey.c.
2744
     */
2745
0
    if (unlikely(bits < 2048)) {
2746
0
      not_approved = true;
2747
0
    }
2748
2749
0
    if (signature->size != pub.size) {
2750
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2751
0
      goto cleanup;
2752
0
    }
2753
2754
0
    ret = _gnutls_mpi_init_scan_nz(&tmp[0], signature->data,
2755
0
                 signature->size);
2756
0
    if (ret < 0) {
2757
0
      gnutls_assert();
2758
0
      goto cleanup;
2759
0
    }
2760
2761
0
    ret = rsa_pkcs1_verify(&pub, vdata->size, vdata->data,
2762
0
               TOMPZ(tmp[0]));
2763
0
    if (ret == 0)
2764
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2765
0
    else
2766
0
      ret = 0;
2767
2768
0
    break;
2769
0
  }
2770
0
  case GNUTLS_PK_RSA_PSS: {
2771
0
    struct rsa_public_key pub;
2772
0
    size_t bits;
2773
2774
0
    if ((sign_params->flags &
2775
0
         GNUTLS_PK_FLAG_RSA_PSS_FIXED_SALT_LENGTH) &&
2776
0
        sign_params->salt_size != vdata->size) {
2777
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2778
0
      goto cleanup;
2779
0
    }
2780
2781
0
    ret = _rsa_params_to_pubkey(pk_params, &pub);
2782
0
    if (ret < 0) {
2783
0
      gnutls_assert();
2784
0
      goto cleanup;
2785
0
    }
2786
2787
#ifdef ENABLE_FIPS140
2788
    if (_gnutls_fips_mode_enabled() &&
2789
        _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
2790
      /* gnutls_rnd (called from rnd_nonce_func)
2791
       * will flip FIPS service indicator; use a
2792
       * temprary context
2793
       */
2794
      gnutls_fips140_context_st context =
2795
        GNUTLS_FIPS140_CONTEXT_INIT;
2796
2797
      ret = gnutls_fips140_push_context(&context);
2798
      if (ret < 0)
2799
        goto cleanup;
2800
2801
      not_approved = !rsa_validate_sp800_56b_r2_public(
2802
        &pub, NULL, rnd_nonce_func);
2803
2804
      ret = gnutls_fips140_pop_context();
2805
      if (ret < 0)
2806
        goto cleanup;
2807
    }
2808
#endif
2809
2810
0
    bits = mpz_sizeinbase(pub.n, 2);
2811
2812
    /* RSA modulus size should be 2048-bit or larger in FIPS
2813
     * 140-3.  In addition to this, only SHA-2 are
2814
     * allowed for SigVer, while Nettle only supports
2815
     * SHA256, SHA384, and SHA512 for RSA-PSS (see
2816
     * _rsa_pss_verify_digest in this file for the details).
2817
     */
2818
0
    if (unlikely(bits < 2048)) {
2819
0
      not_approved = true;
2820
0
    }
2821
2822
0
    if (signature->size != pub.size) {
2823
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2824
0
      goto cleanup;
2825
0
    }
2826
2827
0
    ret = _gnutls_mpi_init_scan_nz(&tmp[0], signature->data,
2828
0
                 signature->size);
2829
0
    if (ret < 0) {
2830
0
      gnutls_assert();
2831
0
      goto cleanup;
2832
0
    }
2833
2834
0
    _gnutls_audit_data(
2835
0
      "pk::bits", CRAU_WORD, bits, "pk::hash", CRAU_STRING,
2836
0
      gnutls_digest_get_name(sign_params->rsa_pss_dig), NULL);
2837
2838
0
    ret = _rsa_pss_verify_digest(sign_params->rsa_pss_dig, &pub,
2839
0
               sign_params->salt_size,
2840
0
               vdata->data, vdata->size,
2841
0
               TOMPZ(tmp[0]));
2842
0
    if (ret == 0)
2843
0
      ret = gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED);
2844
0
    else
2845
0
      ret = 0;
2846
2847
0
    break;
2848
0
  }
2849
0
  case GNUTLS_PK_MLDSA44:
2850
0
  case GNUTLS_PK_MLDSA65:
2851
0
  case GNUTLS_PK_MLDSA87:
2852
0
    not_approved = true;
2853
0
    ret = ml_dsa_verify(algo, signature, vdata,
2854
0
            &pk_params->raw_pub);
2855
0
    if (ret < 0)
2856
0
      goto cleanup;
2857
0
    break;
2858
0
  default:
2859
0
    gnutls_assert();
2860
0
    ret = GNUTLS_E_INTERNAL_ERROR;
2861
0
    goto cleanup;
2862
0
  }
2863
2864
0
cleanup:
2865
0
  if (ret < 0) {
2866
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2867
0
  } else if (not_approved) {
2868
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2869
0
  } else {
2870
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
2871
0
  }
2872
2873
0
  _gnutls_mpi_release(&tmp[0]);
2874
0
  _gnutls_mpi_release(&tmp[1]);
2875
2876
0
  gnutls_audit_pop_context();
2877
2878
0
  FAIL_IF_LIB_ERROR;
2879
0
  return ret;
2880
0
}
2881
2882
static inline const struct ecc_curve *get_supported_nist_curve(int curve)
2883
0
{
2884
0
  switch (curve) {
2885
0
#ifdef ENABLE_NON_SUITEB_CURVES
2886
0
  case GNUTLS_ECC_CURVE_SECP192R1:
2887
0
    return nettle_get_secp_192r1();
2888
0
  case GNUTLS_ECC_CURVE_SECP224R1:
2889
0
    return nettle_get_secp_224r1();
2890
0
#endif
2891
0
  case GNUTLS_ECC_CURVE_SECP256R1:
2892
0
    return nettle_get_secp_256r1();
2893
0
  case GNUTLS_ECC_CURVE_SECP384R1:
2894
0
    return nettle_get_secp_384r1();
2895
0
  case GNUTLS_ECC_CURVE_SECP521R1:
2896
0
    return nettle_get_secp_521r1();
2897
0
  default:
2898
0
    return NULL;
2899
0
  }
2900
0
}
2901
2902
static inline const char *get_supported_nist_curve_order(int curve)
2903
0
{
2904
0
  static const struct {
2905
0
    int curve;
2906
0
    const char *order;
2907
0
  } orders[] = {
2908
0
#ifdef ENABLE_NON_SUITEB_CURVES
2909
0
    { GNUTLS_ECC_CURVE_SECP192R1, "ffffffffffffffffffffffff99def836"
2910
0
                "146bc9b1b4d22831" },
2911
0
    { GNUTLS_ECC_CURVE_SECP224R1, "ffffffffffffffffffffffffffff16a2"
2912
0
                "e0b8f03e13dd29455c5c2a3d" },
2913
0
#endif
2914
0
    { GNUTLS_ECC_CURVE_SECP256R1,
2915
0
      "ffffffff00000000ffffffffffffffff"
2916
0
      "bce6faada7179e84f3b9cac2fc632551" },
2917
0
    { GNUTLS_ECC_CURVE_SECP384R1,
2918
0
      "ffffffffffffffffffffffffffffffff"
2919
0
      "ffffffffffffffffc7634d81f4372ddf"
2920
0
      "581a0db248b0a77aecec196accc52973" },
2921
0
    { GNUTLS_ECC_CURVE_SECP521R1, "1fffffffffffffffffffffffffffffff"
2922
0
                "ffffffffffffffffffffffffffffffff"
2923
0
                "ffa51868783bf2f966b7fcc0148f709a"
2924
0
                "5d03bb5c9b8899c47aebb6fb71e91386"
2925
0
                "409" },
2926
0
  };
2927
0
  size_t i;
2928
0
2929
0
  for (i = 0; i < sizeof(orders) / sizeof(orders[0]); i++) {
2930
0
    if (orders[i].curve == curve)
2931
0
      return orders[i].order;
2932
0
  }
2933
0
  return NULL;
2934
0
}
2935
2936
static inline const char *get_supported_nist_curve_modulus(int curve)
2937
0
{
2938
0
  static const struct {
2939
0
    int curve;
2940
0
    const char *order;
2941
0
  } orders[] = {
2942
0
#ifdef ENABLE_NON_SUITEB_CURVES
2943
0
    { GNUTLS_ECC_CURVE_SECP192R1, "fffffffffffffffffffffffffffffffe"
2944
0
                "ffffffffffffffff" },
2945
0
    { GNUTLS_ECC_CURVE_SECP224R1, "ffffffffffffffffffffffffffffffff"
2946
0
                "000000000000000000000001" },
2947
0
#endif
2948
0
    { GNUTLS_ECC_CURVE_SECP256R1,
2949
0
      "ffffffff000000010000000000000000"
2950
0
      "00000000ffffffffffffffffffffffff" },
2951
0
    { GNUTLS_ECC_CURVE_SECP384R1,
2952
0
      "ffffffffffffffffffffffffffffffff"
2953
0
      "fffffffffffffffffffffffffffffffe"
2954
0
      "ffffffff0000000000000000ffffffff" },
2955
0
    { GNUTLS_ECC_CURVE_SECP521R1,
2956
0
      "1ff"
2957
0
      "ffffffffffffffffffffffffffffffff"
2958
0
      "ffffffffffffffffffffffffffffffff"
2959
0
      "ffffffffffffffffffffffffffffffff"
2960
0
      "ffffffffffffffffffffffffffffffff" },
2961
0
  };
2962
0
  size_t i;
2963
0
2964
0
  for (i = 0; i < sizeof(orders) / sizeof(orders[0]); i++) {
2965
0
    if (orders[i].curve == curve)
2966
0
      return orders[i].order;
2967
0
  }
2968
0
  return NULL;
2969
0
}
2970
2971
static inline const struct ecc_curve *get_supported_gost_curve(int curve)
2972
0
{
2973
0
  switch (curve) {
2974
0
#if ENABLE_GOST
2975
0
  case GNUTLS_ECC_CURVE_GOST256CPA:
2976
0
  case GNUTLS_ECC_CURVE_GOST256CPXA:
2977
0
  case GNUTLS_ECC_CURVE_GOST256B:
2978
0
    return nettle_get_gost_gc256b();
2979
0
  case GNUTLS_ECC_CURVE_GOST512A:
2980
0
    return nettle_get_gost_gc512a();
2981
0
#endif
2982
0
  default:
2983
0
    return NULL;
2984
0
  }
2985
0
}
2986
2987
static int _wrap_nettle_pk_curve_exists(gnutls_ecc_curve_t curve)
2988
0
{
2989
0
  switch (curve) {
2990
0
  case GNUTLS_ECC_CURVE_ED25519:
2991
0
  case GNUTLS_ECC_CURVE_X25519:
2992
0
  case GNUTLS_ECC_CURVE_ED448:
2993
0
  case GNUTLS_ECC_CURVE_X448:
2994
0
    return 1;
2995
0
  default:
2996
0
    return ((get_supported_nist_curve(curve) != NULL ||
2997
0
       get_supported_gost_curve(curve) != NULL) ?
2998
0
        1 :
2999
0
        0);
3000
0
  }
3001
0
}
3002
3003
static int _wrap_nettle_pk_exists(gnutls_pk_algorithm_t pk)
3004
0
{
3005
0
  switch (pk) {
3006
0
  case GNUTLS_PK_RSA:
3007
0
#ifdef ENABLE_DSA
3008
0
  case GNUTLS_PK_DSA:
3009
0
#endif
3010
0
  case GNUTLS_PK_DH:
3011
0
  case GNUTLS_PK_ECDSA:
3012
0
  case GNUTLS_PK_ECDH_X25519:
3013
0
  case GNUTLS_PK_RSA_PSS:
3014
0
  case GNUTLS_PK_RSA_OAEP:
3015
0
  case GNUTLS_PK_EDDSA_ED25519:
3016
0
#if ENABLE_GOST
3017
0
  case GNUTLS_PK_GOST_01:
3018
0
  case GNUTLS_PK_GOST_12_256:
3019
0
  case GNUTLS_PK_GOST_12_512:
3020
0
#endif
3021
0
  case GNUTLS_PK_ECDH_X448:
3022
0
  case GNUTLS_PK_EDDSA_ED448:
3023
0
    return 1;
3024
0
  case GNUTLS_PK_MLKEM768:
3025
0
  case GNUTLS_PK_MLKEM1024:
3026
0
    return ml_kem_exists(pk);
3027
0
  case GNUTLS_PK_MLDSA44:
3028
0
  case GNUTLS_PK_MLDSA65:
3029
0
  case GNUTLS_PK_MLDSA87:
3030
0
    return ml_dsa_exists(pk);
3031
0
  default:
3032
0
    return 0;
3033
0
  }
3034
0
}
3035
3036
static int _wrap_nettle_pk_sign_exists(gnutls_sign_algorithm_t sign)
3037
0
{
3038
0
  switch (sign) {
3039
0
  case GNUTLS_SIGN_RSA_SHA1:
3040
0
#ifdef ENABLE_DSA
3041
0
  case GNUTLS_SIGN_DSA_SHA1:
3042
0
#endif
3043
0
  case GNUTLS_SIGN_RSA_MD5:
3044
0
  case GNUTLS_SIGN_RSA_MD2:
3045
0
  case GNUTLS_SIGN_RSA_RMD160:
3046
0
  case GNUTLS_SIGN_RSA_SHA256:
3047
0
  case GNUTLS_SIGN_RSA_SHA384:
3048
0
  case GNUTLS_SIGN_RSA_SHA512:
3049
0
  case GNUTLS_SIGN_RSA_SHA224:
3050
0
#ifdef ENABLE_DSA
3051
0
  case GNUTLS_SIGN_DSA_SHA224:
3052
0
  case GNUTLS_SIGN_DSA_SHA256:
3053
0
#endif
3054
0
  case GNUTLS_SIGN_ECDSA_SHA1:
3055
0
  case GNUTLS_SIGN_ECDSA_SHA224:
3056
0
  case GNUTLS_SIGN_ECDSA_SHA256:
3057
0
  case GNUTLS_SIGN_ECDSA_SHA384:
3058
0
  case GNUTLS_SIGN_ECDSA_SHA512:
3059
0
#ifdef ENABLE_DSA
3060
0
  case GNUTLS_SIGN_DSA_SHA384:
3061
0
  case GNUTLS_SIGN_DSA_SHA512:
3062
0
#endif
3063
0
  case GNUTLS_SIGN_ECDSA_SHA3_224:
3064
0
  case GNUTLS_SIGN_ECDSA_SHA3_256:
3065
0
  case GNUTLS_SIGN_ECDSA_SHA3_384:
3066
0
  case GNUTLS_SIGN_ECDSA_SHA3_512:
3067
3068
0
#ifdef ENABLE_DSA
3069
0
  case GNUTLS_SIGN_DSA_SHA3_224:
3070
0
  case GNUTLS_SIGN_DSA_SHA3_256:
3071
0
  case GNUTLS_SIGN_DSA_SHA3_384:
3072
0
  case GNUTLS_SIGN_DSA_SHA3_512:
3073
0
#endif
3074
0
  case GNUTLS_SIGN_RSA_SHA3_224:
3075
0
  case GNUTLS_SIGN_RSA_SHA3_256:
3076
0
  case GNUTLS_SIGN_RSA_SHA3_384:
3077
0
  case GNUTLS_SIGN_RSA_SHA3_512:
3078
3079
0
  case GNUTLS_SIGN_RSA_PSS_SHA256:
3080
0
  case GNUTLS_SIGN_RSA_PSS_SHA384:
3081
0
  case GNUTLS_SIGN_RSA_PSS_SHA512:
3082
0
  case GNUTLS_SIGN_EDDSA_ED25519:
3083
0
  case GNUTLS_SIGN_RSA_RAW:
3084
3085
0
  case GNUTLS_SIGN_ECDSA_SECP256R1_SHA256:
3086
0
  case GNUTLS_SIGN_ECDSA_SECP384R1_SHA384:
3087
0
  case GNUTLS_SIGN_ECDSA_SECP521R1_SHA512:
3088
3089
0
  case GNUTLS_SIGN_RSA_PSS_RSAE_SHA256:
3090
0
  case GNUTLS_SIGN_RSA_PSS_RSAE_SHA384:
3091
0
  case GNUTLS_SIGN_RSA_PSS_RSAE_SHA512:
3092
3093
0
#if ENABLE_GOST
3094
0
  case GNUTLS_SIGN_GOST_94:
3095
0
  case GNUTLS_SIGN_GOST_256:
3096
0
  case GNUTLS_SIGN_GOST_512:
3097
0
#endif
3098
0
  case GNUTLS_SIGN_EDDSA_ED448:
3099
0
    return 1;
3100
0
  default:
3101
0
    return 0;
3102
0
  }
3103
0
}
3104
3105
/* Generates algorithm's parameters. That is:
3106
 *  For DSA: p, q, and g are generated.
3107
 *  For RSA: nothing
3108
 *  For ECDSA/EDDSA: nothing
3109
 */
3110
static int wrap_nettle_pk_generate_params(gnutls_pk_algorithm_t algo,
3111
            unsigned int level /*bits or curve */,
3112
            gnutls_pk_params_st *params)
3113
0
{
3114
0
  int ret;
3115
0
  unsigned int i, q_bits;
3116
3117
0
  FAIL_IF_LIB_ERROR;
3118
3119
0
  params->algo = algo;
3120
3121
0
  switch (algo) {
3122
0
#ifdef ENABLE_DSA
3123
0
  case GNUTLS_PK_DSA:
3124
0
#endif
3125
0
  case GNUTLS_PK_DH: {
3126
0
    struct dsa_params pub;
3127
0
    struct dss_params_validation_seeds cert;
3128
0
    unsigned index;
3129
3130
0
    dsa_params_init(&pub);
3131
3132
0
    if (GNUTLS_BITS_HAVE_SUBGROUP(level)) {
3133
0
      q_bits = GNUTLS_BITS_TO_SUBGROUP(level);
3134
0
      level = GNUTLS_BITS_TO_GROUP(level);
3135
0
    } else {
3136
0
      q_bits = _gnutls_pk_bits_to_subgroup_bits(level);
3137
0
    }
3138
3139
0
    if (q_bits == 0)
3140
0
      return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
3141
3142
0
    if (_gnutls_fips_mode_enabled() != 0 ||
3143
0
        params->pkflags & GNUTLS_PK_FLAG_PROVABLE) {
3144
0
      if (algo == GNUTLS_PK_DSA)
3145
0
        index = 1;
3146
0
      else
3147
0
        index = 2;
3148
3149
0
      if (params->palgo != 0 &&
3150
0
          params->palgo != GNUTLS_DIG_SHA384) {
3151
0
        ret = GNUTLS_E_INVALID_REQUEST;
3152
0
        goto dsa_fail;
3153
0
      }
3154
3155
0
      params->palgo = GNUTLS_DIG_SHA384;
3156
3157
0
      if (params->seed_size) {
3158
0
        ret = _dsa_generate_dss_pqg(&pub, &cert, index,
3159
0
                  params->seed_size,
3160
0
                  params->seed, NULL,
3161
0
                  NULL, level,
3162
0
                  q_bits);
3163
0
      } else {
3164
0
        ret = dsa_generate_dss_pqg(&pub, &cert, index,
3165
0
                 NULL,
3166
0
                 rnd_tmpkey_func,
3167
0
                 NULL, NULL, level,
3168
0
                 q_bits);
3169
0
      }
3170
0
      if (ret != 1 || HAVE_LIB_ERROR()) {
3171
0
        gnutls_assert();
3172
0
        ret = GNUTLS_E_PK_GENERATION_ERROR;
3173
0
        goto dsa_fail;
3174
0
      }
3175
3176
0
      if (cert.seed_length &&
3177
0
          cert.seed_length < sizeof(params->seed)) {
3178
0
        params->seed_size = cert.seed_length;
3179
0
        memcpy(params->seed, cert.seed,
3180
0
               cert.seed_length);
3181
0
      }
3182
3183
      /* verify the generated parameters */
3184
0
      ret = dsa_validate_dss_pqg(&pub, &cert, index);
3185
0
      if (ret != 1) {
3186
0
        gnutls_assert();
3187
0
        ret = GNUTLS_E_PK_GENERATION_ERROR;
3188
0
        goto dsa_fail;
3189
0
      }
3190
0
    } else {
3191
0
      if (q_bits < 160)
3192
0
        q_bits = 160;
3193
3194
0
      ret = dsa_generate_params(&pub, NULL, rnd_tmpkey_func,
3195
0
              NULL, NULL, level, q_bits);
3196
0
      if (ret != 1 || HAVE_LIB_ERROR()) {
3197
0
        gnutls_assert();
3198
0
        ret = GNUTLS_E_PK_GENERATION_ERROR;
3199
0
        goto dsa_fail;
3200
0
      }
3201
0
    }
3202
3203
0
    params->params_nr = 0;
3204
3205
0
    ret = _gnutls_mpi_init_multi(&params->params[DSA_P],
3206
0
               &params->params[DSA_Q],
3207
0
               &params->params[DSA_G], NULL);
3208
0
    if (ret < 0) {
3209
0
      gnutls_assert();
3210
0
      goto dsa_fail;
3211
0
    }
3212
0
    params->params_nr = 3;
3213
3214
0
    mpz_set(TOMPZ(params->params[DSA_P]), pub.p);
3215
0
    mpz_set(TOMPZ(params->params[DSA_Q]), pub.q);
3216
0
    mpz_set(TOMPZ(params->params[DSA_G]), pub.g);
3217
3218
0
    ret = 0;
3219
3220
0
  dsa_fail:
3221
0
    dsa_params_clear(&pub);
3222
3223
0
    if (ret < 0)
3224
0
      goto fail;
3225
3226
0
    break;
3227
0
  }
3228
0
  case GNUTLS_PK_RSA_PSS:
3229
0
  case GNUTLS_PK_RSA_OAEP:
3230
0
  case GNUTLS_PK_RSA:
3231
0
  case GNUTLS_PK_ECDSA:
3232
0
  case GNUTLS_PK_EDDSA_ED25519:
3233
0
  case GNUTLS_PK_EDDSA_ED448:
3234
0
  case GNUTLS_PK_ECDH_X25519:
3235
0
  case GNUTLS_PK_ECDH_X448:
3236
0
#if ENABLE_GOST
3237
0
  case GNUTLS_PK_GOST_01:
3238
0
  case GNUTLS_PK_GOST_12_256:
3239
0
  case GNUTLS_PK_GOST_12_512:
3240
0
#endif
3241
0
  case GNUTLS_PK_MLKEM768:
3242
0
  case GNUTLS_PK_MLDSA44:
3243
0
  case GNUTLS_PK_MLDSA65:
3244
0
  case GNUTLS_PK_MLDSA87:
3245
0
    break;
3246
0
  default:
3247
0
    gnutls_assert();
3248
0
    return GNUTLS_E_INVALID_REQUEST;
3249
0
  }
3250
3251
0
  FAIL_IF_LIB_ERROR;
3252
0
  return 0;
3253
3254
0
fail:
3255
3256
0
  for (i = 0; i < params->params_nr; i++) {
3257
0
    _gnutls_mpi_release(&params->params[i]);
3258
0
  }
3259
0
  params->params_nr = 0;
3260
3261
0
  FAIL_IF_LIB_ERROR;
3262
0
  return ret;
3263
0
}
3264
3265
#ifdef ENABLE_FIPS140
3266
int _gnutls_dh_generate_key(gnutls_dh_params_t dh_params,
3267
          gnutls_datum_t *priv_key, gnutls_datum_t *pub_key);
3268
3269
int _gnutls_dh_compute_key(gnutls_dh_params_t dh_params,
3270
         const gnutls_datum_t *priv_key,
3271
         const gnutls_datum_t *pub_key,
3272
         const gnutls_datum_t *peer_key, gnutls_datum_t *Z);
3273
3274
int _gnutls_ecdh_compute_key(gnutls_ecc_curve_t curve, const gnutls_datum_t *x,
3275
           const gnutls_datum_t *y, const gnutls_datum_t *k,
3276
           const gnutls_datum_t *peer_x,
3277
           const gnutls_datum_t *peer_y, gnutls_datum_t *Z);
3278
3279
int _gnutls_ecdh_generate_key(gnutls_ecc_curve_t curve, gnutls_datum_t *x,
3280
            gnutls_datum_t *y, gnutls_datum_t *k);
3281
3282
int _gnutls_dh_generate_key(gnutls_dh_params_t dh_params,
3283
          gnutls_datum_t *priv_key, gnutls_datum_t *pub_key)
3284
{
3285
  gnutls_pk_params_st params;
3286
  int ret;
3287
3288
  gnutls_pk_params_init(&params);
3289
  params.params[DH_P] = _gnutls_mpi_copy(dh_params->params[0]);
3290
  params.params[DH_G] = _gnutls_mpi_copy(dh_params->params[1]);
3291
3292
  params.params_nr = 5;
3293
  params.algo = GNUTLS_PK_DH;
3294
3295
  priv_key->data = NULL;
3296
  pub_key->data = NULL;
3297
3298
  ret = _gnutls_pk_generate_keys(GNUTLS_PK_DH, dh_params->q_bits, &params,
3299
               0);
3300
  if (ret < 0) {
3301
    return gnutls_assert_val(ret);
3302
  }
3303
3304
  ret = _gnutls_mpi_dprint_lz(params.params[DH_X], priv_key);
3305
  if (ret < 0) {
3306
    gnutls_assert();
3307
    goto fail;
3308
  }
3309
3310
  ret = _gnutls_mpi_dprint_lz(params.params[DH_Y], pub_key);
3311
  if (ret < 0) {
3312
    gnutls_assert();
3313
    goto fail;
3314
  }
3315
3316
  ret = 0;
3317
  goto cleanup;
3318
fail:
3319
  gnutls_free(pub_key->data);
3320
  gnutls_free(priv_key->data);
3321
cleanup:
3322
  gnutls_pk_params_clear(&params);
3323
  gnutls_pk_params_release(&params);
3324
  return ret;
3325
}
3326
3327
/* Note that the value of Z will have the leading bytes stripped if they are zero -
3328
 * which follows the TLS approach. */
3329
int _gnutls_dh_compute_key(gnutls_dh_params_t dh_params,
3330
         const gnutls_datum_t *priv_key,
3331
         const gnutls_datum_t *pub_key,
3332
         const gnutls_datum_t *peer_key, gnutls_datum_t *Z)
3333
{
3334
  gnutls_pk_params_st pub, priv;
3335
  int ret;
3336
3337
  gnutls_pk_params_init(&pub);
3338
  pub.params_nr = 5;
3339
  pub.algo = GNUTLS_PK_DH;
3340
3341
  gnutls_pk_params_init(&priv);
3342
  priv.params_nr = 5;
3343
  priv.algo = GNUTLS_PK_DH;
3344
3345
  if (_gnutls_mpi_init_scan_nz(&pub.params[DH_Y], peer_key->data,
3346
             peer_key->size) != 0) {
3347
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3348
    goto cleanup;
3349
  }
3350
3351
  priv.params[DH_P] = _gnutls_mpi_copy(dh_params->params[0]);
3352
  priv.params[DH_G] = _gnutls_mpi_copy(dh_params->params[1]);
3353
  if (dh_params->params[2])
3354
    priv.params[DH_Q] = _gnutls_mpi_copy(dh_params->params[2]);
3355
3356
  if (_gnutls_mpi_init_scan_nz(&priv.params[DH_X], priv_key->data,
3357
             priv_key->size) != 0) {
3358
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3359
    goto cleanup;
3360
  }
3361
3362
  Z->data = NULL;
3363
3364
  ret = _gnutls_pk_derive(GNUTLS_PK_DH, Z, &priv, &pub);
3365
  if (ret < 0) {
3366
    gnutls_assert();
3367
    goto cleanup;
3368
  }
3369
3370
  ret = 0;
3371
cleanup:
3372
  gnutls_pk_params_clear(&pub);
3373
  gnutls_pk_params_release(&pub);
3374
  gnutls_pk_params_clear(&priv);
3375
  gnutls_pk_params_release(&priv);
3376
  return ret;
3377
}
3378
3379
int _gnutls_ecdh_generate_key(gnutls_ecc_curve_t curve, gnutls_datum_t *x,
3380
            gnutls_datum_t *y, gnutls_datum_t *k)
3381
{
3382
  gnutls_pk_params_st params;
3383
  int ret;
3384
3385
  gnutls_pk_params_init(&params);
3386
  params.params_nr = 3;
3387
  params.curve = curve;
3388
  params.algo = GNUTLS_PK_ECDSA;
3389
3390
  x->data = NULL;
3391
  y->data = NULL;
3392
  k->data = NULL;
3393
3394
  ret = _gnutls_pk_generate_keys(GNUTLS_PK_ECDSA, curve, &params, 0);
3395
  if (ret < 0) {
3396
    return gnutls_assert_val(ret);
3397
  }
3398
3399
  ret = _gnutls_mpi_dprint_lz(params.params[ECC_X], x);
3400
  if (ret < 0) {
3401
    gnutls_assert();
3402
    goto fail;
3403
  }
3404
3405
  ret = _gnutls_mpi_dprint_lz(params.params[ECC_Y], y);
3406
  if (ret < 0) {
3407
    gnutls_assert();
3408
    goto fail;
3409
  }
3410
3411
  ret = _gnutls_mpi_dprint_lz(params.params[ECC_K], k);
3412
  if (ret < 0) {
3413
    gnutls_assert();
3414
    goto fail;
3415
  }
3416
3417
  ret = 0;
3418
  goto cleanup;
3419
fail:
3420
  gnutls_free(y->data);
3421
  gnutls_free(x->data);
3422
  gnutls_free(k->data);
3423
cleanup:
3424
  gnutls_pk_params_clear(&params);
3425
  gnutls_pk_params_release(&params);
3426
  return ret;
3427
}
3428
3429
int _gnutls_ecdh_compute_key(gnutls_ecc_curve_t curve, const gnutls_datum_t *x,
3430
           const gnutls_datum_t *y, const gnutls_datum_t *k,
3431
           const gnutls_datum_t *peer_x,
3432
           const gnutls_datum_t *peer_y, gnutls_datum_t *Z)
3433
{
3434
  gnutls_pk_params_st pub, priv;
3435
  int ret;
3436
3437
  gnutls_pk_params_init(&pub);
3438
  pub.params_nr = 3;
3439
  pub.algo = GNUTLS_PK_ECDSA;
3440
  pub.curve = curve;
3441
3442
  gnutls_pk_params_init(&priv);
3443
  priv.params_nr = 3;
3444
  priv.algo = GNUTLS_PK_ECDSA;
3445
  priv.curve = curve;
3446
3447
  if (_gnutls_mpi_init_scan_nz(&pub.params[ECC_Y], peer_y->data,
3448
             peer_y->size) != 0) {
3449
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3450
    goto cleanup;
3451
  }
3452
3453
  if (_gnutls_mpi_init_scan_nz(&pub.params[ECC_X], peer_x->data,
3454
             peer_x->size) != 0) {
3455
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3456
    goto cleanup;
3457
  }
3458
3459
  if (_gnutls_mpi_init_scan_nz(&priv.params[ECC_Y], y->data, y->size) !=
3460
      0) {
3461
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3462
    goto cleanup;
3463
  }
3464
3465
  if (_gnutls_mpi_init_scan_nz(&priv.params[ECC_X], x->data, x->size) !=
3466
      0) {
3467
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3468
    goto cleanup;
3469
  }
3470
3471
  if (_gnutls_mpi_init_scan_nz(&priv.params[ECC_K], k->data, k->size) !=
3472
      0) {
3473
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3474
    goto cleanup;
3475
  }
3476
3477
  Z->data = NULL;
3478
3479
  ret = _gnutls_pk_derive(GNUTLS_PK_ECDSA, Z, &priv, &pub);
3480
  if (ret < 0) {
3481
    gnutls_assert();
3482
    goto cleanup;
3483
  }
3484
3485
  ret = 0;
3486
cleanup:
3487
  gnutls_pk_params_clear(&pub);
3488
  gnutls_pk_params_release(&pub);
3489
  gnutls_pk_params_clear(&priv);
3490
  gnutls_pk_params_release(&priv);
3491
  return ret;
3492
}
3493
3494
static gnutls_sign_algorithm_t pct_pk_to_sign(gnutls_pk_algorithm_t algo,
3495
                const gnutls_x509_spki_st *spki)
3496
{
3497
  switch (algo) {
3498
  case GNUTLS_PK_RSA_PSS:
3499
    return gnutls_pk_to_sign(algo, spki->rsa_pss_dig);
3500
#ifdef ENABLE_DSA
3501
  case GNUTLS_PK_DSA:
3502
#endif
3503
  case GNUTLS_PK_ECDSA:
3504
    return gnutls_pk_to_sign(algo, spki->dsa_dig);
3505
  case GNUTLS_PK_EDDSA_ED25519:
3506
    return GNUTLS_SIGN_EDDSA_ED25519;
3507
  case GNUTLS_PK_EDDSA_ED448:
3508
    return GNUTLS_SIGN_EDDSA_ED448;
3509
#if ENABLE_GOST
3510
  case GNUTLS_PK_GOST_01:
3511
    return GNUTLS_SIGN_GOST_94;
3512
  case GNUTLS_PK_GOST_12_256:
3513
    return GNUTLS_SIGN_GOST_256;
3514
  case GNUTLS_PK_GOST_12_512:
3515
    return GNUTLS_SIGN_GOST_512;
3516
#endif
3517
  case GNUTLS_PK_MLDSA44:
3518
    return GNUTLS_SIGN_MLDSA44;
3519
  case GNUTLS_PK_MLDSA65:
3520
    return GNUTLS_SIGN_MLDSA65;
3521
  case GNUTLS_PK_MLDSA87:
3522
    return GNUTLS_SIGN_MLDSA87;
3523
  default:
3524
    return GNUTLS_SIGN_UNKNOWN;
3525
  }
3526
}
3527
3528
static int pct_hash_sign_test(gnutls_pk_algorithm_t algo,
3529
            const gnutls_pk_params_st *params,
3530
            const gnutls_x509_spki_st *spki,
3531
            const gnutls_datum_t *data)
3532
{
3533
  gnutls_privkey_t privkey = NULL;
3534
  gnutls_pubkey_t pubkey = NULL;
3535
  gnutls_x509_privkey_t xprivkey = NULL;
3536
  gnutls_datum_t sig = { NULL, 0 };
3537
  gnutls_sign_algorithm_t sign_algo;
3538
  int ret;
3539
3540
  sign_algo = pct_pk_to_sign(algo, spki);
3541
  if (sign_algo == GNUTLS_SIGN_UNKNOWN)
3542
    return gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3543
3544
  ret = gnutls_x509_privkey_init(&xprivkey);
3545
  if (ret < 0) {
3546
    gnutls_assert();
3547
    goto cleanup;
3548
  }
3549
3550
  ret = _gnutls_pk_params_copy(&xprivkey->params, params);
3551
  if (ret < 0) {
3552
    gnutls_assert();
3553
    goto cleanup;
3554
  }
3555
3556
  ret = gnutls_privkey_init(&privkey);
3557
  if (ret < 0) {
3558
    gnutls_assert();
3559
    goto cleanup;
3560
  }
3561
3562
  ret = gnutls_privkey_import_x509(privkey, xprivkey,
3563
           GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
3564
  if (ret < 0) {
3565
    gnutls_assert();
3566
    goto cleanup;
3567
  }
3568
  xprivkey = NULL;
3569
3570
  ret = gnutls_pubkey_init(&pubkey);
3571
  if (ret < 0) {
3572
    gnutls_assert();
3573
    goto cleanup;
3574
  }
3575
3576
  ret = gnutls_pubkey_import_privkey(pubkey, privkey, 0, 0);
3577
  if (ret < 0) {
3578
    gnutls_assert();
3579
    goto cleanup;
3580
  }
3581
3582
  ret = gnutls_privkey_sign_data2(privkey, sign_algo, 0, data, &sig);
3583
  if (ret < 0) {
3584
    gnutls_assert();
3585
    goto cleanup;
3586
  }
3587
3588
  /* Ignore algorithm disablement through configuration during PCT.  */
3589
  ret = gnutls_pubkey_verify_data2(
3590
    pubkey, sign_algo, GNUTLS_VERIFY_ALLOW_BROKEN, data, &sig);
3591
  if (ret < 0) {
3592
    gnutls_assert();
3593
    goto cleanup;
3594
  }
3595
3596
cleanup:
3597
  if (ret < 0) {
3598
    _gnutls_debug_log("PCT: %s hash+sign self-test failed: %s\n",
3599
          gnutls_sign_get_name(sign_algo),
3600
          gnutls_strerror(ret));
3601
  } else {
3602
    _gnutls_debug_log("PCT: %s hash+sign self-test succeeded\n",
3603
          gnutls_sign_get_name(sign_algo));
3604
  }
3605
3606
  gnutls_x509_privkey_deinit(xprivkey);
3607
  gnutls_privkey_deinit(privkey);
3608
  gnutls_pubkey_deinit(pubkey);
3609
  _gnutls_free_datum(&sig);
3610
3611
  return ret;
3612
}
3613
3614
static int pct_test(gnutls_pk_algorithm_t algo,
3615
        const gnutls_pk_params_st *params)
3616
{
3617
  int ret;
3618
  gnutls_datum_t sig = { NULL, 0 };
3619
  const char const_data[20] = "onetwothreefourfive";
3620
  const char const_data_sha256[32] = "onetwothreefourfivesixseveneigh";
3621
  const char const_data_sha384[48] =
3622
    "onetwothreefourfivesixseveneightnineteneleventw";
3623
  const char const_data_sha512[64] =
3624
    "onetwothreefourfivesixseveneightnineteneleventwelvethirteenfour";
3625
  gnutls_datum_t ddata, tmp = { NULL, 0 };
3626
  char gen_data[MAX_HASH_SIZE];
3627
  gnutls_x509_spki_st spki;
3628
3629
  ret = _gnutls_x509_spki_copy(&spki, &params->spki);
3630
  if (ret < 0) {
3631
    gnutls_assert();
3632
    goto cleanup;
3633
  }
3634
3635
  if (algo == GNUTLS_PK_DSA || algo == GNUTLS_PK_EC) {
3636
    unsigned hash_len;
3637
    const mac_entry_st *me;
3638
3639
    me = _gnutls_dsa_q_to_hash(params, &hash_len);
3640
    spki.dsa_dig = MAC_TO_DIG(me->id);
3641
    gnutls_rnd(GNUTLS_RND_NONCE, gen_data, hash_len);
3642
3643
    ddata.data = (void *)gen_data;
3644
    ddata.size = hash_len;
3645
  } else if (algo == GNUTLS_PK_GOST_01 || algo == GNUTLS_PK_GOST_12_256) {
3646
    ddata.data = (void *)const_data_sha256;
3647
    ddata.size = sizeof(const_data_sha256);
3648
  } else if (algo == GNUTLS_PK_GOST_12_512) {
3649
    ddata.data = (void *)const_data_sha512;
3650
    ddata.size = sizeof(const_data_sha512);
3651
  } else if (algo == GNUTLS_PK_RSA_PSS) {
3652
    if (spki.rsa_pss_dig == GNUTLS_DIG_UNKNOWN)
3653
      spki.rsa_pss_dig = GNUTLS_DIG_SHA256;
3654
3655
    switch (spki.rsa_pss_dig) {
3656
    case GNUTLS_DIG_SHA256:
3657
      ddata.data = (void *)const_data_sha256;
3658
      ddata.size = sizeof(const_data_sha256);
3659
      break;
3660
    case GNUTLS_DIG_SHA384:
3661
      ddata.data = (void *)const_data_sha384;
3662
      ddata.size = sizeof(const_data_sha384);
3663
      break;
3664
    case GNUTLS_DIG_SHA512:
3665
      ddata.data = (void *)const_data_sha512;
3666
      ddata.size = sizeof(const_data_sha512);
3667
      break;
3668
    default:
3669
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3670
      goto cleanup;
3671
    }
3672
  } else if (algo == GNUTLS_PK_RSA_OAEP) {
3673
    if (spki.rsa_oaep_dig == GNUTLS_DIG_UNKNOWN)
3674
      spki.rsa_oaep_dig = GNUTLS_DIG_SHA256;
3675
    ddata.data = (void *)const_data;
3676
    ddata.size = sizeof(const_data);
3677
  } else {
3678
    ddata.data = (void *)const_data;
3679
    ddata.size = sizeof(const_data);
3680
  }
3681
3682
  switch (algo) {
3683
  case GNUTLS_PK_RSA:
3684
    /* To comply with FIPS 140-3 IG 10.3.A, additional comment 1,
3685
     * Perform both key transport and signature PCTs for
3686
     * unrestricted RSA key.  */
3687
    ret = pct_test(GNUTLS_PK_RSA_OAEP, params);
3688
    if (ret < 0) {
3689
      gnutls_assert();
3690
      break;
3691
    }
3692
    ret = pct_test(GNUTLS_PK_RSA_PSS, params);
3693
    if (ret < 0) {
3694
      gnutls_assert();
3695
      break;
3696
    }
3697
    break;
3698
  case GNUTLS_PK_RSA_OAEP:
3699
    ret = _gnutls_pk_encrypt(GNUTLS_PK_RSA_OAEP, &sig, &ddata,
3700
           params, &spki);
3701
    if (ret < 0) {
3702
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3703
    }
3704
    if (ret == 0 && ddata.size == sig.size &&
3705
        memeq(ddata.data, sig.data, sig.size)) {
3706
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3707
    }
3708
    if (ret == 0 &&
3709
        _gnutls_pk_decrypt(algo, &tmp, &sig, params, &spki) < 0) {
3710
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3711
    }
3712
    if (ret == 0 && !(tmp.size == ddata.size &&
3713
          memeq(tmp.data, ddata.data, tmp.size))) {
3714
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3715
    }
3716
    if (ret == 0 &&
3717
        _gnutls_pk_decrypt2(algo, &sig, tmp.data, tmp.size, params,
3718
          &spki) < 0) {
3719
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3720
    }
3721
    if (ret == 0 && !(tmp.size == ddata.size &&
3722
          memeq(tmp.data, ddata.data, tmp.size))) {
3723
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3724
    }
3725
3726
    if (ret < 0) {
3727
      goto cleanup;
3728
    }
3729
3730
    free(sig.data);
3731
    sig.data = NULL;
3732
3733
    break;
3734
  case GNUTLS_PK_EC: /* we only do keys for ECDSA */
3735
  case GNUTLS_PK_EDDSA_ED25519:
3736
  case GNUTLS_PK_EDDSA_ED448:
3737
#ifdef ENABLE_DSA
3738
  case GNUTLS_PK_DSA:
3739
#endif
3740
  case GNUTLS_PK_RSA_PSS:
3741
  case GNUTLS_PK_GOST_01:
3742
  case GNUTLS_PK_GOST_12_256:
3743
  case GNUTLS_PK_GOST_12_512:
3744
  case GNUTLS_PK_MLDSA44:
3745
  case GNUTLS_PK_MLDSA65:
3746
  case GNUTLS_PK_MLDSA87:
3747
    ret = _gnutls_pk_sign(algo, &sig, &ddata, params, &spki);
3748
    if (ret < 0) {
3749
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3750
      goto cleanup;
3751
    }
3752
3753
    ret = _gnutls_pk_verify(algo, &ddata, &sig, params, &spki);
3754
    if (ret < 0) {
3755
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3756
      goto cleanup;
3757
    }
3758
3759
    /* Exercise the combined hash+sign operation, using
3760
     * the abstract key interface.
3761
     *
3762
     * FIXME: rework this once the crypto-backend
3763
     * interface natively supports hash+sign operation, see:
3764
     * https://gitlab.com/gnutls/gnutls/-/merge_requests/2066
3765
     */
3766
    ret = pct_hash_sign_test(algo, params, &spki, &ddata);
3767
    if (ret < 0) {
3768
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3769
      goto cleanup;
3770
    }
3771
    break;
3772
  case GNUTLS_PK_DH: {
3773
    mpz_t y;
3774
3775
    /* Perform SP800 56A (rev 3) 5.6.2.1.4 Owner Assurance
3776
     * of Pair-wise Consistency check, even if we only
3777
     * support ephemeral DH, as it is required by FIPS
3778
     * 140-3 IG 10.3.A.
3779
     *
3780
     * Use the private key, x, along with the generator g
3781
     * and prime modulus p included in the domain
3782
     * parameters associated with the key pair to compute
3783
     * g^x mod p. Compare the result to the public key, y.
3784
     */
3785
    mpz_init(y);
3786
    mpz_powm(y, TOMPZ(params->params[DSA_G]),
3787
       TOMPZ(params->params[DSA_X]),
3788
       TOMPZ(params->params[DSA_P]));
3789
    if (unlikely(mpz_cmp(y, TOMPZ(params->params[DSA_Y])) != 0)) {
3790
      ret = gnutls_assert_val(GNUTLS_E_PK_GENERATION_ERROR);
3791
      mpz_clear(y);
3792
      goto cleanup;
3793
    }
3794
    mpz_clear(y);
3795
    break;
3796
  }
3797
  case GNUTLS_PK_ECDH_X25519:
3798
  case GNUTLS_PK_ECDH_X448:
3799
    break;
3800
  case GNUTLS_PK_MLKEM768:
3801
  case GNUTLS_PK_MLKEM1024:
3802
    if (!ml_kem_exists(algo)) {
3803
      ret = gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
3804
      goto cleanup;
3805
    }
3806
    break;
3807
  default:
3808
    ret = gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
3809
    goto cleanup;
3810
  }
3811
3812
  ret = 0;
3813
cleanup:
3814
  if (ret == GNUTLS_E_PK_GENERATION_ERROR) {
3815
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
3816
  }
3817
  _gnutls_x509_spki_clear(&spki);
3818
  gnutls_free(sig.data);
3819
  gnutls_free(tmp.data);
3820
  return ret;
3821
}
3822
#endif
3823
3824
static inline int eddsa_public_key(gnutls_pk_algorithm_t algo, uint8_t *pub,
3825
           const uint8_t *priv)
3826
0
{
3827
0
  switch (algo) {
3828
0
  case GNUTLS_PK_EDDSA_ED25519:
3829
0
    ed25519_sha512_public_key(pub, priv);
3830
0
    return 0;
3831
0
  case GNUTLS_PK_EDDSA_ED448:
3832
0
    ed448_shake256_public_key(pub, priv);
3833
0
    return 0;
3834
0
  default:
3835
0
    return gnutls_assert_val(
3836
0
      GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
3837
0
  }
3838
0
}
3839
3840
static inline int edwards_curve_mul_g(gnutls_pk_algorithm_t algo, uint8_t *q,
3841
              const uint8_t *n)
3842
0
{
3843
0
  switch (algo) {
3844
0
  case GNUTLS_PK_ECDH_X25519:
3845
0
    curve25519_mul_g(q, n);
3846
0
    return 0;
3847
0
  case GNUTLS_PK_ECDH_X448:
3848
0
    curve448_mul_g(q, n);
3849
0
    return 0;
3850
0
  default:
3851
0
    return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
3852
0
  }
3853
0
}
3854
3855
static inline int dh_find_q(const gnutls_pk_params_st *pk_params, mpz_t q)
3856
0
{
3857
0
  gnutls_datum_t prime = { NULL, 0 };
3858
0
  gnutls_datum_t generator = { NULL, 0 };
3859
0
  uint8_t *data_q;
3860
0
  size_t n_q;
3861
0
  bigint_t _q;
3862
0
  int ret = 0;
3863
0
3864
0
  ret = _gnutls_mpi_dprint(pk_params->params[DSA_P], &prime);
3865
0
  if (ret < 0) {
3866
0
    gnutls_assert();
3867
0
    goto cleanup;
3868
0
  }
3869
0
3870
0
  ret = _gnutls_mpi_dprint(pk_params->params[DSA_G], &generator);
3871
0
  if (ret < 0) {
3872
0
    gnutls_assert();
3873
0
    goto cleanup;
3874
0
  }
3875
0
3876
0
  if (!_gnutls_dh_prime_match_fips_approved(
3877
0
        prime.data, prime.size, generator.data, generator.size,
3878
0
        &data_q, &n_q)) {
3879
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3880
0
    goto cleanup;
3881
0
  }
3882
0
3883
0
  if (_gnutls_mpi_init_scan_nz(&_q, data_q, n_q) != 0) {
3884
0
    ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
3885
0
    goto cleanup;
3886
0
  }
3887
0
3888
0
  mpz_set(q, TOMPZ(_q));
3889
0
  _gnutls_mpi_release(&_q);
3890
0
3891
0
cleanup:
3892
0
  gnutls_free(prime.data);
3893
0
  gnutls_free(generator.data);
3894
0
3895
0
  return ret;
3896
0
}
3897
3898
/* To generate a DH key either q must be set in the params or
3899
 * level should be set to the number of required bits.
3900
 */
3901
static int
3902
wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo,
3903
           unsigned int level /*bits or curve */,
3904
           gnutls_pk_params_st *params, unsigned ephemeral
3905
           /*non-zero if they are ephemeral keys */)
3906
0
{
3907
0
  int ret;
3908
0
  unsigned int i;
3909
0
  unsigned rnd_level;
3910
0
  nettle_random_func *rnd_func;
3911
0
  bool not_approved = false;
3912
3913
0
  FAIL_IF_LIB_ERROR;
3914
3915
  /* check if the curve relates to the algorithm used */
3916
0
  if (IS_EC(algo) && gnutls_ecc_curve_get_pk(level) != algo) {
3917
0
    ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
3918
0
    goto cleanup;
3919
0
  }
3920
3921
0
  if (ephemeral) {
3922
0
    rnd_level = GNUTLS_RND_RANDOM;
3923
0
    rnd_func = rnd_tmpkey_func;
3924
0
  } else {
3925
0
    rnd_func = rnd_key_func;
3926
0
    rnd_level = GNUTLS_RND_KEY;
3927
0
  }
3928
3929
0
  _gnutls_audit_new_context_with_data("name", CRAU_STRING, "pk::generate",
3930
0
              "pk::algorithm", CRAU_STRING,
3931
0
              gnutls_pk_get_name(algo), NULL);
3932
3933
0
  switch (algo) {
3934
0
#ifdef ENABLE_DSA
3935
0
  case GNUTLS_PK_DSA:
3936
#ifdef ENABLE_FIPS140
3937
    if (_gnutls_fips_mode_enabled() != 0) {
3938
      struct dsa_params pub;
3939
      mpz_t x, y;
3940
3941
      /* DSA is currently being defined as sunset with the
3942
       * current draft of FIPS 186-5 */
3943
      not_approved = true;
3944
3945
      if (params->params[DSA_Q] == NULL) {
3946
        ret = gnutls_assert_val(
3947
          GNUTLS_E_INVALID_REQUEST);
3948
        goto cleanup;
3949
      }
3950
3951
      _dsa_params_get(params, &pub);
3952
3953
      mpz_init(x);
3954
      mpz_init(y);
3955
3956
      ret = dsa_generate_dss_keypair(&pub, y, x, NULL,
3957
                   rnd_func, NULL, NULL);
3958
      if (ret != 1 || HAVE_LIB_ERROR()) {
3959
        gnutls_assert();
3960
        ret = GNUTLS_E_PK_GENERATION_ERROR;
3961
        goto dsa_fail;
3962
      }
3963
3964
      ret = _gnutls_mpi_init_multi(&params->params[DSA_Y],
3965
                 &params->params[DSA_X],
3966
                 NULL);
3967
      if (ret < 0) {
3968
        gnutls_assert();
3969
        goto dsa_fail;
3970
      }
3971
3972
      mpz_set(TOMPZ(params->params[DSA_Y]), y);
3973
      mpz_set(TOMPZ(params->params[DSA_X]), x);
3974
      params->params_nr += 2;
3975
3976
    dsa_fail:
3977
      mpz_clear(x);
3978
      mpz_clear(y);
3979
3980
      if (ret < 0)
3981
        goto cleanup;
3982
3983
      break;
3984
    }
3985
#endif
3986
0
    FALLTHROUGH;
3987
0
#endif
3988
0
  case GNUTLS_PK_DH: {
3989
0
    struct dsa_params pub;
3990
0
    mpz_t r;
3991
0
    mpz_t x, y;
3992
0
    int max_tries;
3993
0
    unsigned have_q = 0;
3994
0
    mpz_t q;
3995
0
    mpz_t primesub1;
3996
0
    mpz_t ypowq;
3997
3998
0
    if (algo != params->algo) {
3999
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4000
0
      goto cleanup;
4001
0
    }
4002
4003
0
    _dsa_params_get(params, &pub);
4004
4005
0
    if (params->params[DSA_Q] != NULL)
4006
0
      have_q = 1;
4007
4008
    /* This check is for the case !ENABLE_FIPS140 */
4009
0
    if (algo == GNUTLS_PK_DSA && have_q == 0) {
4010
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4011
0
      goto cleanup;
4012
0
    }
4013
4014
0
    mpz_init(r);
4015
0
    mpz_init(x);
4016
0
    mpz_init(y);
4017
4018
0
    mpz_init(q);
4019
0
    mpz_init(primesub1);
4020
0
    mpz_init(ypowq);
4021
4022
0
    max_tries = 3;
4023
0
    do {
4024
0
      if (have_q) {
4025
0
        mpz_set(r, pub.q);
4026
0
        mpz_sub_ui(r, r, 2);
4027
0
        nettle_mpz_random(x, NULL, rnd_func, r);
4028
0
        mpz_add_ui(x, x, 1);
4029
0
      } else {
4030
0
        unsigned size = mpz_sizeinbase(pub.p, 2);
4031
0
        if (level == 0)
4032
0
          level = MIN(size,
4033
0
                DH_EXPONENT_SIZE(size));
4034
0
        nettle_mpz_random_size(x, NULL, rnd_func,
4035
0
                   level);
4036
4037
0
        if (level >= size)
4038
0
          mpz_mod(x, x, pub.p);
4039
0
      }
4040
4041
0
      mpz_powm(y, pub.g, x, pub.p);
4042
4043
0
      max_tries--;
4044
0
      if (max_tries <= 0) {
4045
0
        gnutls_assert();
4046
0
        ret = GNUTLS_E_RANDOM_FAILED;
4047
0
        goto dh_fail;
4048
0
      }
4049
4050
0
      if (HAVE_LIB_ERROR()) {
4051
0
        gnutls_assert();
4052
0
        ret = GNUTLS_E_LIB_IN_ERROR_STATE;
4053
0
        goto dh_fail;
4054
0
      }
4055
4056
0
    } while (mpz_cmp_ui(y, 1) == 0);
4057
4058
#ifdef ENABLE_FIPS140
4059
    if (_gnutls_fips_mode_enabled()) {
4060
      /* Perform FFC full public key validation checks
4061
       * according to SP800-56A (revision 3), 5.6.2.3.1.
4062
       */
4063
4064
      /* Step 1: 2 <= y <= p - 2 */
4065
      mpz_sub_ui(primesub1, pub.p, 1);
4066
4067
      if (mpz_cmp_ui(y, 2) < 0 ||
4068
          mpz_cmp(y, primesub1) >= 0) {
4069
        ret = gnutls_assert_val(GNUTLS_E_RANDOM_FAILED);
4070
        goto dh_fail;
4071
      }
4072
4073
      /* Step 2: 1 = y^q mod p */
4074
      if (have_q)
4075
        mpz_set(q, pub.q);
4076
      else {
4077
        ret = dh_find_q(params, q);
4078
        if (ret < 0)
4079
          goto dh_fail;
4080
      }
4081
4082
      mpz_powm(ypowq, y, q, pub.p);
4083
      if (mpz_cmp_ui(ypowq, 1) != 0) {
4084
        ret = gnutls_assert_val(GNUTLS_E_RANDOM_FAILED);
4085
        goto dh_fail;
4086
      }
4087
    }
4088
#endif
4089
4090
0
    ret = _gnutls_mpi_init_multi(&params->params[DSA_Y],
4091
0
               &params->params[DSA_X], NULL);
4092
0
    if (ret < 0) {
4093
0
      gnutls_assert();
4094
0
      goto dh_fail;
4095
0
    }
4096
4097
0
    mpz_set(TOMPZ(params->params[DSA_Y]), y);
4098
0
    mpz_set(TOMPZ(params->params[DSA_X]), x);
4099
0
    params->params_nr += 2;
4100
4101
0
    ret = 0;
4102
4103
0
  dh_fail:
4104
0
    mpz_clear(r);
4105
0
    mpz_clear(x);
4106
0
    mpz_clear(y);
4107
0
    mpz_clear(q);
4108
0
    mpz_clear(primesub1);
4109
0
    mpz_clear(ypowq);
4110
4111
0
    if (ret < 0)
4112
0
      goto cleanup;
4113
4114
0
    break;
4115
0
  }
4116
0
  case GNUTLS_PK_RSA_PSS:
4117
0
  case GNUTLS_PK_RSA_OAEP:
4118
0
  case GNUTLS_PK_RSA: {
4119
0
    struct rsa_public_key pub;
4120
0
    struct rsa_private_key priv;
4121
4122
0
    rsa_public_key_init(&pub);
4123
0
    rsa_private_key_init(&priv);
4124
4125
0
    mpz_set_ui(pub.e, 65537);
4126
4127
0
    if ((params->pkflags & GNUTLS_PK_FLAG_PROVABLE) ||
4128
0
        _gnutls_fips_mode_enabled() != 0) {
4129
0
      params->pkflags |= GNUTLS_PK_FLAG_PROVABLE;
4130
0
      if (params->palgo != 0 &&
4131
0
          params->palgo != GNUTLS_DIG_SHA384) {
4132
0
        ret = GNUTLS_E_INVALID_REQUEST;
4133
0
        goto rsa_fail;
4134
0
      }
4135
4136
0
      params->palgo = GNUTLS_DIG_SHA384;
4137
4138
0
      if (params->seed_size) {
4139
0
        ret = _rsa_generate_fips186_4_keypair(
4140
0
          &pub, &priv, params->seed_size,
4141
0
          params->seed, NULL, NULL, level);
4142
0
      } else {
4143
0
        unsigned retries = 0;
4144
        /* The provable RSA key generation process is deterministic
4145
         * but has an internal maximum iteration counter and when
4146
         * exceed will fail for certain random seeds. This is a very
4147
         * rare condition, but it nevertheless happens and even CI builds fail
4148
         * occasionally. When we generate the random seed internally, remediate
4149
         * by retrying a different seed on failure. */
4150
0
        do {
4151
0
          params->seed_size =
4152
0
            sizeof(params->seed);
4153
0
          ret = rsa_generate_fips186_4_keypair(
4154
0
            &pub, &priv, NULL, rnd_func,
4155
0
            NULL, NULL, &params->seed_size,
4156
0
            params->seed, level);
4157
0
        } while (ret != 1 && ++retries < 3);
4158
0
      }
4159
0
    } else {
4160
0
      not_approved = true;
4161
4162
0
      ret = rsa_generate_keypair(&pub, &priv, NULL, rnd_func,
4163
0
               NULL, NULL, level, 0);
4164
0
    }
4165
0
    if (ret != 1 || HAVE_LIB_ERROR()) {
4166
0
      gnutls_assert();
4167
0
      ret = GNUTLS_E_PK_GENERATION_ERROR;
4168
0
      goto rsa_fail;
4169
0
    }
4170
4171
0
    params->params_nr = 0;
4172
0
    for (i = 0; i < RSA_PRIVATE_PARAMS; i++) {
4173
0
      ret = _gnutls_mpi_init(&params->params[i]);
4174
0
      if (ret < 0) {
4175
0
        gnutls_assert();
4176
0
        goto rsa_fail;
4177
0
      }
4178
0
      params->params_nr++;
4179
0
    }
4180
4181
    /* In FIPS 140-3, pub.n should be 2048-bit or larger; it
4182
     * is assured in rsa_generate_fips186_4_keypair in
4183
     * lib/nettle/int/rsa-keygen-fips186.c. */
4184
4185
0
    mpz_set(TOMPZ(params->params[RSA_MODULUS]), pub.n);
4186
0
    mpz_set(TOMPZ(params->params[RSA_PUB]), pub.e);
4187
0
    mpz_set(TOMPZ(params->params[RSA_PRIV]), priv.d);
4188
0
    mpz_set(TOMPZ(params->params[RSA_PRIME1]), priv.p);
4189
0
    mpz_set(TOMPZ(params->params[RSA_PRIME2]), priv.q);
4190
0
    mpz_set(TOMPZ(params->params[RSA_COEF]), priv.c);
4191
0
    mpz_set(TOMPZ(params->params[RSA_E1]), priv.a);
4192
0
    mpz_set(TOMPZ(params->params[RSA_E2]), priv.b);
4193
4194
0
    ret = 0;
4195
4196
0
  rsa_fail:
4197
0
    rsa_private_key_clear(&priv);
4198
0
    rsa_public_key_clear(&pub);
4199
4200
0
    if (ret < 0)
4201
0
      goto cleanup;
4202
4203
0
    break;
4204
0
  }
4205
0
  case GNUTLS_PK_EDDSA_ED25519:
4206
0
  case GNUTLS_PK_EDDSA_ED448: {
4207
0
    unsigned size = gnutls_ecc_curve_get_size(level);
4208
4209
0
    if (params->pkflags & GNUTLS_PK_FLAG_PROVABLE) {
4210
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4211
0
      goto cleanup;
4212
0
    }
4213
4214
0
    if (unlikely(get_eddsa_curve(algo) != level)) {
4215
0
      ret = gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4216
0
      goto cleanup;
4217
0
    }
4218
4219
0
    if (size == 0) {
4220
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4221
0
      goto cleanup;
4222
0
    }
4223
4224
0
    params->curve = level;
4225
4226
0
    params->raw_priv.data = gnutls_malloc(size);
4227
0
    if (params->raw_priv.data == NULL) {
4228
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4229
0
      goto cleanup;
4230
0
    }
4231
4232
0
    params->raw_pub.data = gnutls_malloc(size);
4233
0
    if (params->raw_pub.data == NULL) {
4234
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4235
0
      goto cleanup;
4236
0
    }
4237
4238
0
    ret = gnutls_rnd(rnd_level, params->raw_priv.data, size);
4239
0
    if (ret < 0) {
4240
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4241
0
      goto cleanup;
4242
0
    }
4243
0
    params->raw_pub.size = size;
4244
0
    params->raw_priv.size = size;
4245
4246
0
    ret = eddsa_public_key(algo, params->raw_pub.data,
4247
0
               params->raw_priv.data);
4248
0
    if (ret < 0)
4249
0
      goto cleanup;
4250
4251
0
    break;
4252
0
  }
4253
0
  case GNUTLS_PK_ECDSA:
4254
0
    if (params->pkflags & GNUTLS_PK_FLAG_PROVABLE)
4255
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4256
4257
0
    {
4258
0
      struct ecc_scalar key;
4259
0
      struct ecc_point pub;
4260
0
      const struct ecc_curve *curve;
4261
0
      struct ecc_scalar n;
4262
0
      struct ecc_scalar m;
4263
0
      struct ecc_point r;
4264
0
      mpz_t x, y, xx, yy, nn, mm;
4265
4266
0
      curve = get_supported_nist_curve(level);
4267
0
      if (curve == NULL) {
4268
0
        ret = gnutls_assert_val(
4269
0
          GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4270
0
        goto cleanup;
4271
0
      }
4272
4273
0
      _gnutls_audit_data("pk::curve", CRAU_STRING,
4274
0
             gnutls_ecc_curve_get_name(level),
4275
0
             NULL);
4276
4277
      /* P-192 is not supported in FIPS 140-3 */
4278
0
      if (level == GNUTLS_ECC_CURVE_SECP192R1) {
4279
0
        not_approved = true;
4280
0
      }
4281
4282
0
      mpz_init(x);
4283
0
      mpz_init(y);
4284
0
      mpz_init(xx);
4285
0
      mpz_init(yy);
4286
0
      mpz_init(nn);
4287
0
      mpz_init(mm);
4288
4289
0
      ecc_scalar_init(&key, curve);
4290
0
      ecc_point_init(&pub, curve);
4291
0
      ecc_scalar_init(&n, curve);
4292
0
      ecc_scalar_init(&m, curve);
4293
0
      ecc_point_init(&r, curve);
4294
4295
0
      ecdsa_generate_keypair(&pub, &key, NULL, rnd_func);
4296
0
      if (HAVE_LIB_ERROR()) {
4297
0
        ret = gnutls_assert_val(
4298
0
          GNUTLS_E_LIB_IN_ERROR_STATE);
4299
0
        goto ecc_fail;
4300
0
      }
4301
4302
0
      ret = _gnutls_mpi_init_multi(&params->params[ECC_X],
4303
0
                 &params->params[ECC_Y],
4304
0
                 &params->params[ECC_K],
4305
0
                 NULL);
4306
0
      if (ret < 0) {
4307
0
        gnutls_assert();
4308
0
        goto ecc_fail;
4309
0
      }
4310
4311
0
      params->curve = level;
4312
0
      params->params_nr = ECC_PRIVATE_PARAMS;
4313
4314
0
      ecc_point_get(&pub, x, y);
4315
4316
#ifdef ENABLE_FIPS140
4317
      if (_gnutls_fips_mode_enabled()) {
4318
        /* Perform ECC full public key validation checks
4319
         * according to SP800-56A (revision 3), 5.6.2.3.3.
4320
         */
4321
4322
        const char *order, *modulus;
4323
4324
        /* Step 1: verify that Q is not an identity
4325
         * element (an infinity point).  Note that this
4326
         * cannot happen in the nettle implementation,
4327
         * because it cannot represent an infinity point
4328
         * on curves. */
4329
        if (mpz_cmp_ui(x, 0) == 0 &&
4330
            mpz_cmp_ui(y, 0) == 0) {
4331
          ret = gnutls_assert_val(
4332
            GNUTLS_E_ILLEGAL_PARAMETER);
4333
          goto ecc_fail;
4334
        }
4335
4336
        /* Step 2: verify that both coordinates of Q are
4337
         * in the range [0, p - 1].
4338
         *
4339
         * Step 3: verify that Q lie on the curve
4340
         *
4341
         * Both checks are performed in nettle.  */
4342
        if (!ecc_point_set(&r, x, y)) {
4343
          ret = gnutls_assert_val(
4344
            GNUTLS_E_ILLEGAL_PARAMETER);
4345
          goto ecc_fail;
4346
        }
4347
4348
        /* Step 4: verify that n * Q, where n is the
4349
         * curve order, result in an identity element
4350
         *
4351
         * Since nettle internally cannot represent an
4352
         * identity element on curves, we validate this
4353
         * instead:
4354
         *
4355
         *   (n - 1) * Q = -Q
4356
         *
4357
         * That effectively means: n * Q = -Q + Q = O
4358
         */
4359
        order = get_supported_nist_curve_order(level);
4360
        if (unlikely(order == NULL)) {
4361
          ret = gnutls_assert_val(
4362
            GNUTLS_E_INTERNAL_ERROR);
4363
          goto ecc_fail;
4364
        }
4365
4366
        ret = mpz_set_str(nn, order, 16);
4367
        if (unlikely(ret < 0)) {
4368
          ret = gnutls_assert_val(
4369
            GNUTLS_E_MPI_SCAN_FAILED);
4370
          goto ecc_fail;
4371
        }
4372
4373
        modulus =
4374
          get_supported_nist_curve_modulus(level);
4375
        if (unlikely(modulus == NULL)) {
4376
          ret = gnutls_assert_val(
4377
            GNUTLS_E_INTERNAL_ERROR);
4378
          goto ecc_fail;
4379
        }
4380
4381
        ret = mpz_set_str(mm, modulus, 16);
4382
        if (unlikely(ret < 0)) {
4383
          ret = gnutls_assert_val(
4384
            GNUTLS_E_MPI_SCAN_FAILED);
4385
          goto ecc_fail;
4386
        }
4387
4388
        /* (n - 1) * Q = -Q */
4389
        mpz_sub_ui(nn, nn, 1);
4390
        ecc_scalar_set(&n, nn);
4391
        ecc_point_mul(&r, &n, &r);
4392
        ecc_point_get(&r, xx, yy);
4393
        mpz_sub(mm, mm, y);
4394
4395
        if (mpz_cmp(xx, x) != 0 ||
4396
            mpz_cmp(yy, mm) != 0) {
4397
          ret = gnutls_assert_val(
4398
            GNUTLS_E_ILLEGAL_PARAMETER);
4399
          goto ecc_fail;
4400
        }
4401
      } else {
4402
        not_approved = true;
4403
      }
4404
#endif
4405
4406
0
      mpz_set(TOMPZ(params->params[ECC_X]), x);
4407
0
      mpz_set(TOMPZ(params->params[ECC_Y]), y);
4408
4409
0
      ecc_scalar_get(&key, TOMPZ(params->params[ECC_K]));
4410
4411
0
      ret = 0;
4412
4413
0
    ecc_fail:
4414
0
      mpz_clear(x);
4415
0
      mpz_clear(y);
4416
0
      mpz_clear(xx);
4417
0
      mpz_clear(yy);
4418
0
      mpz_clear(nn);
4419
0
      mpz_clear(mm);
4420
0
      ecc_point_clear(&pub);
4421
0
      ecc_scalar_clear(&key);
4422
0
      ecc_point_clear(&r);
4423
0
      ecc_scalar_clear(&n);
4424
0
      ecc_scalar_clear(&m);
4425
4426
0
      if (ret < 0)
4427
0
        goto cleanup;
4428
4429
0
      break;
4430
0
    }
4431
0
#if ENABLE_GOST
4432
0
  case GNUTLS_PK_GOST_01:
4433
0
  case GNUTLS_PK_GOST_12_256:
4434
0
  case GNUTLS_PK_GOST_12_512:
4435
0
    if (params->pkflags & GNUTLS_PK_FLAG_PROVABLE)
4436
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4437
4438
0
    {
4439
0
      struct ecc_scalar key;
4440
0
      struct ecc_point pub;
4441
0
      const struct ecc_curve *curve;
4442
0
      const mac_entry_st *me;
4443
4444
      /* GOST curves are not approved */
4445
0
      not_approved = true;
4446
4447
0
      curve = get_supported_gost_curve(level);
4448
0
      if (curve == NULL) {
4449
0
        ret = gnutls_assert_val(
4450
0
          GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4451
0
        goto cleanup;
4452
0
      }
4453
4454
0
      me = hash_to_entry(_gnutls_gost_digest(algo));
4455
0
      if (!me || me->output_size * 8 != ecc_bit_size(curve)) {
4456
0
        ret = gnutls_assert_val(
4457
0
          GNUTLS_E_INVALID_REQUEST);
4458
0
        goto cleanup;
4459
0
      }
4460
4461
0
      ecc_scalar_init(&key, curve);
4462
0
      ecc_point_init(&pub, curve);
4463
4464
0
      gostdsa_generate_keypair(&pub, &key, NULL,
4465
0
             rnd_key_func);
4466
0
      if (HAVE_LIB_ERROR()) {
4467
0
        ret = gnutls_assert_val(
4468
0
          GNUTLS_E_LIB_IN_ERROR_STATE);
4469
0
        goto ecc_fail;
4470
0
      }
4471
4472
0
      ret = _gnutls_mpi_init_multi(&params->params[GOST_X],
4473
0
                 &params->params[GOST_Y],
4474
0
                 &params->params[GOST_K],
4475
0
                 NULL);
4476
0
      if (ret < 0) {
4477
0
        gnutls_assert();
4478
0
        goto gost_fail;
4479
0
      }
4480
4481
0
      params->curve = level;
4482
0
      params->params_nr = GOST_PRIVATE_PARAMS;
4483
4484
0
      ecc_point_get(&pub, TOMPZ(params->params[GOST_X]),
4485
0
              TOMPZ(params->params[GOST_Y]));
4486
0
      ecc_scalar_get(&key, TOMPZ(params->params[GOST_K]));
4487
4488
0
      ret = 0;
4489
4490
0
    gost_fail:
4491
0
      ecc_point_clear(&pub);
4492
0
      ecc_scalar_clear(&key);
4493
4494
0
      if (ret < 0)
4495
0
        goto cleanup;
4496
4497
0
      break;
4498
0
    }
4499
0
#endif
4500
0
  case GNUTLS_PK_ECDH_X25519:
4501
0
  case GNUTLS_PK_ECDH_X448: {
4502
0
    unsigned size = gnutls_ecc_curve_get_size(level);
4503
4504
0
    not_approved = true;
4505
4506
0
    if (size == 0) {
4507
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4508
0
      goto cleanup;
4509
0
    }
4510
4511
0
    params->curve = level;
4512
4513
0
    params->raw_priv.data = gnutls_malloc(size);
4514
0
    if (params->raw_priv.data == NULL) {
4515
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4516
0
      goto cleanup;
4517
0
    }
4518
4519
0
    params->raw_pub.data = gnutls_malloc(size);
4520
0
    if (params->raw_pub.data == NULL) {
4521
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4522
0
      goto cleanup;
4523
0
    }
4524
4525
0
    ret = gnutls_rnd(rnd_level, params->raw_priv.data, size);
4526
0
    if (ret < 0) {
4527
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4528
0
      goto cleanup;
4529
0
    }
4530
0
    params->raw_pub.size = size;
4531
0
    params->raw_priv.size = size;
4532
4533
0
    ret = edwards_curve_mul_g(algo, params->raw_pub.data,
4534
0
            params->raw_priv.data);
4535
0
    if (ret < 0)
4536
0
      goto cleanup;
4537
0
    break;
4538
0
  }
4539
0
  case GNUTLS_PK_MLKEM768:
4540
0
  case GNUTLS_PK_MLKEM1024:
4541
    /* unapproved until we implement CAST/PCT per IG 10.3.A(14). */
4542
0
    not_approved = true;
4543
0
    ret = ml_kem_generate_keypair(algo, &params->raw_priv,
4544
0
                &params->raw_pub);
4545
0
    if (ret < 0)
4546
0
      goto cleanup;
4547
0
    break;
4548
0
  case GNUTLS_PK_MLDSA44:
4549
0
  case GNUTLS_PK_MLDSA65:
4550
0
  case GNUTLS_PK_MLDSA87:
4551
0
    if (params->pkflags & GNUTLS_PK_FLAG_PROVABLE)
4552
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4553
4554
0
    not_approved = true;
4555
4556
0
    if (!(params->pkflags & GNUTLS_PK_FLAG_EXPAND_KEYS_FROM_SEED)) {
4557
0
      _gnutls_free_key_datum(&params->raw_seed);
4558
0
      params->raw_seed.data = gnutls_malloc(32);
4559
0
      params->raw_seed.size = 32;
4560
0
      ret = gnutls_rnd(GNUTLS_RND_KEY, params->raw_seed.data,
4561
0
           params->raw_seed.size);
4562
0
      if (ret < 0)
4563
0
        goto cleanup;
4564
0
    }
4565
4566
0
    ret = ml_dsa_generate_keypair(algo, &params->raw_priv,
4567
0
                &params->raw_pub,
4568
0
                &params->raw_seed);
4569
0
    if (ret < 0)
4570
0
      goto cleanup;
4571
0
    break;
4572
0
  default:
4573
0
    gnutls_assert();
4574
0
    return GNUTLS_E_INVALID_REQUEST;
4575
0
  }
4576
4577
0
  params->algo = algo;
4578
4579
#ifdef ENABLE_FIPS140
4580
  if (_gnutls_fips_mode_enabled()) {
4581
    ret = pct_test(algo, params);
4582
    if (ret < 0) {
4583
      gnutls_assert();
4584
      goto cleanup;
4585
    }
4586
  }
4587
#endif
4588
4589
0
cleanup:
4590
0
  if (ret < 0) {
4591
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
4592
0
    for (i = 0; i < params->params_nr; i++) {
4593
0
      _gnutls_mpi_release(&params->params[i]);
4594
0
    }
4595
0
    params->params_nr = 0;
4596
0
    gnutls_free(params->raw_priv.data);
4597
0
    gnutls_free(params->raw_pub.data);
4598
0
  } else if (not_approved) {
4599
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
4600
0
  } else {
4601
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
4602
0
  }
4603
4604
0
  gnutls_audit_pop_context();
4605
4606
0
  FAIL_IF_LIB_ERROR;
4607
0
  return ret;
4608
0
}
4609
4610
static int wrap_nettle_pk_verify_priv_params(gnutls_pk_algorithm_t algo,
4611
               const gnutls_pk_params_st *params)
4612
0
{
4613
0
  int ret;
4614
4615
0
  switch (algo) {
4616
0
  case GNUTLS_PK_RSA:
4617
0
  case GNUTLS_PK_RSA_PSS:
4618
0
  case GNUTLS_PK_RSA_OAEP: {
4619
0
    bigint_t t1 = NULL, t2 = NULL;
4620
4621
0
    if (params->params_nr != RSA_PRIVATE_PARAMS)
4622
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4623
4624
0
    ret = _gnutls_mpi_init_multi(&t1, &t2, NULL);
4625
0
    if (ret < 0)
4626
0
      return gnutls_assert_val(ret);
4627
4628
0
    _gnutls_mpi_mulm(t1, params->params[RSA_PRIME1],
4629
0
         params->params[RSA_PRIME2],
4630
0
         params->params[RSA_MODULUS]);
4631
0
    if (_gnutls_mpi_cmp_ui(t1, 0) != 0) {
4632
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4633
0
      goto rsa_cleanup;
4634
0
    }
4635
4636
0
    if (!mpz_invert(TOMPZ(t1), TOMPZ(params->params[RSA_PRIME2]),
4637
0
        TOMPZ(params->params[RSA_PRIME1]))) {
4638
0
      ret = gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
4639
0
      goto rsa_cleanup;
4640
0
    }
4641
0
    if (_gnutls_mpi_cmp(t1, params->params[RSA_COEF]) != 0) {
4642
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4643
0
      goto rsa_cleanup;
4644
0
    }
4645
4646
    /* [RSA_PRIME1] = d % p-1, [RSA_PRIME2] = d % q-1 */
4647
0
    _gnutls_mpi_sub_ui(t1, params->params[RSA_PRIME1], 1);
4648
0
    ret = _gnutls_mpi_modm(t2, params->params[RSA_PRIV], t1);
4649
0
    if (ret < 0) {
4650
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4651
0
      goto rsa_cleanup;
4652
0
    }
4653
4654
0
    if (_gnutls_mpi_cmp(params->params[RSA_E1], t2) != 0) {
4655
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4656
0
      goto rsa_cleanup;
4657
0
    }
4658
4659
0
    _gnutls_mpi_sub_ui(t1, params->params[RSA_PRIME2], 1);
4660
4661
0
    ret = _gnutls_mpi_modm(t2, params->params[RSA_PRIV], t1);
4662
0
    if (ret < 0) {
4663
0
      ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
4664
0
      goto rsa_cleanup;
4665
0
    }
4666
4667
0
    if (_gnutls_mpi_cmp(params->params[RSA_E2], t2) != 0) {
4668
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4669
0
      goto rsa_cleanup;
4670
0
    }
4671
4672
0
    ret = 0;
4673
4674
0
  rsa_cleanup:
4675
0
    zrelease_mpi_key(&t1);
4676
0
    zrelease_mpi_key(&t2);
4677
0
  }
4678
4679
0
  break;
4680
0
#ifdef ENABLE_DSA
4681
0
  case GNUTLS_PK_DSA:
4682
0
#endif
4683
0
  case GNUTLS_PK_DH: {
4684
0
    bigint_t t1 = NULL;
4685
4686
0
    if (params->params_nr != DSA_PRIVATE_PARAMS)
4687
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4688
4689
0
    ret = _gnutls_mpi_init(&t1);
4690
0
    if (ret < 0)
4691
0
      return gnutls_assert_val(ret);
4692
4693
0
    ret = _gnutls_mpi_powm(t1, params->params[DSA_G],
4694
0
               params->params[DSA_X],
4695
0
               params->params[DSA_P]);
4696
0
    if (ret < 0) {
4697
0
      gnutls_assert();
4698
0
      goto dsa_cleanup;
4699
0
    }
4700
4701
0
    if (_gnutls_mpi_cmp(t1, params->params[DSA_Y]) != 0) {
4702
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4703
0
      goto dsa_cleanup;
4704
0
    }
4705
4706
0
    ret = 0;
4707
4708
0
  dsa_cleanup:
4709
0
    zrelease_mpi_key(&t1);
4710
0
  }
4711
4712
0
  break;
4713
0
  case GNUTLS_PK_ECDSA: {
4714
0
    struct ecc_point r, pub;
4715
0
    struct ecc_scalar priv;
4716
0
    mpz_t x1, y1, x2, y2;
4717
0
    const struct ecc_curve *curve;
4718
4719
0
    if (params->params_nr != ECC_PRIVATE_PARAMS)
4720
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4721
4722
0
    curve = get_supported_nist_curve(params->curve);
4723
0
    if (curve == NULL)
4724
0
      return gnutls_assert_val(
4725
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4726
4727
0
    ret = _ecc_params_to_pubkey(params, &pub, curve);
4728
0
    if (ret < 0)
4729
0
      return gnutls_assert_val(ret);
4730
4731
0
    ret = _ecc_params_to_privkey(params, &priv, curve);
4732
0
    if (ret < 0) {
4733
0
      ecc_point_clear(&pub);
4734
0
      return gnutls_assert_val(ret);
4735
0
    }
4736
4737
0
    ecc_point_init(&r, curve);
4738
    /* verify that x,y lie on the curve */
4739
0
    ret = ecc_point_set(&r, TOMPZ(params->params[ECC_X]),
4740
0
            TOMPZ(params->params[ECC_Y]));
4741
0
    if (ret == 0) {
4742
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4743
0
      goto ecc_cleanup;
4744
0
    }
4745
0
    ecc_point_clear(&r);
4746
4747
0
    ecc_point_init(&r, curve);
4748
0
    ecc_point_mul_g(&r, &priv);
4749
4750
0
    mpz_init(x1);
4751
0
    mpz_init(y1);
4752
0
    ecc_point_get(&r, x1, y1);
4753
0
    ecc_point_zclear(&r);
4754
4755
0
    mpz_init(x2);
4756
0
    mpz_init(y2);
4757
0
    ecc_point_get(&pub, x2, y2);
4758
4759
    /* verify that k*(Gx,Gy)=(x,y) */
4760
0
    if (mpz_cmp(x1, x2) != 0 || mpz_cmp(y1, y2) != 0) {
4761
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4762
0
      goto ecc_cleanup;
4763
0
    }
4764
4765
0
    ret = 0;
4766
4767
0
  ecc_cleanup:
4768
0
    ecc_scalar_zclear(&priv);
4769
0
    ecc_point_clear(&pub);
4770
4771
0
    mpz_clear(x1);
4772
0
    mpz_clear(y1);
4773
0
    mpz_clear(x2);
4774
0
    mpz_clear(y2);
4775
0
  } break;
4776
0
  case GNUTLS_PK_EDDSA_ED25519:
4777
0
  case GNUTLS_PK_EDDSA_ED448: {
4778
0
    gnutls_ecc_curve_t curve;
4779
0
    const gnutls_ecc_curve_entry_st *e;
4780
0
    uint8_t pub[57]; /* can accommodate both curves */
4781
4782
0
    curve = get_eddsa_curve(algo);
4783
0
    e = _gnutls_ecc_curve_get_params(curve);
4784
0
    if (e == NULL)
4785
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4786
4787
0
    if (params->raw_pub.data == NULL) {
4788
0
      return 0; /* nothing to verify */
4789
0
    }
4790
4791
0
    if (params->raw_pub.size != e->size)
4792
0
      return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4793
4794
0
    ret = eddsa_public_key(algo, pub, params->raw_priv.data);
4795
0
    if (ret < 0)
4796
0
      return ret;
4797
4798
0
    if (!memeq(params->raw_pub.data, pub, e->size))
4799
0
      return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4800
4801
0
    ret = 0;
4802
0
    break;
4803
0
  }
4804
0
  case GNUTLS_PK_ECDH_X25519:
4805
0
  case GNUTLS_PK_ECDH_X448: {
4806
0
    gnutls_ecc_curve_t curve;
4807
0
    const gnutls_ecc_curve_entry_st *e;
4808
0
    uint8_t pub[57]; /* can accommodate both curves */
4809
4810
0
    curve = get_ecdh_curve(algo);
4811
0
    e = _gnutls_ecc_curve_get_params(curve);
4812
0
    if (e == NULL)
4813
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4814
4815
0
    if (params->raw_pub.data == NULL) {
4816
0
      return 0; /* nothing to verify */
4817
0
    }
4818
4819
0
    if (params->raw_pub.size != e->size)
4820
0
      return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4821
4822
0
    ret = edwards_curve_mul_g(algo, pub, params->raw_priv.data);
4823
0
    if (ret < 0)
4824
0
      return ret;
4825
4826
0
    if (!memeq(params->raw_pub.data, pub, e->size))
4827
0
      return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4828
4829
0
    ret = 0;
4830
0
    break;
4831
0
  }
4832
0
  case GNUTLS_PK_MLKEM768:
4833
0
  case GNUTLS_PK_MLKEM1024:
4834
0
    if (!ml_kem_exists(algo))
4835
0
      return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
4836
4837
0
    ret = 0;
4838
0
    break;
4839
0
  case GNUTLS_PK_MLDSA44:
4840
0
  case GNUTLS_PK_MLDSA65:
4841
0
  case GNUTLS_PK_MLDSA87: {
4842
0
    if (!ml_dsa_exists(algo))
4843
0
      return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
4844
4845
0
    ret = 0;
4846
0
    break;
4847
0
  }
4848
0
#if ENABLE_GOST
4849
0
  case GNUTLS_PK_GOST_01:
4850
0
  case GNUTLS_PK_GOST_12_256:
4851
0
  case GNUTLS_PK_GOST_12_512: {
4852
0
    struct ecc_point r, pub;
4853
0
    struct ecc_scalar priv;
4854
0
    mpz_t x1, y1, x2, y2;
4855
0
    const struct ecc_curve *curve;
4856
4857
0
    if (params->params_nr != GOST_PRIVATE_PARAMS)
4858
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4859
4860
0
    curve = get_supported_gost_curve(params->curve);
4861
0
    if (curve == NULL)
4862
0
      return gnutls_assert_val(
4863
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4864
4865
0
    ret = _gost_params_to_pubkey(params, &pub, curve);
4866
0
    if (ret < 0)
4867
0
      return gnutls_assert_val(ret);
4868
4869
0
    ret = _gost_params_to_privkey(params, &priv, curve);
4870
0
    if (ret < 0) {
4871
0
      ecc_point_clear(&pub);
4872
0
      return gnutls_assert_val(ret);
4873
0
    }
4874
4875
0
    ecc_point_init(&r, curve);
4876
    /* verify that x,y lie on the curve */
4877
0
    ret = gost_point_set(&r, TOMPZ(params->params[GOST_X]),
4878
0
             TOMPZ(params->params[GOST_Y]));
4879
0
    if (ret == 0) {
4880
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4881
0
      goto gost_cleanup;
4882
0
    }
4883
0
    ecc_point_clear(&r);
4884
4885
0
    ecc_point_init(&r, curve);
4886
0
    gost_point_mul_g(&r, &priv);
4887
4888
0
    mpz_init(x1);
4889
0
    mpz_init(y1);
4890
0
    ecc_point_get(&r, x1, y1);
4891
0
    ecc_point_zclear(&r);
4892
4893
0
    mpz_init(x2);
4894
0
    mpz_init(y2);
4895
0
    ecc_point_get(&pub, x2, y2);
4896
4897
    /* verify that k*(Gx,Gy)=(x,y) */
4898
0
    if (mpz_cmp(x1, x2) != 0 || mpz_cmp(y1, y2) != 0) {
4899
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4900
0
      goto gost_cleanup;
4901
0
    }
4902
4903
0
    ret = 0;
4904
4905
0
  gost_cleanup:
4906
0
    ecc_scalar_zclear(&priv);
4907
0
    ecc_point_clear(&pub);
4908
4909
0
    mpz_clear(x1);
4910
0
    mpz_clear(y1);
4911
0
    mpz_clear(x2);
4912
0
    mpz_clear(y2);
4913
0
  } break;
4914
0
#endif
4915
0
  default:
4916
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4917
0
  }
4918
4919
0
  return ret;
4920
0
}
4921
4922
static int wrap_nettle_pk_verify_pub_params(gnutls_pk_algorithm_t algo,
4923
              const gnutls_pk_params_st *params)
4924
0
{
4925
0
  int ret;
4926
4927
0
  switch (algo) {
4928
0
  case GNUTLS_PK_RSA:
4929
0
  case GNUTLS_PK_RSA_PSS:
4930
0
  case GNUTLS_PK_RSA_OAEP:
4931
0
#ifdef ENABLE_DSA
4932
0
  case GNUTLS_PK_DSA:
4933
0
#endif
4934
0
  case GNUTLS_PK_EDDSA_ED25519:
4935
0
  case GNUTLS_PK_EDDSA_ED448:
4936
0
    return 0;
4937
0
  case GNUTLS_PK_ECDSA: {
4938
    /* just verify that x and y lie on the curve */
4939
0
    struct ecc_point r, pub;
4940
0
    const struct ecc_curve *curve;
4941
4942
0
    if (params->params_nr != ECC_PUBLIC_PARAMS)
4943
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4944
4945
0
    curve = get_supported_nist_curve(params->curve);
4946
0
    if (curve == NULL)
4947
0
      return gnutls_assert_val(
4948
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4949
4950
0
    ret = _ecc_params_to_pubkey(params, &pub, curve);
4951
0
    if (ret < 0)
4952
0
      return gnutls_assert_val(ret);
4953
4954
0
    ecc_point_init(&r, curve);
4955
    /* verify that x,y lie on the curve */
4956
0
    ret = ecc_point_set(&r, TOMPZ(params->params[ECC_X]),
4957
0
            TOMPZ(params->params[ECC_Y]));
4958
0
    if (ret == 0) {
4959
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4960
0
      goto ecc_cleanup;
4961
0
    }
4962
0
    ecc_point_clear(&r);
4963
4964
0
    ret = 0;
4965
4966
0
  ecc_cleanup:
4967
0
    ecc_point_clear(&pub);
4968
0
  } break;
4969
0
#if ENABLE_GOST
4970
0
  case GNUTLS_PK_GOST_01:
4971
0
  case GNUTLS_PK_GOST_12_256:
4972
0
  case GNUTLS_PK_GOST_12_512: {
4973
    /* just verify that x and y lie on the curve */
4974
0
    struct ecc_point r, pub;
4975
0
    const struct ecc_curve *curve;
4976
4977
0
    if (params->params_nr != GOST_PUBLIC_PARAMS)
4978
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4979
4980
0
    curve = get_supported_gost_curve(params->curve);
4981
0
    if (curve == NULL)
4982
0
      return gnutls_assert_val(
4983
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
4984
4985
0
    ret = _gost_params_to_pubkey(params, &pub, curve);
4986
0
    if (ret < 0)
4987
0
      return gnutls_assert_val(ret);
4988
4989
0
    ecc_point_init(&r, curve);
4990
    /* verify that x,y lie on the curve */
4991
0
    ret = ecc_point_set(&r, TOMPZ(params->params[GOST_X]),
4992
0
            TOMPZ(params->params[GOST_Y]));
4993
0
    if (ret == 0) {
4994
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
4995
0
      goto gost_cleanup;
4996
0
    }
4997
0
    ecc_point_clear(&r);
4998
4999
0
    ret = 0;
5000
5001
0
  gost_cleanup:
5002
0
    ecc_point_clear(&pub);
5003
0
  } break;
5004
0
#endif
5005
0
  default:
5006
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
5007
0
  }
5008
5009
0
  return ret;
5010
0
}
5011
5012
static int calc_rsa_exp(gnutls_pk_params_st *params)
5013
0
{
5014
0
  bigint_t tmp;
5015
0
  int ret;
5016
5017
0
  if (params->params_nr < RSA_PRIVATE_PARAMS - 2) {
5018
0
    gnutls_assert();
5019
0
    return GNUTLS_E_INTERNAL_ERROR;
5020
0
  }
5021
5022
0
  params->params[RSA_E1] = params->params[RSA_E2] = NULL;
5023
5024
0
  ret = _gnutls_mpi_init_multi(&tmp, &params->params[RSA_E1],
5025
0
             &params->params[RSA_E2], NULL);
5026
0
  if (ret < 0)
5027
0
    return gnutls_assert_val(ret);
5028
5029
  /* [6] = d % p-1, [7] = d % q-1 */
5030
0
  _gnutls_mpi_sub_ui(tmp, params->params[RSA_PRIME1], 1);
5031
0
  ret = _gnutls_mpi_modm(params->params[RSA_E1],
5032
0
             params->params[RSA_PRIV] /*d */, tmp);
5033
0
  if (ret < 0)
5034
0
    goto fail;
5035
5036
0
  _gnutls_mpi_sub_ui(tmp, params->params[RSA_PRIME2], 1);
5037
0
  ret = _gnutls_mpi_modm(params->params[RSA_E2],
5038
0
             params->params[RSA_PRIV] /*d */, tmp);
5039
0
  if (ret < 0)
5040
0
    goto fail;
5041
5042
0
  zrelease_mpi_key(&tmp);
5043
5044
0
  return 0;
5045
5046
0
fail:
5047
0
  zrelease_mpi_key(&tmp);
5048
0
  zrelease_mpi_key(&params->params[RSA_E1]);
5049
0
  zrelease_mpi_key(&params->params[RSA_E2]);
5050
5051
0
  return ret;
5052
0
}
5053
5054
static int calc_rsa_priv(gnutls_pk_params_st *params)
5055
0
{
5056
0
  bigint_t lcm, p1, q1;
5057
0
  int ret;
5058
5059
0
  params->params[RSA_PRIV] = NULL;
5060
5061
0
  ret = _gnutls_mpi_init_multi(&params->params[RSA_PRIV], &lcm, &p1, &q1,
5062
0
             NULL);
5063
0
  if (ret < 0)
5064
0
    return gnutls_assert_val(ret);
5065
5066
  /* lcm(p - 1, q - 1) */
5067
0
  mpz_sub_ui(p1, params->params[RSA_PRIME1], 1);
5068
0
  mpz_sub_ui(q1, params->params[RSA_PRIME2], 1);
5069
0
  mpz_lcm(lcm, p1, q1);
5070
5071
0
  zrelease_mpi_key(&p1);
5072
0
  zrelease_mpi_key(&q1);
5073
5074
  /* d = e^{-1} (mod lcm) */
5075
0
  ret = mpz_invert(params->params[RSA_PRIV], params->params[RSA_PUB],
5076
0
       lcm);
5077
5078
0
  zrelease_mpi_key(&lcm);
5079
5080
0
  if (ret == 0) {
5081
0
    zrelease_mpi_key(&params->params[RSA_PRIV]);
5082
0
    return GNUTLS_E_INVALID_REQUEST;
5083
0
  }
5084
5085
0
  return 0;
5086
0
}
5087
5088
#ifdef ENABLE_DSA
5089
static int calc_dsa_pub(gnutls_pk_params_st *params)
5090
0
{
5091
0
  int ret;
5092
5093
0
  params->params[DSA_Y] = NULL;
5094
5095
0
  ret = _gnutls_mpi_init(&params->params[DSA_Y]);
5096
0
  if (ret < 0)
5097
0
    return gnutls_assert_val(ret);
5098
5099
  /* y = g^x mod p */
5100
0
  ret = _gnutls_mpi_powm(params->params[DSA_Y], params->params[DSA_G],
5101
0
             params->params[DSA_X], params->params[DSA_P]);
5102
0
  if (ret < 0) {
5103
0
    zrelease_mpi_key(&params->params[DSA_Y]);
5104
0
    return gnutls_assert_val(ret);
5105
0
  }
5106
5107
0
  return 0;
5108
0
}
5109
#endif
5110
5111
static int wrap_nettle_pk_fixup(gnutls_pk_algorithm_t algo,
5112
        gnutls_direction_t direction,
5113
        gnutls_pk_params_st *params)
5114
0
{
5115
0
  int ret = 0;
5116
5117
0
  if (direction != GNUTLS_IMPORT)
5118
0
    return 0;
5119
5120
0
  switch (algo) {
5121
0
  case GNUTLS_PK_RSA: {
5122
0
    struct rsa_private_key priv;
5123
5124
0
    if (params->params[RSA_PRIV] == NULL) {
5125
0
      ret = calc_rsa_priv(params);
5126
0
      if (ret < 0)
5127
0
        return gnutls_assert_val(ret);
5128
0
      params->params_nr++;
5129
0
    }
5130
5131
    /* do not trust the generated values. Some old private keys
5132
     * generated by us have mess on the values. Those were very
5133
     * old but it seemed some of the shipped example private
5134
     * keys were as old.
5135
     */
5136
0
    if (params->params_nr < RSA_PRIVATE_PARAMS - 3)
5137
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5138
5139
0
    if (params->params[RSA_COEF] == NULL) {
5140
0
      ret = _gnutls_mpi_init(&params->params[RSA_COEF]);
5141
0
      if (ret < 0)
5142
0
        return gnutls_assert_val(ret);
5143
0
    }
5144
5145
0
    if (mpz_cmp_ui(TOMPZ(params->params[RSA_PRIME1]), 0) == 0)
5146
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5147
5148
0
    if (mpz_invert(TOMPZ(params->params[RSA_COEF]),
5149
0
             TOMPZ(params->params[RSA_PRIME2]),
5150
0
             TOMPZ(params->params[RSA_PRIME1])) == 0)
5151
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5152
5153
    /* calculate exp1 [6] and exp2 [7] */
5154
0
    zrelease_mpi_key(&params->params[RSA_E1]);
5155
0
    zrelease_mpi_key(&params->params[RSA_E2]);
5156
5157
    /* marks RSA_COEF as present */
5158
0
    params->params_nr = RSA_PRIVATE_PARAMS - 2;
5159
0
    ret = calc_rsa_exp(params);
5160
0
    if (ret < 0)
5161
0
      return gnutls_assert_val(ret);
5162
5163
0
    params->params_nr = RSA_PRIVATE_PARAMS;
5164
5165
    /* perform nettle's internal checks */
5166
0
    _rsa_params_to_privkey(params, &priv);
5167
0
    ret = rsa_private_key_prepare(&priv);
5168
0
    if (ret == 0) {
5169
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5170
0
    }
5171
0
    ret = 0;
5172
0
  } break;
5173
0
  case GNUTLS_PK_EDDSA_ED25519:
5174
0
  case GNUTLS_PK_EDDSA_ED448:
5175
0
    if (unlikely(get_eddsa_curve(algo) != params->curve))
5176
0
      return gnutls_assert_val(
5177
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
5178
5179
0
    if (params->raw_priv.data == NULL)
5180
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5181
5182
0
    if (params->raw_pub.data == NULL) {
5183
0
      params->raw_pub.data =
5184
0
        gnutls_malloc(params->raw_priv.size);
5185
0
    }
5186
5187
0
    if (params->raw_pub.data == NULL)
5188
0
      return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
5189
5190
0
    ret = eddsa_public_key(algo, params->raw_pub.data,
5191
0
               params->raw_priv.data);
5192
0
    if (ret < 0) {
5193
0
      gnutls_free(params->raw_pub.data);
5194
0
      return ret;
5195
0
    }
5196
5197
0
    params->raw_pub.size = params->raw_priv.size;
5198
0
    break;
5199
5200
0
  case GNUTLS_PK_ECDH_X25519:
5201
0
  case GNUTLS_PK_ECDH_X448:
5202
0
    if (unlikely(get_ecdh_curve(algo) != params->curve))
5203
0
      return gnutls_assert_val(
5204
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
5205
5206
0
    if (params->raw_priv.data == NULL)
5207
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5208
5209
0
    if (params->raw_pub.data == NULL) {
5210
0
      params->raw_pub.data =
5211
0
        gnutls_malloc(params->raw_priv.size);
5212
0
    }
5213
5214
0
    if (params->raw_pub.data == NULL)
5215
0
      return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
5216
5217
0
    ret = edwards_curve_mul_g(algo, params->raw_pub.data,
5218
0
            params->raw_priv.data);
5219
0
    if (ret < 0) {
5220
0
      gnutls_free(params->raw_pub.data);
5221
0
      return ret;
5222
0
    }
5223
5224
0
    params->raw_pub.size = params->raw_priv.size;
5225
0
    break;
5226
5227
0
  case GNUTLS_PK_RSA_PSS:
5228
0
    if (params->params_nr < RSA_PRIVATE_PARAMS - 3)
5229
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5230
5231
0
    if (params->spki.rsa_pss_dig != 0) {
5232
0
      unsigned pub_size = nettle_mpz_sizeinbase_256_u(
5233
0
        TOMPZ(params->params[RSA_MODULUS]));
5234
      /* sanity check for private key */
5235
0
      CHECK_INVALID_RSA_PSS_PARAMS(
5236
0
        gnutls_hash_get_len(params->spki.rsa_pss_dig),
5237
0
        params->spki.salt_size, pub_size,
5238
0
        GNUTLS_E_PK_INVALID_PUBKEY_PARAMS);
5239
0
    }
5240
0
    break;
5241
5242
0
  case GNUTLS_PK_MLDSA44:
5243
0
  case GNUTLS_PK_MLDSA65:
5244
0
  case GNUTLS_PK_MLDSA87:
5245
0
    if (params->raw_priv.data == NULL)
5246
0
      return gnutls_assert_val(GNUTLS_E_PK_INVALID_PRIVKEY);
5247
5248
0
    if (params->raw_pub.data == NULL) {
5249
0
      ret = ml_dsa_privkey_to_pubkey(algo, &params->raw_priv,
5250
0
                   &params->raw_pub);
5251
0
      if (ret < 0) {
5252
0
        if (ret == GNUTLS_E_UNIMPLEMENTED_FEATURE) {
5253
0
          _gnutls_debug_log(
5254
0
            "Deriving public key from an ML-DSA private key is not implemented; ignoring the request\n");
5255
0
          return 0;
5256
0
        }
5257
0
        return gnutls_assert_val(ret);
5258
0
      }
5259
0
    }
5260
0
    break;
5261
5262
0
#ifdef ENABLE_DSA
5263
0
  case GNUTLS_PK_DSA:
5264
0
    if (params->params[DSA_Y] == NULL) {
5265
0
      ret = calc_dsa_pub(params);
5266
0
      if (ret < 0)
5267
0
        return gnutls_assert_val(ret);
5268
0
      params->params_nr++;
5269
0
    }
5270
0
    break;
5271
0
#endif
5272
0
#if ENABLE_GOST
5273
0
  case GNUTLS_PK_GOST_01:
5274
0
  case GNUTLS_PK_GOST_12_256:
5275
0
  case GNUTLS_PK_GOST_12_512: {
5276
0
    struct ecc_point r;
5277
0
    struct ecc_scalar priv;
5278
0
    const struct ecc_curve *curve;
5279
5280
0
    if (params->params_nr != GOST_PRIVATE_PARAMS)
5281
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
5282
5283
0
    curve = get_supported_gost_curve(params->curve);
5284
0
    if (curve == NULL)
5285
0
      return gnutls_assert_val(
5286
0
        GNUTLS_E_ECC_UNSUPPORTED_CURVE);
5287
5288
0
    if (ecc_bit_size(curve) <
5289
0
        _gnutls_mpi_get_nbits(params->params[GOST_K]))
5290
0
      gostdsa_unmask_key(curve,
5291
0
             TOMPZ(params->params[GOST_K]));
5292
5293
0
    ret = _gost_params_to_privkey(params, &priv, curve);
5294
0
    if (ret < 0) {
5295
0
      return gnutls_assert_val(ret);
5296
0
    }
5297
5298
0
    ecc_point_init(&r, curve);
5299
0
    gost_point_mul_g(&r, &priv);
5300
5301
0
    ecc_point_get(&r, params->params[GOST_X],
5302
0
            params->params[GOST_Y]);
5303
5304
0
    ecc_point_clear(&r);
5305
0
    ecc_scalar_clear(&priv);
5306
0
  } break;
5307
0
#endif
5308
0
  case GNUTLS_PK_EC:
5309
0
    if (params->params_nr == ECC_PRIVATE_PARAMS) {
5310
0
      return 0;
5311
0
    }
5312
0
    struct ecc_scalar s;
5313
0
    struct ecc_point p;
5314
0
    mpz_t pk, px, py;
5315
0
    gnutls_datum_t k = { NULL, 0 };
5316
0
    unsigned char exported_mpz_buf[MAX_PRIME_CURVE_COORD_SIZE] = {
5317
0
      0
5318
0
    };
5319
0
    uint8_t x_buf[MAX_PRIME_CURVE_COORD_SIZE] = { 0 };
5320
0
    uint8_t y_buf[MAX_PRIME_CURVE_COORD_SIZE] = { 0 };
5321
0
    size_t count = 0;
5322
5323
0
    const struct ecc_curve *ecc =
5324
0
      get_supported_nist_curve(params->curve);
5325
5326
0
    if (ecc == NULL) {
5327
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
5328
0
    }
5329
5330
0
    size_t coord_size = gnutls_ecc_curve_get_size(params->curve);
5331
0
    if (coord_size == 0) {
5332
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
5333
0
    }
5334
5335
0
    mpz_init(pk);
5336
0
    mpz_init(px);
5337
0
    mpz_init(py);
5338
5339
0
    ret = _gnutls_mpi_dprint(params->params[ECC_K], &k);
5340
0
    if (ret != 0) {
5341
0
      ret = gnutls_assert_val(ret);
5342
0
      goto cleanup;
5343
0
    }
5344
5345
0
    mpz_import(pk, k.size, 1, 1, 1, 0, k.data);
5346
5347
0
    ecc_scalar_init(&s, ecc);
5348
5349
0
    if (!ecc_scalar_set(&s, pk)) {
5350
0
      ret = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
5351
0
      goto cleanup;
5352
0
    }
5353
5354
0
    ecc_point_init(&p, ecc);
5355
0
    ecc_point_mul_g(&p, &s);
5356
0
    ecc_point_get(&p, px, py);
5357
5358
0
    mpz_export(exported_mpz_buf, &count, 1, 1, 1, 0, px);
5359
0
    memcpy(x_buf + (coord_size - count), exported_mpz_buf, count);
5360
5361
0
    zeroize_key(exported_mpz_buf, MAX_PRIME_CURVE_COORD_SIZE);
5362
0
    mpz_export(exported_mpz_buf, &count, 1, 1, 1, 0, py);
5363
0
    memcpy(y_buf + (coord_size - count), exported_mpz_buf, count);
5364
5365
0
    ret = _gnutls_mpi_init_scan(&params->params[ECC_X], x_buf,
5366
0
              coord_size);
5367
0
    if (ret != 0) {
5368
0
      ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
5369
0
      goto cleanup;
5370
0
    }
5371
0
    params->params_nr++;
5372
5373
0
    ret = _gnutls_mpi_init_scan(&params->params[ECC_Y], y_buf,
5374
0
              coord_size);
5375
0
    if (ret != 0) {
5376
0
      ret = gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED);
5377
0
      goto cleanup;
5378
0
    }
5379
0
    params->params_nr++;
5380
5381
0
  cleanup:
5382
0
    ecc_point_clear(&p);
5383
0
    ecc_scalar_clear(&s);
5384
0
    mpz_clear(pk);
5385
0
    mpz_clear(px);
5386
0
    mpz_clear(py);
5387
5388
0
    _gnutls_free_key_datum(&k);
5389
0
    break;
5390
0
  default:
5391
0
    break;
5392
0
  }
5393
5394
0
  return ret;
5395
0
}
5396
5397
int crypto_pk_prio = INT_MAX;
5398
5399
gnutls_crypto_pk_st _gnutls_pk_ops = {
5400
  .encrypt = _wrap_nettle_pk_encrypt,
5401
  .decrypt = _wrap_nettle_pk_decrypt,
5402
  .decrypt2 = _wrap_nettle_pk_decrypt2,
5403
  .sign = _wrap_nettle_pk_sign,
5404
  .verify = _wrap_nettle_pk_verify,
5405
  .verify_priv_params = wrap_nettle_pk_verify_priv_params,
5406
  .verify_pub_params = wrap_nettle_pk_verify_pub_params,
5407
  .generate_params = wrap_nettle_pk_generate_params,
5408
  .generate_keys = wrap_nettle_pk_generate_keys,
5409
  .pk_fixup_private_params = wrap_nettle_pk_fixup,
5410
  .derive = _wrap_nettle_pk_derive,
5411
  .encaps = _wrap_nettle_pk_encaps,
5412
  .decaps = _wrap_nettle_pk_decaps,
5413
  .curve_exists = _wrap_nettle_pk_curve_exists,
5414
  .pk_exists = _wrap_nettle_pk_exists,
5415
  .sign_exists = _wrap_nettle_pk_sign_exists
5416
};