sl@0: /* rsa_pss.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 2005. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2005 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0}; sl@0: sl@0: #if defined(_MSC_VER) && defined(_ARM_) sl@0: #pragma optimize("g", off) sl@0: #endif sl@0: sl@0: EXPORT_C int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, sl@0: const EVP_MD *Hash, const unsigned char *EM, int sLen) sl@0: { sl@0: int i; sl@0: int ret = 0; sl@0: int hLen, maskedDBLen, MSBits, emLen; sl@0: const unsigned char *H; sl@0: unsigned char *DB = NULL; sl@0: EVP_MD_CTX ctx; sl@0: unsigned char H_[EVP_MAX_MD_SIZE]; sl@0: sl@0: hLen = EVP_MD_size(Hash); sl@0: /* sl@0: * Negative sLen has special meanings: sl@0: * -1 sLen == hLen sl@0: * -2 salt length is autorecovered from signature sl@0: * -N reserved sl@0: */ sl@0: if (sLen == -1) sLen = hLen; sl@0: else if (sLen == -2) sLen = -2; sl@0: else if (sLen < -2) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); sl@0: goto err; sl@0: } sl@0: sl@0: MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; sl@0: emLen = RSA_size(rsa); sl@0: if (EM[0] & (0xFF << MSBits)) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_FIRST_OCTET_INVALID); sl@0: goto err; sl@0: } sl@0: if (MSBits == 0) sl@0: { sl@0: EM++; sl@0: emLen--; sl@0: } sl@0: if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */ sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_DATA_TOO_LARGE); sl@0: goto err; sl@0: } sl@0: if (EM[emLen - 1] != 0xbc) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_LAST_OCTET_INVALID); sl@0: goto err; sl@0: } sl@0: maskedDBLen = emLen - hLen - 1; sl@0: H = EM + maskedDBLen; sl@0: DB = OPENSSL_malloc(maskedDBLen); sl@0: if (!DB) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash); sl@0: for (i = 0; i < maskedDBLen; i++) sl@0: DB[i] ^= EM[i]; sl@0: if (MSBits) sl@0: DB[0] &= 0xFF >> (8 - MSBits); sl@0: for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ; sl@0: if (DB[i++] != 0x1) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_RECOVERY_FAILED); sl@0: goto err; sl@0: } sl@0: if (sLen >= 0 && (maskedDBLen - i) != sLen) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); sl@0: goto err; sl@0: } sl@0: EVP_MD_CTX_init(&ctx); sl@0: EVP_DigestInit_ex(&ctx, Hash, NULL); sl@0: EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); sl@0: EVP_DigestUpdate(&ctx, mHash, hLen); sl@0: if (maskedDBLen - i) sl@0: EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i); sl@0: EVP_DigestFinal(&ctx, H_, NULL); sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: if (memcmp(H_, H, hLen)) sl@0: { sl@0: RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE); sl@0: ret = 0; sl@0: } sl@0: else sl@0: ret = 1; sl@0: sl@0: err: sl@0: if (DB) sl@0: OPENSSL_free(DB); sl@0: sl@0: return ret; sl@0: sl@0: } sl@0: sl@0: EXPORT_C int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, sl@0: const unsigned char *mHash, sl@0: const EVP_MD *Hash, int sLen) sl@0: { sl@0: int i; sl@0: int ret = 0; sl@0: int hLen, maskedDBLen, MSBits, emLen; sl@0: unsigned char *H, *salt = NULL, *p; sl@0: EVP_MD_CTX ctx; sl@0: sl@0: hLen = EVP_MD_size(Hash); sl@0: /* sl@0: * Negative sLen has special meanings: sl@0: * -1 sLen == hLen sl@0: * -2 salt length is maximized sl@0: * -N reserved sl@0: */ sl@0: if (sLen == -1) sLen = hLen; sl@0: else if (sLen == -2) sLen = -2; sl@0: else if (sLen < -2) sl@0: { sl@0: RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); sl@0: goto err; sl@0: } sl@0: sl@0: MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; sl@0: emLen = RSA_size(rsa); sl@0: if (MSBits == 0) sl@0: { sl@0: *EM++ = 0; sl@0: emLen--; sl@0: } sl@0: if (sLen == -2) sl@0: { sl@0: sLen = emLen - hLen - 2; sl@0: } sl@0: else if (emLen < (hLen + sLen + 2)) sl@0: { sl@0: RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, sl@0: RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); sl@0: goto err; sl@0: } sl@0: if (sLen > 0) sl@0: { sl@0: salt = OPENSSL_malloc(sLen); sl@0: if (!salt) sl@0: { sl@0: RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, sl@0: ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: if (!RAND_bytes(salt, sLen)) sl@0: goto err; sl@0: } sl@0: maskedDBLen = emLen - hLen - 1; sl@0: H = EM + maskedDBLen; sl@0: EVP_MD_CTX_init(&ctx); sl@0: EVP_DigestInit_ex(&ctx, Hash, NULL); sl@0: EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); sl@0: EVP_DigestUpdate(&ctx, mHash, hLen); sl@0: if (sLen) sl@0: EVP_DigestUpdate(&ctx, salt, sLen); sl@0: EVP_DigestFinal(&ctx, H, NULL); sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: sl@0: /* Generate dbMask in place then perform XOR on it */ sl@0: PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash); sl@0: sl@0: p = EM; sl@0: sl@0: /* Initial PS XORs with all zeroes which is a NOP so just update sl@0: * pointer. Note from a test above this value is guaranteed to sl@0: * be non-negative. sl@0: */ sl@0: p += emLen - sLen - hLen - 2; sl@0: *p++ ^= 0x1; sl@0: if (sLen > 0) sl@0: { sl@0: for (i = 0; i < sLen; i++) sl@0: *p++ ^= salt[i]; sl@0: } sl@0: if (MSBits) sl@0: EM[0] &= 0xFF >> (8 - MSBits); sl@0: sl@0: /* H is already in place so just set final 0xbc */ sl@0: sl@0: EM[emLen - 1] = 0xbc; sl@0: sl@0: ret = 1; sl@0: sl@0: err: sl@0: if (salt) sl@0: OPENSSL_free(salt); sl@0: sl@0: return ret; sl@0: sl@0: } sl@0: sl@0: #if defined(_MSC_VER) sl@0: #pragma optimize("",on) sl@0: #endif