Coverage Report

Created: 2024-07-23 07:36

/src/gnutls/lib/nettle/backport/rsa-oaep-decrypt.c
Line
Count
Source (jump to first uncovered line)
1
/* rsa-oaep-decrypt.c
2
3
   The RSA publickey algorithm. OAEP decryption.
4
5
   Copyright (C) 2021-2024 Nicolas Mora
6
   Copyright (C) 2024 Daiki Ueno
7
8
   This file is part of GNU Nettle.
9
10
   GNU Nettle is free software: you can redistribute it and/or
11
   modify it under the terms of either:
12
13
     * the GNU Lesser General Public License as published by the Free
14
       Software Foundation; either version 3 of the License, or (at your
15
       option) any later version.
16
17
   or
18
19
     * the GNU General Public License as published by the Free
20
       Software Foundation; either version 2 of the License, or (at your
21
       option) any later version.
22
23
   or both in parallel, as here.
24
25
   GNU Nettle is distributed in the hope that it will be useful,
26
   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28
   General Public License for more details.
29
30
   You should have received copies of the GNU General Public License and
31
   the GNU Lesser General Public License along with this program.  If
32
   not, see http://www.gnu.org/licenses/.
33
*/
34
35
#if HAVE_CONFIG_H
36
#include "config.h"
37
#endif
38
39
#include <nettle/rsa.h>
40
#include "int/rsa-oaep.h"
41
42
#include "gmp-glue.h"
43
#include "nettle-internal.h"
44
#include "oaep.h"
45
#include "rsa-internal.h"
46
47
int
48
_rsa_oaep_decrypt (const struct rsa_public_key *pub,
49
       const struct rsa_private_key *key,
50
       void *random_ctx, nettle_random_func *random,
51
       void *hash_ctx, const struct nettle_hash *hash,
52
       size_t label_length, const uint8_t *label,
53
       size_t *length, uint8_t *message,
54
       const uint8_t *ciphertext)
55
0
{
56
0
  TMP_GMP_DECL (m, mp_limb_t);
57
0
  TMP_GMP_DECL (em, uint8_t);
58
0
  int res;
59
60
0
  TMP_GMP_ALLOC (m, mpz_size (pub->n));
61
0
  TMP_GMP_ALLOC (em, key->size);
62
63
0
  mpn_set_base256 (m, mpz_size (pub->n), ciphertext, pub->size);
64
65
  /* Check that input is in range. */
66
0
  if (mpn_cmp (m, mpz_limbs_read (pub->n), mpz_size (pub->n)) >= 0)
67
0
    {
68
0
      TMP_GMP_FREE (em);
69
0
      TMP_GMP_FREE (m);
70
0
      return 0;
71
0
    }
72
73
0
  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
74
75
0
  mpn_get_base256 (em, key->size, m, mpz_size (pub->n));
76
77
0
  res &= _oaep_decode_mgf1 (em, key->size, hash_ctx, hash, label_length, label,
78
0
          length, message);
79
80
0
  TMP_GMP_FREE (em);
81
0
  TMP_GMP_FREE (m);
82
0
  return res;
83
0
}
84
85
int
86
rsa_oaep_sha256_decrypt (const struct rsa_public_key *pub,
87
       const struct rsa_private_key *key,
88
       void *random_ctx, nettle_random_func *random,
89
       size_t label_length, const uint8_t *label,
90
       size_t *length, uint8_t *message,
91
       const uint8_t *ciphertext)
92
0
{
93
0
  struct sha256_ctx ctx;
94
95
0
  sha256_init (&ctx);
96
97
0
  return _rsa_oaep_decrypt (pub, key, random_ctx, random,
98
0
          &ctx, &nettle_sha256, label_length, label,
99
0
          length, message, ciphertext);
100
0
}
101
102
int
103
rsa_oaep_sha384_decrypt (const struct rsa_public_key *pub,
104
       const struct rsa_private_key *key,
105
       void *random_ctx, nettle_random_func *random,
106
       size_t label_length, const uint8_t *label,
107
       size_t *length, uint8_t *message,
108
       const uint8_t *ciphertext)
109
0
{
110
0
  struct sha384_ctx ctx;
111
112
0
  sha384_init (&ctx);
113
114
0
  return _rsa_oaep_decrypt (pub, key, random_ctx, random,
115
0
          &ctx, &nettle_sha384, label_length, label,
116
0
          length, message, ciphertext);
117
0
}
118
119
int
120
rsa_oaep_sha512_decrypt (const struct rsa_public_key *pub,
121
       const struct rsa_private_key *key,
122
       void *random_ctx, nettle_random_func *random,
123
       size_t label_length, const uint8_t *label,
124
       size_t *length, uint8_t *message,
125
       const uint8_t *ciphertext)
126
0
{
127
0
  struct sha512_ctx ctx;
128
129
0
  sha512_init (&ctx);
130
131
0
  return _rsa_oaep_decrypt (pub, key, random_ctx, random,
132
0
          &ctx, &nettle_sha512, label_length, label,
133
0
          length, message, ciphertext);
134
0
}