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