/src/gnutls/lib/nettle/backport/rsa-oaep-encrypt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* rsa-oaep-encrypt.c |
2 | | |
3 | | The RSA publickey algorithm. OAEP encryption. |
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 "nettle-internal.h" |
43 | | #include "oaep.h" |
44 | | #include "rsa-internal.h" |
45 | | |
46 | | int |
47 | | _rsa_oaep_encrypt (const struct rsa_public_key *key, |
48 | | void *random_ctx, nettle_random_func *random, |
49 | | void *hash_ctx, const struct nettle_hash *hash, |
50 | | size_t label_length, const uint8_t *label, |
51 | | size_t length, const uint8_t *message, |
52 | | uint8_t *ciphertext) |
53 | 0 | { |
54 | 0 | mpz_t gibberish; |
55 | |
|
56 | 0 | mpz_init (gibberish); |
57 | |
|
58 | 0 | if (_oaep_encode_mgf1 (gibberish, key->size, |
59 | 0 | random_ctx, random, |
60 | 0 | hash_ctx, hash, |
61 | 0 | label_length, label, |
62 | 0 | length, message)) |
63 | 0 | { |
64 | 0 | mpz_powm (gibberish, gibberish, key->e, key->n); |
65 | 0 | nettle_mpz_get_str_256 (key->size, ciphertext, gibberish); |
66 | 0 | mpz_clear (gibberish); |
67 | 0 | return 1; |
68 | 0 | } |
69 | | |
70 | 0 | mpz_clear (gibberish); |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | | int |
75 | | rsa_oaep_sha256_encrypt (const struct rsa_public_key *key, |
76 | | void *random_ctx, nettle_random_func *random, |
77 | | size_t label_length, const uint8_t *label, |
78 | | size_t length, const uint8_t *message, |
79 | | uint8_t *ciphertext) |
80 | 0 | { |
81 | 0 | struct sha256_ctx ctx; |
82 | |
|
83 | 0 | sha256_init (&ctx); |
84 | |
|
85 | 0 | return _rsa_oaep_encrypt (key, |
86 | 0 | random_ctx, random, |
87 | 0 | &ctx, &nettle_sha256, |
88 | 0 | label_length, label, |
89 | 0 | length, message, |
90 | 0 | ciphertext); |
91 | 0 | } |
92 | | |
93 | | int |
94 | | rsa_oaep_sha384_encrypt (const struct rsa_public_key *key, |
95 | | void *random_ctx, nettle_random_func *random, |
96 | | size_t label_length, const uint8_t *label, |
97 | | size_t length, const uint8_t *message, |
98 | | uint8_t *ciphertext) |
99 | 0 | { |
100 | 0 | struct sha384_ctx ctx; |
101 | |
|
102 | 0 | sha384_init (&ctx); |
103 | |
|
104 | 0 | return _rsa_oaep_encrypt (key, |
105 | 0 | random_ctx, random, |
106 | 0 | &ctx, &nettle_sha384, |
107 | 0 | label_length, label, |
108 | 0 | length, message, |
109 | 0 | ciphertext); |
110 | 0 | } |
111 | | |
112 | | int |
113 | | rsa_oaep_sha512_encrypt (const struct rsa_public_key *key, |
114 | | void *random_ctx, nettle_random_func *random, |
115 | | size_t label_length, const uint8_t *label, |
116 | | size_t length, const uint8_t *message, |
117 | | uint8_t *ciphertext) |
118 | 0 | { |
119 | 0 | struct sha512_ctx ctx; |
120 | |
|
121 | 0 | sha512_init (&ctx); |
122 | |
|
123 | 0 | return _rsa_oaep_encrypt (key, |
124 | 0 | random_ctx, random, |
125 | 0 | &ctx, &nettle_sha512, |
126 | 0 | label_length, label, |
127 | 0 | length, message, |
128 | 0 | ciphertext); |
129 | 0 | } |