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