-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce publich header for mlkem-native API
Application sources using an mlkem-native build should not have to add the source tree of mlkem-native to the include paths. Instead, there should be a single standaloe header sufficient to include all necessary declarations. Previously, this was not true: Applications would need to include `kem.h` from the source tree, which in turn would pull in, for example, `config.h` and `cbmc.h`. On the other hand, the header of mlkem-native is not static, but depends on information from the build, namely the security level and the namespacing: The security level determines the sizes of key material which callers need to know about. The namespacing determines the name of the function symbols, which callers need to know about. It is therefore not possible to completely detach the mlkem-native API header from the source tree used to build mlkem-native. One way to address this is to merely require the build configuration header to be included in the public API header. This works well for single-level builds, but is inconvenient if the consumer wants to use multiple instances of mlkem-native for different security levels; in this case, including the public header multiple times would pull in multiple configuration files, which would lead to clashes of #define's clauses. On the other hand, requiring the consumer to duplicate the level and namespacing information in the API header is inconvenient for simple tests or single-level builds. This introduces a public API header mlkem-native.h, which addresses the above as follows: mlkem-native.h requires the build level and build namespace. By default, it is obtained by including the underlying build configuration (assumed to be present in the include paths). Alternatively, the user can set BUILD_INFO_LVL and BUILD_INFO_NAMESPACE explicitly in mlkem-native.h, and thereby avoid any dependency between mlkem-native.h and config.h. In this setting, mlkem-native.h leaves no other traces than static size declarations and build-specific API declarations. In particular, it can be included multiple times. This commit adjusts all tests and examples to no longer include `kem.h`, but only `mlkem-native.h`. In all cases but the monolithic build example, no further change is required, with the API header pulling the underlying configuration automatically. The monolithic build test `examples/monolithic_build` is adjusted as follows: Where previously it included two instances of mlkem-native, it now includes three, one per security level. The previous `mlkem_native_all.c` is renamed to `mlkem_native_monobuild.c`, and is the source file needed for a monolithic build of a _single_ configuration. This is then included 3 times in `mlkem_native_all.c`. To provide a header for this file, we include `mlkem-native.h` 3 times, using manually set `BUILD_INFO_LVL` and `BUILD_INFO_NAMESPACE` to avoid nameclashes from pulling in the full configuration. Signed-off-by: Hanno Becker <[email protected]>
- Loading branch information
1 parent
4b4e881
commit 06d16c9
Showing
28 changed files
with
3,543 additions
and
2,937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../mlkem/mlkem_native.h |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <kem.h> | ||
#include <mlkem_native.h> | ||
|
||
int main(void) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2024 The mlkem-native project authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef MLKEM_NATIVE_CONFIG_H | ||
#define MLKEM_NATIVE_CONFIG_H | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_K | ||
* | ||
* Description: Determines the security level for ML-KEM | ||
* - MLKEM_K=2 corresponds to ML-KEM-512 | ||
* - MLKEM_K=3 corresponds to ML-KEM-768 | ||
* - MLKEM_K=4 corresponds to ML-KEM-1024 | ||
* | ||
* This can also be set using CFLAGS. | ||
* | ||
*****************************************************************************/ | ||
#ifndef MLKEM_K | ||
#define MLKEM_K 2 /* Change this for different security strengths */ | ||
#endif | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_NATIVE_CONFIG_FILE | ||
* | ||
* Description: If defined, this is a header that will be included instead | ||
* of mlkem/config.h. | ||
* | ||
* This _must_ be set on the command line using | ||
* `-DMLKEM_NATIVE_CONFIG_FILE="..."`. | ||
* | ||
* When you need to build mlkem-native in multiple configurations, | ||
* using varying MLKEM_NATIE_CONFIG_FILE can be more convenient | ||
* then configuring everything through CFLAGS. | ||
* | ||
*****************************************************************************/ | ||
/* #define MLKEM_NATIVE_CONFIG_FILE "config.h" */ | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_NAMESPACE | ||
* | ||
* Description: The macros to use to namespace global symbols | ||
* from mlkem/. | ||
*****************************************************************************/ | ||
#define CONCAT(a, b) a##b | ||
#define MLKEM_NAMESPACE(sym) CONCAT(mlkem512_, sym) | ||
|
||
/****************************************************************************** | ||
* Name: FIPS202_NAMESPACE | ||
* | ||
* Description: The macros to use to namespace global symbols | ||
* from mlkem/fips202/. | ||
*****************************************************************************/ | ||
#define FIPS202_NAMESPACE(sym) CONCAT(mlkem512_, sym) | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_USE_NATIVE | ||
* | ||
* Description: Determines whether a native backend should | ||
* be used, if available. | ||
* | ||
* This can also be set using CFLAGS. | ||
* | ||
*****************************************************************************/ | ||
/* #define MLKEM_USE_NATIVE */ | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_NATIVE_ARITH_BACKEND | ||
* | ||
* Description: The arithmetic backend to use. | ||
* | ||
* This must be the filename of an arithmetic backend. | ||
* See the existing backends for examples. | ||
* | ||
* This can be set using CFLAGS. | ||
* | ||
*****************************************************************************/ | ||
#if defined(MLKEM_USE_NATIVE) && !defined(MLKEM_NATIVE_ARITH_BACKEND) | ||
#define MLKEM_NATIVE_ARITH_BACKEND "native/default.h" | ||
#endif /* MLKEM_NATIVE_ARITH_BACKEND */ | ||
|
||
/****************************************************************************** | ||
* Name: MLKEM_NATIVE_FIPS202_BACKEND | ||
* | ||
* Description: The FIPS-202 backend to use. | ||
* | ||
* This must be the filename of an FIPS-202 backend. | ||
* | ||
* This can be set using CFLAGS. | ||
* | ||
*****************************************************************************/ | ||
#if defined(MLKEM_USE_NATIVE) && !defined(MLKEM_NATIVE_FIPS202_BACKEND) | ||
#define MLKEM_NATIVE_FIPS202_BACKEND "fips202/native/default.h" | ||
#endif /* MLKEM_NATIVE_FIPS202_BACKEND */ | ||
|
||
#endif /* MLkEM_NATIVE_CONFIG_H */ |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.