369a6bab3b02317b58d321283f4f7bff3dd69cf1
[oweals/openssl.git] / providers / fips / selftest.c
1 /*
2  * Copyright 2019 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 <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/params.h>
13 #include <openssl/crypto.h>
14 #include "e_os.h"
15 /*
16  * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
17  * module because all such initialisation should be associated with an
18  * individual OPENSSL_CTX. That doesn't work with the self test though because
19  * it should be run once regardless of the number of OPENSSL_CTXs we have.
20  */
21 #define ALLOW_RUN_ONCE_IN_FIPS
22 #include <internal/thread_once.h>
23 #include "selftest.h"
24
25 #define FIPS_STATE_INIT     0
26 #define FIPS_STATE_SELFTEST 1
27 #define FIPS_STATE_RUNNING  2
28 #define FIPS_STATE_ERROR    3
29
30 /* The size of a temp buffer used to read in data */
31 #define INTEGRITY_BUF_SIZE (4096)
32 #define MAX_MD_SIZE 64
33 #define MAC_NAME    "HMAC"
34 #define DIGEST_NAME "SHA256"
35
36 static int FIPS_state = FIPS_STATE_INIT;
37 static CRYPTO_RWLOCK *self_test_lock = NULL;
38 static unsigned char fixed_key[32] = { 0 };
39
40 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
41 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
42 {
43     /*
44      * This lock gets freed in platform specific ways that may occur after we
45      * do mem leak checking. If we don't know how to free it for a particular
46      * platform then we just leak it deliberately. So we temporarily disable the
47      * mem leak checking while we allocate this.
48      */
49     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
50     self_test_lock = CRYPTO_THREAD_lock_new();
51     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
52
53     return self_test_lock != NULL;
54 }
55
56 /*
57  * This is the Default Entry Point (DEP) code. Every platform must have a DEP.
58  * See FIPS 140-2 IG 9.10
59  *
60  * If we're run on a platform where we don't know how to define the DEP then
61  * the self-tests will never get triggered (FIPS_state never moves to
62  * FIPS_STATE_SELFTEST). This will be detected as an error when SELF_TEST_post()
63  * is called from OSSL_provider_init(), and so the fips module will be unusable
64  * on those platforms.
65  */
66 #if defined(_WIN32) || defined(__CYGWIN__)
67 # ifdef __CYGWIN__
68 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
69 #  include <windows.h>
70 /*
71  * this has side-effect of _WIN32 getting defined, which otherwise is
72  * mutually exclusive with __CYGWIN__...
73  */
74 # endif
75
76 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
77 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
78 {
79     switch (fdwReason) {
80     case DLL_PROCESS_ATTACH:
81         FIPS_state = FIPS_STATE_SELFTEST;
82         break;
83     case DLL_PROCESS_DETACH:
84         CRYPTO_THREAD_lock_free(self_test_lock);
85         break;
86     default:
87         break;
88     }
89     return TRUE;
90 }
91 #elif defined(__GNUC__)
92
93 static __attribute__((constructor)) void init(void)
94 {
95     FIPS_state = FIPS_STATE_SELFTEST;
96 }
97
98
99 static __attribute__((destructor)) void cleanup(void)
100 {
101     CRYPTO_THREAD_lock_free(self_test_lock);
102 }
103
104 #endif
105
106 /*
107  * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
108  * the result matches the expected value.
109  * Return 1 if verified, or 0 if it fails.
110  */
111 static int verify_integrity(BIO *bio, OSSL_BIO_read_ex_fn read_ex_cb,
112                             unsigned char *expected, size_t expected_len,
113                             OPENSSL_CTX *libctx)
114 {
115     int ret = 0, status;
116     unsigned char out[MAX_MD_SIZE];
117     unsigned char buf[INTEGRITY_BUF_SIZE];
118     size_t bytes_read = 0, out_len = 0;
119     EVP_MAC *mac = NULL;
120     EVP_MAC_CTX *ctx = NULL;
121     OSSL_PARAM params[3], *p = params;
122
123     mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
124     ctx = EVP_MAC_CTX_new(mac);
125     if (mac == NULL || ctx == NULL)
126         goto err;
127
128     *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME,
129                                             strlen(DIGEST_NAME) + 1);
130     *p++ = OSSL_PARAM_construct_octet_string("key", fixed_key,
131                                              sizeof(fixed_key));
132     *p = OSSL_PARAM_construct_end();
133
134     if (EVP_MAC_CTX_set_params(ctx, params) <= 0
135         || !EVP_MAC_init(ctx))
136         goto err;
137
138     while (1) {
139         status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
140         if (status != 1)
141             break;
142         if (!EVP_MAC_update(ctx, buf, bytes_read))
143             goto err;
144     }
145     if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
146         goto err;
147
148     if (expected_len != out_len
149             || memcmp(expected, out, out_len) != 0)
150         goto err;
151     ret = 1;
152 err:
153     EVP_MAC_CTX_free(ctx);
154     EVP_MAC_free(mac);
155     return ret;
156 }
157
158 /* This API is triggered either on loading of the FIPS module or on demand */
159 int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
160 {
161     int ok = 0;
162     int kats_already_passed = 0;
163     long checksum_len;
164     BIO *bio_module = NULL, *bio_indicator = NULL;
165     unsigned char *module_checksum = NULL;
166     unsigned char *indicator_checksum = NULL;
167     int loclstate;
168
169     if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
170         return 0;
171
172     CRYPTO_THREAD_read_lock(self_test_lock);
173     loclstate = FIPS_state;
174     CRYPTO_THREAD_unlock(self_test_lock);
175
176     if (loclstate == FIPS_STATE_RUNNING) {
177         if (!on_demand_test)
178             return 1;
179     } else if (loclstate != FIPS_STATE_SELFTEST) {
180         return 0;
181     }
182
183     CRYPTO_THREAD_write_lock(self_test_lock);
184     if (FIPS_state == FIPS_STATE_RUNNING) {
185         if (!on_demand_test) {
186             CRYPTO_THREAD_unlock(self_test_lock);
187             return 1;
188         }
189         FIPS_state = FIPS_STATE_SELFTEST;
190     } else if (FIPS_state != FIPS_STATE_SELFTEST) {
191         CRYPTO_THREAD_unlock(self_test_lock);
192         return 0;
193     }
194     if (st == NULL
195             || st->module_checksum_data == NULL)
196         goto end;
197
198     module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
199                                          &checksum_len);
200     if (module_checksum == NULL)
201         goto end;
202     bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
203
204     /* Always check the integrity of the fips module */
205     if (bio_module == NULL
206             || !verify_integrity(bio_module, st->bio_read_ex_cb,
207                                  module_checksum, checksum_len, st->libctx))
208         goto end;
209
210     /* This will be NULL during installation - so the self test KATS will run */
211     if (st->indicator_data != NULL) {
212         /*
213          * If the kats have already passed indicator is set - then check the
214          * integrity of the indicator.
215          */
216         if (st->indicator_checksum_data == NULL)
217             goto end;
218         indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
219                                                 &checksum_len);
220         if (indicator_checksum == NULL)
221             goto end;
222
223         bio_indicator =
224             (*st->bio_new_buffer_cb)(st->indicator_data,
225                                      strlen(st->indicator_data));
226         if (bio_indicator == NULL
227                 || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
228                                      indicator_checksum, checksum_len,
229                                      st->libctx))
230             goto end;
231         else
232             kats_already_passed = 1;
233     }
234
235     /* Only runs the KAT's during installation OR on_demand() */
236     if (on_demand_test || kats_already_passed == 0) {
237         /*TODO (3.0) Add self test KATS */
238     }
239     ok = 1;
240 end:
241     OPENSSL_free(module_checksum);
242     OPENSSL_free(indicator_checksum);
243
244     if (st != NULL) {
245         (*st->bio_free_cb)(bio_indicator);
246         (*st->bio_free_cb)(bio_module);
247     }
248     FIPS_state = ok ? FIPS_STATE_RUNNING : FIPS_STATE_ERROR;
249     CRYPTO_THREAD_unlock(self_test_lock);
250
251     return ok;
252 }