Leancrypto 1.7.2
Post-Quantum Cryptographic Library
Macros | Functions
PKCS#8 Message Parsing

Macros

#define LC_PKCS8_MSG_ON_STACK(name)
 Allocate memory for struct lc_pkcs8_message holding given number of preallocated sinfo members. More...
 

Functions

int lc_pkcs8_decode (struct lc_pkcs8_message *pkcs8, const uint8_t *data, size_t datalen)
 Decode a PKCS#8 message. More...
 
int lc_pkcs8_set_privkey (struct lc_pkcs8_message *pkcs8, struct lc_x509_key_data *privkey)
 Set the private key for the PKCS8 message. More...
 
void lc_pkcs8_message_clear (struct lc_pkcs8_message *pkcs8)
 Clear the resources used by the PKCS#8 message parsing state. More...
 
int lc_pkcs8_signature_gen (uint8_t *sig_data, size_t *siglen, const struct lc_pkcs8_message *pkcs8, const uint8_t *m, size_t mlen, const struct lc_hash *prehash_algo)
 Generate signature over user-supplied data. More...
 

Detailed Description

Concept of PKCS#8 parsing in leancrypto

The leancrypto library provides a PKCS#8 parser which can read and understand PKCS#8 messages. To appropriately use the PKCS#8 parser, please consider the following rules:

  1. The parser interprets the provided PKCS#8 data blob and fills a data structure which allows immediate access to the certificate properties by the leancrypto code. The data structure pkcs8_message is provided as part of the official header file. But it is NOT considered to be an API. I.e. member variables or the structure format may change between versions of leancrypto without announcement. The reason for providing the data structure in the official header file is to support stack-only clients. Aas a PKCS#8 message may contain an arbitrary amount of X.509 certificates and signer information sections, the macro LC_PKCS8_MSG_ON_STACK is provided to allocate stack memory for a given number of signers and X.509 certificates. If more certificates or signers are found, heap is transparently allocated.
  2. The parser fills the data structure with pointers into the original PKCS#8 data blob. The caller MUST keep the original PKCS#8 data blob at the same location for the life time of the associated instance of the pkcs8_message data structure.
  3. The PKCS#8 parser API call only interprets and parses the PKCS#8 data blob. It does NOT enforce any kind of restrictions or policies. The caller MUST use the provided verification API to enforce policies on the given certificate.

The PKCS#8 message parser currently only covers signature verification to support use cases of secure boot.

For performing a proper PKCS#8 validation, execute the following steps:

  1. Initialize and load the certificate trust store by executing the following sub tasks. This assumes that each trusted public key is represented as an X.509 certificate which implies the set of sub steps must be executed for each certificate that shall be part of the trust store.

    a. Load the X.509 data blob with lc_x509_certificate_parse

    b. Register the certificate in the PKCS#8 trust store with lc_pkcs8_trust_anchor_add

  2. Load the PKCS#8 data blob using lc_pkcs8_message_parse.
  3. Validate the PKCS#8 message which verifies the encapsulated data and the certificate chain using lc_pkcs8_verify.

Macro Definition Documentation

◆ LC_PKCS8_MSG_ON_STACK

#define LC_PKCS8_MSG_ON_STACK (   name)
Value:
_Pragma("GCC diagnostic push") _Pragma( \
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
_Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
LC_ALIGNED_BUFFER(name##_ctx_buf, 8); \
struct lc_pkcs8_message *name = \
(struct lc_pkcs8_message *)name##_ctx_buf; \
_Pragma("GCC diagnostic pop")
#define LC_ALIGNED_BUFFER(name, size, alignment)
Allocate aligned stack memory.

Allocate memory for struct lc_pkcs8_message holding given number of preallocated sinfo members.

This allocation allows the PKCS8 parsing to avoid allocate memory and keep all operations on stack. In case more signers than num_sinfo or more X.509 certificates than num_x509 are parsed from the PKCS8 message, then first all pre-allocated structures are used and then new ones are allocated.

When not using this macro, which is perfectly legal, an simply allocating struct lc_pkcs8_message on stack, then for all parsed signers and X.509 certificates, a new memory entry is allocated.

Parameters
[in]nameName of stack variable

Definition at line 78 of file lc_pkcs8_common.h.

Function Documentation

◆ lc_pkcs8_decode()

int lc_pkcs8_decode ( struct lc_pkcs8_message *  pkcs8,
const uint8_t *  data,
size_t  datalen 
)

Decode a PKCS#8 message.

The function parses a PKCS#8 data buffer into a data structure that allows accessing the various data points of the PKCS#8 message.

Note
The pkcs8 data structure will contain pointers to the data buffer. I.e. the message parsing analyzes data and finds all relevant data in the raw X.509 data blob. The caller MUST therefore keep the data pointer constant as long as the pkcs8 pointer is valid.
This function only loads and parses the PKCS#8 message into the data structure to allow leancrypto to immediately access the information. In addition, the PKCS#8 decoding parses the private key into the leancrypto internal representation of the key. I.e. an ML-DSA key is parsed such that it can be directly used with all leancrypto APIs.

This API offers parsing of:

  • ML-DSA expanded key (i.e. full ML-DSA key) - RFC 9881 chapter 6
  • ML-DSA seed which is expanded into the full ML-DSA key during parsing - RFC 9881 chapter 6.
  • ML-DSA expanded and seed key where the parsing operation verifies that both correspond - RFC 9881 chapter 6.
  • For all other algorithms, the common key type
Parameters
[in,out]pkcs8The data structure that is filled with all parameters from the PKCS#8 message data buffer. The buffer must have been allocated by the caller. It is permissible to keep it on the stack.
[in]dataRaw PKCS#8 data blob following RFC5652
[in]datalenLength of the raw PKCS#8 message buffer
Returns
0 on success or < 0 on error

◆ lc_pkcs8_message_clear()

void lc_pkcs8_message_clear ( struct lc_pkcs8_message *  pkcs8)

Clear the resources used by the PKCS#8 message parsing state.

Parameters
[in]pkcs8Certificate structure to be cleared

◆ lc_pkcs8_set_privkey()

int lc_pkcs8_set_privkey ( struct lc_pkcs8_message *  pkcs8,
struct lc_x509_key_data *  privkey 
)

Set the private key for the PKCS8 message.

The function sets the pointers to the private key in the PKCS#8 message correctly. For encoding, the private key is read, for decoding, the private key buffer is filled by the parser.

Note
Without this call, the parsing or generation of a PKCS#8 message will fail as the main goal of a PKCS#8 message is to transport private keys.
Parameters
[in,out]pkcs8The PKCS#8 data structure that shall receive the private key.
[in]privkeyRaw private key data
Returns
0 on success or < 0 on error

◆ lc_pkcs8_signature_gen()

int lc_pkcs8_signature_gen ( uint8_t *  sig_data,
size_t *  siglen,
const struct lc_pkcs8_message *  pkcs8,
const uint8_t *  m,
size_t  mlen,
const struct lc_hash *  prehash_algo 
)

Generate signature over user-supplied data.

This is the PKCS#8 equivalent to lc_x509_signature_gen.

Parameters
[out]sig_dataCaller-supplied buffer with signature (it needs to be at least as large as reported by lc_x509_get_signature_size_from_sk or lc_x509_get_signature_size_from_cert)
[in,out]siglenLength of the sig_data buffer, the value will be updated such that it reflects the length of the signature.
[in]pkcs8The PKCS#8 message holding the private key
[in]mMessage to be signed
[in]mlenLength of message
[in]prehash_algoIt is permissible that the message is prehashed. If so, it is indicated by this parameter which points to the used message digest the caller used to generate the prehashed message digest. This forces the use of the Hash[ML|SLH]-DSA.
Returns
0 on success or < 0 on error