Skip to content

Commit 36c0aa6

Browse files
pablodelaramdcornu
authored andcommitted
fips: add generic self tests for non-x86 architectures
Signed-off-by: Pablo de Lara <[email protected]>
1 parent 7a78040 commit 36c0aa6

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

fips/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
src_include += -I $(srcdir)/fips
3131
extern_hdrs += include/isal_crypto_api.h include/aes_cbc_internal.h include/aes_xts.h include/aes_keyexp.h include/sha1_mb.h include/sha256_mb.h
3232

33-
lsrc += fips/self_tests.c
33+
lsrc_x86_64 += fips/self_tests.c
34+
lsrc_aarch64 += fips/self_tests_generic.c
3435
lsrc += fips/aes_self_tests.c
3536
lsrc += fips/sha_self_tests.c
3637

fips/self_tests_generic.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 */

0 commit comments

Comments
 (0)