2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #define OPENSSL_FIPSAPI
62 #include <openssl/evp.h>
63 #include <openssl/rsa.h>
64 #include <openssl/err.h>
65 #include <openssl/sha.h>
66 #include <openssl/fips.h>
70 /* FIPS versions of RSA_sign() and RSA_verify().
71 * These will only have to deal with SHA* signatures and by including
72 * pregenerated encodings all ASN1 dependencies can be avoided
75 /* Standard encodings including NULL parameter */
77 static const unsigned char sha1_bin[] = {
78 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
82 static const unsigned char sha224_bin[] = {
83 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
84 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c
87 static const unsigned char sha256_bin[] = {
88 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
89 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
92 static const unsigned char sha384_bin[] = {
93 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
94 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30
97 static const unsigned char sha512_bin[] = {
98 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
99 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40
102 /* Alternate encodings with absent parameters. We don't generate signature
103 * using this format but do tolerate received signatures of this form.
106 static unsigned char sha1_nn_bin[] = {
107 0x30, 0x1f, 0x30, 0x07, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x04,
111 static unsigned char sha224_nn_bin[] = {
112 0x30, 0x2b, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
113 0x04, 0x02, 0x04, 0x04, 0x1c
116 static unsigned char sha256_nn_bin[] = {
117 0x30, 0x2f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
118 0x04, 0x02, 0x01, 0x04, 0x20
121 static unsigned char sha384_nn_bin[] = {
122 0x30, 0x3f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
123 0x04, 0x02, 0x02, 0x04, 0x30
126 static unsigned char sha512_nn_bin[] = {
127 0x30, 0x4f, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
128 0x04, 0x02, 0x03, 0x04, 0x40
132 static const unsigned char *fips_digestinfo_encoding(int nid, unsigned int *len)
138 *len = sizeof(sha1_bin);
142 *len = sizeof(sha224_bin);
146 *len = sizeof(sha256_bin);
150 *len = sizeof(sha384_bin);
154 *len = sizeof(sha512_bin);
163 static const unsigned char *fips_digestinfo_nn_encoding(int nid, unsigned int *len)
169 *len = sizeof(sha1_nn_bin);
173 *len = sizeof(sha224_nn_bin);
174 return sha224_nn_bin;
177 *len = sizeof(sha256_nn_bin);
178 return sha256_nn_bin;
181 *len = sizeof(sha384_nn_bin);
182 return sha384_nn_bin;
185 *len = sizeof(sha512_nn_bin);
186 return sha512_nn_bin;
194 int FIPS_rsa_sign_ctx(RSA *rsa, EVP_MD_CTX *ctx,
195 int rsa_pad_mode, int saltlen, const EVP_MD *mgf1Hash,
196 unsigned char *sigret, unsigned int *siglen)
198 unsigned int md_len, rv;
199 unsigned char md[EVP_MAX_MD_SIZE];
200 FIPS_digestfinal(ctx, md, &md_len);
201 rv = FIPS_rsa_sign_digest(rsa, md, md_len,
202 M_EVP_MD_CTX_md(ctx),
203 rsa_pad_mode, saltlen,
204 mgf1Hash, sigret, siglen);
205 OPENSSL_cleanse(md, md_len);
210 int FIPS_rsa_sign_digest(RSA *rsa, const unsigned char *md, int md_len,
211 const EVP_MD *mhash, int rsa_pad_mode, int saltlen,
212 const EVP_MD *mgf1Hash,
213 unsigned char *sigret, unsigned int *siglen)
217 const unsigned char *der;
219 /* Largest DigestInfo: 19 (max encoding) + max MD */
220 unsigned char tmpdinfo[19 + EVP_MAX_MD_SIZE];
222 FIPS_selftest_check();
224 md_type = M_EVP_MD_type(mhash);
226 if (rsa_pad_mode == RSA_X931_PADDING)
229 memcpy(tmpdinfo, md, md_len);
230 hash_id = RSA_X931_hash_id(md_type);
233 RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
236 tmpdinfo[md_len] = (unsigned char)hash_id;
239 else if (rsa_pad_mode == RSA_PKCS1_PADDING)
242 der = fips_digestinfo_encoding(md_type, &dlen);
246 RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
249 memcpy(tmpdinfo, der, dlen);
250 memcpy(tmpdinfo + dlen, md, md_len);
255 else if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
259 sbuf = OPENSSL_malloc(RSA_size(rsa));
262 RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,ERR_R_MALLOC_FAILURE);
265 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, sbuf, md, mhash,
268 j=rsa->meth->rsa_priv_enc(i,sbuf,sigret,rsa,RSA_NO_PADDING);
275 OPENSSL_cleanse(sbuf, i);
281 if (i > (j-RSA_PKCS1_PADDING_SIZE))
283 RSAerr(RSA_F_FIPS_RSA_SIGN_DIGEST,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
286 /* NB: call underlying method directly to avoid FIPS blocking */
287 j=rsa->meth->rsa_priv_enc(i,tmpdinfo,sigret,rsa,rsa_pad_mode);
295 OPENSSL_cleanse(tmpdinfo,i);
299 int FIPS_rsa_verify_ctx(RSA *rsa, EVP_MD_CTX *ctx,
300 int rsa_pad_mode, int saltlen, const EVP_MD *mgf1Hash,
301 unsigned char *sigbuf, unsigned int siglen)
303 unsigned int md_len, rv;
304 unsigned char md[EVP_MAX_MD_SIZE];
305 FIPS_digestfinal(ctx, md, &md_len);
306 rv = FIPS_rsa_verify_digest(rsa, md, md_len, M_EVP_MD_CTX_md(ctx),
307 rsa_pad_mode, saltlen, mgf1Hash,
309 OPENSSL_cleanse(md, md_len);
313 int FIPS_rsa_verify_digest(RSA *rsa, const unsigned char *dig, int diglen,
314 const EVP_MD *mhash, int rsa_pad_mode, int saltlen,
315 const EVP_MD *mgf1Hash,
316 unsigned char *sigbuf, unsigned int siglen)
321 const unsigned char *der;
323 int rsa_dec_pad_mode;
325 if (siglen != (unsigned int)RSA_size(rsa))
327 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_WRONG_SIGNATURE_LENGTH);
331 FIPS_selftest_check();
333 md_type = M_EVP_MD_type(mhash);
335 s= OPENSSL_malloc((unsigned int)siglen);
338 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,ERR_R_MALLOC_FAILURE);
342 if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
343 rsa_dec_pad_mode = RSA_NO_PADDING;
345 rsa_dec_pad_mode = rsa_pad_mode;
347 /* NB: call underlying method directly to avoid FIPS blocking */
348 i=rsa->meth->rsa_pub_dec((int)siglen,sigbuf,s, rsa, rsa_dec_pad_mode);
350 if (i <= 0) goto err;
352 if (rsa_pad_mode == RSA_X931_PADDING)
355 if (i != (int)(diglen + 1))
357 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
360 hash_id = RSA_X931_hash_id(md_type);
363 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
366 if (s[diglen] != (unsigned char)hash_id)
368 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
371 if (memcmp(s, dig, diglen))
373 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
378 else if (rsa_pad_mode == RSA_PKCS1_PADDING)
381 der = fips_digestinfo_encoding(md_type, &dlen);
385 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_UNKNOWN_ALGORITHM_TYPE);
389 /* Compare, DigestInfo length, DigestInfo header and finally
390 * digest value itself
393 /* If length mismatch try alternate encoding */
394 if (i != (int)(dlen + diglen))
395 der = fips_digestinfo_nn_encoding(md_type, &dlen);
397 if ((i != (int)(dlen + diglen)) || memcmp(der, s, dlen)
398 || memcmp(s + dlen, dig, diglen))
400 RSAerr(RSA_F_FIPS_RSA_VERIFY_DIGEST,RSA_R_BAD_SIGNATURE);
406 else if (rsa_pad_mode == RSA_PKCS1_PSS_PADDING)
408 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, dig, mhash, mgf1Hash,
416 OPENSSL_cleanse(s, siglen);