45#ifndef PQCEE_SAFEQUARD_API_H
46#define PQCEE_SAFEQUARD_API_H
56#define DLMS_CHALLENGE_SIZE 64
61#define AES_256_KEY_SIZE 32
66#define ML_DSA_65_SEED_SIZE 32
71#define ML_DSA_65_PUBLIC_KEY_SIZE 1952
76#define ML_DSA_65_SIGNATURE_SIZE 3309
81#define ML_KEM_768_SEED_SIZE 64
86#define ML_KEM_768_ENCAP_KEY_SIZE 1184
91#define ML_KEM_768_DECAP_KEY_SIZE 2400
96#define ML_KEM_768_CIPHERTEXT_SIZE 1088
101#define ML_KEM_SHARED_SECRET_SIZE 32
106#define ML_KEM_SHARED_SECRET_IV_SIZE 16
111#define ML_KEM_SHARED_SECRET_AUTH_IV_SIZE 12
116#define ML_KEM_SHARED_SECRET_AUTH_TAG_SIZE 16
121#define SHA_512_BLOCK_SIZE 128
126#define SHA_512_DIGEST_SIZE 64
131#define SAFEQUARD_SUCCESS 0
136#define SAFEQUARD_ERROR_INVALID_LOG_LEVEL -1
141#define SAFEQUARD_ERROR_GENERIC -1000
146#define SAFEQUARD_ERROR_NOT_SUPPORTED -1001
151#define SAFEQUARD_ERROR_NOT_PERMITTED -1002
156#define SAFEQUARD_ERROR_BUFFER_TOO_SMALL -1003
161#define SAFEQUARD_ERROR_ALREADY_EXISTS -1004
166#define SAFEQUARD_ERROR_DOES_NOT_EXIST -1005
171#define SAFEQUARD_ERROR_BAD_STATE -1006
176#define SAFEQUARD_ERROR_INVALID_ARGUMENT -1007
181#define SAFEQUARD_ERROR_INSUFFICIENT_MEMORY -1008
186#define SAFEQUARD_ERROR_INSUFFICIENT_STORAGE -1009
191#define SAFEQUARD_ERROR_COMMUNICATION_FAILURE -1010
196#define SAFEQUARD_ERROR_STORAGE_FAILURE -1011
201#define SAFEQUARD_ERROR_DATA_CORRUPT -1012
206#define SAFEQUARD_ERROR_DATA_INVALID -1013
211#define SAFEQUARD_ERROR_HARDWARE_FAILURE -1014
216#define SAFEQUARD_ERROR_CORRUPTION_DETECTED -1015
221#define SAFEQUARD_ERROR_INSUFFICIENT_ENTROPY -1016
226#define SAFEQUARD_ERROR_INVALID_SIGNATURE -1017
231#define SAFEQUARD_ERROR_INVALID_PADDING -1018
236#define SAFEQUARD_ERROR_INSUFFICIENT_DATA -1019
241#define SAFEQUARD_ERROR_INVALID_HANDLE -1020
246#define SAFEQUARD_ERROR_BACKEND -1099
252#define SAFEQUARD_ERROR_MALFORMED_DER -1100
257#define SAFEQUARD_ERROR_TRAILING_DATA -1101
264#define SAFEQUARD_ERROR_UNSUPPORTED_ALGORITHM -1102
270#define SAFEQUARD_ERROR_INVALID_PUBLIC_KEY -1103
277#define SAFEQUARD_ERROR_ROOT_NOT_SELF_ISSUED -1105
286#define SAFEQUARD_ERROR_INVALID_ROOT_SIGNATURE -1106
292#define SAFEQUARD_ERROR_INVALID_TIME -1107
297#define SAFEQUARD_ERROR_TIME_SOURCE_UNAVAILABLE -1108
303#define SAFEQUARD_ERROR_CERTIFICATE_NOT_YET_VALID -1109
309#define SAFEQUARD_ERROR_CERTIFICATE_EXPIRED -1110
315#define SAFEQUARD_ERROR_ISSUER_MISMATCH -1111
323#define SAFEQUARD_ERROR_CERTIFICATE_MUST_BE_V3 -1112
329#define SAFEQUARD_ERROR_DUPLICATE_EXTENSION -1113
338#define SAFEQUARD_ERROR_UNKNOWN_CRITICAL_EXTENSION -1114
344#define SAFEQUARD_ERROR_MISSING_BASIC_CONSTRAINTS -1115
349#define SAFEQUARD_ERROR_BASIC_CONSTRAINTS_NOT_CRITICAL -1116
355#define SAFEQUARD_ERROR_EXPECTED_CA -1117
360#define SAFEQUARD_ERROR_LEAF_IS_CA -1118
365#define SAFEQUARD_ERROR_MISSING_KEY_USAGE -1119
370#define SAFEQUARD_ERROR_KEY_USAGE_NOT_CRITICAL -1120
376#define SAFEQUARD_ERROR_INVALID_CA_KEY_USAGE -1121
385#define SAFEQUARD_ERROR_INVALID_LEAF_KEY_USAGE -1122
391#define SAFEQUARD_ERROR_MISSING_EXTENDED_KEY_USAGE -1123
397#define SAFEQUARD_ERROR_EXTENDED_KEY_USAGE_NOT_CRITICAL -1124
403#define SAFEQUARD_ERROR_INVALID_CODE_SIGNING_USAGE -1125
409#define SAFEQUARD_ERROR_PATH_LENGTH_EXCEEDED -1126
415#define SAFEQUARD_ERROR_TOO_MANY_INTERMEDIATES -1127
421#define SAFEQUARD_ERROR_INVALID_PKCS8 -1128
430#define SAFEQUARD_ERROR_PKCS8_DECRYPTION_FAILED -1129
436#define SAFEQUARD_ERROR_INVALID_PRIVATE_KEY_ALGORITHM -1130
444#define SAFEQUARD_ERROR_SEED_NOT_AVAILABLE -1131
450#define SAFEQUARD_ERROR_INVALID_SEED_ENCODING -1132
455#define SAFEQUARD_LOG_LEVEL_ERROR 1
460#define SAFEQUARD_LOG_LEVEL_WARN 2
465#define SAFEQUARD_LOG_LEVEL_INFO 3
470#define SAFEQUARD_LOG_LEVEL_DEBUG 4
478#define SAFEQUARD_LOG_MESSAGE_CAPACITY 64
527 size_t signature_size);
548 size_t public_key_size,
551 const uint8_t *signature,
552 size_t signature_size);
582 const uint8_t *trusted_cert,
583 size_t trusted_cert_size,
584 const uint8_t *leaf_cert,
585 size_t leaf_cert_size,
614 const uint8_t *signature,
615 size_t signature_size);
648 size_t encap_key_size,
688 size_t encap_key_size,
689 const uint8_t *other_info,
690 size_t other_info_size,
692 size_t ciphertext_size,
739 const uint8_t *other_info,
740 size_t other_info_size,
741 const uint8_t *ciphertext,
742 size_t ciphertext_size,
744 size_t ss_context_size,
770 const uint8_t *encap_key,
771 size_t encap_key_size,
773 size_t ciphertext_size,
774 uint8_t *encrypted_message,
775 size_t encrypted_message_size);
803 const uint8_t *encrypted_message,
804 size_t encrypted_message_size,
805 const uint8_t *ciphertext,
806 size_t ciphertext_size,
808 size_t message_size);
838 const uint8_t *message,
840 uint8_t *encrypted_message,
841 size_t encrypted_message_size);
872 const uint8_t *encrypted_message,
873 size_t encrypted_message_size,
875 size_t message_size);
933 const uint8_t *chunk,
int safequard_dlms_key_agreement_decap(const uint8_t *context, size_t context_size, const uint8_t *other_info, size_t other_info_size, const uint8_t *ciphertext, size_t ciphertext_size, uint8_t *ss_context, size_t ss_context_size, size_t *out)
Decap an ML-KEM-768 decapsulation key for DLMS key agreement.
void safequard_panic_wrap(void)
Panic the current thread.
int safequard_mlkem768_init(uint8_t *context, size_t context_size, uint8_t *encap_key, size_t encap_key_size, size_t *out)
Generate a new ML-KEM-768 keypair for encrypting and decrypting messages.
int safequard_sha512_update(uint8_t *context, size_t context_size, const uint8_t *chunk, size_t chunk_size)
Update a SHA-512 context for streaming hashing operations.
int safequard_mlkem768_finalise(uint8_t *context, size_t context_size)
Finalise the decryption process.
int safequard_mldsa65_sign(const uint8_t *seed, size_t seed_size, const uint8_t *data, size_t data_size, uint8_t *signature, size_t signature_size)
Sign an ML-DSA-65 message.
int safequard_dlms_auth_encrypt(const uint8_t *context, size_t context_size, const uint8_t *aad, size_t aad_size, const uint8_t *message, size_t message_size, uint8_t *encrypted_message, size_t encrypted_message_size)
Encrypt with authentication a message using AES-256-GCM.
int safequard_sha512(const uint8_t *message, size_t message_size, uint8_t *hash, size_t hash_size)
Compute a SHA-512 digest of a message.
int safequard_init(uint32_t max_log_level)
Initialise the safequard_api library.
int safequard_mlkem768_decrypt(const uint8_t *context, size_t context_size, const uint8_t *encrypted_message, size_t encrypted_message_size, const uint8_t *ciphertext, size_t ciphertext_size, uint8_t *message, size_t message_size)
Decrypt an ML-KEM-768 encrypted message.
int safequard_sha512_init(uint8_t *context, size_t context_size, size_t *out)
Generate a new SHA-512 context for streaming hash operations.
int safequard_mlkem768_encrypt(const uint8_t *message, size_t message_size, const uint8_t *encap_key, size_t encap_key_size, uint8_t *ciphertext, size_t ciphertext_size, uint8_t *encrypted_message, size_t encrypted_message_size)
Encrypt a message using an encapsulation key.
int safequard_dlms_generate_challenge(uint8_t *challenge, size_t challenge_size)
Generate a 64-byte challenge for DLMS authentication.
int safequard_fv_import_key(uint8_t *context, size_t context_size, const uint8_t *trusted_cert, size_t trusted_cert_size, const uint8_t *leaf_cert, size_t leaf_cert_size, size_t *out)
Import an ML-DSA-65 public key to prepare for verification.
int safequard_sha512_finalise(uint8_t *context, size_t context_size, uint8_t *hash, size_t hash_size)
Finalise a SHA-512 operation and receive the hash. context is reset if the operation is successful.
void safequard_log_message(uint32_t level, const char *message, size_t message_length)
Log a message.
int safequard_dlms_key_agreement_encap(const uint8_t *encap_key, size_t encap_key_size, const uint8_t *other_info, size_t other_info_size, uint8_t *ciphertext, size_t ciphertext_size, uint8_t *context, size_t context_size, size_t *out)
Encap an ML-KEM-768 encapsulation key for DLMS key agreement.
int safequard_fv_verify(const uint8_t *context, size_t context_size, const uint8_t *data, size_t data_size, const uint8_t *signature, size_t signature_size)
Verify an ML-DSA-65-signed data using a certificate trust chain.
int64_t get_current_time(void)
Get the current unix time.
int safequard_dlms_auth_decrypt(const uint8_t *context, size_t context_size, const uint8_t *aad, size_t aad_size, const uint8_t *encrypted_message, size_t encrypted_message_size, uint8_t *message, size_t message_size)
Decrypt with authentication a message using AES-256-GCM.
int safequard_mldsa65_verify(const uint8_t *public_key, size_t public_key_size, const uint8_t *data, size_t data_size, const uint8_t *signature, size_t signature_size)
Verify an ML-DSA-65-signed data.