1999-02-17 21:11:08 +00:00
|
|
|
/* crypto/rsa/rsa_oaep.c */
|
|
|
|
/* 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) */
|
|
|
|
|
1999-04-27 04:18:53 +00:00
|
|
|
#if !defined(NO_SHA) && !defined(NO_SHA1)
|
1999-02-17 21:11:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rand.h>
|
1999-02-17 21:11:08 +00:00
|
|
|
|
|
|
|
int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen);
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
|
|
|
|
unsigned char *from, int flen, unsigned char *param, int plen)
|
1999-02-17 21:11:08 +00:00
|
|
|
{
|
|
|
|
int i, emlen = tlen - 1;
|
|
|
|
unsigned char *db, *seed;
|
|
|
|
unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
|
|
|
|
{
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
|
|
|
|
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
|
|
|
|
{
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-06-01 22:19:21 +00:00
|
|
|
dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
|
1999-02-17 21:11:08 +00:00
|
|
|
if (dbmask == NULL)
|
|
|
|
{
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
to[0] = 0;
|
|
|
|
seed = to + 1;
|
|
|
|
db = to + SHA_DIGEST_LENGTH + 1;
|
|
|
|
|
|
|
|
SHA1(param, plen, db);
|
|
|
|
memset(db + SHA_DIGEST_LENGTH, 0,
|
|
|
|
emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
|
|
|
|
db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
|
|
|
|
memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
|
2000-01-13 20:59:17 +00:00
|
|
|
if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
|
|
|
|
return (0);
|
1999-02-17 21:11:08 +00:00
|
|
|
#ifdef PKCS_TESTVECT
|
|
|
|
memcpy(seed,
|
|
|
|
"\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
|
|
|
|
20);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
|
|
|
|
for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
|
|
|
|
db[i] ^= dbmask[i];
|
|
|
|
|
|
|
|
MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
|
|
|
|
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
|
|
|
|
seed[i] ^= seedmask[i];
|
|
|
|
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(dbmask);
|
1999-02-17 21:11:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
|
|
|
|
unsigned char *from, int flen, int num, unsigned char *param,
|
|
|
|
int plen)
|
1999-02-17 21:11:08 +00:00
|
|
|
{
|
|
|
|
int i, dblen, mlen = -1;
|
|
|
|
unsigned char *maskeddb;
|
1999-04-29 20:31:56 +00:00
|
|
|
int lzero;
|
2001-06-06 18:48:57 +00:00
|
|
|
unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
|
1999-02-17 21:11:08 +00:00
|
|
|
|
1999-04-29 20:31:56 +00:00
|
|
|
if (--num < 2 * SHA_DIGEST_LENGTH + 1)
|
2001-06-06 18:48:57 +00:00
|
|
|
goto decoding_err;
|
1999-02-17 21:11:08 +00:00
|
|
|
|
2001-06-06 17:24:03 +00:00
|
|
|
lzero = num - flen;
|
|
|
|
if (lzero < 0)
|
2001-06-06 18:48:57 +00:00
|
|
|
goto decoding_err;
|
2001-06-06 17:24:03 +00:00
|
|
|
maskeddb = from - lzero + SHA_DIGEST_LENGTH;
|
|
|
|
|
1999-04-29 20:31:56 +00:00
|
|
|
dblen = num - SHA_DIGEST_LENGTH;
|
2000-06-01 22:19:21 +00:00
|
|
|
db = OPENSSL_malloc(dblen);
|
1999-02-17 21:11:08 +00:00
|
|
|
if (db == NULL)
|
|
|
|
{
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
|
1999-04-29 20:31:56 +00:00
|
|
|
for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
|
|
|
|
seed[i] ^= from[i - lzero];
|
1999-02-17 21:11:08 +00:00
|
|
|
|
|
|
|
MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
|
|
|
|
for (i = 0; i < dblen; i++)
|
|
|
|
db[i] ^= maskeddb[i];
|
|
|
|
|
|
|
|
SHA1(param, plen, phash);
|
|
|
|
|
|
|
|
if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0)
|
2001-06-06 18:48:57 +00:00
|
|
|
goto decoding_err;
|
1999-02-17 21:11:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
|
|
|
|
if (db[i] != 0x00)
|
|
|
|
break;
|
|
|
|
if (db[i] != 0x01 || i++ >= dblen)
|
2001-06-08 14:17:12 +00:00
|
|
|
goto decoding_err;
|
1999-02-17 21:11:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mlen = dblen - i;
|
|
|
|
if (tlen < mlen)
|
|
|
|
{
|
2001-06-08 14:17:12 +00:00
|
|
|
RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
|
1999-02-17 21:11:08 +00:00
|
|
|
mlen = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy(to, db + i, mlen);
|
|
|
|
}
|
|
|
|
}
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(db);
|
1999-02-17 21:11:08 +00:00
|
|
|
return (mlen);
|
2001-06-06 18:48:57 +00:00
|
|
|
|
|
|
|
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;
|
1999-02-17 21:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen)
|
|
|
|
{
|
|
|
|
long i, outlen = 0;
|
|
|
|
unsigned char cnt[4];
|
|
|
|
SHA_CTX c;
|
|
|
|
unsigned char md[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
for (i = 0; outlen < len; i++)
|
|
|
|
{
|
|
|
|
cnt[0] = (i >> 24) & 255, cnt[1] = (i >> 16) & 255,
|
|
|
|
cnt[2] = (i >> 8) & 255, cnt[3] = i & 255;
|
|
|
|
SHA1_Init(&c);
|
|
|
|
SHA1_Update(&c, seed, seedlen);
|
|
|
|
SHA1_Update(&c, cnt, 4);
|
|
|
|
if (outlen + SHA_DIGEST_LENGTH <= len)
|
|
|
|
{
|
|
|
|
SHA1_Final(mask + outlen, &c);
|
|
|
|
outlen += SHA_DIGEST_LENGTH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SHA1_Final(md, &c);
|
|
|
|
memcpy(mask + outlen, md, len - outlen);
|
|
|
|
outlen = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
1999-04-27 03:19:12 +00:00
|
|
|
#endif
|