3 The RSA publickey algorithm. Side channel resistant PKCS#1 decryption.
5 Copyright (C) 2001, 2012 Niels Möller
6 Copyright (C) 2018 Red Hat, Inc.
8 This file is part of GNU Nettle.
10 GNU Nettle is free software: you can redistribute it and/or
11 modify it under the terms of either:
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.
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.
23 or both in parallel, as here.
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.
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/.
46 #include "pkcs1-internal.h"
48 /* Inputs are always cast to uint32_t values. But all values used in this
49 * function should never exceed the maximum value of a uint32_t anyway.
50 * these macros returns 1 on success, 0 on failure */
51 #define NOT_EQUAL(a, b) \
52 ((0U - ((uint32_t)(a) ^ (uint32_t)(b))) >> 31)
54 ((((uint32_t)(a) ^ (uint32_t)(b)) - 1U) >> 31)
55 #define GREATER_OR_EQUAL(a, b) \
56 (1U - (((uint32_t)(a) - (uint32_t)(b)) >> 31))
59 _pkcs1_sec_decrypt (size_t length, uint8_t *message,
60 size_t padded_message_length,
61 const volatile uint8_t *padded_message)
66 assert (padded_message_length >= length);
68 t = padded_message_length - length - 1;
70 /* Check format, padding, message_size */
71 ok = EQUAL(padded_message[0], 0); /* ok if padded_message[0] == 0 */
72 ok &= EQUAL(padded_message[1], 2); /* ok if padded_message[1] == 2 */
73 for (i = 2; i < t; i++) /* check padding has no zeros */
75 ok &= NOT_EQUAL(padded_message[i], 0);
77 ok &= EQUAL(padded_message[t], 0); /* ok if terminator == 0 */
79 /* fill destination buffer regardless of outcome */
80 cnd_memcpy(ok, message, padded_message + t + 1, length);
86 _pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message,
87 size_t padded_message_length,
88 const volatile uint8_t *padded_message)
90 volatile int not_found = 1;
92 volatile size_t offset;
93 size_t buflen, msglen;
96 /* Check format, padding, message_size */
97 ok = EQUAL(padded_message[0], 0);
98 ok &= EQUAL(padded_message[1], 2);
100 /* length is discovered in a side-channel silent way.
101 * not_found goes to 0 when the terminator is found.
102 * offset strts at 3 as it includes the terminator and
103 * the fomat bytes already */
105 for (i = 2; i < padded_message_length; i++)
107 not_found &= NOT_EQUAL(padded_message[i], 0);
110 /* check if we ran out of buffer */
111 ok &= NOT_EQUAL(not_found, 1);
112 /* padding must be >= 11 (2 format bytes + 8 pad bytes min. + terminator) */
113 ok &= GREATER_OR_EQUAL(offset, 11);
115 /* offset can vary between 3 and padded_message_length, due to the loop
116 * above, therefore msglen can't underflow */
117 msglen = padded_message_length - offset;
119 /* we always fill the whole buffer but only up to
120 * padded_message_length length */
122 if (buflen > padded_message_length) { /* input independent branch */
123 buflen = padded_message_length;
126 /* if the message length is larger than the buffer we must fail */
127 ok &= GREATER_OR_EQUAL(buflen, msglen);
129 /* fill destination buffer fully regardless of outcome. Copies the message
130 * in a memory access independent way. The destination message buffer will
131 * be clobbered past the message length. */
132 shift = padded_message_length - buflen;
133 cnd_memcpy(ok, message, padded_message + shift, buflen);
135 /* In this loop, the bits of the 'offset' variable are used as shifting
136 * conditions, starting from the least significant bit. The end result is
137 * that the buffer is shifted left exactly 'offset' bytes. */
138 for (shift = 1; shift < buflen; shift <<= 1, offset >>= 1)
140 /* 'ok' is both a least significant bit mask and a condition */
141 cnd_memcpy(offset & ok, message, message + shift, buflen - shift);
144 /* update length only if we succeeded, otherwise leave unchanged */
145 *length = (msglen & (-(size_t) ok)) + (*length & ((size_t) ok - 1));