92#include "mcelcommon.h"
94#if (!defined(MCEL_CONFIG_DILITHIUM) && !defined(MCEL_CONFIG_SPHINCSPLUS))
99# define MCEL_CONFIG_DILITHIUM
102#if defined(MCEL_CONFIG_DILITHIUM)
103# include "dilithium.h"
104#elif defined(MCEL_CONFIG_SPHINCSPLUS)
105# include "sphincsplus.h"
107# error Invalid parameter set!
115#define MCEL_USE_RCS_ENCRYPTION
118#if defined(MCEL_USE_RCS_ENCRYPTION)
120# define mcel_cipher_state qsc_rcs_state
121# define mcel_cipher_dispose qsc_rcs_dispose
122# define mcel_cipher_initialize qsc_rcs_initialize
123# define mcel_cipher_keyparams qsc_rcs_keyparams
124# define mcel_cipher_set_associated qsc_rcs_set_associated
125# define mcel_cipher_transform qsc_rcs_transform
128# define mcel_cipher_state qsc_aes_gcm256_state
129# define mcel_cipher_dispose qsc_aes_gcm256_dispose
130# define mcel_cipher_initialize qsc_aes_gcm256_initialize
131# define mcel_cipher_keyparams qsc_aes_keyparams
132# define mcel_cipher_set_associated qsc_aes_gcm256_set_associated
133# define mcel_cipher_transform qsc_aes_gcm256_transform
187#if defined(MCEL_CONFIG_DILITHIUM)
192# define MCEL_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
198# define MCEL_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
204# define MCEL_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
206# if defined(QSC_DILITHIUM_S1P44)
211# define MCEL_PARAMETER_SET 1U
212# elif defined(QSC_DILITHIUM_S3P65)
217# define MCEL_PARAMETER_SET 2U
218# elif defined(QSC_DILITHIUM_S5P87)
223# define MCEL_PARAMETER_SET 3U
225# error "The parameter set is not supported!"
231# define mcel_signature_generate_keypair qsc_dilithium_generate_keypair
236# define mcel_signature_sign qsc_dilithium_sign
241# define mcel_signature_verify qsc_dilithium_verify
247# define MCEL_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_SPHINCSPLUS_PRIVATEKEY_SIZE)
253# define MCEL_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_SPHINCSPLUS_PUBLICKEY_SIZE)
259# define MCEL_ASYMMETRIC_SIGNATURE_SIZE (QSC_SPHINCSPLUS_SIGNATURE_SIZE)
265# if defined(QSC_SPHINCSPLUS_S1S128SHAKERS)
266# define MCEL_PARAMETER_SET 4U
267# elif defined(QSC_SPHINCSPLUS_S3S192SHAKERS)
268# define MCEL_PARAMETER_SET 5U
269# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS)
270# define MCEL_PARAMETER_SET 6U
271# elif defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
272# define MCEL_PARAMETER_SET 7U
274# error "The parameter set is not supported!"
281# define udif_signature_generate_keypair qsc_sphincsplus_generate_keypair
286# define udif_signature_sign qsc_sphincsplus_sign
291# define udif_signature_verify qsc_sphincsplus_verify
298#define MCEL_BLOCK_HASH_SIZE 32U
304#define MCEL_BLOCK_HEADER_ENCODED_SIZE 62U
310#define MCEL_BLOCK_ENCODED_FIXED_SIZE ((size_t)MCEL_BLOCK_HEADER_ENCODED_SIZE + ((size_t)MCEL_BLOCK_HASH_SIZE * 2U))
316#define MCEL_BLOCK_KEYID_SIZE 32U
322#define MCEL_BLOCK_VERSION 1U
328#define MCEL_CHECKPOINT_HEADER_ENCODED_SIZE 62U
334#define MCEL_CHECKPOINT_BUNDLE_FIXED_SIZE (MCEL_CHECKPOINT_HEADER_ENCODED_SIZE + (MCEL_BLOCK_HASH_SIZE * 2U))
340#define MCEL_CHECKPOINT_BUNDLE_ENCODED_SIZE (MCEL_ASYMMETRIC_SIGNATURE_SIZE + MCEL_BLOCK_HASH_SIZE + MCEL_CHECKPOINT_BUNDLE_FIXED_SIZE)
346#define MCEL_CHECKPOINT_KEYID_SIZE 32U
352#define MCEL_CHECKPOINT_SIGNED_COMMIT_SIZE (MCEL_ASYMMETRIC_SIGNATURE_SIZE + MCEL_BLOCK_HASH_SIZE)
358#define MCEL_CHECKPOINT_VERSION 1U
364#define MCEL_KEYROTATE_PAYLOAD_VERSION 0x01U
370#define MCEL_KEYROTATE_PAYLOAD_FIXED_SIZE (1U + 1U + (uint32_t)MCEL_CHECKPOINT_KEYID_SIZE + 2U)
376#define MCEL_KEYROTATE_PAYLOAD_KEY_SIZE (MCEL_KEYROTATE_PAYLOAD_FIXED_SIZE + MCEL_ASYMMETRIC_VERIFY_KEY_SIZE)
382#define MCEL_LEDGER_NAMESPACE_ID_MAX 64U
388#define MCEL_PAYLOAD_MAX_SIZE 0xFFFFFFFFUL
394#define MCEL_RCS256_KEY_SIZE 32U
400#define MCEL_RCS256_MAC_SIZE 32U
406#define MCEL_RCS_NONCE_SIZE 32U
412#define MCEL_RCS_INFO_SIZE 48U
418#define MCEL_RECORD_FLAG_ENCRYPTED 0x01U
424#define MCEL_RECORD_VERSION 1U
430#define MCEL_RECORD_KEYID_SIZE 32U
436#define MCEL_RECORD_HEADER_ENCODED_SIZE 58U
442#define MCEL_RECORD_TYPE_KEYROTATE 0x3U
448#define MCEL_SIGNED_HASH_SIZE (MCEL_ASYMMETRIC_SIGNATURE_SIZE + MCEL_BLOCK_HASH_SIZE)
454#if defined(QSC_SYSTEM_OS_WINDOWS)
455# define MCEL_STORE_LOC_BLOCKS "mcel\\blocks"
457# define MCEL_STORE_LOC_BLOCKS "mcel/blocks"
464#if defined(QSC_SYSTEM_OS_WINDOWS)
465# define MCEL_STORE_LOC_CHECKPOINTS "mcel\\checkpoints"
467# define MCEL_STORE_LOC_CHECKPOINTS "mcel/checkpoints"
474#if defined(QSC_SYSTEM_OS_WINDOWS)
475# define MCEL_STORE_LOC_HEAD "mcel\\head"
477# define MCEL_STORE_LOC_HEAD "mcel/head"
484#if defined(QSC_SYSTEM_OS_WINDOWS)
485# define MCEL_STORE_LOC_RECORDS "mcel\\records"
487# define MCEL_STORE_LOC_RECORDS "mcel/records"
552 bool (*
write)(
void*
context,
const uint8_t* loc,
size_t loclen,
const uint8_t* data,
size_t datalen);
566 bool (*
read)(
void*
context,
const uint8_t* loc,
size_t loclen, uint8_t* data,
size_t datalen,
size_t* outread);
580 bool (*
append)(
void*
context,
const uint8_t* loc,
size_t loclen,
const uint8_t* data,
size_t datalen, uint64_t* outpos);
592 bool (*
size)(
void*
context,
const uint8_t* loc,
size_t loclen, uint64_t* outlen);
743 const uint8_t* blkcommit,
const uint8_t* reccommits,
size_t reccount);
788 size_t itemcount,
const uint8_t* publickey);
804 const uint8_t* prevcommit,
const uint8_t* sigcommit,
size_t siglen);
829 const uint8_t* bundle,
size_t bundlelen,
const uint8_t* publickey);
869 size_t second,
const uint8_t* proof,
size_t prooflen);
919 const uint8_t* blkroot,
const uint8_t* prevcommit,
const uint8_t* privatekey,
bool (*rng_generate)(uint8_t*,
size_t));
932MCEL_EXPORT_API
bool mcel_checkpoint_sign(uint8_t* sigcommit,
size_t* siglen,
const uint8_t* chkcommit,
933 const uint8_t* privatekey,
bool (*rng_generate)(uint8_t*,
size_t));
946MCEL_EXPORT_API
bool mcel_checkpoint_verify(uint8_t* chkcommit,
size_t* commitlen,
const uint8_t* sigcommit,
947 size_t siglen,
const uint8_t* publickey);
973 uint64_t sequence, uint8_t flags,
const uint8_t* newkeyid,
const uint8_t* newpubkey,
size_t pubkeylen);
1015 const uint8_t* publickey, uint8_t* headbuf,
size_t headbuflen);
1033 const uint8_t* reccommits,
size_t reccount, uint8_t* blockbuf,
size_t blockbuflen, uint64_t* outpos);
1050 const uint8_t* blkroot,
const void* sigkey, uint8_t* bundlebuf,
size_t bundlebuflen, uint64_t* outpos);
1082MCEL_EXPORT_API
bool mcel_payload_commit(uint8_t* output,
bool encrypted,
const uint8_t* payload,
size_t paylen);
1114 const uint8_t* ad,
size_t adlen,
const uint8_t* key, uint8_t* nonce);
1129 const uint8_t* ad,
size_t adlen,
const uint8_t* key, uint8_t* nonce);
#define MCEL_CHECKPOINT_KEYID_SIZE
The checkpoint key identifier size in bytes.
Definition mcel.h:346
MCEL_EXPORT_API bool mcel_block_encode(uint8_t *output, size_t outlen, const mcel_block_header *header, const uint8_t *blkroot, const uint8_t *blkcommit, const uint8_t *reccommits, size_t reccount)
Serialize a sealed MCEL block into a canonical byte string.
Definition mcel.c:129
MCEL_EXPORT_API bool mcel_block_seal(uint8_t *blkroot, uint8_t *blkcommit, const mcel_block_header *header, const uint8_t *reccommits, size_t reccount)
Seal a MCEL block by computing the Merkle root and block commitment.
Definition mcel.c:226
MCEL_EXPORT_API bool mcel_checkpoint_prove_consistency(uint8_t *proof, size_t prooflen, const uint8_t *leaves, size_t oldcount, size_t newcount)
Generate a MCEL Merkle consistency proof between two tree sizes.
Definition mcel.c:742
MCEL_EXPORT_API bool mcel_record_decrypt_payload(uint8_t *output, size_t outlen, const uint8_t *ciphertext, size_t ctlen, const uint8_t *ad, size_t adlen, const uint8_t *key, uint8_t *nonce)
Decrypt a record payload using the AEAD cipher.
Definition mcel.c:1562
#define MCEL_LEDGER_NAMESPACE_ID_MAX
The maximum namespace identifier size in bytes.
Definition mcel.h:382
MCEL_EXPORT_API bool mcel_ledger_get_checkpoint_head(mcel_ledger_state *state, uint8_t *head_commit, mcel_checkpoint_header *head_header)
Get the current checkpoint head from the ledger state.
Definition mcel.c:1166
MCEL_EXPORT_API size_t mcel_block_encoded_size(size_t reccount)
Get the required buffer size for an encoded MCEL block.
Definition mcel.c:173
MCEL_EXPORT_API bool mcel_record_commit(uint8_t *output, const mcel_record_header *header, const uint8_t *pldcommit)
Compute a MCEL record commitment from a record header and payload commitment.
Definition mcel.c:1656
MCEL_EXPORT_API bool mcel_ledger_seal_block(mcel_ledger_state *state, uint8_t *blkroot, uint8_t *blkcommit, const mcel_block_header *header, const uint8_t *reccommits, size_t reccount, uint8_t *blockbuf, size_t blockbuflen, uint64_t *outpos)
Seal a block from record commitments and write the sealed block through the storage callbacks.
Definition mcel.c:1189
MCEL_EXPORT_API bool mcel_payload_commit(uint8_t *output, bool encrypted, const uint8_t *payload, size_t paylen)
Compute a MCEL payload commitment.
Definition mcel.c:1450
mcel_policy_ops
The MCEL policy operation identifiers.
Definition mcel.h:508
@ mcel_policyop_append_record
Definition mcel.h:509
@ mcel_policyop_seal_checkpoint
Definition mcel.h:510
MCEL_EXPORT_API bool mcel_store_callbacks_initialize(mcel_store_callbacks *output, const mcel_store_callbacks *input, void *context)
Initialize and validate the MCEL storage callback table.
Definition mcel.c:1686
MCEL_EXPORT_API bool mcel_ledger_initialize(mcel_ledger_state *state, const mcel_store_callbacks *store, const uint8_t *nsid, size_t nsidlen, const uint8_t *publickey, uint8_t *headbuf, size_t headbuflen)
Initialize (open) a MCEL ledger namespace and load the checkpoint head if present.
Definition mcel.c:1093
MCEL_EXPORT_API size_t mcel_keyrotate_record_create(mcel_record_header *header, uint8_t *payload, size_t payload_len, uint64_t sequence, uint8_t flags, const uint8_t *newkeyid, const uint8_t *newpubkey, size_t pubkeylen)
Create a key rotation record header and payload.
Definition mcel.c:960
#define MCEL_BLOCK_KEYID_SIZE
The MCEL block key identifier size in bytes.
Definition mcel.h:316
MCEL_EXPORT_API size_t mcel_keyrotate_payload_size(size_t pubkeylen)
Get the required buffer size for a key rotation record payload.
Definition mcel.c:946
MCEL_EXPORT_API bool mcel_checkpoint_verify(uint8_t *chkcommit, size_t *commitlen, const uint8_t *sigcommit, size_t siglen, const uint8_t *publickey)
Verify a MCEL signed checkpoint commitment using Dilithium.
Definition mcel.c:920
MCEL_EXPORT_API bool mcel_policy_apply(mcel_policy_errors *perr, const mcel_policy *policy, const mcel_policy_context *state, mcel_policy_ops op, const mcel_record_header *recordhdr, const mcel_checkpoint_header *checkpointhdr)
Apply namespace policy rules to a MCEL operation.
Definition mcel.c:1474
MCEL_EXPORT_API bool mcel_checkpoint_chain_link_verify(const uint8_t *prevcommit, const uint8_t *curprevcommit, const mcel_checkpoint_header *prevhdr, const mcel_checkpoint_header *curhdr)
Verify the chain linkage between two verified checkpoints.
Definition mcel.c:447
mcel_record_types
The MCEL record type identifiers.
Definition mcel.h:495
@ mcel_record_type_checkpoint
Definition mcel.h:497
@ mcel_record_type_policy
Definition mcel.h:500
@ mcel_record_type_none
Definition mcel.h:496
@ mcel_record_type_event
Definition mcel.h:498
@ mcel_record_type_key_rotate
Definition mcel.h:499
MCEL_EXPORT_API void mcel_record_encrypt_payload(uint8_t *output, size_t outlen, const uint8_t *plaintext, size_t ptlen, const uint8_t *ad, size_t adlen, const uint8_t *key, uint8_t *nonce)
Encrypt a record payload using the AEAD cipher.
Definition mcel.c:1630
MCEL_EXPORT_API bool mcel_checkpoint_bundle_encode(uint8_t *output, size_t outlen, const mcel_checkpoint_header *header, const uint8_t *blkroot, const uint8_t *prevcommit, const uint8_t *sigcommit, size_t siglen)
Serialize a MCEL checkpoint bundle into a canonical byte string.
Definition mcel.c:323
mcel_policy_errors
The MCEL policy error values.
Definition mcel.h:518
@ mcel_policyerr_invalid_parameter
Definition mcel.h:520
@ mcel_policyerr_record_type_denied
Definition mcel.h:521
@ mcel_policyerr_keyid_mismatch
Definition mcel.h:526
@ mcel_policyerr_sequence_invalid
Definition mcel.h:524
@ mcel_policyerr_timestamp_invalid
Definition mcel.h:525
@ mcel_policyerr_plaintext_denied
Definition mcel.h:523
@ mcel_policyerr_none
Definition mcel.h:519
@ mcel_policyerr_payload_too_large
Definition mcel.h:522
MCEL_EXPORT_API bool mcel_block_encode_header(uint8_t *output, const mcel_block_header *header)
Encode a MCEL block header using canonical fixed-size encoding.
Definition mcel.c:189
MCEL_EXPORT_API bool mcel_checkpoint_sign(uint8_t *sigcommit, size_t *siglen, const uint8_t *chkcommit, const uint8_t *privatekey, bool(*rng_generate)(uint8_t *, size_t))
Sign a MCEL checkpoint commitment using Dilithium.
Definition mcel.c:899
#define MCEL_RECORD_KEYID_SIZE
The record signer or policy key identifier size in bytes.
Definition mcel.h:430
MCEL_EXPORT_API bool mcel_checkpoint_audit_path_verify(uint8_t *outheadcommit, const mcel_checkpoint_audit_item *items, size_t itemcount, const uint8_t *publickey)
Verify an ordered audit path of MCEL checkpoint bundles.
Definition mcel.c:251
MCEL_EXPORT_API bool mcel_record_encode_header(uint8_t *output, const mcel_record_header *header)
Encode a MCEL record header using canonical fixed-size encoding.
Definition mcel.c:1594
MCEL_EXPORT_API size_t mcel_checkpoint_bundle_encoded_size(size_t siglen)
Get the required buffer size for an encoded MCEL checkpoint bundle.
Definition mcel.c:312
MCEL_EXPORT_API bool mcel_ledger_append_record(mcel_ledger_state *state, uint8_t *reccommit, uint64_t *outpos, const mcel_record_header *header, const uint8_t *payload, size_t paylen)
Append a record to the ledger record log and return its commitment.
Definition mcel.c:1009
#define MCEL_BLOCK_HASH_SIZE
The MCEL 256-bit digest size in bytes.
Definition mcel.h:298
MCEL_EXPORT_API bool mcel_checkpoint_commit(uint8_t *output, const mcel_checkpoint_header *header, const uint8_t *blkroot, const uint8_t *pldcommit)
Compute a MCEL checkpoint commitment from a checkpoint header, block root, and previous checkpoint co...
Definition mcel.c:485
MCEL_EXPORT_API bool mcel_block_commit(uint8_t *output, const mcel_block_header *header, const uint8_t *blkroot)
Compute a MCEL block commitment from a block header and Merkle root.
Definition mcel.c:99
MCEL_EXPORT_API bool mcel_checkpoint_encode_header(uint8_t *output, const mcel_checkpoint_header *header)
Encode a MCEL checkpoint header using canonical fixed-size encoding.
Definition mcel.c:706
MCEL_EXPORT_API bool mcel_ledger_seal_checkpoint(mcel_ledger_state *state, uint8_t *chkcommit, const mcel_checkpoint_header *header, const uint8_t *blkroot, const void *sigkey, uint8_t *bundlebuf, size_t bundlebuflen, uint64_t *outpos)
Seal a checkpoint from a sealed block root and update the ledger head.
Definition mcel.c:1249
MCEL_EXPORT_API bool mcel_checkpoint_bundle_verify(uint8_t *chkcommit, mcel_checkpoint_header *header, uint8_t *blkroot, uint8_t *prevcommit, const uint8_t *bundle, size_t bundlelen, const uint8_t *publickey)
Verify a serialized MCEL checkpoint bundle.
Definition mcel.c:373
MCEL_EXPORT_API bool mcel_checkpoint_consistency_verify(const uint8_t *firstroot, const uint8_t *secondroot, size_t first, size_t second, const uint8_t *proof, size_t prooflen)
Verify a MCEL Merkle consistency proof between two tree roots.
Definition mcel.c:517
MCEL_EXPORT_API bool mcel_ledger_verify_integrity(mcel_ledger_state *state, uint8_t *headbuf, size_t headbuflen, const mcel_checkpoint_audit_item *audit, size_t auditcount)
Verify the cryptographic integrity of the ledger state.
Definition mcel.c:1348
MCEL_EXPORT_API bool mcel_checkpoint_decode_header(mcel_checkpoint_header *header, const uint8_t *input)
Decode a MCEL checkpoint header from its canonical encoding.
Definition mcel.c:671
MCEL_EXPORT_API bool mcel_checkpoint_seal(uint8_t *chkcommit, uint8_t *sigcommit, size_t *siglen, const mcel_checkpoint_header *header, const uint8_t *blkroot, const uint8_t *prevcommit, const uint8_t *privatekey, bool(*rng_generate)(uint8_t *, size_t))
Seal a MCEL checkpoint from a sealed block by generating the checkpoint commitment and signing it.
Definition mcel.c:868
The MCEL audit path item container.
Definition mcel.h:630
size_t bundlelen
Definition mcel.h:632
const uint8_t * bundle
Definition mcel.h:631
The MCEL ledger instance state.
Definition mcel.h:659
uint8_t have_head
Definition mcel.h:666
size_t nsidlen
Definition mcel.h:662
const uint8_t * publickey
Definition mcel.h:663
mcel_store_callbacks store
Definition mcel.h:660
mcel_checkpoint_header head_header
Definition mcel.h:665
uint8_t head_commit[MCEL_BLOCK_HASH_SIZE]
Definition mcel.h:664
uint8_t nsid[MCEL_LEDGER_NAMESPACE_ID_MAX]
Definition mcel.h:661
The MCEL policy context (caller-maintained).
Definition mcel.h:692
uint64_t last_record_timestamp
Definition mcel.h:696
uint8_t have_checkpoint
Definition mcel.h:693
uint64_t last_record_sequence
Definition mcel.h:695
mcel_checkpoint_header checkpoint
Definition mcel.h:694
The MCEL namespace policy container.
Definition mcel.h:674
uint32_t allowed_record_mask
Definition mcel.h:676
uint8_t require_encryption
Definition mcel.h:677
uint8_t enforce_monotonic_time
Definition mcel.h:678
uint8_t enforce_monotonic_seq
Definition mcel.h:679
size_t max_payload_size
Definition mcel.h:675
uint8_t enforce_keyid_link
Definition mcel.h:680
The MCEL storage callback table.
Definition mcel.h:538
bool(* flush)(void *context, const uint8_t *loc, size_t loclen)
Flush any buffered data for a logical location.
Definition mcel.h:603
bool(* read)(void *context, const uint8_t *loc, size_t loclen, uint8_t *data, size_t datalen, size_t *outread)
Read a complete object from a logical location.
Definition mcel.h:566
bool(* size)(void *context, const uint8_t *loc, size_t loclen, uint64_t *outlen)
Get the size of an object at a logical location.
Definition mcel.h:592
bool(* append)(void *context, const uint8_t *loc, size_t loclen, const uint8_t *data, size_t datalen, uint64_t *outpos)
Append bytes to an append-only object.
Definition mcel.h:580
bool(* write)(void *context, const uint8_t *loc, size_t loclen, const uint8_t *data, size_t datalen)
Write a complete object at a logical location.
Definition mcel.h:552
void * context
Definition mcel.h:539