1 /**********************************************************************
2 Copyright(c) 2024 Intel Corporation All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in
10 the documentation and/or other materials provided with the
11 distribution.
12 * Neither the name of Intel Corporation nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 **********************************************************************/
27
28 #include "isal_crypto_api.h"
29 #include "internal_fips.h"
30
31 #ifdef FIPS_MODE
32 #include <stdatomic.h>
33 #include <unistd.h>
34 #define SLEEP(x) usleep(x)
35 #define TIME 1 // 1 microsecond
36
37 #define SELF_TEST_DONE_AND_OK 0
38 #define SELF_TEST_DONE_AND_FAIL 1
39 #define SELF_TEST_NOT_DONE 2
40 #define SELF_TEST_RUNNING 3
41 #endif /* FIPS_MODE */
42
43 int
isal_self_tests(void)44 isal_self_tests(void)
45 {
46 #ifdef FIPS_MODE
47 static atomic_int self_tests_status = SELF_TEST_NOT_DONE;
48 int self_tests_not_done = SELF_TEST_NOT_DONE;
49
50 if (atomic_load(&self_tests_status) == SELF_TEST_DONE_AND_OK)
51 return 0;
52
53 if (atomic_load(&self_tests_status) == SELF_TEST_DONE_AND_FAIL)
54 return ISAL_CRYPTO_ERR_SELF_TEST;
55
56 if (atomic_compare_exchange_strong(&self_tests_status, &self_tests_not_done,
57 SELF_TEST_RUNNING)) {
58 if (_aes_self_tests() != 0) {
59 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_FAIL);
60 return ISAL_CRYPTO_ERR_SELF_TEST;
61 }
62 if (_sha_self_tests() != 0) {
63 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_FAIL);
64 return ISAL_CRYPTO_ERR_SELF_TEST;
65 }
66 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_OK);
67
68 return 0;
69 } else {
70 /* At this stage, only a thread that encountered SELF_TEST_RUNNING reaches here */
71 while (atomic_load(&self_tests_status) == SELF_TEST_RUNNING)
72 SLEEP(TIME);
73
74 /* After waiting for the status to change from "SELF_TEST_RUNNING",
75 * read the self test status and return success or failure */
76 if (self_tests_status == SELF_TEST_DONE_AND_OK)
77 return 0;
78 else
79 return ISAL_CRYPTO_ERR_SELF_TEST;
80 }
81 #else /* FIPS_MODE disabled */
82 return ISAL_CRYPTO_ERR_FIPS_DISABLED;
83 #endif /* FIPS_MODE */
84 }
85