Skip to content

Commit dc0fe2c

Browse files
authored
Merge pull request #12001 from JacobBarthelmeh/fenrir_4
harden error queue and GMULT cases, add force zero to ColdFire DES port
2 parents f10bbcc + 42fcdbe commit dc0fe2c

4 files changed

Lines changed: 95 additions & 49 deletions

File tree

‎tests/api.c‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22763,6 +22763,9 @@ static int test_wolfSSL_ERR_print_errors_cb(void)
2276322763
"wolfSSL error occurred, error = 275 line:100 file:asn.c",
2276422764
buf + 53, 55), 0);
2276522765
ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
22766+
/* queue and its read cursor were reset, nothing left to pull */
22767+
ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
22768+
ExpectIntEQ(ERR_get_error(), 0);
2276622769

2276722770
BIO_free(bio);
2276822771
#endif
@@ -22790,13 +22793,43 @@ static int test_WOLFSSL_ERROR_MSG(void)
2279022793
*/
2279122794
static int test_wc_ERR_remove_state(void)
2279222795
{
22793-
int res = TEST_SKIPPED;
22796+
EXPECT_DECLS;
2279422797
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
22795-
wc_ERR_remove_state();
22798+
ExpectIntEQ(wc_ERR_remove_state(), 0);
2279622799

22797-
res = TEST_SUCCESS;
22800+
#if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
22801+
defined(DEBUG_WOLFSSL)
22802+
/* the read cursor must be reset with the nodes it points into */
22803+
ERR_clear_error();
22804+
ERR_put_error(0, SYS_F_ACCEPT, -173, "ssl.c", 0);
22805+
ERR_put_error(0, SYS_F_BIND, -275, "asn.c", 100);
22806+
ExpectIntEQ(wc_ERR_remove_state(), 0);
22807+
ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
22808+
ExpectIntEQ(ERR_get_error(), 0);
2279822809
#endif
22799-
return res;
22810+
22811+
#if defined(WOLFSSL_HAVE_ERROR_QUEUE) && !defined(NO_ERROR_QUEUE) && \
22812+
defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
22813+
!defined(ERROR_QUEUE_PER_THREAD)
22814+
{
22815+
/* with the global queue the returned file string must not point into
22816+
* the node, which is freed when the error is removed */
22817+
const char* file = NULL;
22818+
int line = 0;
22819+
22820+
ERR_clear_error();
22821+
ERR_put_error(0, SYS_F_BIND, -275, "asn.c", 100);
22822+
ExpectIntNE(ERR_peek_last_error_line(&file, &line), 0);
22823+
ExpectIntNE(ERR_get_error(), 0);
22824+
ExpectNotNull(file);
22825+
if (file != NULL) {
22826+
ExpectIntEQ(XSTRCMP(file, "asn.c"), 0);
22827+
}
22828+
ExpectIntEQ(line, 100);
22829+
}
22830+
#endif
22831+
#endif
22832+
return EXPECT_RESULT();
2280022833
} /* End test_wc_ERR_remove_state */
2280122834
/*
2280222835
* Testing wc_ERR_print_errors_fp

‎wolfcrypt/src/aes.c‎

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9708,12 +9708,16 @@ static void GMULT(byte* X, byte* Y, GcmScratch* WC_RESTRICT ws)
97089708
byte y = Y[i];
97099709
for (j = 0; j < 8; j++)
97109710
{
9711-
if (y & 0x80) {
9712-
xorbuf(Z, V, WC_AES_BLOCK_SIZE);
9711+
byte mask;
9712+
int k;
9713+
9714+
mask = (byte)(0U - ((y >> 7) & 0x01U));
9715+
for (k = 0; k < WC_AES_BLOCK_SIZE; k++) {
9716+
Z[k] ^= (byte)(V[k] & mask);
97139717
}
97149718

97159719
RIGHTSHIFTX(V);
9716-
y = y << 1;
9720+
y = (byte)(y << 1);
97179721
}
97189722
}
97199723
XMEMCPY(X, Z, WC_AES_BLOCK_SIZE);
@@ -11081,31 +11085,18 @@ static void GMULT(word32* X, word32* Y, GcmScratch* WC_RESTRICT ws)
1108111085
word32 y = Y[i];
1108211086
for (j = 0; j < 32; j++)
1108311087
{
11084-
if (y & 0x80000000) {
11085-
Z[0] ^= V[0];
11086-
Z[1] ^= V[1];
11087-
Z[2] ^= V[2];
11088-
Z[3] ^= V[3];
11089-
}
11088+
word32 mask = (word32)0U - (y >> 31);
11089+
word32 carry = (word32)0U - (V[3] & 0x00000001);
1109011090

11091-
if (V[3] & 0x00000001) {
11092-
V[3] >>= 1;
11093-
V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
11094-
V[2] >>= 1;
11095-
V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
11096-
V[1] >>= 1;
11097-
V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
11098-
V[0] >>= 1;
11099-
V[0] ^= 0xE1000000;
11100-
} else {
11101-
V[3] >>= 1;
11102-
V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
11103-
V[2] >>= 1;
11104-
V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
11105-
V[1] >>= 1;
11106-
V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
11107-
V[0] >>= 1;
11108-
}
11091+
Z[0] ^= V[0] & mask;
11092+
Z[1] ^= V[1] & mask;
11093+
Z[2] ^= V[2] & mask;
11094+
Z[3] ^= V[3] & mask;
11095+
11096+
V[3] = (V[3] >> 1) | (V[2] << 31);
11097+
V[2] = (V[2] >> 1) | (V[1] << 31);
11098+
V[1] = (V[1] >> 1) | (V[0] << 31);
11099+
V[0] = (V[0] >> 1) ^ (0xE1000000 & carry);
1110911100
y <<= 1;
1111011101
}
1111111102
}
@@ -21746,8 +21737,12 @@ static void AesGcmSivGMult(AesGcmSivPolyval* poly)
2174621737
for (i = 0; i < WC_AES_BLOCK_SIZE; i++) {
2174721738
byte y = poly->h[i];
2174821739
for (j = 0; j < 8; j++) {
21749-
if (y & 0x80) {
21750-
xorbuf(Z, V, WC_AES_BLOCK_SIZE);
21740+
byte mask;
21741+
int k;
21742+
21743+
mask = (byte)(0U - ((y >> 7) & 0x01U));
21744+
for (k = 0; k < WC_AES_BLOCK_SIZE; k++) {
21745+
Z[k] ^= (byte)(V[k] & mask);
2175121746
}
2175221747
AesGcmSivMulX(V);
2175321748
y = (byte)(y << 1);

‎wolfcrypt/src/des3.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out)
628628
out += size;
629629

630630
}
631+
ForceZero(secKey, secDesc->length3);
631632
wc_UnLockMutex(&Mutex_DesSEC) ;
632633

633634
}

‎wolfcrypt/src/logging.c‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,16 @@ static struct wc_error_queue* wc_current_node;
10701070
/* heap info currently used for allocation of entries */
10711071
static void* wc_error_heap;
10721072

1073+
/* Copies of the file and reason strings of the last node peeked or pulled.
1074+
* Pointers returned to the caller point here rather than into a node, so a
1075+
* remove or clear from another thread after the mutex is released can not
1076+
* leave the caller with a pointer to freed memory. The contents are only valid
1077+
* until the next peek or pull by the same thread. Only written with the mutex
1078+
* held and the last byte is never written, so the strings are always
1079+
* terminated. */
1080+
static THREAD_LS_T char wc_errq_file[WOLFSSL_MAX_ERROR_SZ];
1081+
static THREAD_LS_T char wc_errq_reason[WOLFSSL_MAX_ERROR_SZ];
1082+
10731083
/* mutex for list operation protection */
10741084
static wolfSSL_Mutex wc_error_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wc_error_mutex);
10751085
#define ERRQ_MUTEX_INIT() wc_InitMutex(&wc_error_mutex)
@@ -1109,6 +1119,21 @@ int wc_LoggingCleanup(void)
11091119
return 0;
11101120
}
11111121

1122+
/* Copy the node strings asked for to the static buffers and return those.
1123+
* Must be called with the mutex held. */
1124+
static void exportErrorNodeStrings(const struct wc_error_queue* err,
1125+
const char **file, const char **reason)
1126+
{
1127+
if (file != NULL) {
1128+
XMEMCPY(wc_errq_file, err->file, WOLFSSL_MAX_ERROR_SZ - 1);
1129+
*file = wc_errq_file;
1130+
}
1131+
if (reason != NULL) {
1132+
XMEMCPY(wc_errq_reason, err->error, WOLFSSL_MAX_ERROR_SZ - 1);
1133+
*reason = wc_errq_reason;
1134+
}
1135+
}
1136+
11121137
static int peekErrorNode(int idx, const char **file, const char **reason,
11131138
int *line)
11141139
{
@@ -1135,12 +1160,7 @@ static int peekErrorNode(int idx, const char **file, const char **reason,
11351160
return BAD_STATE_E;
11361161
}
11371162

1138-
if (file != NULL) {
1139-
*file = err->file;
1140-
}
1141-
if (reason != NULL) {
1142-
*reason = err->error;
1143-
}
1163+
exportErrorNodeStrings(err, file, reason);
11441164
if (line != NULL) {
11451165
*line = err->line;
11461166
}
@@ -1153,8 +1173,10 @@ static int peekErrorNode(int idx, const char **file, const char **reason,
11531173
* idx : if -1 then the most recent node is looked at,
11541174
* otherwise search through queue for node at the given index starting
11551175
* from the absolute head wc_errors
1156-
* file : pointer to internal file string
1157-
* reason : pointer to internal error reason
1176+
* file : pointer to a copy of the file string, valid until the next peek or
1177+
* pull
1178+
* reason : pointer to a copy of the error reason, valid until the next peek
1179+
* or pull
11581180
* line : line number that error happened at
11591181
*
11601182
* Returns a negative value in error case, on success returns the nodes error
@@ -1185,12 +1207,7 @@ static int pullErrorNode(const char **file, const char **reason, int *line)
11851207
return BAD_STATE_E;
11861208
}
11871209

1188-
if (file != NULL) {
1189-
*file = err->file;
1190-
}
1191-
if (reason != NULL) {
1192-
*reason = err->error;
1193-
}
1210+
exportErrorNodeStrings(err, file, reason);
11941211
if (line != NULL) {
11951212
*line = err->line;
11961213
}
@@ -1201,8 +1218,8 @@ static int pullErrorNode(const char **file, const char **reason, int *line)
12011218
}
12021219

12031220
/* Pulls the current node from error queue and increments current state.
1204-
* Note: this does not delete nodes because input arguments are pointing to
1205-
* node buffers.
1221+
* Note: this does not delete nodes. The file and reason pointers returned are
1222+
* to copies that are valid until the next peek or pull.
12061223
*
12071224
* file pointer to file that error was in. Can be NULL to return no file.
12081225
* reason error string giving reason for error. Can be NULL to return no reason.

0 commit comments

Comments
 (0)