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 42 int 43 isal_self_tests(void) 44 { 45 static atomic_int self_tests_status = SELF_TEST_NOT_DONE; 46 int self_tests_not_done = SELF_TEST_NOT_DONE; 47 48 if (atomic_load(&self_tests_status) == SELF_TEST_DONE_AND_OK) 49 return 0; 50 51 if (atomic_load(&self_tests_status) == SELF_TEST_DONE_AND_FAIL) 52 return ISAL_CRYPTO_ERR_SELF_TEST; 53 54 if (atomic_compare_exchange_strong(&self_tests_status, &self_tests_not_done, 55 SELF_TEST_RUNNING)) { 56 if (_aes_self_tests() != 0) { 57 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_FAIL); 58 return ISAL_CRYPTO_ERR_SELF_TEST; 59 } 60 if (_sha_self_tests() != 0) { 61 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_FAIL); 62 return ISAL_CRYPTO_ERR_SELF_TEST; 63 } 64 atomic_store(&self_tests_status, SELF_TEST_DONE_AND_OK); 65 66 return 0; 67 } else { 68 /* At this stage, only a thread that encountered SELF_TEST_RUNNING reaches here */ 69 while (atomic_load(&self_tests_status) == SELF_TEST_RUNNING) 70 SLEEP(TIME); 71 72 /* After waiting for the status to change from "SELF_TEST_RUNNING", 73 * read the self test status and return success or failure */ 74 if (self_tests_status == SELF_TEST_DONE_AND_OK) 75 return 0; 76 else 77 return ISAL_CRYPTO_ERR_SELF_TEST; 78 } 79 } 80 #else /* FIPS_MODE disabled */ 81 #include <stdio.h> 82 int 83 isal_self_tests(void) 84 { 85 fprintf(stderr, "FIPS Mode is not enabled\n"); 86 87 return ISAL_CRYPTO_ERR_SELF_TEST; 88 } 89 #endif /* FIPS_MODE */ 90