2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2005-05-28 20:44:37 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2005-05-28 20:44:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2005-05-28 20:44:37 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
2010-03-11 13:40:42 +00:00
|
|
|
#include "rsa_locl.h"
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2005-08-07 22:21:49 +00:00
|
|
|
#if defined(_MSC_VER) && defined(_ARM_)
|
2015-01-22 03:40:55 +00:00
|
|
|
# pragma optimize("g", off)
|
2005-08-07 22:21:49 +00:00
|
|
|
#endif
|
|
|
|
|
2005-05-28 20:44:37 +00:00
|
|
|
int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
|
2015-01-22 03:40:55 +00:00
|
|
|
const EVP_MD *Hash, const unsigned char *EM,
|
|
|
|
int sLen)
|
|
|
|
{
|
|
|
|
return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
|
|
|
|
}
|
2010-03-11 13:40:42 +00:00
|
|
|
|
|
|
|
int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
|
2015-01-22 03:40:55 +00:00
|
|
|
const EVP_MD *Hash, const EVP_MD *mgf1Hash,
|
|
|
|
const unsigned char *EM, int sLen)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
int hLen, maskedDBLen, MSBits, emLen;
|
|
|
|
const unsigned char *H;
|
|
|
|
unsigned char *DB = NULL;
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char H_[EVP_MAX_MD_SIZE];
|
2015-11-27 13:02:12 +00:00
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
2005-06-02 18:25:36 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (mgf1Hash == NULL)
|
|
|
|
mgf1Hash = Hash;
|
2010-03-11 13:40:42 +00:00
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
hLen = EVP_MD_size(Hash);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (hLen < 0)
|
|
|
|
goto err;
|
2015-01-05 11:30:03 +00:00
|
|
|
/*-
|
|
|
|
* Negative sLen has special meanings:
|
|
|
|
* -1 sLen == hLen
|
|
|
|
* -2 salt length is autorecovered from signature
|
2017-02-27 11:40:35 +00:00
|
|
|
* -3 salt length is maximized
|
2015-01-05 11:30:03 +00:00
|
|
|
* -N reserved
|
|
|
|
*/
|
2017-01-17 17:51:24 +00:00
|
|
|
if (sLen == RSA_PSS_SALTLEN_DIGEST)
|
2015-01-22 03:40:55 +00:00
|
|
|
sLen = hLen;
|
2017-01-17 17:51:24 +00:00
|
|
|
else if (sLen < RSA_PSS_SALTLEN_MAX) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
|
|
|
|
goto err;
|
|
|
|
}
|
2005-06-02 18:25:36 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
|
|
|
|
emLen = RSA_size(rsa);
|
|
|
|
if (EM[0] & (0xFF << MSBits)) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (MSBits == 0) {
|
|
|
|
EM++;
|
|
|
|
emLen--;
|
|
|
|
}
|
2017-02-27 11:40:35 +00:00
|
|
|
if (emLen < hLen + 2) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-17 17:51:24 +00:00
|
|
|
if (sLen == RSA_PSS_SALTLEN_MAX) {
|
|
|
|
sLen = emLen - hLen - 2;
|
2017-02-27 11:40:35 +00:00
|
|
|
} else if (sLen > emLen - hLen - 2) { /* sLen can be small negative */
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (EM[emLen - 1] != 0xbc) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
maskedDBLen = emLen - hLen - 1;
|
|
|
|
H = EM + maskedDBLen;
|
|
|
|
DB = OPENSSL_malloc(maskedDBLen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (DB == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
|
|
|
|
goto err;
|
|
|
|
for (i = 0; i < maskedDBLen; i++)
|
|
|
|
DB[i] ^= EM[i];
|
|
|
|
if (MSBits)
|
|
|
|
DB[0] &= 0xFF >> (8 - MSBits);
|
|
|
|
for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
|
|
|
|
if (DB[i++] != 0x1) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-17 17:51:24 +00:00
|
|
|
if (sLen != RSA_PSS_SALTLEN_AUTO && (maskedDBLen - i) != sLen) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx, Hash, NULL)
|
|
|
|
|| !EVP_DigestUpdate(ctx, zeroes, sizeof zeroes)
|
|
|
|
|| !EVP_DigestUpdate(ctx, mHash, hLen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
if (maskedDBLen - i) {
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, H_, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
if (memcmp(H_, H, hLen)) {
|
|
|
|
RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE);
|
|
|
|
ret = 0;
|
|
|
|
} else
|
|
|
|
ret = 1;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(DB);
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-05-28 20:44:37 +00:00
|
|
|
|
|
|
|
int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *mHash,
|
|
|
|
const EVP_MD *Hash, int sLen)
|
|
|
|
{
|
|
|
|
return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
|
|
|
|
}
|
2010-03-11 13:40:42 +00:00
|
|
|
|
|
|
|
int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *mHash,
|
|
|
|
const EVP_MD *Hash, const EVP_MD *mgf1Hash,
|
|
|
|
int sLen)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
int hLen, maskedDBLen, MSBits, emLen;
|
|
|
|
unsigned char *H, *salt = NULL, *p;
|
2015-11-27 13:02:12 +00:00
|
|
|
EVP_MD_CTX *ctx = NULL;
|
2005-06-02 18:25:36 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (mgf1Hash == NULL)
|
|
|
|
mgf1Hash = Hash;
|
2010-03-11 13:40:42 +00:00
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
hLen = EVP_MD_size(Hash);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (hLen < 0)
|
|
|
|
goto err;
|
2015-01-05 11:30:03 +00:00
|
|
|
/*-
|
|
|
|
* Negative sLen has special meanings:
|
|
|
|
* -1 sLen == hLen
|
|
|
|
* -2 salt length is maximized
|
2017-02-27 11:40:35 +00:00
|
|
|
* -3 same as above (on signing)
|
2015-01-05 11:30:03 +00:00
|
|
|
* -N reserved
|
|
|
|
*/
|
2017-01-17 17:51:24 +00:00
|
|
|
if (sLen == RSA_PSS_SALTLEN_DIGEST)
|
2015-01-22 03:40:55 +00:00
|
|
|
sLen = hLen;
|
2017-01-17 17:51:24 +00:00
|
|
|
else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN)
|
|
|
|
sLen = RSA_PSS_SALTLEN_MAX;
|
|
|
|
else if (sLen < RSA_PSS_SALTLEN_MAX) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
|
|
|
|
goto err;
|
|
|
|
}
|
2005-06-02 18:25:36 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
|
|
|
|
emLen = RSA_size(rsa);
|
|
|
|
if (MSBits == 0) {
|
|
|
|
*EM++ = 0;
|
|
|
|
emLen--;
|
|
|
|
}
|
2017-02-27 11:40:35 +00:00
|
|
|
if (emLen < hLen + 2) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,
|
|
|
|
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-01-17 17:51:24 +00:00
|
|
|
if (sLen == RSA_PSS_SALTLEN_MAX) {
|
2015-01-22 03:40:55 +00:00
|
|
|
sLen = emLen - hLen - 2;
|
2017-02-27 11:40:35 +00:00
|
|
|
} else if (sLen > emLen - hLen - 2) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,
|
|
|
|
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (sLen > 0) {
|
|
|
|
salt = OPENSSL_malloc(sLen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (salt == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,
|
|
|
|
ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (RAND_bytes(salt, sLen) <= 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
maskedDBLen = emLen - hLen - 1;
|
|
|
|
H = EM + maskedDBLen;
|
2015-12-01 23:49:35 +00:00
|
|
|
ctx = EVP_MD_CTX_new();
|
2015-11-27 13:02:12 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
if (!EVP_DigestInit_ex(ctx, Hash, NULL)
|
|
|
|
|| !EVP_DigestUpdate(ctx, zeroes, sizeof zeroes)
|
|
|
|
|| !EVP_DigestUpdate(ctx, mHash, hLen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:02:12 +00:00
|
|
|
if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, H, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Generate dbMask in place then perform XOR on it */
|
|
|
|
if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
|
|
|
|
goto err;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = EM;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Initial PS XORs with all zeroes which is a NOP so just update pointer.
|
|
|
|
* Note from a test above this value is guaranteed to be non-negative.
|
|
|
|
*/
|
|
|
|
p += emLen - sLen - hLen - 2;
|
|
|
|
*p++ ^= 0x1;
|
|
|
|
if (sLen > 0) {
|
|
|
|
for (i = 0; i < sLen; i++)
|
|
|
|
*p++ ^= salt[i];
|
|
|
|
}
|
|
|
|
if (MSBits)
|
|
|
|
EM[0] &= 0xFF >> (8 - MSBits);
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* H is already in place so just set final 0xbc */
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
EM[emLen - 1] = 0xbc;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 1;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(salt);
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
2005-05-28 20:44:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-08-07 22:21:49 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2015-01-22 03:40:55 +00:00
|
|
|
# pragma optimize("",on)
|
2005-08-07 22:21:49 +00:00
|
|
|
#endif
|