xref: /onnv-gate/usr/src/common/openssl/crypto/rsa/rsa_pss.c (revision 2139:6243c3338933)
1*2139Sjp161948 /* rsa_pss.c */
2*2139Sjp161948 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*2139Sjp161948  * project 2005.
4*2139Sjp161948  */
5*2139Sjp161948 /* ====================================================================
6*2139Sjp161948  * Copyright (c) 2005 The OpenSSL Project.  All rights reserved.
7*2139Sjp161948  *
8*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
9*2139Sjp161948  * modification, are permitted provided that the following conditions
10*2139Sjp161948  * are met:
11*2139Sjp161948  *
12*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
13*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
14*2139Sjp161948  *
15*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
16*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in
17*2139Sjp161948  *    the documentation and/or other materials provided with the
18*2139Sjp161948  *    distribution.
19*2139Sjp161948  *
20*2139Sjp161948  * 3. All advertising materials mentioning features or use of this
21*2139Sjp161948  *    software must display the following acknowledgment:
22*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
23*2139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*2139Sjp161948  *
25*2139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*2139Sjp161948  *    endorse or promote products derived from this software without
27*2139Sjp161948  *    prior written permission. For written permission, please contact
28*2139Sjp161948  *    licensing@OpenSSL.org.
29*2139Sjp161948  *
30*2139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
31*2139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
32*2139Sjp161948  *    permission of the OpenSSL Project.
33*2139Sjp161948  *
34*2139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
35*2139Sjp161948  *    acknowledgment:
36*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
37*2139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*2139Sjp161948  *
39*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*2139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*2139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*2139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*2139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*2139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*2139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*2139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*2139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*2139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*2139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*2139Sjp161948  * ====================================================================
52*2139Sjp161948  *
53*2139Sjp161948  * This product includes cryptographic software written by Eric Young
54*2139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
55*2139Sjp161948  * Hudson (tjh@cryptsoft.com).
56*2139Sjp161948  *
57*2139Sjp161948  */
58*2139Sjp161948 
59*2139Sjp161948 #include <stdio.h>
60*2139Sjp161948 #include "cryptlib.h"
61*2139Sjp161948 #include <openssl/bn.h>
62*2139Sjp161948 #include <openssl/rsa.h>
63*2139Sjp161948 #include <openssl/evp.h>
64*2139Sjp161948 #include <openssl/rand.h>
65*2139Sjp161948 #include <openssl/sha.h>
66*2139Sjp161948 
67*2139Sjp161948 static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
68*2139Sjp161948 
69*2139Sjp161948 #if defined(_MSC_VER) && defined(_ARM_)
70*2139Sjp161948 #pragma optimize("g", off)
71*2139Sjp161948 #endif
72*2139Sjp161948 
RSA_verify_PKCS1_PSS(RSA * rsa,const unsigned char * mHash,const EVP_MD * Hash,const unsigned char * EM,int sLen)73*2139Sjp161948 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
74*2139Sjp161948 			const EVP_MD *Hash, const unsigned char *EM, int sLen)
75*2139Sjp161948 	{
76*2139Sjp161948 	int i;
77*2139Sjp161948 	int ret = 0;
78*2139Sjp161948 	int hLen, maskedDBLen, MSBits, emLen;
79*2139Sjp161948 	const unsigned char *H;
80*2139Sjp161948 	unsigned char *DB = NULL;
81*2139Sjp161948 	EVP_MD_CTX ctx;
82*2139Sjp161948 	unsigned char H_[EVP_MAX_MD_SIZE];
83*2139Sjp161948 
84*2139Sjp161948 	hLen = EVP_MD_size(Hash);
85*2139Sjp161948 	/*
86*2139Sjp161948 	 * Negative sLen has special meanings:
87*2139Sjp161948 	 *	-1	sLen == hLen
88*2139Sjp161948 	 *	-2	salt length is autorecovered from signature
89*2139Sjp161948 	 *	-N	reserved
90*2139Sjp161948 	 */
91*2139Sjp161948 	if      (sLen == -1)	sLen = hLen;
92*2139Sjp161948 	else if (sLen == -2)	sLen = -2;
93*2139Sjp161948 	else if (sLen < -2)
94*2139Sjp161948 		{
95*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
96*2139Sjp161948 		goto err;
97*2139Sjp161948 		}
98*2139Sjp161948 
99*2139Sjp161948 	MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
100*2139Sjp161948 	emLen = RSA_size(rsa);
101*2139Sjp161948 	if (EM[0] & (0xFF << MSBits))
102*2139Sjp161948 		{
103*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_FIRST_OCTET_INVALID);
104*2139Sjp161948 		goto err;
105*2139Sjp161948 		}
106*2139Sjp161948 	if (MSBits == 0)
107*2139Sjp161948 		{
108*2139Sjp161948 		EM++;
109*2139Sjp161948 		emLen--;
110*2139Sjp161948 		}
111*2139Sjp161948 	if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */
112*2139Sjp161948 		{
113*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_DATA_TOO_LARGE);
114*2139Sjp161948 		goto err;
115*2139Sjp161948 		}
116*2139Sjp161948 	if (EM[emLen - 1] != 0xbc)
117*2139Sjp161948 		{
118*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_LAST_OCTET_INVALID);
119*2139Sjp161948 		goto err;
120*2139Sjp161948 		}
121*2139Sjp161948 	maskedDBLen = emLen - hLen - 1;
122*2139Sjp161948 	H = EM + maskedDBLen;
123*2139Sjp161948 	DB = OPENSSL_malloc(maskedDBLen);
124*2139Sjp161948 	if (!DB)
125*2139Sjp161948 		{
126*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE);
127*2139Sjp161948 		goto err;
128*2139Sjp161948 		}
129*2139Sjp161948 	PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash);
130*2139Sjp161948 	for (i = 0; i < maskedDBLen; i++)
131*2139Sjp161948 		DB[i] ^= EM[i];
132*2139Sjp161948 	if (MSBits)
133*2139Sjp161948 		DB[0] &= 0xFF >> (8 - MSBits);
134*2139Sjp161948 	for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ;
135*2139Sjp161948 	if (DB[i++] != 0x1)
136*2139Sjp161948 		{
137*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_RECOVERY_FAILED);
138*2139Sjp161948 		goto err;
139*2139Sjp161948 		}
140*2139Sjp161948 	if (sLen >= 0 && (maskedDBLen - i) != sLen)
141*2139Sjp161948 		{
142*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
143*2139Sjp161948 		goto err;
144*2139Sjp161948 		}
145*2139Sjp161948 	EVP_MD_CTX_init(&ctx);
146*2139Sjp161948 	EVP_DigestInit_ex(&ctx, Hash, NULL);
147*2139Sjp161948 	EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
148*2139Sjp161948 	EVP_DigestUpdate(&ctx, mHash, hLen);
149*2139Sjp161948 	if (maskedDBLen - i)
150*2139Sjp161948 		EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i);
151*2139Sjp161948 	EVP_DigestFinal(&ctx, H_, NULL);
152*2139Sjp161948 	EVP_MD_CTX_cleanup(&ctx);
153*2139Sjp161948 	if (memcmp(H_, H, hLen))
154*2139Sjp161948 		{
155*2139Sjp161948 		RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE);
156*2139Sjp161948 		ret = 0;
157*2139Sjp161948 		}
158*2139Sjp161948 	else
159*2139Sjp161948 		ret = 1;
160*2139Sjp161948 
161*2139Sjp161948 	err:
162*2139Sjp161948 	if (DB)
163*2139Sjp161948 		OPENSSL_free(DB);
164*2139Sjp161948 
165*2139Sjp161948 	return ret;
166*2139Sjp161948 
167*2139Sjp161948 	}
168*2139Sjp161948 
RSA_padding_add_PKCS1_PSS(RSA * rsa,unsigned char * EM,const unsigned char * mHash,const EVP_MD * Hash,int sLen)169*2139Sjp161948 int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
170*2139Sjp161948 			const unsigned char *mHash,
171*2139Sjp161948 			const EVP_MD *Hash, int sLen)
172*2139Sjp161948 	{
173*2139Sjp161948 	int i;
174*2139Sjp161948 	int ret = 0;
175*2139Sjp161948 	int hLen, maskedDBLen, MSBits, emLen;
176*2139Sjp161948 	unsigned char *H, *salt = NULL, *p;
177*2139Sjp161948 	EVP_MD_CTX ctx;
178*2139Sjp161948 
179*2139Sjp161948 	hLen = EVP_MD_size(Hash);
180*2139Sjp161948 	/*
181*2139Sjp161948 	 * Negative sLen has special meanings:
182*2139Sjp161948 	 *	-1	sLen == hLen
183*2139Sjp161948 	 *	-2	salt length is maximized
184*2139Sjp161948 	 *	-N	reserved
185*2139Sjp161948 	 */
186*2139Sjp161948 	if      (sLen == -1)	sLen = hLen;
187*2139Sjp161948 	else if (sLen == -2)	sLen = -2;
188*2139Sjp161948 	else if (sLen < -2)
189*2139Sjp161948 		{
190*2139Sjp161948 		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
191*2139Sjp161948 		goto err;
192*2139Sjp161948 		}
193*2139Sjp161948 
194*2139Sjp161948 	MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
195*2139Sjp161948 	emLen = RSA_size(rsa);
196*2139Sjp161948 	if (MSBits == 0)
197*2139Sjp161948 		{
198*2139Sjp161948 		*EM++ = 0;
199*2139Sjp161948 		emLen--;
200*2139Sjp161948 		}
201*2139Sjp161948 	if (sLen == -2)
202*2139Sjp161948 		{
203*2139Sjp161948 		sLen = emLen - hLen - 2;
204*2139Sjp161948 		}
205*2139Sjp161948 	else if (emLen < (hLen + sLen + 2))
206*2139Sjp161948 		{
207*2139Sjp161948 		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
208*2139Sjp161948 		   RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
209*2139Sjp161948 		goto err;
210*2139Sjp161948 		}
211*2139Sjp161948 	if (sLen > 0)
212*2139Sjp161948 		{
213*2139Sjp161948 		salt = OPENSSL_malloc(sLen);
214*2139Sjp161948 		if (!salt)
215*2139Sjp161948 			{
216*2139Sjp161948 			RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
217*2139Sjp161948 		   		ERR_R_MALLOC_FAILURE);
218*2139Sjp161948 			goto err;
219*2139Sjp161948 			}
220*2139Sjp161948 		if (!RAND_bytes(salt, sLen))
221*2139Sjp161948 			goto err;
222*2139Sjp161948 		}
223*2139Sjp161948 	maskedDBLen = emLen - hLen - 1;
224*2139Sjp161948 	H = EM + maskedDBLen;
225*2139Sjp161948 	EVP_MD_CTX_init(&ctx);
226*2139Sjp161948 	EVP_DigestInit_ex(&ctx, Hash, NULL);
227*2139Sjp161948 	EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
228*2139Sjp161948 	EVP_DigestUpdate(&ctx, mHash, hLen);
229*2139Sjp161948 	if (sLen)
230*2139Sjp161948 		EVP_DigestUpdate(&ctx, salt, sLen);
231*2139Sjp161948 	EVP_DigestFinal(&ctx, H, NULL);
232*2139Sjp161948 	EVP_MD_CTX_cleanup(&ctx);
233*2139Sjp161948 
234*2139Sjp161948 	/* Generate dbMask in place then perform XOR on it */
235*2139Sjp161948 	PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash);
236*2139Sjp161948 
237*2139Sjp161948 	p = EM;
238*2139Sjp161948 
239*2139Sjp161948 	/* Initial PS XORs with all zeroes which is a NOP so just update
240*2139Sjp161948 	 * pointer. Note from a test above this value is guaranteed to
241*2139Sjp161948 	 * be non-negative.
242*2139Sjp161948 	 */
243*2139Sjp161948 	p += emLen - sLen - hLen - 2;
244*2139Sjp161948 	*p++ ^= 0x1;
245*2139Sjp161948 	if (sLen > 0)
246*2139Sjp161948 		{
247*2139Sjp161948 		for (i = 0; i < sLen; i++)
248*2139Sjp161948 			*p++ ^= salt[i];
249*2139Sjp161948 		}
250*2139Sjp161948 	if (MSBits)
251*2139Sjp161948 		EM[0] &= 0xFF >> (8 - MSBits);
252*2139Sjp161948 
253*2139Sjp161948 	/* H is already in place so just set final 0xbc */
254*2139Sjp161948 
255*2139Sjp161948 	EM[emLen - 1] = 0xbc;
256*2139Sjp161948 
257*2139Sjp161948 	ret = 1;
258*2139Sjp161948 
259*2139Sjp161948 	err:
260*2139Sjp161948 	if (salt)
261*2139Sjp161948 		OPENSSL_free(salt);
262*2139Sjp161948 
263*2139Sjp161948 	return ret;
264*2139Sjp161948 
265*2139Sjp161948 	}
266*2139Sjp161948 
267*2139Sjp161948 #if defined(_MSC_VER)
268*2139Sjp161948 #pragma optimize("",on)
269*2139Sjp161948 #endif
270