- fix coverity issues 966593-966596
[openssl.git] / crypto / rsa / rsa_pss.c
1 /* rsa_pss.c */
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 #define OPENSSL_FIPSAPI
60
61 #include <stdio.h>
62 #include "cryptlib.h"
63 #include <openssl/bn.h>
64 #include <openssl/rsa.h>
65 #include <openssl/evp.h>
66 #include <openssl/rand.h>
67 #include <openssl/sha.h>
68 #include "rsa_locl.h"
69
70 #ifdef OPENSSL_FIPS
71 #include <openssl/fips.h>
72 #endif
73
74 __fips_constseg
75 static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
76
77 #if defined(_MSC_VER) && defined(_ARM_)
78 #pragma optimize("g", off)
79 #endif
80
81 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
82                         const EVP_MD *Hash, const unsigned char *EM, int sLen)
83         {
84         return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
85         }
86
87 int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
88                         const EVP_MD *Hash, const EVP_MD *mgf1Hash,
89                         const unsigned char *EM, int sLen)
90         {
91         int i;
92         int ret = 0;
93         int hLen, maskedDBLen, MSBits, emLen;
94         const unsigned char *H;
95         unsigned char *DB = NULL;
96         EVP_MD_CTX ctx;
97         unsigned char H_[EVP_MAX_MD_SIZE];
98         EVP_MD_CTX_init(&ctx);
99
100         if (mgf1Hash == NULL)
101                 mgf1Hash = Hash;
102
103         hLen = M_EVP_MD_size(Hash);
104         if (hLen < 0)
105                 goto err;
106         /*
107          * Negative sLen has special meanings:
108          *      -1      sLen == hLen
109          *      -2      salt length is autorecovered from signature
110          *      -N      reserved
111          */
112         if      (sLen == -1)    sLen = hLen;
113         else if (sLen == -2)    sLen = -2;
114         else if (sLen < -2)
115                 {
116                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
117                 goto err;
118                 }
119
120         MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
121         emLen = RSA_size(rsa);
122         if (EM[0] & (0xFF << MSBits))
123                 {
124                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID);
125                 goto err;
126                 }
127         if (MSBits == 0)
128                 {
129                 EM++;
130                 emLen--;
131                 }
132         if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */
133                 {
134                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
135                 goto err;
136                 }
137         if (EM[emLen - 1] != 0xbc)
138                 {
139                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID);
140                 goto err;
141                 }
142         maskedDBLen = emLen - hLen - 1;
143         H = EM + maskedDBLen;
144         DB = OPENSSL_malloc(maskedDBLen);
145         if (!DB)
146                 {
147                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE);
148                 goto err;
149                 }
150         if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
151                 goto err;
152         for (i = 0; i < maskedDBLen; i++)
153                 DB[i] ^= EM[i];
154         if (MSBits)
155                 DB[0] &= 0xFF >> (8 - MSBits);
156         for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ;
157         if (DB[i++] != 0x1)
158                 {
159                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED);
160                 goto err;
161                 }
162         if (sLen >= 0 && (maskedDBLen - i) != sLen)
163                 {
164                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
165                 goto err;
166                 }
167         if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
168                 || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
169                 || !EVP_DigestUpdate(&ctx, mHash, hLen))
170                 goto err;
171         if (maskedDBLen - i)
172                 {
173                 if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i))
174                         goto err;
175                 }
176         if (!EVP_DigestFinal_ex(&ctx, H_, NULL))
177                 goto err;
178         if (memcmp(H_, H, hLen))
179                 {
180                 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE);
181                 ret = 0;
182                 }
183         else 
184                 ret = 1;
185
186         err:
187         if (DB)
188                 OPENSSL_free(DB);
189         EVP_MD_CTX_cleanup(&ctx);
190
191         return ret;
192
193         }
194
195 int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
196                         const unsigned char *mHash,
197                         const EVP_MD *Hash, int sLen)
198         {
199         return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
200         }
201
202 int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
203                         const unsigned char *mHash,
204                         const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen)
205         {
206         int i;
207         int ret = 0;
208         int hLen, maskedDBLen, MSBits, emLen;
209         unsigned char *H, *salt = NULL, *p;
210         EVP_MD_CTX ctx;
211
212         if (mgf1Hash == NULL)
213                 mgf1Hash = Hash;
214
215         hLen = M_EVP_MD_size(Hash);
216         if (hLen < 0)
217                 goto err;
218         /*
219          * Negative sLen has special meanings:
220          *      -1      sLen == hLen
221          *      -2      salt length is maximized
222          *      -N      reserved
223          */
224         if      (sLen == -1)    sLen = hLen;
225         else if (sLen == -2)    sLen = -2;
226         else if (sLen < -2)
227                 {
228                 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
229                 goto err;
230                 }
231
232         MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
233         emLen = RSA_size(rsa);
234         if (MSBits == 0)
235                 {
236                 *EM++ = 0;
237                 emLen--;
238                 }
239         if (sLen == -2)
240                 {
241                 sLen = emLen - hLen - 2;
242                 }
243         else if (emLen < (hLen + sLen + 2))
244                 {
245                 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
246                 goto err;
247                 }
248         if (sLen > 0)
249                 {
250                 salt = OPENSSL_malloc(sLen);
251                 if (!salt)
252                         {
253                         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,ERR_R_MALLOC_FAILURE);
254                         goto err;
255                         }
256                 if (RAND_bytes(salt, sLen) <= 0)
257                         goto err;
258                 }
259         maskedDBLen = emLen - hLen - 1;
260         H = EM + maskedDBLen;
261         EVP_MD_CTX_init(&ctx);
262         if (!EVP_DigestInit_ex(&ctx, Hash, NULL)
263                 || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes)
264                 || !EVP_DigestUpdate(&ctx, mHash, hLen))
265                 goto err;
266         if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen))
267                 goto err;
268         if (!EVP_DigestFinal_ex(&ctx, H, NULL))
269                 goto err;
270         EVP_MD_CTX_cleanup(&ctx);
271
272         /* Generate dbMask in place then perform XOR on it */
273         if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
274                 goto err;
275
276         p = EM;
277
278         /* Initial PS XORs with all zeroes which is a NOP so just update
279          * pointer. Note from a test above this value is guaranteed to
280          * be non-negative.
281          */
282         p += emLen - sLen - hLen - 2;
283         *p++ ^= 0x1;
284         if (sLen > 0)
285                 {
286                 for (i = 0; i < sLen; i++)
287                         *p++ ^= salt[i];
288                 }
289         if (MSBits)
290                 EM[0] &= 0xFF >> (8 - MSBits);
291
292         /* H is already in place so just set final 0xbc */
293
294         EM[emLen - 1] = 0xbc;
295
296         ret = 1;
297
298         err:
299         if (salt)
300                 OPENSSL_free(salt);
301
302         return ret;
303
304         }
305
306 #if defined(_MSC_VER)
307 #pragma optimize("",on)
308 #endif