Skip to content

Commit f4bbb50

Browse files
authored
Move large stack buffers onto heap (#121)
* Move large-ish buffers onto heap * Annotate the failure condition for CHK macro
1 parent 621ed42 commit f4bbb50

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

src/BPSecLib_Private.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,20 @@ void BSL_LogEvent(int severity, const char *filename, int lineno, const char *fu
209209
#define BSL_LOG_DEBUG(...) BSL_LogEvent(LOG_DEBUG, __FILE__, __LINE__, __func__, __VA_ARGS__)
210210
// NOLINTEND(misc-include-cleaner)
211211

212-
/** @brief Helpful macros for expressing invariants, pre/post conditions, and arg validation
213-
*
212+
/** @brief Helpful macros for expressing invariants, pre/post conditions, and arg validation.
213+
* The expression is nominally true and only false during exceptional cases.
214214
*/
215215
#define CHK_TEMPL(expr, msg, return_code) \
216216
do \
217217
{ \
218-
if (!(expr)) \
218+
if (!LIKELY(expr)) \
219219
{ \
220220
BSL_LOG_ERR("" msg " (" #expr ") ... [errcode=" #return_code "]"); \
221221
assert(!(expr)); \
222222
return return_code; \
223223
} \
224224
} \
225-
while (0)
225+
while (0) /* GCOV_EXCL_LINE */
226226

227227
#define CHK_AS_BOOL(expr) CHK_TEMPL(expr, "Failed Property Check: Failed to satisfy", BSL_ERR_ARG_INVALID)
228228

src/CryptoInterface.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ typedef struct BSL_AuthCtx_s
124124
* @note Private value
125125
*/
126126
size_t block_size;
127+
/** Storage for input blocks.
128+
* After init this is sized to #block_size.
129+
*/
130+
BSL_Data_t in_buf;
127131
} BSL_AuthCtx_t;
128132

129133
/**
@@ -141,6 +145,14 @@ typedef struct BSL_Cipher_s
141145
void *keyhandle;
142146
/// block size of cipher context
143147
size_t block_size;
148+
/** Storage for input blocks.
149+
* After init this is sized to #block_size.
150+
*/
151+
BSL_Data_t in_buf;
152+
/** Storage for output blocks.
153+
* After init this is sized to #block_size.
154+
*/
155+
BSL_Data_t out_buf;
144156
} BSL_Cipher_t;
145157

146158
/**
@@ -242,12 +254,12 @@ int BSL_Crypto_UnwrapKey(void *kek_handle, BSL_Data_t *wrapped_key, void **cek_h
242254

243255
/**
244256
* Initialize crypto context resources and set as encoding or decoding
245-
* @param cipher_ctx pointer to context to initialize
257+
* @param[out] cipher_ctx pointer to context to initialize
246258
* @param aes_var AES GCM variant to use
247259
* @param enc enum for BSL_CRYPTO_ENCRYPT or BSL_CRYPTO_DECRYPT
248-
* @param init_vec pointer to initialization vector (IV) data
249-
* @param iv_len length of IV data
250-
* @param key_handle key handle to use
260+
* @param[in] init_vec pointer to initialization vector (IV) data
261+
* @param[in] iv_len length of IV data
262+
* @param[in] key_handle key handle to use
251263
* @return 0 if successful
252264
*/
253265
int BSL_Cipher_Init(BSL_Cipher_t *cipher_ctx, BSL_CipherMode_e enc, BSL_CryptoCipherAESVariant_e aes_var,
@@ -275,11 +287,6 @@ int BSL_Crypto_RemoveRegistryKey(const char *keyid);
275287
*/
276288
int BSL_Cipher_AddAAD(BSL_Cipher_t *cipher_ctx, const void *aad, int aad_len);
277289

278-
/**
279-
* @todo Doxygen
280-
*/
281-
int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, BSL_Data_t plaintext, BSL_Data_t ciphertext);
282-
283290
/**
284291
* Add data to encrypt or decrypt to the context sequentially
285292
* @param cipher_ctx pointer to context to add data to
@@ -290,6 +297,9 @@ int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, BSL_Data_t plaintext, BSL_Data_
290297
*/
291298
int BSL_Cipher_AddSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqReader_t *reader, BSL_SeqWriter_t *writer);
292299

300+
/// @overload
301+
int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, const BSL_Data_t *input, BSL_Data_t *output);
302+
293303
/**
294304
* Get the tag of the crypto operation
295305
* @param cipher_ctx pointer to context to get tag from
@@ -315,6 +325,7 @@ int BSL_Cipher_SetTag(BSL_Cipher_t *cipher_ctx, const void *tag);
315325
* @return 0 if successful
316326
*/
317327
int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer);
328+
/// @overload
318329
int BSL_Cipher_FinalizeData(BSL_Cipher_t *cipher_ctx, BSL_Data_t *extra);
319330

320331
/**

src/crypto/CryptoInterface.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, void *keyhandle, BSL_CryptoCipherS
390390
BSL_LOG_ERR("invalid block size zero, assuming %zu", hmac_ctx->block_size);
391391
}
392392

393+
res = BSL_Data_InitBuffer(&hmac_ctx->in_buf, hmac_ctx->block_size);
394+
CHK_PROPERTY(!res);
395+
393396
key_info->stats.stats[BSL_CRYPTO_KEYSTATS_TIMES_USED]++;
394397

395398
return 0;
@@ -439,6 +442,7 @@ int BSL_AuthCtx_Finalize(BSL_AuthCtx_t *hmac_ctx, void **hmac, size_t *hmac_len)
439442

440443
int BSL_AuthCtx_Deinit(BSL_AuthCtx_t *hmac_ctx)
441444
{
445+
BSL_Data_Deinit(&hmac_ctx->in_buf);
442446
EVP_MD_CTX_free(hmac_ctx->libhandle);
443447
memset(hmac_ctx, 0, sizeof(BSL_AuthCtx_t));
444448
return 0;
@@ -490,6 +494,12 @@ int BSL_Cipher_Init(BSL_Cipher_t *cipher_ctx, BSL_CipherMode_e enc, BSL_CryptoCi
490494
res = EVP_CipherInit_ex(cipher_ctx->libhandle, NULL, NULL, key->raw.ptr, init_vec, -1);
491495
CHK_PROPERTY(res == 1);
492496

497+
res = BSL_Data_InitBuffer(&cipher_ctx->in_buf, cipher_ctx->block_size);
498+
CHK_PROPERTY(!res);
499+
500+
res = BSL_Data_InitBuffer(&cipher_ctx->out_buf, cipher_ctx->block_size);
501+
CHK_PROPERTY(!res);
502+
493503
key->stats.stats[BSL_CRYPTO_KEYSTATS_TIMES_USED]++;
494504

495505
return 0;
@@ -508,17 +518,17 @@ int BSL_Cipher_AddAAD(BSL_Cipher_t *cipher_ctx, const void *aad, int aad_len)
508518
return 0;
509519
}
510520

511-
int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, BSL_Data_t plaintext, BSL_Data_t ciphertext)
521+
int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, const BSL_Data_t *input, BSL_Data_t *output)
512522
{
513523
ASSERT_ARG_NONNULL(cipher_ctx);
514-
int cipherlen = (int)ciphertext.len;
515-
if (EVP_CipherUpdate(cipher_ctx->libhandle, ciphertext.ptr, &cipherlen, plaintext.ptr, (int)plaintext.len) != 1)
524+
int cipherlen = (int)output->len;
525+
if (EVP_CipherUpdate(cipher_ctx->libhandle, output->ptr, &cipherlen, input->ptr, (int)input->len) != 1)
516526
{
517527
return -1;
518528
}
519529

520530
BSL_CryptoKey_t *key = (BSL_CryptoKey_t *)cipher_ctx->keyhandle;
521-
key->stats.stats[BSL_CRYPTO_KEYSTATS_BYTES_PROCESSED] += plaintext.len;
531+
key->stats.stats[BSL_CRYPTO_KEYSTATS_BYTES_PROCESSED] += input->len;
522532

523533
return cipherlen;
524534
}
@@ -596,7 +606,7 @@ int BSL_Cipher_FinalizeData(BSL_Cipher_t *cipher_ctx, BSL_Data_t *extra)
596606
CHK_PROPERTY(res == 1);
597607
BSL_LOG_DEBUG("extra->len = %zu | got len = %d", extra->len, len);
598608
memset(extra->ptr, 0, extra->len);
599-
BSL_LOG_INFO("Completed EVP_CipherFinal_ex");
609+
BSL_LOG_DEBUG("Completed EVP_CipherFinal_ex");
600610
if (len > 0)
601611
{
602612
memcpy(extra->ptr, buf, sizeof(buf));
@@ -610,11 +620,8 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
610620
CHK_ARG_NONNULL(cipher_ctx);
611621
CHK_ARG_NONNULL(writer);
612622

613-
// finalize can add 1 cipher block
614-
uint8_t buf[cipher_ctx->block_size];
615-
616623
int evp_len = 0;
617-
int res = EVP_CipherFinal_ex(cipher_ctx->libhandle, buf, &evp_len);
624+
int res = EVP_CipherFinal_ex(cipher_ctx->libhandle, cipher_ctx->out_buf.ptr, &evp_len);
618625
if (res != 1)
619626
{
620627
BSL_LOG_ERR("EVP_CipherFinal_ex error %s", ERR_error_string(ERR_get_error(), NULL));
@@ -624,7 +631,7 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
624631
if (evp_len > 0)
625632
{
626633
size_t bsl_len = evp_len;
627-
BSL_SeqWriter_Put(writer, buf, bsl_len);
634+
BSL_SeqWriter_Put(writer, cipher_ctx->out_buf.ptr, bsl_len);
628635
}
629636

630637
return 0;
@@ -633,6 +640,8 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
633640
int BSL_Cipher_Deinit(BSL_Cipher_t *cipher_ctx)
634641
{
635642
CHK_ARG_NONNULL(cipher_ctx);
643+
BSL_Data_Deinit(&cipher_ctx->out_buf);
644+
BSL_Data_Deinit(&cipher_ctx->in_buf);
636645
EVP_CIPHER_CTX_free(cipher_ctx->libhandle);
637646
memset(cipher_ctx, 0, sizeof(*cipher_ctx));
638647
return BSL_SUCCESS;

0 commit comments

Comments
 (0)