Skip to content

Commit 0da78a7

Browse files
committed
move several MP error codes from wolfssl/wolfcrypt/sp_int.h, wolfssl/wolfcrypt/tfm.h, and wolfssl/wolfcrypt/integer.h, to wolfssl/wolfcrypt/error-crypt.h, harmonizing their names and numbers.
wolfssl/wolfcrypt/error-crypt.h: add WC_FIRST_E. wolfcrypt/src/error.c: add MP error code strings. wolfssl/error-ssl.h: add WOLFSSL_FIRST_E and WOLFSSL_LAST_E. wolfcrypt/test/test.c: update error_test() for new error code layout, refactoring the "missing" check. src/internal.c: use WC_FIRST_E and WC_LAST_E in wolfSSL_ERR_reason_error_string(). src/ssl.c: fix wolfSSL_ERR_GET_REASON() to identify in-range error codes using WC_FIRST_E, WC_LAST_E, WOLFSSL_FIRST_E, and WOLFSSL_LAST_E. sp_int.h: provide for WOLFSSL_DEBUG_TRACE_ERROR_CODES, and refactor MP error codes as enums, for consistency with other error codes. wolfcrypt/src/ecc.c: fix 2 identicalInnerCondition's.
1 parent 4f4fb4b commit 0da78a7

13 files changed

Lines changed: 78 additions & 45 deletions

File tree

src/internal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25142,7 +25142,7 @@ const char* wolfSSL_ERR_reason_error_string(unsigned long e)
2514225142
}
2514325143

2514425144
/* pass to wolfCrypt */
25145-
if (error < MAX_CODE_E && error > MIN_CODE_E) {
25145+
if (error <= WC_FIRST_E && error >= WC_LAST_E) {
2514625146
return wc_GetErrorString(error);
2514725147
}
2514825148

src/ssl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15361,7 +15361,9 @@ int wolfSSL_ERR_GET_REASON(unsigned long err)
1536115361
ret = 0 - ret; /* setting as negative value */
1536215362
/* wolfCrypt range is less than MAX (-100)
1536315363
wolfSSL range is MIN (-300) and lower */
15364-
if (ret < MAX_CODE_E && ret > MIN_CODE_E) {
15364+
if ((ret <= WC_FIRST_E && ret >= WC_LAST_E) ||
15365+
(ret <= WOLFSSL_FIRST_E && ret >= WOLFSSL_LAST_E))
15366+
{
1536515367
return ret;
1536615368
}
1536715369
else {

support/gen-debug-trace-error-codes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BEGIN {
1212
print("#undef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H") >> "wolfssl/debug-untrace-error-codes.h";
1313
}
1414
{
15-
if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)[,[:space:]]")) {
15+
if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)([,[:space:]]|$)")) {
1616
1717
# for mawkward compatibility -- gawk allows errcode_a as the 3rd arg to match().
1818
gsub("^[[:space:]]+", "", $0);

wolfcrypt/src/ecc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,8 +2491,7 @@ static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
24912491
}
24922492
if (err == MP_OKAY && mp_iszero((MP_INT_SIZE*)t2)) {
24932493
/* T2 = X * X */
2494-
if (err == MP_OKAY)
2495-
err = mp_sqr(x, t2);
2494+
err = mp_sqr(x, t2);
24962495
if (err == MP_OKAY)
24972496
err = mp_montgomery_reduce(t2, modulus, mp);
24982497
/* T1 = T2 + T1 */
@@ -2506,8 +2505,7 @@ static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
25062505
/* use "a" in calc */
25072506

25082507
/* T2 = T1 * T1 */
2509-
if (err == MP_OKAY)
2510-
err = mp_sqr(t1, t2);
2508+
err = mp_sqr(t1, t2);
25112509
if (err == MP_OKAY)
25122510
err = mp_montgomery_reduce(t2, modulus, mp);
25132511
/* T1 = T2 * a */

wolfcrypt/src/error.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ const char* wc_GetErrorString(int error)
4444
{
4545
switch (error) {
4646

47+
case MP_MEM :
48+
return "MP integer dynamic memory allocation failed";
49+
50+
case MP_VAL :
51+
return "MP integer invalid argument";
52+
53+
case MP_WOULDBLOCK :
54+
return "MP integer non-blocking operation would block";
55+
56+
case MP_NOT_INF:
57+
return "MP point not at infinity";
58+
4759
case OPEN_RAN_E :
4860
return "opening random device error";
4961

wolfcrypt/src/sp_int.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This library provides single precision (SP) integer math functions.
3131
#endif
3232

3333
#include <wolfssl/wolfcrypt/settings.h>
34+
#include <wolfssl/wolfcrypt/error-crypt.h>
3435

3536
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
3637

wolfcrypt/src/tfm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5685,9 +5685,9 @@ int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap)
56855685

56865686
err = fp_randprime(a, len, rng, heap);
56875687
switch(err) {
5688-
case FP_VAL:
5688+
case WC_NO_ERR_TRACE(MP_VAL):
56895689
return MP_VAL;
5690-
case FP_MEM:
5690+
case WC_NO_ERR_TRACE(MP_MEM):
56915691
return MP_MEM;
56925692
default:
56935693
break;

wolfcrypt/test/test.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,40 +2623,54 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
26232623
int i;
26242624
int j = 0;
26252625
/* Values that are not or no longer error codes. */
2626-
int missing[] = { -124, -166, -167, -168, -169, 0 };
2626+
static const struct {
2627+
int first;
2628+
int last;
2629+
} missing[] = {
2630+
{ -6, -100 },
2631+
{ -124, -124 },
2632+
{ -166, -169 }
2633+
};
26272634

26282635
/* Check that all errors have a string and it's the same through the two
26292636
* APIs. Check that the values that are not errors map to the unknown
26302637
* string.
26312638
*/
2632-
for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
2639+
for (i = WC_FIRST_E; i >= WC_LAST_E; i--) {
2640+
int this_missing = 0;
2641+
for (j = 0; j < (int)XELEM_CNT(missing); ++j) {
2642+
if ((i <= missing[j].first) && (i >= missing[j].last)) {
2643+
this_missing = 1;
2644+
break;
2645+
}
2646+
}
26332647
errStr = wc_GetErrorString(i);
26342648
wc_ErrorString(i, out);
26352649

2636-
if (i != missing[j]) {
2650+
if (! this_missing) {
26372651
if (XSTRCMP(errStr, unknownStr) == 0) {
26382652
WOLFSSL_MSG("errStr unknown");
2639-
return WC_TEST_RET_ENC_NC;
2653+
return WC_TEST_RET_ENC_I(-i);
26402654
}
26412655
if (XSTRCMP(out, unknownStr) == 0) {
26422656
WOLFSSL_MSG("out unknown");
2643-
return WC_TEST_RET_ENC_NC;
2657+
return WC_TEST_RET_ENC_I(-i);
26442658
}
26452659
if (XSTRCMP(errStr, out) != 0) {
26462660
WOLFSSL_MSG("errStr does not match output");
2647-
return WC_TEST_RET_ENC_NC;
2661+
return WC_TEST_RET_ENC_I(-i);
26482662
}
26492663
if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ) {
26502664
WOLFSSL_MSG("errStr too long");
2651-
return WC_TEST_RET_ENC_NC;
2665+
return WC_TEST_RET_ENC_I(-i);
26522666
}
26532667
}
26542668
else {
26552669
j++;
26562670
if (XSTRCMP(errStr, unknownStr) != 0)
2657-
return WC_TEST_RET_ENC_NC;
2671+
return WC_TEST_RET_ENC_I(-i);
26582672
if (XSTRCMP(out, unknownStr) != 0)
2659-
return WC_TEST_RET_ENC_NC;
2673+
return WC_TEST_RET_ENC_I(-i);
26602674
}
26612675
}
26622676

wolfssl/error-ssl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#endif
3636

3737
enum wolfSSL_ErrorCodes {
38+
WOLFSSL_FIRST_E = -301,
39+
3840
INPUT_CASE_ERROR = -301, /* process input state error */
3941
PREFIX_ERROR = -302, /* bad index to key rounds */
4042
MEMORY_ERROR = -303, /* out of memory */
@@ -196,8 +198,11 @@ enum wolfSSL_ErrorCodes {
196198
KEY_SHARE_ERROR = -503, /* key share mismatch */
197199
POST_HAND_AUTH_ERROR = -504, /* client won't do post-hand auth */
198200
HRR_COOKIE_ERROR = -505, /* HRR msg cookie mismatch */
199-
UNSUPPORTED_CERTIFICATE = -506 /* unsupported certificate type */
201+
UNSUPPORTED_CERTIFICATE = -506, /* unsupported certificate type */
200202
/* end negotiation parameter errors only 10 for now */
203+
204+
WOLFSSL_LAST_E = -506
205+
201206
/* add strings to wolfSSL_ERR_reason_error_string in internal.c !!!!! */
202207

203208
/* no error strings go down here, add above negotiation errors !!!! */

wolfssl/wolfcrypt/error-crypt.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,21 @@ the error status.
3737
extern "C" {
3838
#endif
3939

40+
#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H
41+
#include <wolfssl/debug-untrace-error-codes.h>
42+
#endif
4043

4144
/* error codes, add string for new errors !!! */
4245
enum {
43-
MAX_CODE_E = -100, /* errors -101 - -299 */
46+
MAX_CODE_E = -1, /* errors -2 - -299 */
47+
WC_FIRST_E = -2, /* errors -2 - -299 */
48+
49+
MP_MEM = -2, /* MP dynamic memory allocation failed. */
50+
MP_VAL = -3, /* MP value passed is not able to be used. */
51+
MP_WOULDBLOCK = -4, /* MP non-blocking operation is returning after
52+
* partial completion. */
53+
MP_NOT_INF = -5, /* MP point not at infinity */
54+
4455
OPEN_RAN_E = -101, /* opening random device error */
4556
READ_RAN_E = -102, /* reading random device error */
4657
WINCRYPT_E = -103, /* windows crypt init error */
@@ -276,13 +287,12 @@ enum {
276287
SM4_CCM_AUTH_E = -299, /* SM4-CCM Authentication check failure */
277288

278289
WC_LAST_E = -299, /* Update this to indicate last error */
279-
MIN_CODE_E = -300 /* errors -101 - -299 */
290+
MIN_CODE_E = -300 /* errors -2 - -299 */
280291

281292
/* add new companion error id strings for any new error codes
282293
wolfcrypt/src/error.c !!! */
283294
};
284295

285-
286296
#ifdef NO_ERROR_STRINGS
287297
#define wc_GetErrorString(error) "no support for error strings built in"
288298
#define wc_ErrorString(err, buf) \

0 commit comments

Comments
 (0)