diff --git a/tests/api.c b/tests/api.c index 8786331f5..1536ff510 100644 --- a/tests/api.c +++ b/tests/api.c @@ -46089,7 +46089,56 @@ static void test_EVP_blake2() AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0); #endif printf(resultFmt, passed); +#endif +} + +#if defined(OPENSSL_EXTRA) +static void list_md_fn(const EVP_MD* m, const char* from, + const char* to, void* arg) +{ + const char* mn; + BIO *bio; + (void) from; + (void) to; + (void) arg; + (void) mn; + (void) bio; + + if (!m) { + /* alias */ + AssertNull(m); + AssertNotNull(to); + } + else { + AssertNotNull(m); + AssertNull(to); + } + AssertNotNull(from); + mn = EVP_get_digestbyname(from); + +#if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE) + /* print to stdout */ + AssertNotNull(arg); + + bio = BIO_new(BIO_s_file()); + BIO_set_fp(bio, arg, BIO_NOCLOSE); + BIO_printf(bio, "-%-14s to use the %s message digest algorithm\n", mn, mn); + BIO_free(bio); +#endif +} +#endif + +static void test_EVP_MD_do_all() +{ +#if defined(OPENSSL_EXTRA) + printf(testingFmt, "test_EVP_MD_do_all"); + + EVP_MD_do_all(list_md_fn, stdout); + /* to confirm previous call gives no harm */ + AssertTrue(1); + + printf(resultFmt, passed); #endif } /*----------------------------------------------------------------------------* @@ -46114,6 +46163,7 @@ void ApiTest(void) #endif test_wolfSSL_ERR_strings(); test_EVP_blake2(); + test_EVP_MD_do_all(); test_wolfSSL_CTX_use_certificate_file(); AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS); test_wolfSSL_CTX_use_PrivateKey_file(); diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index 91a895797..ffb54833d 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -3534,25 +3534,20 @@ int wolfSSL_EVP_Digest(const unsigned char* in, int inSz, unsigned char* out, return WOLFSSL_SUCCESS; } +static const struct alias { + const char *name; + const char *alias; +} alias_tbl[] = +{ + {"MD4", "ssl3-md4"}, + {"MD5", "ssl3-md5"}, + {"SHA1", "ssl3-sha1"}, + {"SHA1", "SHA"}, + { NULL, NULL} +}; + const WOLFSSL_EVP_MD *wolfSSL_EVP_get_digestbyname(const char *name) { - static const struct alias { - const char *name; - const char *alias; - } alias_tbl[] = - { - {"MD4", "ssl3-md4"}, - {"MD5", "ssl3-md5"}, - {"SHA1", "ssl3-sha1"}, - {"SHA1", "SHA"}, -#ifdef HAVE_BLAKE2 - {"BLAKE2b512", "blake2b512"}, -#endif -#ifdef HAVE_BLAKE2S - {"BLAKE2s256", "blake2s256"}, -#endif - { NULL, NULL} - }; char nameUpper[15]; /* 15 bytes should be enough for any name */ size_t i; @@ -3941,7 +3936,49 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md) } return (WOLFSSL_EVP_MD *)NULL; } - + + /* return alias name if has + * @param n message digest type name + * @return alias name, otherwise NULL + */ + static const char* hasAliasName(const char* n) + { + + const char* aliasnm = NULL; + const struct alias *al; + + for (al = alias_tbl; al->name != NULL; al++) + if(XSTRNCMP(n, al->name, XSTRLEN(al->name)+1) == 0) { + aliasnm = al->alias; + break; + } + + return aliasnm; + } + + /* do all md algorithm through a callback function + * @param fn a callback function to be called with all 'md' + * @param args arguments to pass to the callback + * @return none + */ + void wolfSSL_EVP_MD_do_all(void (*fn) (const WOLFSSL_EVP_MD *m, + const char* from, const char* to, void* xx), void* args) + { + const char* alias = NULL; + const struct s_ent *ent; + + /* loop all md */ + for (ent = md_tbl; ent->name != NULL; ent++){ + /* check if the md has alias */ + if((alias = hasAliasName(ent->name)) != NULL) { + fn(NULL, ent->name, ent->name, args); + } + else { + fn(ent->name, ent->name, NULL, args); + } + } + } + #ifndef NO_AES #ifdef HAVE_AES_CBC diff --git a/wolfssl/openssl/evp.h b/wolfssl/openssl/evp.h index 2cdf6fa8f..212bb6910 100644 --- a/wolfssl/openssl/evp.h +++ b/wolfssl/openssl/evp.h @@ -688,6 +688,10 @@ WOLFSSL_API int wolfSSL_PKCS5_PBKDF2_HMAC(const char *pass, int passlen, WOLFSSL_LOCAL int wolfSSL_EVP_get_hashinfo(const WOLFSSL_EVP_MD* evp, int* pHash, int* pHashSz); +WOLFSSL_API void wolfSSL_EVP_MD_do_all(void (*fn) (const WOLFSSL_EVP_MD *md, + const char* from, const char* to, + void* xx), void* args); + #define EVP_CIPH_STREAM_CIPHER WOLFSSL_EVP_CIPH_STREAM_CIPHER #define EVP_CIPH_ECB_MODE WOLFSSL_EVP_CIPH_ECB_MODE #define EVP_CIPH_CBC_MODE WOLFSSL_EVP_CIPH_CBC_MODE @@ -1016,6 +1020,7 @@ typedef WOLFSSL_ASN1_PCTX ASN1_PCTX; #define EVP_blake2b512 wolfSSL_EVP_blake2b512 #define EVP_blake2s256 wolfSSL_EVP_blake2s256 +#define EVP_MD_do_all wolfSSL_EVP_MD_do_all WOLFSSL_API void printPKEY(WOLFSSL_EVP_PKEY *k);