RSA PSS ASN1 signing method
[openssl.git] / crypto / rsa / rsa_pss.c
index ac211e2ffe0121028a94f2d897347c9354c3c3d3..4f87a2939d6eecb181009beebd14354fc3b4143d 100644 (file)
@@ -63,6 +63,7 @@
 #include <openssl/evp.h>
 #include <openssl/rand.h>
 #include <openssl/sha.h>
+#include "rsa_locl.h"
 
 static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
 
@@ -73,6 +74,13 @@ static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
                        const EVP_MD *Hash, const unsigned char *EM, int sLen)
        {
+       return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
+       }
+
+int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
+                       const EVP_MD *Hash, const EVP_MD *mgf1Hash,
+                       const unsigned char *EM, int sLen)
+       {
        int i;
        int ret = 0;
        int hLen, maskedDBLen, MSBits, emLen;
@@ -80,6 +88,10 @@ int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
        unsigned char *DB = NULL;
        EVP_MD_CTX ctx;
        unsigned char H_[EVP_MAX_MD_SIZE];
+       EVP_MD_CTX_init(&ctx);
+
+       if (mgf1Hash == NULL)
+               mgf1Hash = Hash;
 
        hLen = EVP_MD_size(Hash);
        if (hLen < 0)
@@ -128,7 +140,7 @@ int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
                RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE);
                goto err;
                }
-       if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash) < 0)
+       if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
                goto err;
        for (i = 0; i < maskedDBLen; i++)
                DB[i] ^= EM[i];
@@ -145,14 +157,17 @@ int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
                RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
                goto err;
                }
-       EVP_MD_CTX_init(&ctx);
-       EVP_DigestInit_ex(&ctx, Hash, NULL);
-       EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
-       EVP_DigestUpdate(&ctx, mHash, hLen);
+       if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
+               || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
+               || !EVP_DigestUpdate(&ctx, mHash, hLen))
+               goto err;
        if (maskedDBLen - i)
-               EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i);
-       EVP_DigestFinal(&ctx, H_, NULL);
-       EVP_MD_CTX_cleanup(&ctx);
+               {
+               if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i))
+                       goto err;
+               }
+       if (!EVP_DigestFinal(&ctx, H_, NULL))
+               goto err;
        if (memcmp(H_, H, hLen))
                {
                RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE);
@@ -164,6 +179,7 @@ int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
        err:
        if (DB)
                OPENSSL_free(DB);
+       EVP_MD_CTX_cleanup(&ctx);
 
        return ret;
 
@@ -173,12 +189,22 @@ int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
                        const unsigned char *mHash,
                        const EVP_MD *Hash, int sLen)
        {
+       return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
+       }
+
+int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
+                       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;
        EVP_MD_CTX ctx;
 
+       if (mgf1Hash == NULL)
+               mgf1Hash = Hash;
+
        hLen = EVP_MD_size(Hash);
        if (hLen < 0)
                goto err;
@@ -228,16 +254,18 @@ int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
        maskedDBLen = emLen - hLen - 1;
        H = EM + maskedDBLen;
        EVP_MD_CTX_init(&ctx);
-       EVP_DigestInit_ex(&ctx, Hash, NULL);
-       EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
-       EVP_DigestUpdate(&ctx, mHash, hLen);
-       if (sLen)
-               EVP_DigestUpdate(&ctx, salt, sLen);
-       EVP_DigestFinal(&ctx, H, NULL);
+       if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
+               || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
+               || !EVP_DigestUpdate(&ctx, mHash, hLen))
+               goto err;
+       if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen))
+               goto err;
+       if (!EVP_DigestFinal(&ctx, H, NULL))
+               goto err;
        EVP_MD_CTX_cleanup(&ctx);
 
        /* Generate dbMask in place then perform XOR on it */
-       if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash))
+       if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
                goto err;
 
        p = EM;