Skip to content

Commit 6b49cc4

Browse files
KeyID as text string (#7)
* keyid as text string * update conventions * apply formatting * explicit in-out params * final PR updates * apply format * Formatting on CI platform --------- Co-authored-by: Brian Sipos <brian.sipos@jhuapl.edu>
1 parent c034286 commit 6b49cc4

14 files changed

Lines changed: 232 additions & 183 deletions

docs/api/Developer_Guide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ More notes forthcoming.
104104
* The C standard library does not provide containers. Arrays is all we have, so that's what we have to work with.
105105
* Third party libraries providing containers may be more hassle and risk than they are worth.
106106

107+
#### &raquo; M\*Lib structures should not be referenced in the Frontend API
108+
* Keep M\*Lib usage to the BSL backend, and use standard/primative structs for frontend API. The frontend should not include any M\*Lib headers.
107109

108110
# Documentation
109111

src/BPSecLib_Private.h

Lines changed: 131 additions & 112 deletions
Large diffs are not rendered by default.

src/CryptoInterface.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void BSL_CryptoDeinit(void);
141141
* @return 0 if successful
142142
*/
143143
BSL_REQUIRE_CHECK
144-
int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, uint64_t keyid, BSL_CryptoCipherSHAVariant_e sha_var);
144+
int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, const char *keyid, BSL_CryptoCipherSHAVariant_e sha_var);
145145

146146
/**
147147
* Input data to HMAC sign to context
@@ -180,13 +180,13 @@ int BSL_AuthCtx_Deinit(BSL_AuthCtx_t *hmac_ctx);
180180
/**
181181
* @todo Doxygen
182182
*/
183-
int BSL_Crypto_UnwrapKey(BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_key_plaintext, size_t key_id,
183+
int BSL_Crypto_UnwrapKey(BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_key_plaintext, const char *key_id,
184184
size_t aes_variant);
185185

186186
/**
187187
* @todo Doxygen
188188
*/
189-
int BSL_Crypto_WrapKey(BSL_Data_t *wrapped_key, BSL_Data_t cek, size_t content_key_id, size_t aes_variant);
189+
int BSL_Crypto_WrapKey(BSL_Data_t *wrapped_key, BSL_Data_t cek, const char *content_key_id, size_t aes_variant);
190190

191191
/**
192192
* Initialize crypto context resources and set as encoding or decoding
@@ -208,7 +208,7 @@ int BSL_Cipher_Init(BSL_Cipher_t *cipher_ctx, BSL_CipherMode_e enc, BSL_CryptoCi
208208
* @param[out] secret_len Pointer to the stored secret length, if successful.
209209
* @return Zero upon success.
210210
*/
211-
int BSLB_Crypto_GetRegistryKey(uint64_t keyid, const uint8_t **secret, size_t *secret_len);
211+
int BSLB_Crypto_GetRegistryKey(const char *keyid, const uint8_t **secret, size_t *secret_len);
212212

213213
/**
214214
* Add additional authenticated data (AAD) to cipher context
@@ -284,7 +284,7 @@ int BSL_Crypto_GenIV(void *buf, int size);
284284
* @param secret_len length of raw key
285285
* @return Zero upon success.
286286
*/
287-
int BSL_Crypto_AddRegistryKey(uint64_t keyid, const uint8_t *secret, size_t secret_len);
287+
int BSL_Crypto_AddRegistryKey(const char *keyid, const uint8_t *secret, size_t secret_len);
288288

289289
#ifdef __cplusplus
290290
} // extern C

src/backend/AbsSecBlock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
410410
// This is a failure case - should more clearly return?
411411
BSL_LOG_ERR("Unhandled case");
412412
// NOLINTNEXTLINE
413-
exit(1);
413+
return BSL_ERR_DECODING;
414414
}
415415

416416
const size_t item_end = QCBORDecode_Tell(&asbdec);

src/backend/SecParam.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ size_t BSL_SecParam_Sizeof(void)
3131
return sizeof(BSL_SecParam_t);
3232
}
3333

34+
int BSL_SecParam_InitStr(BSL_SecParam_t *self, uint64_t param_id, const char *value)
35+
{
36+
CHK_ARG_NONNULL(self);
37+
CHK_ARG_EXPR(value != NULL);
38+
39+
memset(self, 0, sizeof(*self));
40+
self->param_id = param_id;
41+
self->_type = BSL_SECPARAM_TYPE_STR;
42+
self->_bytelen = strlen(value);
43+
memcpy(self->_bytes, value, strlen(value));
44+
45+
return BSL_SUCCESS;
46+
}
47+
3448
int BSL_SecParam_InitBytestr(BSL_SecParam_t *self, uint64_t param_id, BSL_Data_t value)
3549
{
3650
CHK_ARG_NONNULL(self);

src/crypto/CryptoInterface.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <CryptoInterface.h>
2828

2929
#include <m-dict.h>
30+
#include <m-string.h>
3031
#include <openssl/err.h>
3132
#include <openssl/rand.h>
3233

@@ -53,7 +54,7 @@ static int BSLB_CryptoKey_Deinit(BSLB_CryptoKey_t *key)
5354
/// @cond Doxygen_Suppress
5455
#define M_OPL_BSLB_CryptoKey_t() M_OPEXTEND(M_POD_OPLIST, CLEAR(API_2(BSLB_CryptoKey_Deinit)))
5556
/// Stable dict of crypto keys (key: key ID | value: key)
56-
DICT_DEF2(BSLB_CryptoKeyDict, uint64_t, M_BASIC_OPLIST, BSLB_CryptoKey_t, M_OPL_BSLB_CryptoKey_t())
57+
DICT_DEF2(BSLB_CryptoKeyDict, string_t, STRING_OPLIST, BSLB_CryptoKey_t, M_OPL_BSLB_CryptoKey_t())
5758
/// @endcond
5859

5960
/// Crypto key registry
@@ -71,7 +72,7 @@ void BSL_CryptoDeinit(void)
7172
BSLB_CryptoKeyDict_clear(StaticKeyRegistry);
7273
}
7374

74-
int BSL_Crypto_UnwrapKey(BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_key_plaintext, size_t key_id,
75+
int BSL_Crypto_UnwrapKey(BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_key_plaintext, const char *key_id,
7576
size_t aes_variant)
7677
{
7778
const EVP_CIPHER *cipher = (aes_variant == BSL_CRYPTO_AES_128) ? EVP_aes_128_wrap() : EVP_aes_256_wrap();
@@ -117,7 +118,7 @@ int BSL_Crypto_UnwrapKey(BSL_Data_t *unwrapped_key_output, BSL_Data_t wrapped_ke
117118
return 0;
118119
}
119120

120-
int BSL_Crypto_WrapKey(BSL_Data_t *wrapped_key, BSL_Data_t cek, size_t content_key_id, size_t aes_variant)
121+
int BSL_Crypto_WrapKey(BSL_Data_t *wrapped_key, BSL_Data_t cek, const char *content_key_id, size_t aes_variant)
121122
{
122123
const EVP_CIPHER *cipher = (aes_variant == BSL_CRYPTO_AES_128) ? EVP_aes_128_wrap() : EVP_aes_256_wrap();
123124
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
@@ -166,7 +167,7 @@ int BSL_Crypto_WrapKey(BSL_Data_t *wrapped_key, BSL_Data_t cek, size_t content_k
166167
return 0;
167168
}
168169

169-
int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, uint64_t keyid, BSL_CryptoCipherSHAVariant_e sha_var)
170+
int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, const char *keyid, BSL_CryptoCipherSHAVariant_e sha_var)
170171
{
171172
CHK_ARG_NONNULL(hmac_ctx);
172173

@@ -192,8 +193,11 @@ int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, uint64_t keyid, BSL_CryptoCipherSH
192193
return BSL_ERR_FAILURE;
193194
}
194195

196+
string_t keyid_str;
197+
string_init_set_str(keyid_str, keyid);
198+
195199
pthread_mutex_lock(&StaticCryptoMutex);
196-
const BSLB_CryptoKey_t *key_info = BSLB_CryptoKeyDict_cget(StaticKeyRegistry, keyid);
200+
const BSLB_CryptoKey_t *key_info = BSLB_CryptoKeyDict_cget(StaticKeyRegistry, keyid_str);
197201
if (key_info == NULL)
198202
{
199203
// Special case which should not happen
@@ -430,7 +434,7 @@ int BSL_Crypto_GenIV(void *buf, int size)
430434
return 0;
431435
}
432436

433-
int BSL_Crypto_AddRegistryKey(uint64_t keyid, const uint8_t *secret, size_t secret_len)
437+
int BSL_Crypto_AddRegistryKey(const char *keyid, const uint8_t *secret, size_t secret_len)
434438
{
435439
CHK_ARG_NONNULL(secret);
436440
CHK_ARG_EXPR(secret_len > 0);
@@ -452,20 +456,26 @@ int BSL_Crypto_AddRegistryKey(uint64_t keyid, const uint8_t *secret, size_t secr
452456
return ecode;
453457
}
454458

459+
string_t keyid_str;
460+
string_init_set_str(keyid_str, keyid);
461+
455462
pthread_mutex_lock(&StaticCryptoMutex);
456-
BSLB_CryptoKeyDict_set_at(StaticKeyRegistry, keyid, key);
463+
BSLB_CryptoKeyDict_set_at(StaticKeyRegistry, keyid_str, key);
457464
pthread_mutex_unlock(&StaticCryptoMutex);
458465

459466
return 0;
460467
}
461468

462-
int BSLB_Crypto_GetRegistryKey(uint64_t keyid, const uint8_t **secret, size_t *secret_len)
469+
int BSLB_Crypto_GetRegistryKey(const char *keyid, const uint8_t **secret, size_t *secret_len)
463470
{
464471
CHK_ARG_NONNULL(secret);
465472
// CHK_ARG_NONNULL(secret_len); // Note: secret_len CAN be NULL - this maybe should be fixed.
466473

474+
string_t keyid_str;
475+
string_init_set_str(keyid_str, keyid);
476+
467477
pthread_mutex_lock(&StaticCryptoMutex);
468-
const BSLB_CryptoKey_t *found = BSLB_CryptoKeyDict_cget(StaticKeyRegistry, keyid);
478+
const BSLB_CryptoKey_t *found = BSLB_CryptoKeyDict_cget(StaticKeyRegistry, keyid_str);
469479

470480
if (!found)
471481
{

src/security_context/BCB_AES_GCM.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int BSLX_BCB_Decrypt(BSLX_BCB_t *bcb_context)
105105
CHK_PRECONDITION(bcb_context->aad.len > 0);
106106

107107
// Key must have been set (this feeds the key encryption key)
108-
CHK_PRECONDITION(bcb_context->key_id > 0);
108+
CHK_PRECONDITION(bcb_context->key_id);
109109

110110
// BTSD replacement is not yet allocated
111111
CHK_PRECONDITION(bcb_context->btsd_replacement.ptr != NULL);
@@ -224,7 +224,7 @@ int BSLX_BCB_Encrypt(BSLX_BCB_t *bcb_context)
224224
CHK_PRECONDITION(bcb_context->aad.len > 0);
225225

226226
// Must have a key ID from the security operation parameters
227-
CHK_PRECONDITION(bcb_context->key_id > 0);
227+
CHK_PRECONDITION(bcb_context->key_id);
228228

229229
// BTSD replacement is not yet allocated
230230
CHK_PRECONDITION(bcb_context->btsd_replacement.ptr != NULL);
@@ -469,12 +469,13 @@ int BSLX_BCB_GetParams(const BSL_BundleRef_t *bundle, BSLX_BCB_t *bcb_context, c
469469
}
470470
break;
471471
}
472-
case BSL_SECPARAM_TYPE_INT_KEY_ID:
472+
case BSL_SECPARAM_TYPE_KEY_ID:
473473
{
474-
assert(is_int);
475-
bcb_context->key_id = BSL_SecParam_GetAsUInt64(param);
476-
BSL_LOG_DEBUG("Param[%lu]: KEY_ID value = %lu", param_id, bcb_context->key_id);
477-
BSL_LOG_DEBUG("Key ID = %lu", bcb_context->key_id);
474+
assert(!is_int);
475+
BSL_Data_t res;
476+
assert(BSL_SUCCESS == BSL_SecParam_GetAsBytestr(param, &res));
477+
bcb_context->key_id = (char *)res.ptr;
478+
BSL_LOG_DEBUG("Param[%lu]: KEY_ID value = %s", param_id, bcb_context->key_id);
478479
break;
479480
}
480481
case BSL_SECPARAM_TYPE_INT_FIXED_KEY:

src/security_context/BIB_HMAC_SHA2.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ int BSLX_BIB_InitFromSecOper(BSLX_BIB_t *self, const BSL_SecOper_t *sec_oper)
111111
memset(self, 0, sizeof(*self));
112112
self->sha_variant = -1;
113113
self->integrity_scope_flags = -1;
114-
self->key_id = -1;
115114

116115
for (size_t param_index = 0; param_index < BSL_SecOper_CountParams(sec_oper); param_index++)
117116
{
@@ -124,10 +123,12 @@ int BSLX_BIB_InitFromSecOper(BSLX_BIB_t *self, const BSL_SecOper_t *sec_oper)
124123
int_val = BSL_SecParam_GetAsUInt64(param);
125124
}
126125

127-
if (param_id == BSL_SECPARAM_TYPE_INT_KEY_ID)
126+
if (param_id == BSL_SECPARAM_TYPE_KEY_ID)
128127
{
129-
assert(is_int);
130-
self->key_id = int_val;
128+
assert(!is_int);
129+
BSL_Data_t res;
130+
BSL_SecParam_GetAsBytestr(param, &res);
131+
self->key_id = (char *)res.ptr;
131132
}
132133
else if (param_id == BSL_SECPARAM_TYPE_INT_FIXED_KEY)
133134
{

src/security_context/DefaultSecContext_Private.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ BSL_Data_t BSLX_Bytestr_AsData(BSLX_Bytestr_t *self);
6262

6363
typedef struct BSLX_BIB_s
6464
{
65-
int64_t key_id;
65+
/// @brief set to external pointer which will outloast BIB context
66+
const char *key_id;
6667
BSL_PrimaryBlock_t primary_block;
6768
BSL_CanonicalBlock_t target_block;
6869
BSL_CanonicalBlock_t sec_block;
@@ -84,8 +85,8 @@ int BSLX_BIB_GenHMAC(BSLX_BIB_t *self, BSL_Data_t ippt_data);
8485
*/
8586
typedef struct BSLX_BCB_s
8687
{
87-
size_t err_count;
88-
uint64_t key_id;
88+
size_t err_count;
89+
const char *key_id;
8990

9091
// Data wrappers and containers for borrowed and owned/allocated buffers
9192
// These will ALL be deinitialized at the end, so _Deinit MUST be called.

test/bsl_test_utils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
field.len = sizeof(tgt); \
4040
field.ptr = (uint8_t *)tgt
4141

42-
void BSL_TestUtils_InitBIB_AppendixA1(BIBTestContext *context, BSL_SecRole_e role, uint64_t key_id)
42+
void BSL_TestUtils_InitBIB_AppendixA1(BIBTestContext *context, BSL_SecRole_e role, const char *key_id)
4343
{
4444
quick_data(context->hmac, ApxA1_HMAC);
4545

46-
BSL_SecParam_InitInt64(&context->param_test_key, BSL_SECPARAM_TYPE_INT_KEY_ID, key_id);
46+
BSL_SecParam_InitStr(&context->param_test_key, BSL_SECPARAM_TYPE_KEY_ID, key_id);
4747
BSL_SecParam_InitInt64(&context->param_scope_flags, RFC9173_BIB_PARAMID_INTEG_SCOPE_FLAG, 0);
4848
BSL_SecParam_InitInt64(&context->param_sha_variant, RFC9173_BIB_PARAMID_SHA_VARIANT, RFC9173_BIB_SHA_HMAC512);
4949
BSL_SecParam_InitBytestr(&context->param_hmac, BSL_SECPARAM_TYPE_AUTH_TAG, context->hmac);
@@ -64,7 +64,7 @@ void BSL_TestUtils_InitBCB_Appendix2(BCBTestContext *context, BSL_SecRole_e role
6464
quick_data(context->content_enc_key, ApxA2_ContentEncKey);
6565

6666
BSL_SecParam_InitInt64(&context->param_scope_flags, RFC9173_BCB_SECPARAM_AADSCOPE, 0);
67-
BSL_SecParam_InitInt64(&context->param_test_key_id, BSL_SECPARAM_TYPE_INT_KEY_ID, RFC9173_EXAMPLE_A2_KEY);
67+
BSL_SecParam_InitStr(&context->param_test_key_id, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A2_KEY);
6868
BSL_SecParam_InitInt64(&context->param_aes_variant, RFC9173_BCB_SECPARAM_AESVARIANT,
6969
RFC9173_BCB_AES_VARIANT_A128GCM);
7070
BSL_SecParam_InitBytestr(&context->param_init_vec, RFC9173_BCB_SECPARAM_IV, context->init_vector);
@@ -220,14 +220,14 @@ BSL_HostEIDPattern_t BSL_TestUtils_GetEidPatternFromText(const char *text)
220220
return pat;
221221
}
222222

223-
RFC9173_A1_Params BSL_TestUtils_GetRFC9173_A1Params(uint64_t key_id)
223+
RFC9173_A1_Params BSL_TestUtils_GetRFC9173_A1Params(const char *key_id)
224224
{
225225
RFC9173_A1_Params params = { 0 };
226226
BSL_SecParam_InitInt64(&params.sha_variant, RFC9173_TestVectors_AppendixA1.bib_asb_sha_variant_key,
227227
RFC9173_TestVectors_AppendixA1.bib_asb_sha_variant_value);
228228
BSL_SecParam_InitInt64(&params.scope_flags, RFC9173_TestVectors_AppendixA1.bib_asb_scope_flags_key,
229229
RFC9173_TestVectors_AppendixA1.bib_asb_scope_flags_value);
230-
BSL_SecParam_InitInt64(&params.test_key_id, BSL_SECPARAM_TYPE_INT_KEY_ID, key_id);
230+
BSL_SecParam_InitStr(&params.test_key_id, BSL_SECPARAM_TYPE_KEY_ID, key_id);
231231
return params;
232232
}
233233

0 commit comments

Comments
 (0)