From 6ca1981e015a1b63b0b1ae7d497304a841b5098f Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Wed, 14 Aug 2024 23:58:57 -0500 Subject: [PATCH] support/gen-debug-trace-error-codes.sh: add coverage of wolfssl/wolfcrypt/sp_int.h. 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. wolfcrypt/src/srp.c and src/pk.c: cast mixed enums to ints to suppress C++ -Wenum-compare's. --- src/pk.c | 2 +- support/gen-debug-trace-error-codes.sh | 25 ++++++++++++++++++-- wolfcrypt/src/ecc.c | 6 ++--- wolfcrypt/src/sp_int.c | 1 + wolfcrypt/src/srp.c | 3 ++- wolfssl/wolfcrypt/error-crypt.h | 3 +++ wolfssl/wolfcrypt/sp_int.h | 32 +++++++++++++++++--------- 7 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/pk.c b/src/pk.c index 34e2727844..30e3b14f8d 100644 --- a/src/pk.c +++ b/src/pk.c @@ -10316,7 +10316,7 @@ size_t wolfSSL_EC_POINT_point2oct(const WOLFSSL_EC_GROUP *group, /* Check return. When buf is NULL, return will be length only * error. */ - if (ret != ((buf != NULL) ? MP_OKAY : LENGTH_ONLY_E)) { + if (ret != ((buf != NULL) ? (int)MP_OKAY : (int)LENGTH_ONLY_E)) { err = 1; } } diff --git a/support/gen-debug-trace-error-codes.sh b/support/gen-debug-trace-error-codes.sh index 01f32faa80..857e1b37ca 100755 --- a/support/gen-debug-trace-error-codes.sh +++ b/support/gen-debug-trace-error-codes.sh @@ -10,9 +10,25 @@ BEGIN { print("/* automatically generated, do not edit */") > "wolfssl/debug-untrace-error-codes.h"; print("#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H") >> "wolfssl/debug-untrace-error-codes.h"; print("#undef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H") >> "wolfssl/debug-untrace-error-codes.h"; + + sp_int_ifdef_printed = 0; } { - if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)[,[:space:]]")) { + if (FILENAME == "wolfssl/wolfcrypt/sp_int.h") { + if (! sp_int_ifdef_printed) { + print("#if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)") >> "wolfssl/debug-trace-error-codes.h"; + print("#if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)") >> "wolfssl/debug-untrace-error-codes.h"; + sp_int_ifdef_printed = 1; + } + } else { + if (sp_int_ifdef_printed) { + print("#endif") >> "wolfssl/debug-trace-error-codes.h"; + print("#endif") >> "wolfssl/debug-untrace-error-codes.h"; + sp_int_ifdef_printed = 0; + } + } + + if (match($0, "^[[:space:]]+([A-Z][A-Z0-9_]+)[[:space:]]*=[[:space:]]*(-[0-9]+)([,[:space:]]|$)")) { # for mawkward compatibility -- gawk allows errcode_a as the 3rd arg to match(). gsub("^[[:space:]]+", "", $0); @@ -29,9 +45,14 @@ BEGIN { } } END { + if (sp_int_ifdef_printed) { + print("#endif") >> "wolfssl/debug-trace-error-codes.h"; + print("#endif") >> "wolfssl/debug-untrace-error-codes.h"; + } + print("") >> "wolfssl/debug-trace-error-codes.h"; print("#endif /* WOLFSSL_DEBUG_TRACE_ERROR_CODES_H */") >> "wolfssl/debug-trace-error-codes.h"; print("") >> "wolfssl/debug-untrace-error-codes.h"; print("#endif /* WOLFSSL_DEBUG_TRACE_ERROR_CODES_H */") >> "wolfssl/debug-untrace-error-codes.h"; -}' wolfssl/wolfcrypt/error-crypt.h wolfssl/error-ssl.h +}' wolfssl/wolfcrypt/error-crypt.h wolfssl/error-ssl.h wolfssl/wolfcrypt/sp_int.h diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 46d7da16c3..8085fea6d7 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -2491,8 +2491,7 @@ static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, } if (err == MP_OKAY && mp_iszero((MP_INT_SIZE*)t2)) { /* T2 = X * X */ - if (err == MP_OKAY) - err = mp_sqr(x, t2); + err = mp_sqr(x, t2); if (err == MP_OKAY) err = mp_montgomery_reduce(t2, modulus, mp); /* T1 = T2 + T1 */ @@ -2506,8 +2505,7 @@ static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a, /* use "a" in calc */ /* T2 = T1 * T1 */ - if (err == MP_OKAY) - err = mp_sqr(t1, t2); + err = mp_sqr(t1, t2); if (err == MP_OKAY) err = mp_montgomery_reduce(t2, modulus, mp); /* T1 = T2 * a */ diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index a25ddab4ab..35d6aa300d 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -31,6 +31,7 @@ This library provides single precision (SP) integer math functions. #endif #include +#include #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) diff --git a/wolfcrypt/src/srp.c b/wolfcrypt/src/srp.c index 717a93822e..61be6b8116 100644 --- a/wolfcrypt/src/srp.c +++ b/wolfcrypt/src/srp.c @@ -495,7 +495,8 @@ int wc_SrpGetVerifier(Srp* srp, byte* verifier, word32* size) r = MP_INIT_E; /* v = g ^ x % N */ if (!r) r = mp_exptmod(&srp->g, &srp->auth, &srp->N, v); - if (!r) r = *size < (word32)mp_unsigned_bin_size(v) ? BUFFER_E : MP_OKAY; + if (!r) r = *size < (word32)mp_unsigned_bin_size(v) ? + (int)BUFFER_E : (int)MP_OKAY; if (!r) r = mp_to_unsigned_bin(v, verifier); if (!r) *size = (word32)mp_unsigned_bin_size(v); diff --git a/wolfssl/wolfcrypt/error-crypt.h b/wolfssl/wolfcrypt/error-crypt.h index 413868ebbc..c5178835b9 100644 --- a/wolfssl/wolfcrypt/error-crypt.h +++ b/wolfssl/wolfcrypt/error-crypt.h @@ -37,6 +37,9 @@ the error status. extern "C" { #endif +#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H +#include +#endif /* error codes, add string for new errors !!! */ enum { diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 26978acfe0..9cb70062f3 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -741,22 +741,32 @@ typedef struct sp_ecc_ctx { /** Result of comparison is that the first number is less than second. */ #define MP_LT (-1) +#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES_H +#include +#endif + /* ERROR VALUES */ -/** Error value on success. */ -#define MP_OKAY 0 -/** Error value when dynamic memory allocation fails. */ -#define MP_MEM (-2) -/** Error value when value passed is not able to be used. */ -#define MP_VAL (-3) -/** Error value when non-blocking operation is returning after partial - * completion. - */ -#define FP_WOULDBLOCK (-4) +enum { + /** Error value on success. */ + MP_OKAY = 0, + /** Error value when dynamic memory allocation fails. */ + MP_MEM = -2, + /** Error value when value passed is not able to be used. */ + MP_VAL = -3, + /** Error value when non-blocking operation is returning after partial + * completion. + */ + FP_WOULDBLOCK = -4 +}; + +#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES +#include +#endif + /* Unused error. Defined for backward compatibility. */ #define MP_NOT_INF (-5) /* Unused error. Defined for backward compatibility. */ #define MP_RANGE MP_NOT_INF - #ifdef USE_FAST_MATH /* For old FIPS, need FP_MEM defined for old implementation. */ #define FP_MEM (-2)