BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
|
Backend cryptography implementation. More...
#include <BPSecLib_Private.h>
#include <CryptoInterface.h>
#include <m-dict.h>
#include <openssl/err.h>
#include <openssl/rand.h>
Go to the source code of this file.
Data Structures | |
struct | BSLB_CryptoKey_s |
Struct to hold private key information. More... | |
Typedefs | |
typedef struct BSLB_CryptoKey_s | BSLB_CryptoKey_t |
Struct to hold private key information. | |
Functions | |
static int | BSLB_CryptoKey_Deinit (BSLB_CryptoKey_t *key) |
void | BSL_CryptoInit (void) |
Initialize the crypto subsystem. | |
void | BSL_CryptoDeinit (void) |
Deinitialize the crypto subsystem. | |
int | BSL_Crypto_UnwrapKey (BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_key_plaintext, size_t key_id, size_t aes_variant) |
int | BSL_Crypto_WrapKey (BSL_Data_t *wrapped_key, BSL_Data_t cek, size_t content_key_id, size_t aes_variant) |
int | BSL_AuthCtx_Init (BSL_AuthCtx_t *hmac_ctx, uint64_t keyid, BSL_CryptoCipherSHAVariant_e sha_var) |
Initialize HMAC context resources and set private key and SHA variant. | |
int | BSL_AuthCtx_DigestBuffer (BSL_AuthCtx_t *hmac_ctx, const void *data, size_t data_len) |
Input data to HMAC sign to context. | |
int | BSL_AuthCtx_DigestSeq (BSL_AuthCtx_t *hmac_ctx, BSL_SeqReader_t *reader) |
Input data to HMAC sign to context. | |
int | BSL_AuthCtx_Finalize (BSL_AuthCtx_t *hmac_ctx, void **hmac, size_t *hmac_len) |
Finalize HMAC tag. | |
int | BSL_AuthCtx_Deinit (BSL_AuthCtx_t *hmac_ctx) |
Deinitialize HMAC context resources. | |
int | BSL_Cipher_Init (BSL_Cipher_t *cipher_ctx, BSL_CipherMode_e enc, BSL_CryptoCipherAESVariant_e aes_var, const void *init_vec, int iv_len, BSL_Data_t content_enc_key) |
Initialize crypto context resources and set as encoding or decoding. | |
int | BSL_Cipher_AddAAD (BSL_Cipher_t *cipher_ctx, const void *aad, int aad_len) |
Add additional authenticated data (AAD) to cipher context. | |
int | BSL_Cipher_AddData (BSL_Cipher_t *cipher_ctx, BSL_Data_t plaintext, BSL_Data_t ciphertext) |
int | BSL_Cipher_AddSeq (BSL_Cipher_t *cipher_ctx, BSL_SeqReader_t *reader, BSL_SeqWriter_t *writer) |
Add data to encrypt or decrypt to the context sequentially. | |
int | BSL_Cipher_GetTag (BSL_Cipher_t *cipher_ctx, void **tag) |
Get the tag of the crypto operation. | |
int | BSL_Cipher_SetTag (BSL_Cipher_t *cipher_ctx, const void *tag) |
Set the tag of the crypto operation. | |
int | BSL_Cipher_FinalizeData (BSL_Cipher_t *cipher_ctx, BSL_Data_t *extra) |
int | BSL_Cipher_FinalizeSeq (BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer) |
Finalize crypto operation. | |
int | BSL_Cipher_Deinit (BSL_Cipher_t *cipher_ctx) |
De-initialize crypto context resources. | |
int | BSL_Crypto_GenKey (uint8_t *key_buffer, size_t key_length) |
int | BSL_Crypto_GenIV (void *buf, int size) |
Generate initialization vector (IV) for AES-GCM for BCBs. | |
int | BSL_Crypto_AddRegistryKey (uint64_t keyid, const uint8_t *secret, size_t secret_len) |
Add a new key to the crypto key registry. | |
int | BSLB_Crypto_GetRegistryKey (uint64_t keyid, const uint8_t **secret, size_t *secret_len) |
Get pointers to an existing key, if present. | |
Variables | |
static BSLB_CryptoKeyDict_t | StaticKeyRegistry |
Crypto key registry. | |
static pthread_mutex_t | StaticCryptoMutex = PTHREAD_MUTEX_INITIALIZER |
Backend cryptography implementation.
Definition in file CryptoInterface.c.
int BSL_AuthCtx_Deinit | ( | BSL_AuthCtx_t * | hmac_ctx | ) |
Deinitialize HMAC context resources.
[in,out] | hmac_ctx | pointer to hmac context struct to add data to |
Definition at line 250 of file CryptoInterface.c.
References BSL_AuthCtx_s::libhandle.
Referenced by BSLX_BIB_GenHMAC().
int BSL_AuthCtx_DigestBuffer | ( | BSL_AuthCtx_t * | hmac_ctx, |
const void * | data, | ||
size_t | data_len | ||
) |
Input data to HMAC sign to context.
[in,out] | hmac_ctx | pointer to hmac context struct to add data to |
[in] | data | buffer containing data to sign |
data_len | length of incoming data buffer |
Definition at line 214 of file CryptoInterface.c.
References BSL_AuthCtx_s::libhandle.
Referenced by BSLX_BIB_GenHMAC().
int BSL_AuthCtx_DigestSeq | ( | BSL_AuthCtx_t * | hmac_ctx, |
BSL_SeqReader_t * | reader | ||
) |
Input data to HMAC sign to context.
[in,out] | hmac_ctx | pointer to hmac context struct to add data to |
[in] | reader | sequential reader over data to sign |
Definition at line 223 of file CryptoInterface.c.
References BSL_AuthCtx_s::block_size, BSL_SeqReader_Get(), and BSL_AuthCtx_s::libhandle.
int BSL_AuthCtx_Finalize | ( | BSL_AuthCtx_t * | hmac_ctx, |
void ** | hmac, | ||
size_t * | hmac_len | ||
) |
Finalize HMAC tag.
[in,out] | hmac_ctx | pointer to hmac context struct to add data to |
[out] | hmac | ptr to hmac tag |
[out] | hmac_len | ptr to tag length |
Definition at line 237 of file CryptoInterface.c.
References BSL_AuthCtx_s::libhandle.
Referenced by BSLX_BIB_GenHMAC().
int BSL_AuthCtx_Init | ( | BSL_AuthCtx_t * | hmac_ctx, |
uint64_t | keyid, | ||
BSL_CryptoCipherSHAVariant_e | sha_var | ||
) |
Initialize HMAC context resources and set private key and SHA variant.
[in,out] | hmac_ctx | pointer to hmac context struct to init and set |
keyid | ID of private key to use | |
[in] | sha_var | SHA variant, see RFC9173 [2] |
Definition at line 169 of file CryptoInterface.c.
References BSL_AuthCtx_s::block_size, BSL_ERR_FAILURE, BSL_ERR_NOT_FOUND, BSL_LOG_ERR, BSL_AuthCtx_s::libhandle, BSLB_CryptoKey_s::pkey, BSL_AuthCtx_s::SHA_variant, and StaticKeyRegistry.
Referenced by BSLX_BIB_GenHMAC().
int BSL_Cipher_AddAAD | ( | BSL_Cipher_t * | cipher_ctx, |
const void * | aad, | ||
int | aad_len | ||
) |
Add additional authenticated data (AAD) to cipher context.
cipher_ctx | pointer to context to add AAD to |
aad | pointer to AAD |
aad_len | length of AAD |
Definition at line 296 of file CryptoInterface.c.
References BSL_Cipher_s::libhandle.
int BSL_Cipher_AddData | ( | BSL_Cipher_t * | cipher_ctx, |
BSL_Data_t | plaintext, | ||
BSL_Data_t | ciphertext | ||
) |
Definition at line 305 of file CryptoInterface.c.
References BSL_Data_s::len, BSL_Cipher_s::libhandle, and BSL_Data_s::ptr.
int BSL_Cipher_AddSeq | ( | BSL_Cipher_t * | cipher_ctx, |
BSL_SeqReader_t * | reader, | ||
BSL_SeqWriter_t * | writer | ||
) |
Add data to encrypt or decrypt to the context sequentially.
cipher_ctx | pointer to context to add data to | |
[in] | reader | pointer to sequential reader - input to crypto operation is |
[in,out] | writer | pointer to sequential writer - output of crypto operation will be writter |
Definition at line 316 of file CryptoInterface.c.
References BSL_Cipher_s::block_size, BSL_SeqReader_Get(), BSL_SeqWriter_Put(), and BSL_Cipher_s::libhandle.
int BSL_Cipher_Deinit | ( | BSL_Cipher_t * | cipher_ctx | ) |
De-initialize crypto context resources.
cipher_ctx | pointer to context to deinitialize |
Definition at line 396 of file CryptoInterface.c.
References BSL_SUCCESS, and BSL_Cipher_s::libhandle.
int BSL_Cipher_FinalizeData | ( | BSL_Cipher_t * | cipher_ctx, |
BSL_Data_t * | extra | ||
) |
Definition at line 352 of file CryptoInterface.c.
int BSL_Cipher_FinalizeSeq | ( | BSL_Cipher_t * | cipher_ctx, |
BSL_SeqWriter_t * | writer | ||
) |
Finalize crypto operation.
Finalize may or may not add data to writer depending on implementation.
cipher_ctx | pointer to context to finalize | |
[out] | writer | additional written data |
Definition at line 379 of file CryptoInterface.c.
References BSL_SeqWriter_Put(), and BSL_Cipher_s::libhandle.
int BSL_Cipher_GetTag | ( | BSL_Cipher_t * | cipher_ctx, |
void ** | tag | ||
) |
Get the tag of the crypto operation.
cipher_ctx | pointer to context to get tag from | |
[out] | tag | will contain tag information upon successful function completion |
Definition at line 335 of file CryptoInterface.c.
References BSL_Cipher_s::libhandle.
int BSL_Cipher_Init | ( | BSL_Cipher_t * | cipher_ctx, |
BSL_CipherMode_e | enc, | ||
BSL_CryptoCipherAESVariant_e | aes_var, | ||
const void * | init_vec, | ||
int | iv_len, | ||
BSL_Data_t | content_enc_key | ||
) |
Initialize crypto context resources and set as encoding or decoding.
cipher_ctx | pointer to context to initialize |
aes_var | AES GCM variant to use |
enc | enum for BSL_CRYPTO_ENCRYPT or BSL_CRYPTO_DECRYPT |
init_vec | pointer to initialization vector (IV) data |
iv_len | length of IV data |
content_enc_key | AES key to use as Content Encryption Key. |
Definition at line 256 of file CryptoInterface.c.
References BSL_Cipher_s::AES_variant, BSL_Cipher_s::block_size, BSL_CRYPTO_ENCRYPT, BSL_ERR_FAILURE, BSL_LOG_ERR, BSL_Cipher_s::enc, BSL_Data_s::len, BSL_Cipher_s::libhandle, and BSL_Data_s::ptr.
int BSL_Cipher_SetTag | ( | BSL_Cipher_t * | cipher_ctx, |
const void * | tag | ||
) |
Set the tag of the crypto operation.
Tag length is always 16 bytes
cipher_ctx | pointer to context to set tag of | |
[in] | tag | pointer to tag |
Definition at line 342 of file CryptoInterface.c.
References BSL_LOG_INFO, and BSL_Cipher_s::libhandle.
int BSL_Crypto_AddRegistryKey | ( | uint64_t | keyid, |
const uint8_t * | secret, | ||
size_t | secret_len | ||
) |
Add a new key to the crypto key registry.
keyid | key ID that crypto functions will use to access key |
secret | raw key data |
secret_len | length of raw key |
Definition at line 432 of file CryptoInterface.c.
References BSL_Data_CopyFrom(), BSL_Data_Init(), BSL_LOG_ERR, BSLB_CryptoKey_s::pkey, BSLB_CryptoKey_s::raw, and StaticKeyRegistry.
int BSL_Crypto_GenIV | ( | void * | buf, |
int | size | ||
) |
Generate initialization vector (IV) for AES-GCM for BCBs.
[in,out] | buf | to write iv to |
size | size in bytes of iv (MUST be between 8-16, SHOULD be 12 [2]) |
Definition at line 419 of file CryptoInterface.c.
int BSL_Crypto_GenKey | ( | uint8_t * | key_buffer, |
size_t | key_length | ||
) |
Definition at line 404 of file CryptoInterface.c.
int BSL_Crypto_UnwrapKey | ( | BSL_Data_t * | unwrapped_key_output, |
BSL_Data_t | wrapped_key_plaintext, | ||
size_t | key_id, | ||
size_t | aes_variant | ||
) |
Definition at line 74 of file CryptoInterface.c.
References BSL_LOG_ERR, BSLB_Crypto_GetRegistryKey(), BSL_Data_s::len, and BSL_Data_s::ptr.
int BSL_Crypto_WrapKey | ( | BSL_Data_t * | wrapped_key, |
BSL_Data_t | cek, | ||
size_t | content_key_id, | ||
size_t | aes_variant | ||
) |
Definition at line 120 of file CryptoInterface.c.
References BSL_LOG_ERR, BSL_LOG_INFO, BSLB_Crypto_GetRegistryKey(), BSL_Data_s::len, and BSL_Data_s::ptr.
void BSL_CryptoDeinit | ( | void | ) |
Deinitialize the crypto subsystem.
This should be called at the end of the process.
Definition at line 69 of file CryptoInterface.c.
References StaticKeyRegistry.
void BSL_CryptoInit | ( | void | ) |
Initialize the crypto subsystem.
This must be called once per process.
Definition at line 64 of file CryptoInterface.c.
References StaticKeyRegistry.
int BSLB_Crypto_GetRegistryKey | ( | uint64_t | keyid, |
const uint8_t ** | secret, | ||
size_t * | secret_len | ||
) |
Get pointers to an existing key, if present.
keyid | The key to search for. | |
[out] | secret | Pointer to the stored secret buffer, if successful. |
[out] | secret_len | Pointer to the stored secret length, if successful. |
Definition at line 461 of file CryptoInterface.c.
References BSL_ERR_NOT_FOUND, BSL_SUCCESS, BSL_Data_s::len, BSL_Data_s::ptr, BSLB_CryptoKey_s::raw, and StaticKeyRegistry.
Referenced by BSL_Crypto_UnwrapKey(), and BSL_Crypto_WrapKey().
|
static |
Definition at line 44 of file CryptoInterface.c.
|
static |
Definition at line 61 of file CryptoInterface.c.
|
static |
Crypto key registry.
Definition at line 60 of file CryptoInterface.c.
Referenced by BSL_AuthCtx_Init(), BSL_Crypto_AddRegistryKey(), BSL_CryptoDeinit(), BSL_CryptoInit(), and BSLB_Crypto_GetRegistryKey().