1 /* ====================================================================
2 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 #define OPENSSL_FIPSAPI
52 #include <openssl/crypto.h>
53 #include <openssl/rand.h>
54 #include <openssl/fips_rand.h>
55 #include <openssl/err.h>
56 #include <openssl/bio.h>
57 #include <openssl/hmac.h>
58 #include <openssl/rsa.h>
59 #include <openssl/dsa.h>
60 #include <openssl/ecdsa.h>
66 /* Power on self test (POST) support functions */
68 #include <openssl/fips.h>
69 #include "fips_locl.h"
71 /* POST notification callback */
73 int (*fips_post_cb)(int op, int id, int subid, void *ex);
75 void FIPS_post_set_callback(
76 int (*post_cb)(int op, int id, int subid, void *ex))
78 fips_post_cb = post_cb;
81 /* POST status: i.e. status of all tests */
82 #define FIPS_POST_STATUS_NOT_STARTED 0
83 #define FIPS_POST_STATUS_OK 1
84 #define FIPS_POST_STATUS_RUNNING 2
85 #define FIPS_POST_STATUS_FAILED -1
86 static int post_status = 0;
87 /* Set to 1 if any test failed */
88 static int post_failure = 0;
90 /* All tests started */
92 int fips_post_begin(void)
95 post_status = FIPS_POST_STATUS_NOT_STARTED;
97 if (!fips_post_cb(FIPS_POST_BEGIN, 0, 0, NULL))
99 post_status = FIPS_POST_STATUS_RUNNING;
103 void fips_post_end(void)
107 post_status = FIPS_POST_STATUS_FAILED;
108 fips_post_cb(FIPS_POST_END, 0, 0, NULL);
112 post_status = FIPS_POST_STATUS_OK;
113 fips_post_cb(FIPS_POST_END, 1, 0, NULL);
117 /* A self test started */
118 int fips_post_started(int id, int subid, void *ex)
121 return fips_post_cb(FIPS_POST_STARTED, id, subid, ex);
124 /* A self test passed successfully */
125 int fips_post_success(int id, int subid, void *ex)
128 return fips_post_cb(FIPS_POST_SUCCESS, id, subid, ex);
131 /* A self test failed */
132 int fips_post_failed(int id, int subid, void *ex)
136 return fips_post_cb(FIPS_POST_FAIL, id, subid, ex);
139 /* Indicate if a self test failure should be induced */
140 int fips_post_corrupt(int id, int subid, void *ex)
143 return fips_post_cb(FIPS_POST_CORRUPT, id, subid, ex);
146 /* Note: if selftests running return status OK so their operation is
147 * not interrupted. This will only happen while selftests are actually
148 * running so will not interfere with normal operation.
150 int fips_post_status(void)
152 return post_status > 0 ? 1 : 0;
154 /* Run all selftests */
155 int FIPS_selftest(void)
159 if(!FIPS_check_incore_fingerprint())
161 if (!FIPS_selftest_drbg())
163 if (!FIPS_selftest_x931())
165 if (!FIPS_selftest_sha1())
167 if (!FIPS_selftest_hmac())
169 if (!FIPS_selftest_cmac())
171 if (!FIPS_selftest_aes())
173 if (!FIPS_selftest_aes_gcm())
175 if (!FIPS_selftest_des())
177 if (!FIPS_selftest_rsa())
179 if (!FIPS_selftest_ecdsa())
181 if (!FIPS_selftest_dsa())
187 /* Generalized public key test routine. Signs and verifies the data
188 * supplied in tbs using mesage digest md and setting RSA padding mode
189 * pad_mode. If the 'kat' parameter is not NULL it will
190 * additionally check the signature matches it: a known answer test
191 * The string "fail_str" is used for identification purposes in case
192 * of failure. If "pkey" is NULL just perform a message digest check.
195 int fips_pkey_signature_test(int id, EVP_PKEY *pkey,
196 const unsigned char *tbs, size_t tbslen,
197 const unsigned char *kat, size_t katlen,
198 const EVP_MD *digest, int pad_mode,
199 const char *fail_str)
204 unsigned char *sig = NULL;
206 static const unsigned char str1[]="12345678901234567890";
207 DSA_SIG *dsig = NULL;
208 ECDSA_SIG *esig = NULL;
210 FIPS_md_ctx_init(&mctx);
216 tbslen = strlen((char *)tbs);
219 digest = EVP_sha256();
221 subid = M_EVP_MD_type(digest);
224 if (!fips_post_started(id, subid, pkey))
227 if (!pkey || pkey->type == EVP_PKEY_RSA)
231 sigsize = EVP_MAX_MD_SIZE;
233 sigsize = RSA_size(pkey->pkey.rsa);
235 sig = OPENSSL_malloc(sigsize);
238 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
243 if (!FIPS_digestinit(&mctx, digest))
245 if (!FIPS_digestupdate(&mctx, tbs, tbslen))
248 if (!fips_post_corrupt(id, subid, pkey))
250 if (!FIPS_digestupdate(&mctx, tbs, 1))
256 if (!FIPS_digestfinal(&mctx, sig, &siglen))
259 else if (pkey->type == EVP_PKEY_RSA)
261 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
262 pad_mode, 0, NULL, sig, &siglen))
265 else if (pkey->type == EVP_PKEY_DSA)
267 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
271 else if (pkey->type == EVP_PKEY_EC)
273 esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
278 if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
282 /* Debug code to print out self test KAT discrepancies */
284 fprintf(stderr, "%s=", fail_str);
285 for (i = 0; i < siglen; i++)
286 fprintf(stderr, "%02X", sig[i]);
287 fprintf(stderr, "\n");
291 /* If just digest test we've finished */
295 /* Well actually sucess as we've set ret to 1 */
298 if (!FIPS_digestinit(&mctx, digest))
300 if (!FIPS_digestupdate(&mctx, tbs, tbslen))
302 if (pkey->type == EVP_PKEY_RSA)
304 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
305 pad_mode, 0, NULL, sig, siglen);
307 else if (pkey->type == EVP_PKEY_DSA)
309 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
311 else if (pkey->type == EVP_PKEY_EC)
313 ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
318 FIPS_dsa_sig_free(dsig);
320 FIPS_ecdsa_sig_free(esig);
323 FIPS_md_ctx_cleanup(&mctx);
326 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
328 FIPS_add_error_data(2, "Type=", fail_str);
329 fips_post_failed(id, subid, ex);
332 return fips_post_success(id, subid, pkey);
335 /* Generalized symmetric cipher test routine. Encrypt data, verify result
336 * against known answer, decrypt and compare with original plaintext.
339 int fips_cipher_test(int id, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
340 const unsigned char *key,
341 const unsigned char *iv,
342 const unsigned char *plaintext,
343 const unsigned char *ciphertext,
346 unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
347 unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
348 int subid = M_EVP_CIPHER_nid(cipher);
350 OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
351 memset(pltmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
352 memset(citmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
354 if (!fips_post_started(id, subid, NULL))
356 if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
358 if (!FIPS_cipher(ctx, citmp, plaintext, len))
360 if (memcmp(citmp, ciphertext, len))
362 if (!fips_post_corrupt(id, subid, NULL))
364 if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
366 FIPS_cipher(ctx, pltmp, citmp, len);
367 if (memcmp(pltmp, plaintext, len))
373 fips_post_failed(id, subid, NULL);
376 return fips_post_success(id, subid, NULL);