# Is RSA of a random nonce with no padding safe?

Consider the following protocol: Bob has a private RSA key $B_{priv}$, and Alice knows the public key $B_{pub}$. Alice wants to send confidential messages to Bob (no integrity intended). To send a message, Alice randomly generates a single-use symmetric key (say, an AES key) $K$, encrypts the message with this symmetric key, and sends this encrypted message alongside with $K$ encrypted with RSA with Bob's public key.

Suppose we use no padding for $\textrm{RSA}_{B_{pub}}(K)$ (in other words, to pad with zero bytes). The obvious (to me) problems with a lack of padding are that an attacker can guess the plaintext (not possible here, it's random) and that encrypting the same plaintext twice gives the same result (again, not happening here). What are the known weaknesses of this scheme (or is it safe)?

-

If you pad a 128-bit value $K$ with zeroes to the left, and interpret the value numerically with big-endian convention (as happens in PKCS#1 in general), you end up with a value which is no greater than $2^{128}$. If the public exponent is $e = 3$, then the "encrypted value" is no greater than $2^{768}$, and no actual modular reduction takes place. Consequently, a simple cube root extraction on plain integers reveals $K$.

So, that can be quite unsafe.

What is safe is the following:

• you generate a random integer $x$, between $0$ and $n-1$ ($n$ is the RSA modulus);
• you encrypt $x$ by simply raising it to the power $e$ modulo $n$;
• the encryption key you will use with AES is $h(x)$ for some hash function $h$.

It is still relatively limiting, in that this is safe as long as you do one encryption only of $x$. If you want to "send" your symmetric key $K$ to several recipients (a common model, e.g. for email encryption), then the scheme above does not work, and, more generally, you need some per-encryption randomness inserted (as PKCS#1 does for encryption).

-
Ouch, defeated by simple math. Hmm, what if I encrypted $K + 2^{\mathop{lg}(n)-1}$? And incidentally, with multiple recipients, does the unpredictability of the padding matter, or is it enough never to apply RSA to the same plaintext with different keys? (Don't worry, I'm asking out of curiosity, I'm not planning to implement anything like this.) –  Gilles Feb 2 '12 at 0:02
@Gilles: if padding is deterministic, and you encrypt the same $K$ (padded to $\pi(K)$) for three different recipients, with $e = 3$, then you send the value $\pi(K)^3$ modulo $n_1$, $n_2$ and $n_3$. By the Chinese Remainder Theorem, this is sufficient to rebuild $\pi(K)$ modulo $n_1n_2n_3$, and since $\pi(K) \lt n_i$, the modulo disappears. This is the historical justification for using $e = 65537$ instead of $e = 3$, but the real issue is that a deterministic padding for RSA encryption is definitely unsafe. You MUST have a random padding. –  Thomas Pornin Feb 2 '12 at 12:34
@ThomasPornin Your argument doesn't show that per-encryption randomness is required. For example you don't show why padding based on hash(m,e,n) is a problem. Such a scheme is still deterministic, and doesn't suffer from this specific problem. –  CodesInChaos Apr 18 '12 at 13:27
@CodeInChaos: yes, my argument does not show why per-encryption randomness is required, which is why I wrote in my response "more generally". You need per-encryption randomness to avoid exhaustive search attacks on the encrypted text (which may or may not be applicable, depending on context -- it depends whether you are encrypting a meaningful message, or a random session key). –  Thomas Pornin Apr 22 '12 at 22:56
Out of curiosity: Will this scheme also work without the final step $k = H(x)$, e.g. by simply taking the $n$ left- or rightmost bits of $x$? –  lxgr Jul 15 '14 at 18:43