Skip to content

Commit 4813648

Browse files
Add support for wcGetErrorString() including error codes.
It was already used, but not compiled in. A unit test is added. As wcGetErrorString() always returns a string the error handling in class _Hmac has been removed.
1 parent 6fbdf3d commit 4813648

3 files changed

Lines changed: 298 additions & 6 deletions

File tree

scripts/build_ffi.py

Lines changed: 267 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ def make_flags(prefix, fips):
242242
flags.append("--disable-oldtls")
243243
flags.append("--disable-oldnames")
244244
flags.append("--disable-extended-master")
245-
flags.append("--disable-errorstrings")
246245

247246
return " ".join(flags)
248247

@@ -429,6 +428,7 @@ def build_ffi(local_wolfssl, features):
429428

430429
includes_string += """
431430
#include <wolfssl/wolfcrypt/settings.h>
431+
#include <wolfssl/wolfcrypt/error-crypt.h>
432432
433433
#include <wolfssl/wolfcrypt/sha.h>
434434
#include <wolfssl/wolfcrypt/sha256.h>
@@ -466,6 +466,7 @@ def build_ffi(local_wolfssl, features):
466466
}
467467
#endif
468468
469+
int ERROR_STRINGS_ENABLED = """ + str(features["ERROR_STRINGS"]) + """;
469470
int MPAPI_ENABLED = """ + str(features["MPAPI"]) + """;
470471
int SHA_ENABLED = """ + str(features["SHA"]) + """;
471472
int SHA256_ENABLED = """ + str(features["SHA256"]) + """;
@@ -505,6 +506,7 @@ def build_ffi(local_wolfssl, features):
505506
# TODO: change cdef to cdef.
506507
# cdef = ""
507508
cdef = """
509+
extern int ERROR_STRINGS_ENABLED;
508510
extern int MPAPI_ENABLED;
509511
extern int SHA_ENABLED;
510512
extern int SHA256_ENABLED;
@@ -549,6 +551,270 @@ def build_ffi(local_wolfssl, features):
549551
int wc_FreeRng(WC_RNG*);
550552
"""
551553

554+
if features["ERROR_STRINGS"]:
555+
cdef += """
556+
static const int WC_FAILURE;
557+
558+
static const int MAX_CODE_E;
559+
static const int WC_FIRST_E;
560+
561+
static const int WC_SPAN1_FIRST_E;
562+
563+
static const int MP_MEM;
564+
static const int MP_VAL;
565+
static const int MP_WOULDBLOCK;
566+
567+
static const int MP_NOT_INF;
568+
569+
static const int OPEN_RAN_E;
570+
static const int READ_RAN_E;
571+
static const int WINCRYPT_E;
572+
static const int CRYPTGEN_E;
573+
static const int RAN_BLOCK_E;
574+
static const int BAD_MUTEX_E;
575+
static const int WC_TIMEOUT_E;
576+
static const int WC_PENDING_E;
577+
static const int WC_NO_PENDING_E;
578+
579+
static const int MP_INIT_E;
580+
static const int MP_READ_E;
581+
static const int MP_EXPTMOD_E;
582+
static const int MP_TO_E;
583+
static const int MP_SUB_E;
584+
static const int MP_ADD_E;
585+
static const int MP_MUL_E;
586+
static const int MP_MULMOD_E;
587+
static const int MP_MOD_E;
588+
static const int MP_INVMOD_E;
589+
static const int MP_CMP_E;
590+
static const int MP_ZERO_E;
591+
592+
static const int AES_EAX_AUTH_E;
593+
static const int KEY_EXHAUSTED_E;
594+
static const int MEMORY_E;
595+
static const int VAR_STATE_CHANGE_E;
596+
static const int FIPS_DEGRADED_E;
597+
598+
static const int FIPS_CODE_SZ_E;
599+
static const int FIPS_DATA_SZ_E;
600+
601+
static const int RSA_WRONG_TYPE_E;
602+
static const int RSA_BUFFER_E;
603+
static const int BUFFER_E;
604+
static const int ALGO_ID_E;
605+
static const int PUBLIC_KEY_E;
606+
static const int DATE_E;
607+
static const int SUBJECT_E;
608+
static const int ISSUER_E;
609+
static const int CA_TRUE_E;
610+
static const int EXTENSIONS_E;
611+
612+
static const int ASN_PARSE_E;
613+
static const int ASN_VERSION_E;
614+
static const int ASN_GETINT_E;
615+
static const int ASN_RSA_KEY_E;
616+
static const int ASN_OBJECT_ID_E;
617+
static const int ASN_TAG_NULL_E;
618+
static const int ASN_EXPECT_0_E;
619+
static const int ASN_BITSTR_E;
620+
static const int ASN_UNKNOWN_OID_E;
621+
static const int ASN_DATE_SZ_E;
622+
static const int ASN_BEFORE_DATE_E;
623+
static const int ASN_AFTER_DATE_E;
624+
static const int ASN_SIG_OID_E;
625+
static const int ASN_TIME_E;
626+
static const int ASN_INPUT_E;
627+
static const int ASN_SIG_CONFIRM_E;
628+
static const int ASN_SIG_HASH_E;
629+
static const int ASN_SIG_KEY_E;
630+
static const int ASN_DH_KEY_E;
631+
static const int KDF_SRTP_KAT_FIPS_E;
632+
static const int ASN_CRIT_EXT_E;
633+
static const int ASN_ALT_NAME_E;
634+
static const int ASN_NO_PEM_HEADER;
635+
static const int ED25519_KAT_FIPS_E;
636+
static const int ED448_KAT_FIPS_E;
637+
static const int PBKDF2_KAT_FIPS_E;
638+
static const int WC_KEY_MISMATCH_E;
639+
640+
static const int ECC_BAD_ARG_E;
641+
static const int ASN_ECC_KEY_E;
642+
static const int ECC_CURVE_OID_E;
643+
static const int BAD_FUNC_ARG;
644+
static const int NOT_COMPILED_IN;
645+
static const int UNICODE_SIZE_E;
646+
static const int NO_PASSWORD;
647+
static const int ALT_NAME_E;
648+
static const int BAD_OCSP_RESPONDER;
649+
static const int CRL_CERT_DATE_ERR;
650+
651+
static const int AES_GCM_AUTH_E;
652+
static const int AES_CCM_AUTH_E;
653+
654+
static const int ASYNC_INIT_E;
655+
656+
static const int COMPRESS_INIT_E;
657+
static const int COMPRESS_E;
658+
static const int DECOMPRESS_INIT_E;
659+
static const int DECOMPRESS_E;
660+
661+
static const int BAD_ALIGN_E;
662+
static const int ASN_NO_SIGNER_E;
663+
static const int ASN_CRL_CONFIRM_E;
664+
static const int ASN_CRL_NO_SIGNER_E;
665+
static const int ASN_OCSP_CONFIRM_E;
666+
667+
static const int BAD_STATE_E;
668+
static const int BAD_PADDING_E;
669+
670+
static const int REQ_ATTRIBUTE_E;
671+
672+
static const int PKCS7_OID_E;
673+
static const int PKCS7_RECIP_E;
674+
static const int FIPS_NOT_ALLOWED_E;
675+
static const int ASN_NAME_INVALID_E;
676+
677+
static const int RNG_FAILURE_E;
678+
static const int HMAC_MIN_KEYLEN_E;
679+
static const int RSA_PAD_E;
680+
static const int LENGTH_ONLY_E;
681+
682+
static const int IN_CORE_FIPS_E;
683+
static const int AES_KAT_FIPS_E;
684+
static const int DES3_KAT_FIPS_E;
685+
static const int HMAC_KAT_FIPS_E;
686+
static const int RSA_KAT_FIPS_E;
687+
static const int DRBG_KAT_FIPS_E;
688+
static const int DRBG_CONT_FIPS_E;
689+
static const int AESGCM_KAT_FIPS_E;
690+
static const int THREAD_STORE_KEY_E;
691+
static const int THREAD_STORE_SET_E;
692+
693+
static const int MAC_CMP_FAILED_E;
694+
static const int IS_POINT_E;
695+
static const int ECC_INF_E;
696+
static const int ECC_PRIV_KEY_E;
697+
static const int ECC_OUT_OF_RANGE_E;
698+
699+
static const int SRP_CALL_ORDER_E;
700+
static const int SRP_VERIFY_E;
701+
static const int SRP_BAD_KEY_E;
702+
703+
static const int ASN_NO_SKID;
704+
static const int ASN_NO_AKID;
705+
static const int ASN_NO_KEYUSAGE;
706+
static const int SKID_E;
707+
static const int AKID_E;
708+
static const int KEYUSAGE_E;
709+
static const int CERTPOLICIES_E;
710+
711+
static const int WC_INIT_E;
712+
static const int SIG_VERIFY_E;
713+
static const int BAD_COND_E;
714+
static const int SIG_TYPE_E;
715+
static const int HASH_TYPE_E;
716+
717+
static const int FIPS_INVALID_VER_E;
718+
719+
static const int WC_KEY_SIZE_E;
720+
static const int ASN_COUNTRY_SIZE_E;
721+
static const int MISSING_RNG_E;
722+
static const int ASN_PATHLEN_SIZE_E;
723+
static const int ASN_PATHLEN_INV_E;
724+
725+
static const int BAD_KEYWRAP_ALG_E;
726+
static const int BAD_KEYWRAP_IV_E;
727+
static const int WC_CLEANUP_E;
728+
static const int ECC_CDH_KAT_FIPS_E;
729+
static const int DH_CHECK_PUB_E;
730+
static const int BAD_PATH_ERROR;
731+
732+
static const int ASYNC_OP_E;
733+
734+
static const int ECC_PRIVATEONLY_E;
735+
static const int EXTKEYUSAGE_E;
736+
static const int WC_HW_E;
737+
static const int WC_HW_WAIT_E;
738+
739+
static const int PSS_SALTLEN_E;
740+
static const int PRIME_GEN_E;
741+
static const int BER_INDEF_E;
742+
static const int RSA_OUT_OF_RANGE_E;
743+
static const int RSAPSS_PAT_FIPS_E;
744+
static const int ECDSA_PAT_FIPS_E;
745+
static const int DH_KAT_FIPS_E;
746+
static const int AESCCM_KAT_FIPS_E;
747+
static const int SHA3_KAT_FIPS_E;
748+
static const int ECDHE_KAT_FIPS_E;
749+
static const int AES_GCM_OVERFLOW_E;
750+
static const int AES_CCM_OVERFLOW_E;
751+
static const int RSA_KEY_PAIR_E;
752+
static const int DH_CHECK_PRIV_E;
753+
754+
static const int WC_AFALG_SOCK_E;
755+
static const int WC_DEVCRYPTO_E;
756+
757+
static const int ZLIB_INIT_ERROR;
758+
static const int ZLIB_COMPRESS_ERROR;
759+
static const int ZLIB_DECOMPRESS_ERROR;
760+
761+
static const int PKCS7_NO_SIGNER_E;
762+
static const int WC_PKCS7_WANT_READ_E;
763+
764+
static const int CRYPTOCB_UNAVAILABLE;
765+
static const int PKCS7_SIGNEEDS_CHECK;
766+
static const int PSS_SALTLEN_RECOVER_E;
767+
static const int CHACHA_POLY_OVERFLOW;
768+
static const int ASN_SELF_SIGNED_E;
769+
static const int SAKKE_VERIFY_FAIL_E;
770+
static const int MISSING_IV;
771+
static const int MISSING_KEY;
772+
static const int BAD_LENGTH_E;
773+
static const int ECDSA_KAT_FIPS_E;
774+
static const int RSA_PAT_FIPS_E;
775+
static const int KDF_TLS12_KAT_FIPS_E;
776+
static const int KDF_TLS13_KAT_FIPS_E;
777+
static const int KDF_SSH_KAT_FIPS_E;
778+
static const int DHE_PCT_E;
779+
static const int ECC_PCT_E;
780+
static const int FIPS_PRIVATE_KEY_LOCKED_E;
781+
static const int PROTOCOLCB_UNAVAILABLE;
782+
static const int AES_SIV_AUTH_E;
783+
static const int NO_VALID_DEVID;
784+
785+
static const int IO_FAILED_E;
786+
static const int SYSLIB_FAILED_E;
787+
static const int USE_HW_PSK;
788+
789+
static const int ENTROPY_RT_E;
790+
static const int ENTROPY_APT_E;
791+
792+
static const int ASN_DEPTH_E;
793+
static const int ASN_LEN_E;
794+
795+
static const int SM4_GCM_AUTH_E;
796+
static const int SM4_CCM_AUTH_E;
797+
798+
static const int WC_SPAN1_LAST_E;
799+
static const int WC_SPAN1_MIN_CODE_E;
800+
801+
static const int WC_SPAN2_FIRST_E;
802+
803+
static const int DEADLOCK_AVERTED_E;
804+
static const int ASCON_AUTH_E;
805+
static const int WC_ACCEL_INHIBIT_E;
806+
static const int BAD_INDEX_E;
807+
static const int INTERRUPTED_E;
808+
809+
static const int WC_SPAN2_LAST_E;
810+
static const int WC_LAST_E;
811+
812+
static const int WC_SPAN2_MIN_CODE_E;
813+
static const int MIN_CODE_E;
814+
815+
const char* wc_GetErrorString(int error);
816+
"""
817+
552818
if not features["FIPS"] or features["FIPS_VERSION"] > 2:
553819
cdef += """
554820
int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);

tests/test_error_string.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# test_error_string.py
2+
#
3+
# Copyright (C) 2026 wolfSSL Inc.
4+
#
5+
# This file is part of wolfSSL. (formerly known as CyaSSL)
6+
#
7+
# wolfSSL is free software; you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation; either version 2 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# wolfSSL is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
21+
import pytest
22+
23+
from wolfcrypt._ffi import ffi as _ffi
24+
from wolfcrypt._ffi import lib as _lib
25+
26+
27+
@pytest.mark.skipif(not _lib.ERROR_STRINGS_ENABLED, reason="wc_GetErrorString not enabled")
28+
@pytest.mark.parametrize("err", (_lib.WC_FAILURE, _lib.KEY_EXHAUSTED_E, _lib.NO_PASSWORD, _lib.INTERRUPTED_E, 0))
29+
def test_error_string(err):
30+
print(_ffi.string(_lib.wc_GetErrorString(err)).decode())

wolfcrypt/hashes.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,7 @@ def _init(self, hmac, key):
318318
if len(key) > 0:
319319
ret = _lib.wc_HmacSetKey(self._native_object, hmac, key, len(key))
320320
if ret < 0:
321-
err_str = "no error description found"
322-
try:
323-
err_str = _ffi.string(_lib.wc_GetErrorString(ret)).decode()
324-
except:
325-
pass
321+
err_str = _ffi.string(_lib.wc_GetErrorString(ret)).decode()
326322
raise WolfCryptError("wc_HmacSetKey returned {}: {}".format(ret, err_str))
327323
return ret
328324

0 commit comments

Comments
 (0)