Skip to content

Commit 014f55f

Browse files
committed
wolfssl/wolfcrypt/types.h: add WC_WUR_INT(), MAX_UINT_OF(), MAX_SINT_OF(), MIN_SINT_OF(), WC_SAFE_SUM_UNSIGNED(), and WC_SAFE_SUM_SIGNED().
1 parent 3534fad commit 014f55f

2 files changed

Lines changed: 84 additions & 8 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13706,8 +13706,12 @@ static int AesXtsEncryptUpdate(XtsAes* xaes, byte* out, const byte* in, word32 s
1370613706
}
1370713707

1370813708
#ifndef WC_AESXTS_STREAM_NO_REQUEST_ACCOUNTING
13709-
(void)WC_SAFE_SUM_WORD32(stream->bytes_crypted_with_this_tweak, sz,
13710-
stream->bytes_crypted_with_this_tweak);
13709+
if (! WC_SAFE_SUM_WORD32(stream->bytes_crypted_with_this_tweak, sz,
13710+
stream->bytes_crypted_with_this_tweak))
13711+
{
13712+
WOLFSSL_MSG("Overflow of stream->bytes_crypted_with_this_tweak "
13713+
"in AesXtsEncryptUpdate().");
13714+
}
1371113715
#endif
1371213716
#if FIPS_VERSION3_GE(6,0,0)
1371313717
/* SP800-38E - Restrict data unit to 2^20 blocks per key. A block is
@@ -14144,15 +14148,20 @@ static int AesXtsDecryptUpdate(XtsAes* xaes, byte* out, const byte* in, word32 s
1414414148
return BAD_FUNC_ARG;
1414514149
}
1414614150

14147-
if (stream->bytes_crypted_with_this_tweak & ((word32)WC_AES_BLOCK_SIZE - 1U))
14151+
if (stream->bytes_crypted_with_this_tweak &
14152+
((word32)WC_AES_BLOCK_SIZE - 1U))
1414814153
{
14149-
WOLFSSL_MSG("Call to AesXtsDecryptUpdate after previous finalizing call");
14154+
WOLFSSL_MSG("AesXtsDecryptUpdate after previous finalizing call");
1415014155
return BAD_FUNC_ARG;
1415114156
}
1415214157

1415314158
#ifndef WC_AESXTS_STREAM_NO_REQUEST_ACCOUNTING
14154-
(void)WC_SAFE_SUM_WORD32(stream->bytes_crypted_with_this_tweak, sz,
14155-
stream->bytes_crypted_with_this_tweak);
14159+
if (! WC_SAFE_SUM_WORD32(stream->bytes_crypted_with_this_tweak, sz,
14160+
stream->bytes_crypted_with_this_tweak))
14161+
{
14162+
WOLFSSL_MSG("Overflow of stream->bytes_crypted_with_this_tweak "
14163+
"in AesXtsDecryptUpdate().");
14164+
}
1415614165
#endif
1415714166

1415814167
{

wolfssl/wolfcrypt/types.h

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,75 @@ enum {
462462

463463
#define XELEM_CNT(x) (sizeof((x))/sizeof(*(x)))
464464

465-
#define WC_SAFE_SUM_WORD32(in1, in2, out) ((in2) <= 0xffffffffU - (in1) ? \
466-
((out) = (in1) + (in2), 1) : ((out) = 0xffffffffU, 0))
465+
#ifdef NO_INLINE
466+
#define WC_WUR_INT(x) (x)
467+
#else
468+
WC_INLINE WARN_UNUSED_RESULT int WC_WUR_INT(int x) { return x; }
469+
#endif
470+
471+
#ifdef WORD64_AVAILABLE
472+
#define MAX_UINT_OF(x) \
473+
((((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - \
474+
(word64)1)) - (word64)1) \
475+
| \
476+
((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - (word64)1)))
477+
#define MAX_SINT_OF(x) \
478+
((sword64)((((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - \
479+
(word64)2)) - (word64)1) \
480+
| \
481+
((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - \
482+
(word64)2))))
483+
#define MIN_SINT_OF(x) \
484+
((sword64)((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - \
485+
(word64)1)))
486+
#else
487+
#define MAX_UINT_OF(x) \
488+
((((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - \
489+
(word32)1)) - (word32)1) \
490+
| \
491+
((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - (word32)1)))
492+
#define MAX_SINT_OF(x) \
493+
((sword32)((((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - \
494+
(word32)2)) - (word32)1) \
495+
| \
496+
((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - \
497+
(word32)2))))
498+
#define MIN_SINT_OF(x) \
499+
((sword32)((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - \
500+
(word32)1)))
501+
#endif
502+
503+
#define WC_SAFE_SUM_UNSIGNED_NO_WUR(type, in1, in2, out) \
504+
((in2) <= (MAX_UINT_OF(type) - (in1)) ? \
505+
((out) = (in1) + (in2), 1) : \
506+
((out) = MAX_UINT_OF(type), 0))
507+
508+
#define WC_SAFE_SUM_UNSIGNED(type, in1, in2, out) \
509+
WC_WUR_INT(WC_SAFE_SUM_UNSIGNED_NO_WUR(type, in1, in2, out))
510+
511+
#if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && FIPS_VERSION3_LE(6,0,0))
512+
#define WC_SAFE_SUM_WORD32(in1, in2, out) \
513+
WC_SAFE_SUM_UNSIGNED_NO_WUR(word32, in1, in2, out)
514+
#else
515+
#define WC_SAFE_SUM_WORD32(in1, in2, out) \
516+
WC_SAFE_SUM_UNSIGNED(word32, in1, in2, out)
517+
#endif
518+
519+
#define WC_SAFE_SUM_SIGNED_NO_WUR(type, in1, in2, out) \
520+
((((in1) > 0) && ((in2) > 0)) ? \
521+
((in2) <= MAX_SINT_OF(type) - (in1) ? \
522+
((out) = (in1) + (in2), 1) : \
523+
((out) = (type)MAX_SINT_OF(type), 0)) \
524+
: \
525+
((((in1) < 0) && ((in2) < 0)) ? \
526+
((in2) >= MIN_SINT_OF(type) - (in1) ? \
527+
((out) = (in1) + (in2), 1) : \
528+
((out) = (type)MIN_SINT_OF(type), 0)) \
529+
: \
530+
((out) = (in1) + (in2), 1)))
531+
532+
#define WC_SAFE_SUM_SIGNED(type, in1, in2, out) \
533+
WC_WUR_INT(WC_SAFE_SUM_SIGNED_NO_WUR(type, in1, in2, out))
467534

468535
#if defined(HAVE_IO_POOL)
469536
WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);

0 commit comments

Comments
 (0)