File openssl-Extended-OAEP-support.patch of Package compat-openssl098.14113

From 271fef0ef39a1c0cb5233a5adf3ff8733abb375e Mon Sep 17 00:00:00 2001
From: "Dr. Stephen Henson" <steve@openssl.org>
Date: Tue, 21 May 2013 23:55:50 +0100
Subject: [PATCH] Exetended OAEP support.

Extend OAEP support. Generalise the OAEP padding functions to support
arbitrary digests. Extend EVP_PKEY RSA method to handle the new OAEP
padding functions and add ctrls to set the additional parameters.
---
 CHANGES                |   4 ++
 crypto/rsa/rsa.h       |  28 +++++++++-
 crypto/rsa/rsa_err.c   |   7 ++-
 crypto/rsa/rsa_oaep.c  | 105 ++++++++++++++++++++++-------------
 crypto/rsa/rsa_pmeth.c | 121 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 217 insertions(+), 48 deletions(-)

Index: openssl-0.9.8j/crypto/rsa/rsa.h
===================================================================
--- openssl-0.9.8j.orig/crypto/rsa/rsa.h
+++ openssl-0.9.8j/crypto/rsa/rsa.h
@@ -237,6 +237,26 @@ struct rsa_st
                                                 */
 #endif
 
+#define	 EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md)	\
+	        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,	 \
+		EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \
+		EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md)
+
+#define	 EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md)	\
+		EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
+		EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md)
+
+#define	 EVP_PKEY_CTX_get_rsa_mgf1_md(ctx, pmd)	\
+		EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, \
+		EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \
+		EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)pmd)
+
+#define	 EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, l, llen)	\
+		EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,  \
+				EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen, (void *)l)
+
+#define EVP_PKEY_CTRL_RSA_OAEP_MD	(EVP_PKEY_ALG_CTRL + 9)
+#define EVP_PKEY_CTRL_RSA_OAEP_LABEL	(EVP_PKEY_ALG_CTRL + 10)
 
 #define RSA_PKCS1_PADDING	1
 #define RSA_SSLV23_PADDING	2
@@ -364,6 +384,14 @@ int RSA_padding_add_PKCS1_OAEP(unsigned
 int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
 	const unsigned char *f,int fl,int rsa_len,
 	const unsigned char *p,int pl);
+int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+	const unsigned char *from, int flen,
+	const unsigned char *param, int plen,
+	const EVP_MD *md, const EVP_MD *mgf1md);
+int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+	const unsigned char *from, int flen, int num,
+	const unsigned char *param, int plen,
+	const EVP_MD *md, const EVP_MD *mgf1md);
 int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
 	const unsigned char *f,int fl);
 int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
@@ -421,6 +449,7 @@ void ERR_load_RSA_strings(void);
 #define RSA_F_RSA_NULL_PUBLIC_ENCRYPT			 135
 #define RSA_F_RSA_PADDING_ADD_NONE			 107
 #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP		 121
+#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1		 154
 #define RSA_F_RSA_PADDING_ADD_PKCS1_PSS			 125
 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1		 108
 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2		 109
@@ -428,6 +457,7 @@ void ERR_load_RSA_strings(void);
 #define RSA_F_RSA_PADDING_ADD_X931			 127
 #define RSA_F_RSA_PADDING_CHECK_NONE			 111
 #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP		 122
+#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1	         153
 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1		 112
 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2		 113
 #define RSA_F_RSA_PADDING_CHECK_SSLV23			 114
@@ -464,6 +494,7 @@ void ERR_load_RSA_strings(void);
 #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D			 125
 #define RSA_R_D_E_NOT_CONGRUENT_TO_1			 123
 #define RSA_R_FIRST_OCTET_INVALID			 133
+#define RSA_R_INVALID_DIGEST                             157
 #define RSA_R_INVALID_HEADER				 137
 #define RSA_R_INVALID_MESSAGE_LENGTH			 131
 #define RSA_R_INVALID_PADDING				 138
Index: openssl-0.9.8j/crypto/rsa/rsa_err.c
===================================================================
--- openssl-0.9.8j.orig/crypto/rsa/rsa_err.c
+++ openssl-0.9.8j/crypto/rsa/rsa_err.c
@@ -90,6 +90,7 @@ static ERR_STRING_DATA RSA_str_functs[]=
 {ERR_FUNC(RSA_F_RSA_NULL_PUBLIC_ENCRYPT),	"RSA_NULL_PUBLIC_ENCRYPT"},
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_NONE),	"RSA_padding_add_none"},
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP),	"RSA_padding_add_PKCS1_OAEP"},
+{ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1),	"RSA_padding_add_PKCS1_OAEP_mgf1"},
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_PSS),	"RSA_padding_add_PKCS1_PSS"},
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1),	"RSA_padding_add_PKCS1_type_1"},
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2),	"RSA_padding_add_PKCS1_type_2"},
@@ -97,6 +98,7 @@ static ERR_STRING_DATA RSA_str_functs[]=
 {ERR_FUNC(RSA_F_RSA_PADDING_ADD_X931),	"RSA_padding_add_X931"},
 {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_NONE),	"RSA_padding_check_none"},
 {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP),	"RSA_padding_check_PKCS1_OAEP"},
+{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1),	"RSA_padding_check_PKCS1_OAEP_mgf1"},
 {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1),	"RSA_padding_check_PKCS1_type_1"},
 {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2),	"RSA_padding_check_PKCS1_type_2"},
 {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_SSLV23),	"RSA_padding_check_SSLv23"},
@@ -136,6 +138,7 @@ static ERR_STRING_DATA RSA_str_reasons[]
 {ERR_REASON(RSA_R_DMQ1_NOT_CONGRUENT_TO_D),"dmq1 not congruent to d"},
 {ERR_REASON(RSA_R_D_E_NOT_CONGRUENT_TO_1),"d e not congruent to 1"},
 {ERR_REASON(RSA_R_FIRST_OCTET_INVALID)   ,"first octet invalid"},
+{ERR_REASON(RSA_R_INVALID_DIGEST)        ,"invalid digest"},
 {ERR_REASON(RSA_R_INVALID_HEADER)        ,"invalid header"},
 {ERR_REASON(RSA_R_INVALID_MESSAGE_LENGTH),"invalid message length"},
 {ERR_REASON(RSA_R_INVALID_PADDING)       ,"invalid padding"},
Index: openssl-0.9.8j/crypto/rsa/rsa_oaep.c
===================================================================
--- openssl-0.9.8j.orig/crypto/rsa/rsa_oaep.c
+++ openssl-0.9.8j/crypto/rsa/rsa_oaep.c
@@ -28,47 +28,62 @@
 #include <openssl/rand.h>
 #include <openssl/sha.h>
 
-int MGF1(unsigned char *mask, long len,
-	const 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)
 	{
+		return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
+						       param, plen, NULL, NULL);
+	}
+
+int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+				    const unsigned char *from, int flen,
+				    const unsigned char *param, int plen,
+				    const EVP_MD *md, const EVP_MD *mgf1md)
+{
 	int i, emlen = tlen - 1;
 	unsigned char *db, *seed;
-	unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
+	unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
+	int mdlen;
+
+	if (md == NULL)
+		md = EVP_sha1();
+	if (mgf1md == NULL)
+		mgf1md = md;
+
+	mdlen = EVP_MD_size(md);
 
-	if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
+	if (flen > emlen - 2 * mdlen - 1)
 		{
-		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
+		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
 		   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);
+		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, RSA_R_KEY_SIZE_TOO_SMALL);
 		return 0;
 		}
 
-	dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
+	dbmask = OPENSSL_malloc(emlen - mdlen);
 	if (dbmask == NULL)
 		{
-		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
+		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
 		return 0;
 		}
 
 	to[0] = 0;
 	seed = to + 1;
-	db = to + SHA_DIGEST_LENGTH + 1;
+	db = to + mdlen + 1;
 
-	EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
-	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);
-	if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
+	if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
+		return 0;
+	memset(db + mdlen, 0,
+               emlen - flen - 2 * mdlen - 1);
+	db[emlen - flen - mdlen - 1] = 0x01;
+	memcpy(db + emlen - flen - mdlen, from, (unsigned int) flen);
+	if (RAND_bytes(seed, mdlen) <= 0)
 		return 0;
 #ifdef PKCS_TESTVECT
 	memcpy(seed,
@@ -76,12 +91,15 @@ int RSA_padding_add_PKCS1_OAEP(unsigned
 	   20);
 #endif
 
-	MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
-	for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
+	if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
+		return 0;
+	for (i = 0; i < emlen - mdlen; i++)
 		db[i] ^= dbmask[i];
 
-	MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
-	for (i = 0; i < SHA_DIGEST_LENGTH; i++)
+	if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
+		return 0;
+
+	for (i = 0; i < mdlen; i++)
 		seed[i] ^= seedmask[i];
 
 	OPENSSL_free(dbmask);
@@ -92,14 +110,32 @@ int RSA_padding_check_PKCS1_OAEP(unsigne
 	const unsigned char *from, int flen, int num,
 	const unsigned char *param, int plen)
 	{
+	return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from , flen, num,
+							param, plen,
+							NULL, NULL);
+	}
+
+int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+	const unsigned char *from, int flen, int num,
+	const unsigned char *param, int plen,
+	const EVP_MD *md, const EVP_MD *mgf1md)
+	{
 	int i, dblen, mlen = -1;
 	const unsigned char *maskeddb;
 	int lzero;
-	unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
+	unsigned char *db = NULL, seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
 	unsigned char *padded_from;
 	int bad = 0;
+	int mdlen;
+
+	if (md == NULL)
+		md = EVP_sha1();
+	if (mgf1md == NULL)
+		mgf1md = md;
+
+	mdlen = EVP_MD_size(md);
 
-	if (--num < 2 * SHA_DIGEST_LENGTH + 1)
+	if (--num < 2 * mdlen + 1)
 		/* 'num' is the length of the modulus, i.e. does not depend on the
 		 * particular ciphertext. */
 		goto decoding_err;
@@ -117,11 +153,11 @@ int RSA_padding_check_PKCS1_OAEP(unsigne
 		flen = num; /* don't overflow the memcpy to padded_from */
 		}
 
-	dblen = num - SHA_DIGEST_LENGTH;
+	dblen = num - mdlen;
 	db = OPENSSL_malloc(dblen + num);
 	if (db == NULL)
 		{
-		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
+		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
 		return -1;
 		}
 
@@ -131,23 +167,26 @@ int RSA_padding_check_PKCS1_OAEP(unsigne
 	memset(padded_from, 0, lzero);
 	memcpy(padded_from + lzero, from, flen);
 
-	maskeddb = padded_from + SHA_DIGEST_LENGTH;
+	maskeddb = padded_from + mdlen;
 
-	MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
-	for (i = 0; i < SHA_DIGEST_LENGTH; i++)
+	if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
+		return -1;
+	for (i = 0; i < mdlen; i++)
 		seed[i] ^= padded_from[i];
   
-	MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
+	if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
+		return -1;
 	for (i = 0; i < dblen; i++)
 		db[i] ^= maskeddb[i];
 
-	EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
+	if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
+		return -1;
 
-	if (CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
+	if (CRYPTO_memcmp(db, phash, mdlen) != 0 || bad)
 		goto decoding_err;
 	else
 		{
-		for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
+		for (i = mdlen; i < dblen; i++)
 			if (db[i] != 0x00)
 				break;
 		if (i == dblen || db[i] != 0x01)
@@ -159,7 +198,7 @@ int RSA_padding_check_PKCS1_OAEP(unsigne
 			mlen = dblen - ++i;
 			if (tlen < mlen)
 				{
-				RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
+				RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
 				mlen = -1;
 				}
 			else
@@ -172,7 +211,7 @@ int RSA_padding_check_PKCS1_OAEP(unsigne
 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);
+	RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_OAEP_DECODING_ERROR);
 	if (db != NULL) OPENSSL_free(db);
 	return -1;
 	}
@@ -213,8 +252,4 @@ int PKCS1_MGF1(unsigned char *mask, long
 	return 0;
 	}
 
-int MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen)
-	{
-	return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
-	}
 #endif
openSUSE Build Service is sponsored by