xref: /openbsd/lib/libcrypto/rsa/rsa_pss.c (revision c4bb5f42)
1 /* $OpenBSD: rsa_pss.c,v 1.19 2024/03/26 05:26:27 joshua 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_local.h"
70 #include "rsa_local.h"
71 
72 static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
73 
74 int
RSA_verify_PKCS1_PSS(RSA * rsa,const unsigned char * mHash,const EVP_MD * Hash,const unsigned char * EM,int sLen)75 RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, const EVP_MD *Hash,
76     const unsigned char *EM, int sLen)
77 {
78 	return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
79 }
80 LCRYPTO_ALIAS(RSA_verify_PKCS1_PSS);
81 
82 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)83 RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
84     const EVP_MD *Hash, const EVP_MD *mgf1Hash, const unsigned char *EM,
85     int sLen)
86 {
87 	int i;
88 	int ret = 0;
89 	int hLen, maskedDBLen, MSBits, emLen;
90 	const unsigned char *H;
91 	unsigned char *DB = NULL;
92 	EVP_MD_CTX *md_ctx;
93 	unsigned char H_[EVP_MAX_MD_SIZE];
94 
95 	if ((md_ctx = EVP_MD_CTX_new()) == NULL)
96 		goto err;
97 
98 	if (mgf1Hash == NULL)
99 		mgf1Hash = Hash;
100 
101 	hLen = EVP_MD_size(Hash);
102 	if (hLen < 0)
103 		goto err;
104 	/*
105 	 * Negative sLen has special meanings:
106 	 *	-1	sLen == hLen
107 	 *	-2	salt length is autorecovered from signature
108 	 *	-N	reserved
109 	 */
110 	if (sLen == -1)
111 		sLen = hLen;
112 	else if (sLen == -2)
113 		sLen = -2;
114 	else if (sLen < -2) {
115 		RSAerror(RSA_R_SLEN_CHECK_FAILED);
116 		goto err;
117 	}
118 
119 	MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
120 	emLen = RSA_size(rsa);
121 	if (EM[0] & (0xFF << MSBits)) {
122 		RSAerror(RSA_R_FIRST_OCTET_INVALID);
123 		goto err;
124 	}
125 	if (MSBits == 0) {
126 		EM++;
127 		emLen--;
128 	}
129 	if (emLen < (hLen + sLen + 2)) {
130 		/* sLen can be small negative */
131 		RSAerror(RSA_R_DATA_TOO_LARGE);
132 		goto err;
133 	}
134 	if (EM[emLen - 1] != 0xbc) {
135 		RSAerror(RSA_R_LAST_OCTET_INVALID);
136 		goto err;
137 	}
138 	maskedDBLen = emLen - hLen - 1;
139 	H = EM + maskedDBLen;
140 	DB = malloc(maskedDBLen);
141 	if (!DB) {
142 		RSAerror(ERR_R_MALLOC_FAILURE);
143 		goto err;
144 	}
145 	if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
146 		goto err;
147 	for (i = 0; i < maskedDBLen; i++)
148 		DB[i] ^= EM[i];
149 	if (MSBits)
150 		DB[0] &= 0xFF >> (8 - MSBits);
151 	for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++)
152 		;
153 	if (DB[i++] != 0x1) {
154 		RSAerror(RSA_R_SLEN_RECOVERY_FAILED);
155 		goto err;
156 	}
157 	if (sLen >= 0 && (maskedDBLen - i) != sLen) {
158 		RSAerror(RSA_R_SLEN_CHECK_FAILED);
159 		goto err;
160 	}
161 	if (!EVP_DigestInit_ex(md_ctx, Hash, NULL) ||
162 	    !EVP_DigestUpdate(md_ctx, zeroes, sizeof zeroes) ||
163 	    !EVP_DigestUpdate(md_ctx, mHash, hLen))
164 		goto err;
165 	if (maskedDBLen - i) {
166 		if (!EVP_DigestUpdate(md_ctx, DB + i, maskedDBLen - i))
167 			goto err;
168 	}
169 	if (!EVP_DigestFinal_ex(md_ctx, H_, NULL))
170 		goto err;
171 	if (timingsafe_bcmp(H_, H, hLen)) {
172 		RSAerror(RSA_R_BAD_SIGNATURE);
173 		ret = 0;
174 	} else {
175 		ret = 1;
176 	}
177 
178  err:
179 	free(DB);
180 	EVP_MD_CTX_free(md_ctx);
181 
182 	return ret;
183 }
184 LCRYPTO_ALIAS(RSA_verify_PKCS1_PSS_mgf1);
185 
186 int
RSA_padding_add_PKCS1_PSS(RSA * rsa,unsigned char * EM,const unsigned char * mHash,const EVP_MD * Hash,int sLen)187 RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
188     const unsigned char *mHash, const EVP_MD *Hash, int sLen)
189 {
190 	return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
191 }
192 LCRYPTO_ALIAS(RSA_padding_add_PKCS1_PSS);
193 
194 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)195 RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
196     const unsigned char *mHash, const EVP_MD *Hash, const EVP_MD *mgf1Hash,
197     int sLen)
198 {
199 	int i;
200 	int ret = 0;
201 	int hLen, maskedDBLen, MSBits, emLen;
202 	unsigned char *H, *salt = NULL, *p;
203 	EVP_MD_CTX *md_ctx;
204 
205 	if ((md_ctx = EVP_MD_CTX_new()) == NULL)
206 		goto err;
207 
208 	if (mgf1Hash == NULL)
209 		mgf1Hash = Hash;
210 
211 	hLen = EVP_MD_size(Hash);
212 	if (hLen < 0)
213 		goto err;
214 	/*
215 	 * Negative sLen has special meanings:
216 	 *	-1	sLen == hLen
217 	 *	-2	salt length is maximized
218 	 *	-N	reserved
219 	 */
220 	if (sLen == -1)
221 		sLen = hLen;
222 	else if (sLen == -2)
223 		sLen = -2;
224 	else if (sLen < -2) {
225 		RSAerror(RSA_R_SLEN_CHECK_FAILED);
226 		goto err;
227 	}
228 
229 	MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
230 	emLen = RSA_size(rsa);
231 	if (MSBits == 0) {
232 		*EM++ = 0;
233 		emLen--;
234 	}
235 	if (sLen == -2)
236 		sLen = emLen - hLen - 2;
237 	else if (emLen < (hLen + sLen + 2)) {
238 		RSAerror(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
239 		goto err;
240 	}
241 	if (sLen > 0) {
242 		salt = malloc(sLen);
243 		if (!salt) {
244 			RSAerror(ERR_R_MALLOC_FAILURE);
245 			goto err;
246 		}
247 		arc4random_buf(salt, sLen);
248 	}
249 	maskedDBLen = emLen - hLen - 1;
250 	H = EM + maskedDBLen;
251 	if (!EVP_DigestInit_ex(md_ctx, Hash, NULL) ||
252 	    !EVP_DigestUpdate(md_ctx, zeroes, sizeof zeroes) ||
253 	    !EVP_DigestUpdate(md_ctx, mHash, hLen))
254 		goto err;
255 	if (sLen && !EVP_DigestUpdate(md_ctx, salt, sLen))
256 		goto err;
257 	if (!EVP_DigestFinal_ex(md_ctx, H, NULL))
258 		goto err;
259 
260 	/* Generate dbMask in place then perform XOR on it */
261 	if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
262 		goto err;
263 
264 	p = EM;
265 
266 	/*
267 	 * Initial PS XORs with all zeroes which is a NOP so just update
268 	 * pointer. Note from a test above this value is guaranteed to
269 	 * be non-negative.
270 	 */
271 	p += emLen - sLen - hLen - 2;
272 	*p++ ^= 0x1;
273 	if (sLen > 0) {
274 		for (i = 0; i < sLen; i++)
275 			*p++ ^= salt[i];
276 	}
277 	if (MSBits)
278 		EM[0] &= 0xFF >> (8 - MSBits);
279 
280 	/* H is already in place so just set final 0xbc */
281 	EM[emLen - 1] = 0xbc;
282 
283 	ret = 1;
284 
285 err:
286 	free(salt);
287 	EVP_MD_CTX_free(md_ctx);
288 
289 	return ret;
290 }
291 LCRYPTO_ALIAS(RSA_padding_add_PKCS1_PSS_mgf1);
292