/* Written by Ulf Moeller. This software is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
-/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
-
-/* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
- * <URL: http://www.shoup.net/papers/oaep.ps.Z>
- * for problems with the security proof for the
- * original OAEP scheme, which EME-OAEP is based on.
- *
- * A new proof can be found in E. Fujisaki, T. Okamoto,
- * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
- * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
- * The new proof has stronger requirements for the
- * underlying permutation: "partial-one-wayness" instead
- * of one-wayness. For the RSA function, this is
- * an equivalent notion.
- */
-
+/* EME_OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
#if !defined(NO_SHA) && !defined(NO_SHA1)
#include <stdio.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
-int MGF1(unsigned char *mask, long len,
- const unsigned char *seed, long seedlen);
+int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen);
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
- const unsigned char *from, int flen,
- const unsigned char *param, int plen)
+ unsigned char *from, int flen, unsigned char *param, int plen)
{
int i, emlen = tlen - 1;
unsigned char *db, *seed;
}
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
- const unsigned char *from, int flen, int num,
- const unsigned char *param, int plen)
+ unsigned char *from, int flen, int num, unsigned char *param,
+ int plen)
{
int i, dblen, mlen = -1;
- const unsigned char *maskeddb;
+ unsigned char *maskeddb;
int lzero;
- unsigned char *db, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
+ unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
if (--num < 2 * SHA_DIGEST_LENGTH + 1)
- {
- RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
- return (-1);
- }
+ goto decoding_err;
+ lzero = num - flen;
+ if (lzero < 0)
+ goto decoding_err;
+ maskeddb = from - lzero + SHA_DIGEST_LENGTH;
+
dblen = num - SHA_DIGEST_LENGTH;
db = OPENSSL_malloc(dblen);
if (db == NULL)
return (-1);
}
- lzero = num - flen;
- maskeddb = from - lzero + SHA_DIGEST_LENGTH;
-
MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
seed[i] ^= from[i - lzero];
SHA1(param, plen, phash);
if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0)
- RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
+ goto decoding_err;
else
{
for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
if (db[i] != 0x00)
break;
if (db[i] != 0x01 || i++ >= dblen)
- RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP,
- RSA_R_OAEP_DECODING_ERROR);
+ goto decoding_err;
else
{
mlen = dblen - i;
if (tlen < mlen)
{
- RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
+ RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
mlen = -1;
}
else
}
OPENSSL_free(db);
return (mlen);
+
+decoding_err:
+ /* to avoid chosen ciphertext attacks, the error message should not reveal
+ * which kind of decoding error happened */
+ RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
+ if (db != NULL) OPENSSL_free(db);
+ return -1;
}
-int MGF1(unsigned char *mask, long len,
- const unsigned char *seed, long seedlen)
+int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen)
{
long i, outlen = 0;
unsigned char cnt[4];