rsa_oaep.c revision 273399
1/* crypto/rsa/rsa_oaep.c */
2/* Written by Ulf Moeller. This software is distributed on an "AS IS"
3   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
4
5/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
6
7/* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
8 * <URL: http://www.shoup.net/papers/oaep.ps.Z>
9 * for problems with the security proof for the
10 * original OAEP scheme, which EME-OAEP is based on.
11 *
12 * A new proof can be found in E. Fujisaki, T. Okamoto,
13 * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
14 * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
15 * The new proof has stronger requirements for the
16 * underlying permutation: "partial-one-wayness" instead
17 * of one-wayness.  For the RSA function, this is
18 * an equivalent notion.
19 */
20
21#include "constant_time_locl.h"
22
23#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
24#include <stdio.h>
25#include "cryptlib.h"
26#include <openssl/bn.h>
27#include <openssl/rsa.h>
28#include <openssl/evp.h>
29#include <openssl/rand.h>
30#include <openssl/sha.h>
31
32static int MGF1(unsigned char *mask, long len,
33	const unsigned char *seed, long seedlen);
34
35int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
36	const unsigned char *from, int flen,
37	const unsigned char *param, int plen)
38	{
39	int i, emlen = tlen - 1;
40	unsigned char *db, *seed;
41	unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
42
43	if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
44		{
45		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
46		   RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
47		return 0;
48		}
49
50	if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
51		{
52		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
53		return 0;
54		}
55
56	to[0] = 0;
57	seed = to + 1;
58	db = to + SHA_DIGEST_LENGTH + 1;
59
60	if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
61		return 0;
62	memset(db + SHA_DIGEST_LENGTH, 0,
63		emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
64	db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
65	memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
66	if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
67		return 0;
68#ifdef PKCS_TESTVECT
69	memcpy(seed,
70	   "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
71	   20);
72#endif
73
74	dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
75	if (dbmask == NULL)
76		{
77		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
78		return 0;
79		}
80
81	if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0)
82		return 0;
83	for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
84		db[i] ^= dbmask[i];
85
86	if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH) < 0)
87		return 0;
88	for (i = 0; i < SHA_DIGEST_LENGTH; i++)
89		seed[i] ^= seedmask[i];
90
91	OPENSSL_free(dbmask);
92	return 1;
93	}
94
95int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
96	const unsigned char *from, int flen, int num,
97	const unsigned char *param, int plen)
98	{
99	int i, dblen, mlen = -1, one_index = 0, msg_index;
100	unsigned int good, found_one_byte;
101	const unsigned char *maskedseed, *maskeddb;
102	/* |em| is the encoded message, zero-padded to exactly |num| bytes:
103	 * em = Y || maskedSeed || maskedDB */
104	unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
105		phash[EVP_MAX_MD_SIZE];
106
107        if (tlen <= 0 || flen <= 0)
108		return -1;
109
110	/*
111	 * |num| is the length of the modulus; |flen| is the length of the
112	 * encoded message. Therefore, for any |from| that was obtained by
113	 * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
114	 * num < 2 * SHA_DIGEST_LENGTH + 2 must hold for the modulus
115	 * irrespective of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
116	 * This does not leak any side-channel information.
117	 */
118	if (num < flen || num < 2 * SHA_DIGEST_LENGTH + 2)
119		goto decoding_err;
120
121	dblen = num - SHA_DIGEST_LENGTH - 1;
122	db = OPENSSL_malloc(dblen);
123	em = OPENSSL_malloc(num);
124	if (db == NULL || em == NULL)
125		{
126		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
127		goto cleanup;
128		}
129
130	/*
131	 * Always do this zero-padding copy (even when num == flen) to avoid
132	 * leaking that information. The copy still leaks some side-channel
133	 * information, but it's impossible to have a fixed  memory access
134	 * pattern since we can't read out of the bounds of |from|.
135	 *
136	 * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
137	 */
138	memset(em, 0, num);
139	memcpy(em + num - flen, from, flen);
140
141	/*
142	 * The first byte must be zero, however we must not leak if this is
143	 * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA
144	 * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
145	 */
146	good = constant_time_is_zero(em[0]);
147
148	maskedseed = em + 1;
149	maskeddb = em + 1 + SHA_DIGEST_LENGTH;
150
151	if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
152		goto cleanup;
153	for (i = 0; i < SHA_DIGEST_LENGTH; i++)
154		seed[i] ^= maskedseed[i];
155
156	if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
157		goto cleanup;
158	for (i = 0; i < dblen; i++)
159		db[i] ^= maskeddb[i];
160
161	if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
162		goto cleanup;
163
164	good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH));
165
166	found_one_byte = 0;
167	for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
168		{
169		/* Padding consists of a number of 0-bytes, followed by a 1. */
170		unsigned int equals1 = constant_time_eq(db[i], 1);
171		unsigned int equals0 = constant_time_is_zero(db[i]);
172		one_index = constant_time_select_int(~found_one_byte & equals1,
173			i, one_index);
174		found_one_byte |= equals1;
175		good &= (found_one_byte | equals0);
176		}
177
178	good &= found_one_byte;
179
180	/*
181	 * At this point |good| is zero unless the plaintext was valid,
182	 * so plaintext-awareness ensures timing side-channels are no longer a
183	 * concern.
184	 */
185	if (!good)
186		goto decoding_err;
187
188	msg_index = one_index + 1;
189	mlen = dblen - msg_index;
190
191	if (tlen < mlen)
192		{
193		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
194		mlen = -1;
195		}
196	else
197		{
198		memcpy(to, db + msg_index, mlen);
199		goto cleanup;
200		}
201
202decoding_err:
203	/* To avoid chosen ciphertext attacks, the error message should not reveal
204	 * which kind of decoding error happened. */
205	RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
206cleanup:
207	if (db != NULL) OPENSSL_free(db);
208	if (em != NULL) OPENSSL_free(em);
209	return mlen;
210	}
211
212int PKCS1_MGF1(unsigned char *mask, long len,
213	const unsigned char *seed, long seedlen, const EVP_MD *dgst)
214	{
215	long i, outlen = 0;
216	unsigned char cnt[4];
217	EVP_MD_CTX c;
218	unsigned char md[EVP_MAX_MD_SIZE];
219	int mdlen;
220	int rv = -1;
221
222	EVP_MD_CTX_init(&c);
223	mdlen = EVP_MD_size(dgst);
224	if (mdlen < 0)
225		goto err;
226	for (i = 0; outlen < len; i++)
227		{
228		cnt[0] = (unsigned char)((i >> 24) & 255);
229		cnt[1] = (unsigned char)((i >> 16) & 255);
230		cnt[2] = (unsigned char)((i >> 8)) & 255;
231		cnt[3] = (unsigned char)(i & 255);
232		if (!EVP_DigestInit_ex(&c,dgst, NULL)
233			|| !EVP_DigestUpdate(&c, seed, seedlen)
234			|| !EVP_DigestUpdate(&c, cnt, 4))
235			goto err;
236		if (outlen + mdlen <= len)
237			{
238			if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
239				goto err;
240			outlen += mdlen;
241			}
242		else
243			{
244			if (!EVP_DigestFinal_ex(&c, md, NULL))
245				goto err;
246			memcpy(mask + outlen, md, len - outlen);
247			outlen = len;
248			}
249		}
250	rv = 0;
251	err:
252	EVP_MD_CTX_cleanup(&c);
253	return rv;
254	}
255
256static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
257		 long seedlen)
258	{
259	return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
260	}
261#endif
262