Skip to content

Commit 9d2fac9

Browse files
committed
initialization: provide lc_init in all cases
If the leancrypto library is compiled statically, the constructors are not invoked. Instead, the consuming application must invoke lc_init. This is also applied to test cases where each test case now invokes lc_init if leancrypto is compiled statically. Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 4688b64 commit 9d2fac9

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

addon/generate_header.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ header='/*
147147
* variables is desired, proper accessor functions are available. This implies
148148
* that changes to the data structures in newer versions of the library are not
149149
* considered as API changes!
150+
*
151+
* \note The leancrypto library performs an automated initialization during
152+
* its startup using constructors. If your execution environment does not offer
153+
* such constructors, or the library is compiled statically, the function
154+
* `lc_init` must be called by the consuming application before any leancrypto
155+
* API is invoked.
150156
*/
151157
'
152158

internal/api/initialization.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@
2626
extern "C" {
2727
#endif
2828

29-
/* Enable these declarations only if there is no automatic constructor */
30-
#ifndef LC_CONSTRUCTOR_AUTOMATIC_AVAILABLE
3129
void ascon_fastest_impl(void);
3230
void sha256_fastest_impl(void);
3331
void sha512_fastest_impl(void);
3432
void sha3_fastest_impl(void);
3533
void aes_fastest_impl(void);
3634
void kyber_riscv_rvv_selector(void);
37-
#endif /* LC_CONSTRUCTOR_AUTOMATIC_AVAILABLE */
3835

3936
#ifdef __cplusplus
4037
}

internal/api/visibility.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@
6969

7070
#define LC_INIT_FUNCTION(ret, symbol, param...) DSO_PUBLIC ret symbol(param)
7171

72+
#ifdef LC_STATIC
73+
#define LC_TEST_FUNC(ret, symbol, param...) \
74+
int lc_init(unsigned int flags); \
75+
static ret __ ## symbol(param); \
76+
ret symbol(param) { lc_init(0); return __ ## symbol(argc, argv); } \
77+
static ret __ ## symbol(param)
78+
#else
7279
#define LC_TEST_FUNC(ret, symbol, param...) ret symbol(param)
80+
#endif
7381

7482
#pragma GCC diagnostic pop
7583

@@ -85,11 +93,9 @@
8593
void _func(void)
8694
#else /* LC_EFI */
8795

88-
#define LC_CONSTRUCTOR_AUTOMATIC_AVAILABLE
89-
9096
#define LC_CONSTRUCTOR(_func) \
91-
static void __attribute__((constructor)) _func(void); \
92-
static void _func(void)
97+
void __attribute__((constructor)) _func(void); \
98+
void _func(void)
9399
#endif /* LC_EFI */
94100

95101
#endif /* LINUX_KERNEL */

internal/src/meson.build

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ internal_src += files([
77
])
88

99
src_fips += files([
10-
'fips_integrity_check.c'
10+
'fips_integrity_check.c',
11+
'leancrypto_init.c',
1112
])
1213

1314
if get_option('efi').disabled()
@@ -17,10 +18,6 @@ if get_option('efi').disabled()
1718
src_fips += files([
1819
'status.c'
1920
])
20-
else
21-
internal_src += files([
22-
'leancrypto_init.c',
23-
])
2421
endif
2522

2623
if get_option('efi').enabled()

meson.build

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ if get_option('pkcs7_debug').enabled()
232232
add_global_arguments([ '-DLC_PKCS7_DEBUG' ], language: 'c')
233233
endif
234234

235+
# Whether to build the shared library alongside the static library
236+
build_shared = (get_option('default_library') != 'static' and
237+
get_option('efi').disabled())
238+
if (not build_shared)
239+
add_global_arguments([ '-DLC_STATIC' ], language: 'c')
240+
endif
241+
235242
# 64 bit time to not suffer from Y2038 problem
236243
add_global_arguments([ '-D_TIME_BITS=64' ], language: 'c')
237244

@@ -631,10 +638,6 @@ leancrypto_support_libs_fips = [ ]
631638
# External libraries leancrypto should link to
632639
leancrypto_link = [ ]
633640

634-
# Whether to build the shared library alongside the static library
635-
build_shared = (get_option('default_library') != 'static' and
636-
get_option('efi').disabled())
637-
638641
foreach n : subdirs
639642
subdir(n)
640643
endforeach

0 commit comments

Comments
 (0)