0a6178b0c40bbf745174f4d106e87cd81b91ef92
[openssl.git] / crypto / rsa / rsa_pss.c
1 /*
2  * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/evp.h>
15 #include <openssl/rand.h>
16 #include <openssl/sha.h>
17 #include "rsa_locl.h"
18
19 static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
20
21 #if defined(_MSC_VER) && defined(_ARM_)
22 # pragma optimize("g", off)
23 #endif
24
25 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
26                          const EVP_MD *Hash, const unsigned char *EM,
27                          int sLen)
28 {
29     return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
30 }
31
32 int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
33                               const EVP_MD *Hash, const EVP_MD *mgf1Hash,
34                               const unsigned char *EM, int sLen)
35 {
36     int i;
37     int ret = 0;
38     int hLen, maskedDBLen, MSBits, emLen;
39     const unsigned char *H;
40     unsigned char *DB = NULL;
41     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
42     unsigned char H_[EVP_MAX_MD_SIZE];
43
44     if (ctx == NULL)
45         goto err;
46
47     if (mgf1Hash == NULL)
48         mgf1Hash = Hash;
49
50     hLen = EVP_MD_size(Hash);
51     if (hLen < 0)
52         goto err;
53     /*-
54      * Negative sLen has special meanings:
55      *      -1      sLen == hLen
56      *      -2      salt length is autorecovered from signature
57      *      -N      reserved
58      */
59     if (sLen == RSA_PSS_SALTLEN_DIGEST)
60         sLen = hLen;
61     else if (sLen < RSA_PSS_SALTLEN_MAX) {
62         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
63         goto err;
64     }
65
66     MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
67     emLen = RSA_size(rsa);
68     if (EM[0] & (0xFF << MSBits)) {
69         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID);
70         goto err;
71     }
72     if (MSBits == 0) {
73         EM++;
74         emLen--;
75     }
76     if (sLen == RSA_PSS_SALTLEN_MAX) {
77         sLen = emLen - hLen - 2;
78     } else if (emLen < (hLen + sLen + 2)) { /* sLen can be small negative */
79         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE);
80         goto err;
81     }
82     if (EM[emLen - 1] != 0xbc) {
83         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID);
84         goto err;
85     }
86     maskedDBLen = emLen - hLen - 1;
87     H = EM + maskedDBLen;
88     DB = OPENSSL_malloc(maskedDBLen);
89     if (DB == NULL) {
90         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE);
91         goto err;
92     }
93     if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
94         goto err;
95     for (i = 0; i < maskedDBLen; i++)
96         DB[i] ^= EM[i];
97     if (MSBits)
98         DB[0] &= 0xFF >> (8 - MSBits);
99     for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
100     if (DB[i++] != 0x1) {
101         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED);
102         goto err;
103     }
104     if (sLen != RSA_PSS_SALTLEN_AUTO && (maskedDBLen - i) != sLen) {
105         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
106         goto err;
107     }
108     if (!EVP_DigestInit_ex(ctx, Hash, NULL)
109         || !EVP_DigestUpdate(ctx, zeroes, sizeof zeroes)
110         || !EVP_DigestUpdate(ctx, mHash, hLen))
111         goto err;
112     if (maskedDBLen - i) {
113         if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
114             goto err;
115     }
116     if (!EVP_DigestFinal_ex(ctx, H_, NULL))
117         goto err;
118     if (memcmp(H_, H, hLen)) {
119         RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE);
120         ret = 0;
121     } else
122         ret = 1;
123
124  err:
125     OPENSSL_free(DB);
126     EVP_MD_CTX_free(ctx);
127
128     return ret;
129
130 }
131
132 int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
133                               const unsigned char *mHash,
134                               const EVP_MD *Hash, int sLen)
135 {
136     return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
137 }
138
139 int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
140                                    const unsigned char *mHash,
141                                    const EVP_MD *Hash, const EVP_MD *mgf1Hash,
142                                    int sLen)
143 {
144     int i;
145     int ret = 0;
146     int hLen, maskedDBLen, MSBits, emLen;
147     unsigned char *H, *salt = NULL, *p;
148     EVP_MD_CTX *ctx = NULL;
149
150     if (mgf1Hash == NULL)
151         mgf1Hash = Hash;
152
153     hLen = EVP_MD_size(Hash);
154     if (hLen < 0)
155         goto err;
156     /*-
157      * Negative sLen has special meanings:
158      *      -1      sLen == hLen
159      *      -2      salt length is maximized
160      *      -N      reserved
161      */
162     if (sLen == RSA_PSS_SALTLEN_DIGEST)
163         sLen = hLen;
164     else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN)
165         sLen = RSA_PSS_SALTLEN_MAX;
166     else if (sLen < RSA_PSS_SALTLEN_MAX) {
167         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED);
168         goto err;
169     }
170
171     MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
172     emLen = RSA_size(rsa);
173     if (MSBits == 0) {
174         *EM++ = 0;
175         emLen--;
176     }
177     if (sLen == RSA_PSS_SALTLEN_MAX) {
178         sLen = emLen - hLen - 2;
179     } else if (emLen < (hLen + sLen + 2)) {
180         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,
181                RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
182         goto err;
183     }
184     if (sLen > 0) {
185         salt = OPENSSL_malloc(sLen);
186         if (salt == NULL) {
187             RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,
188                    ERR_R_MALLOC_FAILURE);
189             goto err;
190         }
191         if (RAND_bytes(salt, sLen) <= 0)
192             goto err;
193     }
194     maskedDBLen = emLen - hLen - 1;
195     H = EM + maskedDBLen;
196     ctx = EVP_MD_CTX_new();
197     if (ctx == NULL)
198         goto err;
199     if (!EVP_DigestInit_ex(ctx, Hash, NULL)
200         || !EVP_DigestUpdate(ctx, zeroes, sizeof zeroes)
201         || !EVP_DigestUpdate(ctx, mHash, hLen))
202         goto err;
203     if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
204         goto err;
205     if (!EVP_DigestFinal_ex(ctx, H, NULL))
206         goto err;
207
208     /* Generate dbMask in place then perform XOR on it */
209     if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
210         goto err;
211
212     p = EM;
213
214     /*
215      * Initial PS XORs with all zeroes which is a NOP so just update pointer.
216      * Note from a test above this value is guaranteed to be non-negative.
217      */
218     p += emLen - sLen - hLen - 2;
219     *p++ ^= 0x1;
220     if (sLen > 0) {
221         for (i = 0; i < sLen; i++)
222             *p++ ^= salt[i];
223     }
224     if (MSBits)
225         EM[0] &= 0xFF >> (8 - MSBits);
226
227     /* H is already in place so just set final 0xbc */
228
229     EM[emLen - 1] = 0xbc;
230
231     ret = 1;
232
233  err:
234     EVP_MD_CTX_free(ctx);
235     OPENSSL_free(salt);
236
237     return ret;
238
239 }
240
241 #if defined(_MSC_VER)
242 # pragma optimize("",on)
243 #endif