3 The RSA publickey algorithm. PKCS#1 encryption.
5 Copyright (C) 2001, 2012 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
48 pkcs1_encrypt (size_t key_size,
50 void *random_ctx, nettle_random_func *random,
51 size_t length, const uint8_t *message,
54 TMP_GMP_DECL(em, uint8_t);
58 /* The message is encoded as a string of the same length as the
59 * modulo n, of the form
61 * 00 02 pad 00 message
63 * where padding should be at least 8 pseudorandomly generated
64 * *non-zero* octets. */
66 if (length + 11 > key_size)
67 /* Message too long for this key. */
70 /* At least 8 octets of random padding */
71 padding = key_size - length - 3;
74 TMP_GMP_ALLOC(em, key_size - 1);
77 random(random_ctx, padding, em + 1);
79 /* Replace 0-octets with 1 */
80 for (i = 0; i<padding; i++)
85 memcpy(em + padding + 2, message, length);
87 nettle_mpz_set_str_256_u(m, key_size - 1, em);