Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
25164 lines (21820 sloc) 791 KB
/* test.c
*
* Copyright (C) 2006-2019 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/version.h>
#ifndef NO_CRYPT_TEST
/* only for stack size check */
#ifdef HAVE_STACK_SIZE
#include <wolfssl/ssl.h>
#define err_sys err_sys_remap /* remap err_sys */
#include <wolfssl/test.h>
#undef err_sys
#endif
#ifdef USE_FLAT_TEST_H
#include "test.h"
#else
#include "wolfcrypt/test/test.h"
#endif
/* printf mappings */
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#include <mqx.h>
#include <stdlib.h>
#if MQX_USE_IO_OLD
#include <fio.h>
#else
#include <nio.h>
#endif
#elif defined(FREESCALE_KSDK_BM)
#include "fsl_debug_console.h"
#undef printf
#define printf PRINTF
#elif defined(WOLFSSL_APACHE_MYNEWT)
#include <assert.h>
#include <string.h>
#include "sysinit/sysinit.h"
#include "os/os.h"
#ifdef ARCH_sim
#include "mcu/mcu_sim.h"
#endif
#include "os/os_time.h"
#elif defined(WOLFSSL_ESPIDF)
#include <time.h>
#include <sys/time.h>
#elif defined(WOLFSSL_ZEPHYR)
#include <stdio.h>
#define printf printk
#elif defined(MICRIUM)
#include <bsp_ser.h>
void BSP_Ser_Printf (CPU_CHAR* format, ...);
#undef printf
#define printf BSP_Ser_Printf
#elif defined(WOLFSSL_PB)
#include <stdarg.h>
int wolfssl_pb_print(const char*, ...);
#undef printf
#define printf wolfssl_pb_print
#elif defined(WOLFSSL_TELIT_M2MB)
#include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
#include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
/* remap printf */
#undef printf
#define printf M2M_LOG_INFO
/* OS requires occasional sleep() */
#ifndef TEST_SLEEP_MS
#define TEST_SLEEP_MS 50
#endif
#define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
/* don't use file system for these tests, since ./certs dir isn't loaded */
#undef NO_FILESYSTEM
#define NO_FILESYSTEM
#elif defined(THREADX) && !defined(WOLFSSL_WICED)
/* since just testing, use THREADX log printf instead */
int dc_log_printf(char*, ...);
#undef printf
#define printf dc_log_printf
#else
#ifdef XMALLOC_USER
#include <stdlib.h> /* we're using malloc / free direct here */
#endif
#ifndef STRING_USER
#include <stdio.h>
#endif
/* enable way for customer to override test/bench printf */
#ifdef XPRINTF
#undef printf
#define printf XPRINTF
#endif
#endif
#include <wolfssl/wolfcrypt/memory.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/types.h>
#if defined(WOLFSSL_TEST_CERT) || defined(ASN_BER_TO_DER)
#include <wolfssl/wolfcrypt/asn.h>
#else
#include <wolfssl/wolfcrypt/asn_public.h>
#endif
#include <wolfssl/wolfcrypt/md2.h>
#include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/wolfcrypt/md4.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/sha512.h>
#include <wolfssl/wolfcrypt/arc4.h>
#if defined(WC_NO_RNG)
#include <wolfssl/wolfcrypt/integer.h>
#else
#include <wolfssl/wolfcrypt/random.h>
#endif
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/signature.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/wc_encrypt.h>
#include <wolfssl/wolfcrypt/cmac.h>
#include <wolfssl/wolfcrypt/poly1305.h>
#include <wolfssl/wolfcrypt/camellia.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/dh.h>
#include <wolfssl/wolfcrypt/dsa.h>
#include <wolfssl/wolfcrypt/srp.h>
#include <wolfssl/wolfcrypt/idea.h>
#include <wolfssl/wolfcrypt/hc128.h>
#include <wolfssl/wolfcrypt/rabbit.h>
#include <wolfssl/wolfcrypt/chacha.h>
#include <wolfssl/wolfcrypt/chacha20_poly1305.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/ripemd.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#endif
#ifdef HAVE_CURVE25519
#include <wolfssl/wolfcrypt/curve25519.h>
#endif
#ifdef HAVE_ED25519
#include <wolfssl/wolfcrypt/ed25519.h>
#endif
#if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
#include <wolfssl/wolfcrypt/blake2.h>
#endif
#ifdef WOLFSSL_SHA3
#include <wolfssl/wolfcrypt/sha3.h>
#endif
#ifdef HAVE_LIBZ
#include <wolfssl/wolfcrypt/compress.h>
#endif
#ifdef HAVE_PKCS7
#include <wolfssl/wolfcrypt/pkcs7.h>
#endif
#ifdef HAVE_FIPS
#include <wolfssl/wolfcrypt/fips_test.h>
#endif
#ifdef HAVE_SELFTEST
#include <wolfssl/wolfcrypt/selftest.h>
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
#include <wolfssl/wolfcrypt/async.h>
#endif
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
#include <wolfssl/wolfcrypt/logging.h>
#endif
#ifdef WOLFSSL_IMX6_CAAM_BLOB
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#endif
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef _MSC_VER
/* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
#pragma warning(disable: 4996)
#endif
#ifdef OPENSSL_EXTRA
#ifndef WOLFCRYPT_ONLY
#include <wolfssl/openssl/evp.h>
#endif
#include <wolfssl/openssl/rand.h>
#include <wolfssl/openssl/hmac.h>
#include <wolfssl/openssl/aes.h>
#include <wolfssl/openssl/des.h>
#endif
#if defined(NO_FILESYSTEM)
#if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
!defined(USE_CERT_BUFFERS_4096)
#define USE_CERT_BUFFERS_2048
#endif
#if !defined(USE_CERT_BUFFERS_256)
#define USE_CERT_BUFFERS_256
#endif
#endif
#if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
#define ENABLE_ECC384_CERT_GEN_TEST
#endif
#include <wolfssl/certs_test.h>
#ifdef HAVE_NTRU
#include "libntruencrypt/ntru_crypto.h"
#endif
#ifdef WOLFSSL_STATIC_MEMORY
static WOLFSSL_HEAP_HINT* HEAP_HINT;
#else
#define HEAP_HINT NULL
#endif /* WOLFSSL_STATIC_MEMORY */
/* these cases do not have intermediate hashing support */
#if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
&& !defined(WOLFSSL_XILINX_CRYPT)
#define NO_INTM_HASH_TEST
#endif
#if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
static void initDefaultName(void);
#endif
/* for async devices */
static int devId = INVALID_DEVID;
#ifdef HAVE_WNR
const char* wnrConfigFile = "wnr-example.conf";
#endif
typedef struct testVector {
const char* input;
const char* output;
size_t inLen;
size_t outLen;
} testVector;
int error_test(void);
int base64_test(void);
int base16_test(void);
int asn_test(void);
int md2_test(void);
int md5_test(void);
int md4_test(void);
int sha_test(void);
int sha224_test(void);
int sha256_test(void);
int sha512_test(void);
int sha384_test(void);
int sha3_test(void);
int hash_test(void);
int hmac_md5_test(void);
int hmac_sha_test(void);
int hmac_sha224_test(void);
int hmac_sha256_test(void);
int hmac_sha384_test(void);
int hmac_sha512_test(void);
int hmac_blake2b_test(void);
int hmac_sha3_test(void);
int hkdf_test(void);
int x963kdf_test(void);
int arc4_test(void);
int hc128_test(void);
int rabbit_test(void);
int chacha_test(void);
int chacha20_poly1305_aead_test(void);
int des_test(void);
int des3_test(void);
int aes_test(void);
int aes192_test(void);
int aes256_test(void);
int cmac_test(void);
int poly1305_test(void);
int aesgcm_test(void);
int aesgcm_default_test(void);
int gmac_test(void);
int aesccm_test(void);
int aeskeywrap_test(void);
int camellia_test(void);
int rsa_no_pad_test(void);
int rsa_test(void);
int dh_test(void);
int dsa_test(void);
int srp_test(void);
#ifndef WC_NO_RNG
int random_test(void);
#endif /* WC_NO_RNG */
int pwdbased_test(void);
int ripemd_test(void);
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
int openssl_test(void); /* test mini api */
int openssl_pkey_test(void);
int openssl_pkey0_test(void);
int openssl_pkey1_test(void);
int openSSL_evpMD_test(void);
int openssl_evpSig_test(void);
#endif
int pbkdf1_test(void);
int pkcs12_test(void);
int pbkdf2_test(void);
int scrypt_test(void);
#ifdef HAVE_ECC
int ecc_test(void);
#ifdef HAVE_ECC_ENCRYPT
int ecc_encrypt_test(void);
#endif
#ifdef USE_CERT_BUFFERS_256
int ecc_test_buffers(void);
#endif
#endif
#ifdef HAVE_CURVE25519
int curve25519_test(void);
#endif
#ifdef HAVE_ED25519
int ed25519_test(void);
#endif
#ifdef HAVE_BLAKE2
int blake2b_test(void);
#endif
#ifdef HAVE_BLAKE2S
int blake2s_test(void);
#endif
#ifdef HAVE_LIBZ
int compress_test(void);
#endif
#ifdef HAVE_PKCS7
#ifndef NO_PKCS7_ENCRYPTED_DATA
int pkcs7encrypted_test(void);
#endif
#if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
int pkcs7compressed_test(void);
#endif
int pkcs7signed_test(void);
int pkcs7enveloped_test(void);
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
int pkcs7authenveloped_test(void);
#endif
int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz);
#endif
#if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
!defined(NO_FILESYSTEM)
int cert_test(void);
#endif
#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
!defined(NO_FILESYSTEM)
int certext_test(void);
#endif
#if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
int decodedCertCache_test(void);
#endif
#ifdef HAVE_IDEA
int idea_test(void);
#endif
int memory_test(void);
#ifdef HAVE_VALGRIND
int mp_test(void);
#endif
#ifdef WOLFSSL_PUBLIC_MP
int prime_test(void);
#endif
#ifdef ASN_BER_TO_DER
int berder_test(void);
#endif
int logging_test(void);
int mutex_test(void);
#if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
int memcb_test(void);
#endif
#ifdef WOLFSSL_IMX6_CAAM_BLOB
int blob_test(void);
#endif
#ifdef WOLF_CRYPTO_CB
int cryptocb_test(void);
#endif
#ifdef WOLFSSL_CERT_PIV
int certpiv_test(void);
#endif
/* General big buffer size for many tests. */
#define FOURK_BUF 4096
#define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
#ifdef HAVE_STACK_SIZE
static THREAD_RETURN err_sys(const char* msg, int es)
#else
static int err_sys(const char* msg, int es)
#endif
{
printf("%s error = %d\n", msg, es);
EXIT_TEST(-1);
}
#ifndef HAVE_STACK_SIZE
/* func_args from test.h, so don't have to pull in other stuff */
typedef struct func_args {
int argc;
char** argv;
int return_code;
} func_args;
#endif /* !HAVE_STACK_SIZE */
#ifdef HAVE_FIPS
static void myFipsCb(int ok, int err, const char* hash)
{
printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
printf("message = %s\n", wc_GetErrorString(err));
printf("hash = %s\n", hash);
if (err == IN_CORE_FIPS_E) {
printf("In core integrity hash check failure, copy above hash\n");
printf("into verifyCore[] in fips_test.c and rebuild\n");
}
}
#endif /* HAVE_FIPS */
#ifdef WOLFSSL_STATIC_MEMORY
#ifdef BENCH_EMBEDDED
static byte gTestMemory[14000];
#elif defined(WOLFSSL_CERT_EXT)
static byte gTestMemory[140000];
#elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
static byte gTestMemory[160000];
#else
static byte gTestMemory[80000];
#endif
#endif
#ifdef WOLFSSL_PB
int wolfssl_pb_print(const char* msg, ...)
{
int ret;
va_list args;
char tmpBuf[80];
va_start(args, msg);
ret = vsprint(tmpBuf, msg, args);
va_end(args);
fnDumpStringToSystemLog(tmpBuf);
return ret;
}
#endif /* WOLFSSL_PB */
/* optional macro to add sleep between tests */
#ifdef TEST_SLEEP
#include <stdarg.h> /* for var args */
static WC_INLINE void test_pass(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
printf(fmt, args);
va_end(args);
TEST_SLEEP();
}
#else
/* redirect to printf */
#define test_pass printf
/* stub the sleep macro */
#define TEST_SLEEP()
#endif
#ifdef HAVE_STACK_SIZE
THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
#else
int wolfcrypt_test(void* args)
#endif
{
int ret;
printf("------------------------------------------------------------------------------\n");
printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
printf("------------------------------------------------------------------------------\n");
if (args)
((func_args*)args)->return_code = -1; /* error state */
#ifdef WOLFSSL_STATIC_MEMORY
if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
WOLFMEM_GENERAL, 1) != 0) {
printf("unable to load static memory");
return(EXIT_FAILURE);
}
#endif
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
wolfSSL_Debugging_ON();
#endif
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
wc_SetLoggingHeap(HEAP_HINT);
#endif
#ifdef HAVE_FIPS
wolfCrypt_SetCb_fips(myFipsCb);
#endif
#if !defined(NO_BIG_INT)
if (CheckCtcSettings() != 1)
return err_sys("Build vs runtime math mismatch\n", -1000);
#if defined(USE_FAST_MATH) && \
(!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
if (CheckFastMathSettings() != 1)
return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
-1001);
#endif /* USE_FAST_MATH */
#endif /* !NO_BIG_INT */
#if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
initDefaultName();
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
ret = wolfAsync_DevOpen(&devId);
if (ret < 0) {
printf("Async device open failed\nRunning without async\n");
}
#else
(void)devId;
#endif /* WOLFSSL_ASYNC_CRYPT */
#ifdef HAVE_SELFTEST
if ( (ret = wolfCrypt_SelfTest()) != 0)
return err_sys("CAVP selftest failed!\n", ret);
else
test_pass("CAVP selftest passed!\n");
#endif
if ( (ret = error_test()) != 0)
return err_sys("error test failed!\n", ret);
else
test_pass("error test passed!\n");
if ( (ret = memory_test()) != 0)
return err_sys("MEMORY test failed!\n", ret);
else
test_pass("MEMORY test passed!\n");
#ifndef NO_CODING
if ( (ret = base64_test()) != 0)
return err_sys("base64 test failed!\n", ret);
else
test_pass("base64 test passed!\n");
#ifdef WOLFSSL_BASE16
if ( (ret = base16_test()) != 0)
return err_sys("base16 test failed!\n", ret);
else
test_pass("base16 test passed!\n");
#endif
#endif /* !NO_CODING */
#ifndef NO_ASN
if ( (ret = asn_test()) != 0)
return err_sys("asn test failed!\n", ret);
else
test_pass("asn test passed!\n");
#endif
#ifndef NO_MD5
if ( (ret = md5_test()) != 0)
return err_sys("MD5 test failed!\n", ret);
else
test_pass("MD5 test passed!\n");
#endif
#ifdef WOLFSSL_MD2
if ( (ret = md2_test()) != 0)
return err_sys("MD2 test failed!\n", ret);
else
test_pass("MD2 test passed!\n");
#endif
#ifndef NO_MD4
if ( (ret = md4_test()) != 0)
return err_sys("MD4 test failed!\n", ret);
else
test_pass("MD4 test passed!\n");
#endif
#ifndef NO_SHA
if ( (ret = sha_test()) != 0)
return err_sys("SHA test failed!\n", ret);
else
test_pass("SHA test passed!\n");
#endif
#ifdef WOLFSSL_SHA224
if ( (ret = sha224_test()) != 0)
return err_sys("SHA-224 test failed!\n", ret);
else
test_pass("SHA-224 test passed!\n");
#endif
#ifndef NO_SHA256
if ( (ret = sha256_test()) != 0)
return err_sys("SHA-256 test failed!\n", ret);
else
test_pass("SHA-256 test passed!\n");
#endif
#ifdef WOLFSSL_SHA384
if ( (ret = sha384_test()) != 0)
return err_sys("SHA-384 test failed!\n", ret);
else
test_pass("SHA-384 test passed!\n");
#endif
#ifdef WOLFSSL_SHA512
if ( (ret = sha512_test()) != 0)
return err_sys("SHA-512 test failed!\n", ret);
else
test_pass("SHA-512 test passed!\n");
#endif
#ifdef WOLFSSL_SHA3
if ( (ret = sha3_test()) != 0)
return err_sys("SHA-3 test failed!\n", ret);
else
test_pass("SHA-3 test passed!\n");
#endif
if ( (ret = hash_test()) != 0)
return err_sys("Hash test failed!\n", ret);
else
test_pass("Hash test passed!\n");
#ifdef WOLFSSL_RIPEMD
if ( (ret = ripemd_test()) != 0)
return err_sys("RIPEMD test failed!\n", ret);
else
test_pass("RIPEMD test passed!\n");
#endif
#ifdef HAVE_BLAKE2
if ( (ret = blake2b_test()) != 0)
return err_sys("BLAKE2b test failed!\n", ret);
else
test_pass("BLAKE2b test passed!\n");
#endif
#ifdef HAVE_BLAKE2S
if ( (ret = blake2s_test()) != 0)
return err_sys("BLAKE2s test failed!\n", ret);
else
test_pass("BLAKE2s test passed!\n");
#endif
#ifndef NO_HMAC
#ifndef NO_MD5
if ( (ret = hmac_md5_test()) != 0)
return err_sys("HMAC-MD5 test failed!\n", ret);
else
test_pass("HMAC-MD5 test passed!\n");
#endif
#ifndef NO_SHA
if ( (ret = hmac_sha_test()) != 0)
return err_sys("HMAC-SHA test failed!\n", ret);
else
test_pass("HMAC-SHA test passed!\n");
#endif
#ifdef WOLFSSL_SHA224
if ( (ret = hmac_sha224_test()) != 0)
return err_sys("HMAC-SHA224 test failed!\n", ret);
else
test_pass("HMAC-SHA224 test passed!\n");
#endif
#ifndef NO_SHA256
if ( (ret = hmac_sha256_test()) != 0)
return err_sys("HMAC-SHA256 test failed!\n", ret);
else
test_pass("HMAC-SHA256 test passed!\n");
#endif
#ifdef WOLFSSL_SHA384
if ( (ret = hmac_sha384_test()) != 0)
return err_sys("HMAC-SHA384 test failed!\n", ret);
else
test_pass("HMAC-SHA384 test passed!\n");
#endif
#ifdef WOLFSSL_SHA512
if ( (ret = hmac_sha512_test()) != 0)
return err_sys("HMAC-SHA512 test failed!\n", ret);
else
test_pass("HMAC-SHA512 test passed!\n");
#endif
#ifdef HAVE_BLAKE2
if ( (ret = hmac_blake2b_test()) != 0)
return err_sys("HMAC-BLAKE2 test failed!\n", ret);
else
test_pass("HMAC-BLAKE2 test passed!\n");
#endif
#if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
!defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
!defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
if ( (ret = hmac_sha3_test()) != 0)
return err_sys("HMAC-SHA3 test failed!\n", ret);
else
test_pass("HMAC-SHA3 test passed!\n");
#endif
#ifdef HAVE_HKDF
if ( (ret = hkdf_test()) != 0)
return err_sys("HMAC-KDF test failed!\n", ret);
else
test_pass("HMAC-KDF test passed!\n");
#endif
#endif /* !NO_HMAC */
#if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
if ( (ret = x963kdf_test()) != 0)
return err_sys("X963-KDF test failed!\n", ret);
else
test_pass("X963-KDF test passed!\n");
#endif
#if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
!defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
if ( (ret = gmac_test()) != 0)
return err_sys("GMAC test failed!\n", ret);
else
test_pass("GMAC test passed!\n");
#endif
#ifndef NO_RC4
if ( (ret = arc4_test()) != 0)
return err_sys("ARC4 test failed!\n", ret);
else
test_pass("ARC4 test passed!\n");
#endif
#ifndef NO_HC128
if ( (ret = hc128_test()) != 0)
return err_sys("HC-128 test failed!\n", ret);
else
test_pass("HC-128 test passed!\n");
#endif
#ifndef NO_RABBIT
if ( (ret = rabbit_test()) != 0)
return err_sys("Rabbit test failed!\n", ret);
else
test_pass("Rabbit test passed!\n");
#endif
#ifdef HAVE_CHACHA
if ( (ret = chacha_test()) != 0)
return err_sys("Chacha test failed!\n", ret);
else
test_pass("Chacha test passed!\n");
#endif
#ifdef HAVE_POLY1305
if ( (ret = poly1305_test()) != 0)
return err_sys("POLY1305 test failed!\n", ret);
else
test_pass("POLY1305 test passed!\n");
#endif
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
if ( (ret = chacha20_poly1305_aead_test()) != 0)
return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
else
test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
#endif
#ifndef NO_DES3
if ( (ret = des_test()) != 0)
return err_sys("DES test failed!\n", ret);
else
test_pass("DES test passed!\n");
#endif
#ifndef NO_DES3
if ( (ret = des3_test()) != 0)
return err_sys("DES3 test failed!\n", ret);
else
test_pass("DES3 test passed!\n");
#endif
#ifndef NO_AES
if ( (ret = aes_test()) != 0)
return err_sys("AES test failed!\n", ret);
else
test_pass("AES test passed!\n");
#ifdef WOLFSSL_AES_192
if ( (ret = aes192_test()) != 0)
return err_sys("AES192 test failed!\n", ret);
else
test_pass("AES192 test passed!\n");
#endif
#ifdef WOLFSSL_AES_256
if ( (ret = aes256_test()) != 0)
return err_sys("AES256 test failed!\n", ret);
else
test_pass("AES256 test passed!\n");
#endif
#ifdef HAVE_AESGCM
#if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
if ( (ret = aesgcm_test()) != 0)
return err_sys("AES-GCM test failed!\n", ret);
#endif
#if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
if ((ret = aesgcm_default_test()) != 0) {
return err_sys("AES-GCM test failed!\n", ret);
}
#endif
test_pass("AES-GCM test passed!\n");
#endif
#if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
if ( (ret = aesccm_test()) != 0)
return err_sys("AES-CCM test failed!\n", ret);
else
test_pass("AES-CCM test passed!\n");
#endif
#ifdef HAVE_AES_KEYWRAP
if ( (ret = aeskeywrap_test()) != 0)
return err_sys("AES Key Wrap test failed!\n", ret);
else
test_pass("AES Key Wrap test passed!\n");
#endif
#endif
#ifdef HAVE_CAMELLIA
if ( (ret = camellia_test()) != 0)
return err_sys("CAMELLIA test failed!\n", ret);
else
test_pass("CAMELLIA test passed!\n");
#endif
#ifdef HAVE_IDEA
if ( (ret = idea_test()) != 0)
return err_sys("IDEA test failed!\n", ret);
else
test_pass("IDEA test passed!\n");
#endif
#ifndef WC_NO_RNG
if ( (ret = random_test()) != 0)
return err_sys("RANDOM test failed!\n", ret);
else
test_pass("RANDOM test passed!\n");
#endif /* WC_NO_RNG */
#ifndef NO_RSA
#ifdef WC_RSA_NO_PADDING
if ( (ret = rsa_no_pad_test()) != 0)
return err_sys("RSA NOPAD test failed!\n", ret);
else
test_pass("RSA NOPAD test passed!\n");
#endif
if ( (ret = rsa_test()) != 0)
return err_sys("RSA test failed!\n", ret);
else
test_pass("RSA test passed!\n");
#endif
#ifndef NO_DH
if ( (ret = dh_test()) != 0)
return err_sys("DH test failed!\n", ret);
else
test_pass("DH test passed!\n");
#endif
#ifndef NO_DSA
if ( (ret = dsa_test()) != 0)
return err_sys("DSA test failed!\n", ret);
else
test_pass("DSA test passed!\n");
#endif
#ifdef WOLFCRYPT_HAVE_SRP
if ( (ret = srp_test()) != 0)
return err_sys("SRP test failed!\n", ret);
else
test_pass("SRP test passed!\n");
#endif
#ifndef NO_PWDBASED
if ( (ret = pwdbased_test()) != 0)
return err_sys("PWDBASED test failed!\n", ret);
else
test_pass("PWDBASED test passed!\n");
#endif
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
if ( (ret = openssl_test()) != 0)
return err_sys("OPENSSL test failed!\n", ret);
else
test_pass("OPENSSL test passed!\n");
if ( (ret = openSSL_evpMD_test()) != 0)
return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
else
test_pass("OPENSSL (EVP MD) passed!\n");
if ( (ret = openssl_pkey0_test()) != 0)
return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
else
test_pass("OPENSSL (PKEY0) passed!\n");
if ( (ret = openssl_pkey1_test()) != 0)
return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
else
test_pass("OPENSSL (PKEY1) passed!\n");
if ( (ret = openssl_evpSig_test()) != 0)
return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
else
test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
#endif
#ifdef HAVE_ECC
if ( (ret = ecc_test()) != 0)
return err_sys("ECC test failed!\n", ret);
else
test_pass("ECC test passed!\n");
#if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
if ( (ret = ecc_encrypt_test()) != 0)
return err_sys("ECC Enc test failed!\n", ret);
else
test_pass("ECC Enc test passed!\n");
#endif
#ifdef USE_CERT_BUFFERS_256
if ( (ret = ecc_test_buffers()) != 0)
return err_sys("ECC buffer test failed!\n", ret);
else
test_pass("ECC buffer test passed!\n");
#endif
#endif
#if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
!defined(NO_FILESYSTEM)
if ( (ret = cert_test()) != 0)
return err_sys("CERT test failed!\n", ret);
else
test_pass("CERT test passed!\n");
#endif
#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
!defined(NO_FILESYSTEM)
if ( (ret = certext_test()) != 0)
return err_sys("CERT EXT test failed!\n", ret);
else
test_pass("CERT EXT test passed!\n");
#endif
#if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
if ( (ret = decodedCertCache_test()) != 0)
return err_sys("DECODED CERT CACHE test failed!\n", ret);
else
test_pass("DECODED CERT CACHE test passed!\n");
#endif
#ifdef HAVE_CURVE25519
if ( (ret = curve25519_test()) != 0)
return err_sys("CURVE25519 test failed!\n", ret);
else
test_pass("CURVE25519 test passed!\n");
#endif
#ifdef HAVE_ED25519
if ( (ret = ed25519_test()) != 0)
return err_sys("ED25519 test failed!\n", ret);
else
test_pass("ED25519 test passed!\n");
#endif
#if defined(WOLFSSL_CMAC) && !defined(NO_AES)
if ( (ret = cmac_test()) != 0)
return err_sys("CMAC test failed!\n", ret);
else
test_pass("CMAC test passed!\n");
#endif
#ifdef HAVE_LIBZ
if ( (ret = compress_test()) != 0)
return err_sys("COMPRESS test failed!\n", ret);
else
test_pass("COMPRESS test passed!\n");
#endif
#ifdef HAVE_PKCS7
#ifndef NO_PKCS7_ENCRYPTED_DATA
if ( (ret = pkcs7encrypted_test()) != 0)
return err_sys("PKCS7encrypted test failed!\n", ret);
else
test_pass("PKCS7encrypted test passed!\n");
#endif
#if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
if ( (ret = pkcs7compressed_test()) != 0)
return err_sys("PKCS7compressed test failed!\n", ret);
else
test_pass("PKCS7compressed test passed!\n");
#endif
if ( (ret = pkcs7signed_test()) != 0)
return err_sys("PKCS7signed test failed!\n", ret);
else
test_pass("PKCS7signed test passed!\n");
if ( (ret = pkcs7enveloped_test()) != 0)
return err_sys("PKCS7enveloped test failed!\n", ret);
else
test_pass("PKCS7enveloped test passed!\n");
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
if ( (ret = pkcs7authenveloped_test()) != 0)
return err_sys("PKCS7authenveloped test failed!\n", ret);
else
test_pass("PKCS7authenveloped test passed!\n");
#endif
#endif
#ifdef HAVE_VALGRIND
if ( (ret = mp_test()) != 0)
return err_sys("mp test failed!\n", ret);
else
test_pass("mp test passed!\n");
#endif
#if defined(WOLFSSL_PUBLIC_MP) && !defined(WOLFSSL_SP_MATH)
if ( (ret = prime_test()) != 0)
return err_sys("prime test failed!\n", ret);
else
test_pass("prime test passed!\n");
#endif
#if defined(ASN_BER_TO_DER) && \
(defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
defined(OPENSSL_EXTRA_X509_SMALL))
if ( (ret = berder_test()) != 0)
return err_sys("ber-der test failed!\n", ret);
else
test_pass("ber-der test passed!\n");
#endif
if ( (ret = logging_test()) != 0)
return err_sys("logging test failed!\n", ret);
else
test_pass("logging test passed!\n");
if ( (ret = mutex_test()) != 0)
return err_sys("mutex test failed!\n", ret);
else
test_pass("mutex test passed!\n");
#if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
if ( (ret = memcb_test()) != 0)
return err_sys("memcb test failed!\n", ret);
else
test_pass("memcb test passed!\n");
#endif
#ifdef WOLFSSL_IMX6_CAAM_BLOB
if ( (ret = blob_test()) != 0)
return err_sys("blob test failed!\n", ret);
else
test_pass("blob test passed!\n");
#endif
#ifdef WOLF_CRYPTO_CB
if ( (ret = cryptocb_test()) != 0)
return err_sys("crypto callback test failed!\n", ret);
else
test_pass("crypto callback test passed!\n");
#endif
#ifdef WOLFSSL_CERT_PIV
if ( (ret = certpiv_test()) != 0)
return err_sys("cert piv test failed!\n", ret);
else
test_pass("cert piv test passed!\n");
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
wolfAsync_DevClose(&devId);
#endif
/* cleanup the thread if fixed point cache is enabled and have thread local */
#if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
wc_ecc_fp_free();
#endif
if (args)
((func_args*)args)->return_code = ret;
test_pass("Test complete\n");
EXIT_TEST(ret);
}
#ifndef NO_MAIN_DRIVER
/* so overall tests can pull in test function */
#ifdef WOLFSSL_ESPIDF
void app_main( )
#else
int main(int argc, char** argv)
#endif
{
int ret;
func_args args;
#ifdef WOLFSSL_ESPIDF
/* set dummy wallclock time. */
struct timeval utctime;
struct timezone tz;
utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
utctime.tv_usec = 0;
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
settimeofday(&utctime, &tz);
#endif
#ifdef WOLFSSL_APACHE_MYNEWT
#ifdef ARCH_sim
mcu_sim_parse_args(argc, argv);
#endif
sysinit();
/* set dummy wallclock time. */
struct os_timeval utctime;
struct os_timezone tz;
utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
utctime.tv_usec = 0;
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
os_settimeofday(&utctime, &tz);
#endif
#ifdef HAVE_WNR
if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
err_sys("Whitewood netRandom global config failed", -1002);
return -1001;
}
#endif
#ifndef WOLFSSL_ESPIDF
args.argc = argc;
args.argv = argv;
#endif
if ((ret = wolfCrypt_Init()) != 0) {
printf("wolfCrypt_Init failed %d\n", ret);
err_sys("Error with wolfCrypt_Init!\n", -1003);
}
#ifdef HAVE_STACK_SIZE
StackSizeCheck(&args, wolfcrypt_test);
#else
wolfcrypt_test(&args);
#endif
if ((ret = wolfCrypt_Cleanup()) != 0) {
printf("wolfCrypt_Cleanup failed %d\n", ret);
err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
}
#ifdef HAVE_WNR
if (wc_FreeNetRandom() < 0)
err_sys("Failed to free netRandom context", -1005);
#endif /* HAVE_WNR */
#ifndef WOLFSSL_ESPIDF
return args.return_code;
#endif
}
#endif /* NO_MAIN_DRIVER */
/* helper to save DER, convert to PEM and save PEM */
#if !defined(NO_ASN) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
(defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
#define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB)
#else
#define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, NULL, NULL, pT, eB)
#endif
static int _SaveDerAndPem(const byte* der, int derSz,
byte* pem, int pemSz, const char* fileDer,
const char* filePem, int pemType, int errBase)
{
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
int ret;
XFILE derFile;
derFile = XFOPEN(fileDer, "wb");
if (!derFile) {
return errBase + 0;
}
ret = (int)XFWRITE(der, 1, derSz, derFile);
XFCLOSE(derFile);
if (ret != derSz) {
return errBase + 1;
}
#endif
if (pem && filePem) {
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
XFILE pemFile;
#endif
#ifdef WOLFSSL_DER_TO_PEM
pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
if (pemSz < 0) {
return errBase + 2;
}
#endif
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
pemFile = XFOPEN(filePem, "wb");
if (!pemFile) {
return errBase + 3;
}
ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
XFCLOSE(pemFile);
if (ret != pemSz) {
return errBase + 4;
}
#endif
}
/* suppress unused variable warnings */
(void)filePem;
(void)fileDer;
return 0;
}
#endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
int error_test(void)
{
const char* errStr;
char out[WOLFSSL_MAX_ERROR_SZ];
const char* unknownStr = wc_GetErrorString(0);
#ifdef NO_ERROR_STRINGS
/* Ensure a valid error code's string matches an invalid code's.
* The string is that error strings are not available.
*/
errStr = wc_GetErrorString(OPEN_RAN_E);
wc_ErrorString(OPEN_RAN_E, out);
if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1100;
if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1101;
#else
int i;
int j = 0;
/* Values that are not or no longer error codes. */
int missing[] = { -122, -123, -124, -127, -128, -129,
-163, -164, -165, -166, -167, -168, -169,
-179, -233,
0 };
/* Check that all errors have a string and it's the same through the two
* APIs. Check that the values that are not errors map to the unknown
* string.
*/
for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
errStr = wc_GetErrorString(i);
wc_ErrorString(i, out);
if (i != missing[j]) {
if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
return -1102;
if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
return -1103;
if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
return -1104;
}
else {
j++;
if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1105;
if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1106;
}
}
/* Check if the next possible value has been given a string. */
errStr = wc_GetErrorString(i);
wc_ErrorString(i, out);
if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1107;
if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
return -1108;
#endif
return 0;
}
#ifndef NO_CODING
int base64_test(void)
{
int ret;
const byte good[] = "A+Gd\0\0\0";
const byte goodEnd[] = "A+Gd \r\n";
byte out[128];
word32 outLen;
#ifdef WOLFSSL_BASE64_ENCODE
byte data[3];
word32 dataLen;
byte longData[79] = { 0 };
const byte symbols[] = "+/A=";
#endif
const byte badSmall[] = "AAA Gdj=";
const byte badLarge[] = "AAA~Gdj=";
const byte badEOL[] = "A+Gd ";
int i;
/* Good Base64 encodings. */
outLen = sizeof(out);
ret = Base64_Decode(good, sizeof(good), out, &outLen);
if (ret != 0)
return -1200;
outLen = sizeof(out);
ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
if (ret != 0)
return -1201;
/* Bad parameters. */
outLen = 1;
ret = Base64_Decode(good, sizeof(good), out, &outLen);
if (ret != BAD_FUNC_ARG)
return -1202;
outLen = sizeof(out);
ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
if (ret != ASN_INPUT_E)
return -1203;
/* Bad character at each offset 0-3. */
for (i = 0; i < 4; i++) {
outLen = sizeof(out);
ret = Base64_Decode(badSmall + i, 4, out, &outLen);
if (ret != ASN_INPUT_E)
return -1204 - i;
ret = Base64_Decode(badLarge + i, 4, out, &outLen);
if (ret != ASN_INPUT_E)
return -1214 - i;
}
#ifdef WOLFSSL_BASE64_ENCODE
/* Decode and encode all symbols - non-alphanumeric. */
dataLen = sizeof(data);
ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
if (ret != 0)
return -1224;
outLen = sizeof(out);
ret = Base64_Encode(data, dataLen, NULL, &outLen);
if (ret != LENGTH_ONLY_E)
return -1225;
outLen = sizeof(out);
ret = Base64_Encode(data, dataLen, out, &outLen);
if (ret != 0)
return -1226;
outLen = 7;
ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
if (ret != BUFFER_E)
return -1227;
outLen = sizeof(out);
ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
if (ret != LENGTH_ONLY_E)
return -1228;
outLen = sizeof(out);
ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
if (ret != 0)
return -1229;
outLen = sizeof(out);
ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
if (ret != 0)
return -1230;
/* Data that results in an encoding longer than one line. */
outLen = sizeof(out);
dataLen = sizeof(longData);
ret = Base64_Encode(longData, dataLen, out, &outLen);
if (ret != 0)
return -1231;
outLen = sizeof(out);
ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
if (ret != 0)
return -1232;
outLen = sizeof(out);
ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
if (ret != 0)
return -1233;
#endif
return 0;
}
#ifdef WOLFSSL_BASE16
int base16_test(void)
{
int ret;
const byte testData[] = "SomeDataToEncode\n";
const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
byte encoded[40];
word32 encodedLen;
byte plain[40];
word32 len;
/* length returned includes null termination */
encodedLen = sizeof(encoded);
ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
if (ret != 0)
return -1300;
len = (word32)XSTRLEN((char*)encoded);
if (len != encodedLen - 1)
return -1301;
len = sizeof(plain);
ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
if (ret != 0)
return -1302;
if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
return -1303;
if (encodedLen != sizeof(encodedTestData) ||
XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
return -1304;
}
return 0;
}
#endif /* WOLFSSL_BASE16 */
#endif /* !NO_CODING */
#ifndef NO_ASN
int asn_test(void)
{
int ret;
/* ASN1 encoded date buffer */
const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
byte format;
int length;
const byte* datePart;
#ifndef NO_ASN_TIME
struct tm timearg;
#ifdef WORD64_AVAILABLE
word64 now;
#else
word32 now;
#endif
#endif
ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
&length);
if (ret != 0)
return -1400;
#ifndef NO_ASN_TIME
/* Parameter Validation tests. */
if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
return -1401;
if (wc_GetTime(&now, 0) != BUFFER_E)
return -1402;
now = 0;
if (wc_GetTime(&now, sizeof(now)) != 0) {
return -1403;
}
if (now == 0) {
printf("RTC/Time not set!\n");
return -1404;
}
ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
if (ret != 0)
return -1405;
#endif /* !NO_ASN_TIME */
return 0;
}
#endif /* !NO_ASN */
#ifdef WOLFSSL_MD2
int md2_test(void)
{
Md2 md2;
byte hash[MD2_DIGEST_SIZE];
testVector a, b, c, d, e, f, g;
testVector test_md2[7];
int times = sizeof(test_md2) / sizeof(testVector), i;
a.input = "";
a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
"\x27\x73";
a.inLen = XSTRLEN(a.input);
a.outLen = MD2_DIGEST_SIZE;
b.input = "a";
b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
"\xb5\xd1";
b.inLen = XSTRLEN(b.input);
b.outLen = MD2_DIGEST_SIZE;
c.input = "abc";
c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
"\xd6\xbb";
c.inLen = XSTRLEN(c.input);
c.outLen = MD2_DIGEST_SIZE;
d.input = "message digest";
d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
"\x06\xb0";
d.inLen = XSTRLEN(d.input);
d.outLen = MD2_DIGEST_SIZE;
e.input = "abcdefghijklmnopqrstuvwxyz";
e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
"\x94\x0b";
e.inLen = XSTRLEN(e.input);
e.outLen = MD2_DIGEST_SIZE;
f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
"6789";
f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
"\x38\xcd";
f.inLen = XSTRLEN(f.input);
f.outLen = MD2_DIGEST_SIZE;
g.input = "1234567890123456789012345678901234567890123456789012345678"
"9012345678901234567890";
g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
"\xef\xd8";
g.inLen = XSTRLEN(g.input);
g.outLen = MD2_DIGEST_SIZE;
test_md2[0] = a;
test_md2[1] = b;
test_md2[2] = c;
test_md2[3] = d;
test_md2[4] = e;
test_md2[5] = f;
test_md2[6] = g;
wc_InitMd2(&md2);
for (i = 0; i < times; ++i) {
wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
wc_Md2Final(&md2, hash);
if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
return -1500 - i;
}
return 0;
}
#endif
#ifndef NO_MD5
int md5_test(void)
{
int ret = 0;
wc_Md5 md5, md5Copy;
byte hash[WC_MD5_DIGEST_SIZE];
byte hashcopy[WC_MD5_DIGEST_SIZE];
testVector a, b, c, d, e, f;
testVector test_md5[6];
int times = sizeof(test_md5) / sizeof(testVector), i;
a.input = "";
a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
"\x7e";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_MD5_DIGEST_SIZE;
b.input = "abc";
b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
"\x72";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_MD5_DIGEST_SIZE;
c.input = "message digest";
c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
"\xd0";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_MD5_DIGEST_SIZE;
d.input = "abcdefghijklmnopqrstuvwxyz";
d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
"\x3b";
d.inLen = XSTRLEN(d.input);
d.outLen = WC_MD5_DIGEST_SIZE;
e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
"6789";
e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
"\x9f";
e.inLen = XSTRLEN(e.input);
e.outLen = WC_MD5_DIGEST_SIZE;
f.input = "1234567890123456789012345678901234567890123456789012345678"
"9012345678901234567890";
f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
"\x7a";
f.inLen = XSTRLEN(f.input);
f.outLen = WC_MD5_DIGEST_SIZE;
test_md5[0] = a;
test_md5[1] = b;
test_md5[2] = c;
test_md5[3] = d;
test_md5[4] = e;
test_md5[5] = f;
ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
if (ret != 0)
return -1600;
ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
if (ret != 0) {
wc_Md5Free(&md5);
return -1601;
}
for (i = 0; i < times; ++i) {
ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
(word32)test_md5[i].inLen);
if (ret != 0)
ERROR_OUT(-1602 - i, exit);
ret = wc_Md5GetHash(&md5, hashcopy);
if (ret != 0)
ERROR_OUT(-1603 - i, exit);
ret = wc_Md5Copy(&md5, &md5Copy);
if (ret != 0)
ERROR_OUT(-1604 - i, exit);
ret = wc_Md5Final(&md5, hash);
if (ret != 0)
ERROR_OUT(-1605 - i, exit);
wc_Md5Free(&md5Copy);
if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
ERROR_OUT(-1606 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
ERROR_OUT(-1607 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
#ifdef WOLFSSL_PIC32MZ_HASH
wc_Md5SizeSet(&md5, times * sizeof(large_input));
#endif
for (i = 0; i < times; ++i) {
ret = wc_Md5Update(&md5, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-1608, exit);
}
ret = wc_Md5Final(&md5, hash);
if (ret != 0)
ERROR_OUT(-1609, exit);
if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
ERROR_OUT(-1610, exit);
} /* END LARGE HASH TEST */
exit:
wc_Md5Free(&md5);
wc_Md5Free(&md5Copy);
return ret;
}
#endif /* NO_MD5 */
#ifndef NO_MD4
int md4_test(void)
{
Md4 md4;
byte hash[MD4_DIGEST_SIZE];
testVector a, b, c, d, e, f, g;
testVector test_md4[7];
int times = sizeof(test_md4) / sizeof(testVector), i;
a.input = "";
a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
"\xc0";
a.inLen = XSTRLEN(a.input);
a.outLen = MD4_DIGEST_SIZE;
b.input = "a";
b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
"\x24";
b.inLen = XSTRLEN(b.input);
b.outLen = MD4_DIGEST_SIZE;
c.input = "abc";
c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
"\x9d";
c.inLen = XSTRLEN(c.input);
c.outLen = MD4_DIGEST_SIZE;
d.input = "message digest";
d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
"\x4b";
d.inLen = XSTRLEN(d.input);
d.outLen = MD4_DIGEST_SIZE;
e.input = "abcdefghijklmnopqrstuvwxyz";
e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
"\xa9";
e.inLen = XSTRLEN(e.input);
e.outLen = MD4_DIGEST_SIZE;
f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
"6789";
f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
"\xe4";
f.inLen = XSTRLEN(f.input);
f.outLen = MD4_DIGEST_SIZE;
g.input = "1234567890123456789012345678901234567890123456789012345678"
"9012345678901234567890";
g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
"\x36";
g.inLen = XSTRLEN(g.input);
g.outLen = MD4_DIGEST_SIZE;
test_md4[0] = a;
test_md4[1] = b;
test_md4[2] = c;
test_md4[3] = d;
test_md4[4] = e;
test_md4[5] = f;
test_md4[6] = g;
wc_InitMd4(&md4);
for (i = 0; i < times; ++i) {
wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
wc_Md4Final(&md4, hash);
if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
return -1700 - i;
}
return 0;
}
#endif /* NO_MD4 */
#ifndef NO_SHA
int sha_test(void)
{
int ret = 0;
wc_Sha sha, shaCopy;
byte hash[WC_SHA_DIGEST_SIZE];
byte hashcopy[WC_SHA_DIGEST_SIZE];
testVector a, b, c, d, e;
testVector test_sha[5];
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
"\x90\xaf\xd8\x07\x09";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA_DIGEST_SIZE;
b.input = "abc";
b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
"\x6C\x9C\xD0\xD8\x9D";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
"\xE5\xE5\x46\x70\xF1";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA_DIGEST_SIZE;
d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaa";
d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
"\x2A\x25\xEC\x64\x4D";
d.inLen = XSTRLEN(d.input);
d.outLen = WC_SHA_DIGEST_SIZE;
e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaa";
e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
"\x53\x99\x5E\x26\xA0";
e.inLen = XSTRLEN(e.input);
e.outLen = WC_SHA_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
test_sha[3] = d;
test_sha[4] = e;
ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
if (ret != 0)
return -1800;
ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
if (ret != 0) {
wc_ShaFree(&sha);
return -1801;
}
for (i = 0; i < times; ++i) {
ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-1802 - i, exit);
ret = wc_ShaGetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-1803 - i, exit);
ret = wc_ShaCopy(&sha, &shaCopy);
if (ret != 0)
ERROR_OUT(-1804 - i, exit);
ret = wc_ShaFinal(&sha, hash);
if (ret != 0)
ERROR_OUT(-1805 - i, exit);
wc_ShaFree(&shaCopy);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
ERROR_OUT(-1806 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
ERROR_OUT(-1807 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
"\xc5\xd9\x0a\xac";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
#ifdef WOLFSSL_PIC32MZ_HASH
wc_ShaSizeSet(&sha, times * sizeof(large_input));
#endif
for (i = 0; i < times; ++i) {
ret = wc_ShaUpdate(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-1808, exit);
}
ret = wc_ShaFinal(&sha, hash);
if (ret != 0)
ERROR_OUT(-1809, exit);
if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
ERROR_OUT(-1810, exit);
} /* END LARGE HASH TEST */
exit:
wc_ShaFree(&sha);
wc_ShaFree(&shaCopy);
return ret;
}
#endif /* NO_SHA */
#ifdef WOLFSSL_RIPEMD
int ripemd_test(void)
{
RipeMd ripemd;
int ret;
byte hash[RIPEMD_DIGEST_SIZE];
testVector a, b, c, d;
testVector test_ripemd[4];
int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
a.input = "abc";
a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
"\xb0\x87\xf1\x5a\x0b\xfc";
a.inLen = XSTRLEN(a.input);
a.outLen = RIPEMD_DIGEST_SIZE;
b.input = "message digest";
b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
"\x5f\xfa\x21\x59\x5f\x36";
b.inLen = XSTRLEN(b.input);
b.outLen = RIPEMD_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
"\xf4\x9a\xda\x62\xeb\x2b";
c.inLen = XSTRLEN(c.input);
c.outLen = RIPEMD_DIGEST_SIZE;
d.input = "12345678901234567890123456789012345678901234567890123456"
"789012345678901234567890";
d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
"\x82\xbf\x63\x32\x6b\xfb";
d.inLen = XSTRLEN(d.input);
d.outLen = RIPEMD_DIGEST_SIZE;
test_ripemd[0] = a;
test_ripemd[1] = b;
test_ripemd[2] = c;
test_ripemd[3] = d;
ret = wc_InitRipeMd(&ripemd);
if (ret != 0) {
return -1900;
}
for (i = 0; i < times; ++i) {
ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
(word32)test_ripemd[i].inLen);
if (ret != 0) {
return -1901 - i;
}
ret = wc_RipeMdFinal(&ripemd, hash);
if (ret != 0) {
return -1911 - i;
}
if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
return -1921 - i;
}
return 0;
}
#endif /* WOLFSSL_RIPEMD */
#ifdef HAVE_BLAKE2
#define BLAKE2B_TESTS 3
static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
{
{
0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
},
{
0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
},
{
0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
}
};
int blake2b_test(void)
{
Blake2b b2b;
byte digest[64];
byte input[64];
int i, ret;
for (i = 0; i < (int)sizeof(input); i++)
input[i] = (byte)i;
for (i = 0; i < BLAKE2B_TESTS; i++) {
ret = wc_InitBlake2b(&b2b, 64);
if (ret != 0)
return -2000 - i;
ret = wc_Blake2bUpdate(&b2b, input, i);
if (ret != 0)
return -2010 - 1;
ret = wc_Blake2bFinal(&b2b, digest, 64);
if (ret != 0)
return -2020 - i;
if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
return -2030 - i;
}
}
return 0;
}
#endif /* HAVE_BLAKE2 */
#ifdef HAVE_BLAKE2S
#define BLAKE2S_TESTS 3
static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
{
{
0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
},
{
0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
},
{
0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
}
};
int blake2s_test(void)
{
Blake2s b2s;
byte digest[32];
byte input[64];
int i, ret;
for (i = 0; i < (int)sizeof(input); i++)
input[i] = (byte)i;
for (i = 0; i < BLAKE2S_TESTS; i++) {
ret = wc_InitBlake2s(&b2s, 32);
if (ret != 0)
return -2000 - i;
ret = wc_Blake2sUpdate(&b2s, input, i);
if (ret != 0)
return -2010 - 1;
ret = wc_Blake2sFinal(&b2s, digest, 32);
if (ret != 0)
return -2020 - i;
if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
return -2030 - i;
}
}
return 0;
}
#endif /* HAVE_BLAKE2S */
#ifdef WOLFSSL_SHA224
int sha224_test(void)
{
wc_Sha224 sha, shaCopy;
byte hash[WC_SHA224_DIGEST_SIZE];
byte hashcopy[WC_SHA224_DIGEST_SIZE];
int ret = 0;
testVector a, b, c;
testVector test_sha[3];
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
"\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA224_DIGEST_SIZE;
b.input = "abc";
b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
"\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA224_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
"\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA224_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2100;
ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
if (ret != 0) {
wc_Sha224Free(&sha);
return -2101;
}
for (i = 0; i < times; ++i) {
ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2102 - i, exit);
ret = wc_Sha224GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2103 - i, exit);
ret = wc_Sha224Copy(&sha, &shaCopy);
if (ret != 0)
ERROR_OUT(-2104 - i, exit);
ret = wc_Sha224Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2105 - i, exit);
wc_Sha224Free(&shaCopy);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
ERROR_OUT(-2106 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
ERROR_OUT(-2107 - i, exit);
}
exit:
wc_Sha224Free(&sha);
wc_Sha224Free(&shaCopy);
return ret;
}
#endif
#ifndef NO_SHA256
int sha256_test(void)
{
wc_Sha256 sha, shaCopy;
byte hash[WC_SHA256_DIGEST_SIZE];
byte hashcopy[WC_SHA256_DIGEST_SIZE];
int ret = 0;
testVector a, b, c;
testVector test_sha[3];
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
"\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
"\xb8\x55";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA256_DIGEST_SIZE;
b.input = "abc";
b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
"\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
"\x15\xAD";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA256_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
"\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
"\x06\xC1";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA256_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2200;
ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
if (ret != 0) {
wc_Sha256Free(&sha);
return -2201;
}
for (i = 0; i < times; ++i) {
ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0) {
ERROR_OUT(-2202 - i, exit);
}
ret = wc_Sha256GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2203 - i, exit);
ret = wc_Sha256Copy(&sha, &shaCopy);
if (ret != 0)
ERROR_OUT(-2204 - i, exit);
ret = wc_Sha256Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2205 - i, exit);
wc_Sha256Free(&shaCopy);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
ERROR_OUT(-2206 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
ERROR_OUT(-2207 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
"\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
#ifdef WOLFSSL_PIC32MZ_HASH
wc_Sha256SizeSet(&sha, times * sizeof(large_input));
#endif
for (i = 0; i < times; ++i) {
ret = wc_Sha256Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2208, exit);
}
ret = wc_Sha256Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2209, exit);
if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
ERROR_OUT(-2210, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha256Free(&sha);
wc_Sha256Free(&shaCopy);
return ret;
}
#endif
#ifdef WOLFSSL_SHA512
int sha512_test(void)
{
wc_Sha512 sha, shaCopy;
byte hash[WC_SHA512_DIGEST_SIZE];
byte hashcopy[WC_SHA512_DIGEST_SIZE];
int ret = 0;
testVector a, b, c;
testVector test_sha[3];
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
"\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
"\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
"\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
"\xf9\x27\xda\x3e";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA512_DIGEST_SIZE;
b.input = "abc";
b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
"\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
"\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
"\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
"\xa5\x4c\xa4\x9f";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA512_DIGEST_SIZE;
c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
"jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
"\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
"\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
"\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
"\x87\x4b\xe9\x09";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA512_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2300;
ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
if (ret != 0) {
wc_Sha512Free(&sha);
return -2301;
}
for (i = 0; i < times; ++i) {
ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2302 - i, exit);
ret = wc_Sha512GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2303 - i, exit);
ret = wc_Sha512Copy(&sha, &shaCopy);
if (ret != 0)
ERROR_OUT(-2304 - i, exit);
ret = wc_Sha512Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2305 - i, exit);
wc_Sha512Free(&shaCopy);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
ERROR_OUT(-2306 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
ERROR_OUT(-2307 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
"\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
"\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
"\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha512Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2308, exit);
}
ret = wc_Sha512Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2309, exit);
if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
ERROR_OUT(-2310, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha512Free(&sha);
wc_Sha512Free(&shaCopy);
return ret;
}
#endif
#ifdef WOLFSSL_SHA384
int sha384_test(void)
{
wc_Sha384 sha, shaCopy;
byte hash[WC_SHA384_DIGEST_SIZE];
byte hashcopy[WC_SHA384_DIGEST_SIZE];
int ret = 0;
testVector a, b, c;
testVector test_sha[3];
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
"\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
"\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
"\x98\xb9\x5b";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA384_DIGEST_SIZE;
b.input = "abc";
b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
"\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
"\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
"\xc8\x25\xa7";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA384_DIGEST_SIZE;
c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
"jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
"\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
"\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
"\x74\x60\x39";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA384_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2400;
ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
if (ret != 0) {
wc_Sha384Free(&sha);
return -2401;
}
for (i = 0; i < times; ++i) {
ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2402 - i, exit);
ret = wc_Sha384GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2403 - i, exit);
ret = wc_Sha384Copy(&sha, &shaCopy);
if (ret != 0)
ERROR_OUT(-2404 - i, exit);
ret = wc_Sha384Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2405 - i, exit);
wc_Sha384Free(&shaCopy);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
ERROR_OUT(-2406 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
ERROR_OUT(-2407 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
"\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
"\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha384Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2408, exit);
}
ret = wc_Sha384Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2409, exit);
if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
ERROR_OUT(-2410, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha384Free(&sha);
wc_Sha384Free(&shaCopy);
return ret;
}
#endif /* WOLFSSL_SHA384 */
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224
static int sha3_224_test(void)
{
wc_Sha3 sha;
byte hash[WC_SHA3_224_DIGEST_SIZE];
byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
testVector a, b, c;
testVector test_sha[3];
int ret = 0;
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
"\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA3_224_DIGEST_SIZE;
b.input = "abc";
b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
"\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA3_224_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
"\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA3_224_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2500;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2501 - i, exit);
ret = wc_Sha3_224_GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2502 - i, exit);
ret = wc_Sha3_224_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2503 - i, exit);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
ERROR_OUT(-2504 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
ERROR_OUT(-2505 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
"\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2506, exit);
}
ret = wc_Sha3_224_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2507, exit);
if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
ERROR_OUT(-2508, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha3_224_Free(&sha);
return ret;
}
#endif /* WOLFSSL_NOSHA3_224 */
#ifndef WOLFSSL_NOSHA3_256
static int sha3_256_test(void)
{
wc_Sha3 sha;
byte hash[WC_SHA3_256_DIGEST_SIZE];
byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
testVector a, b, c;
testVector test_sha[3];
int ret = 0;
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
"\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
"\x43\x4a";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA3_256_DIGEST_SIZE;
b.input = "abc";
b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
"\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
"\x15\x32";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA3_256_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
"\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
"\x33\x76";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA3_256_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2600;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2601 - i, exit);
ret = wc_Sha3_256_GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2602 - i, exit);
ret = wc_Sha3_256_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2603 - i, exit);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
ERROR_OUT(-2604 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
ERROR_OUT(-2605 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
"\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2606, exit);
}
ret = wc_Sha3_256_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2607, exit);
if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
ERROR_OUT(-2608, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha3_256_Free(&sha);
return ret;
}
#endif /* WOLFSSL_NOSHA3_256 */
#ifndef WOLFSSL_NOSHA3_384
static int sha3_384_test(void)
{
wc_Sha3 sha;
byte hash[WC_SHA3_384_DIGEST_SIZE];
#ifndef NO_INTM_HASH_TEST
byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
#endif
testVector a, b, c;
testVector test_sha[3];
int ret;
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
"\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
"\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
"\xd5\xf0\x04";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA3_384_DIGEST_SIZE;
#if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
/* NIST test vector with a length that is a multiple of 4 */
b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
"\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
"\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
"\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
"\x19\x87\x22";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA3_384_DIGEST_SIZE;
#else
b.input = "abc";
b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
"\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
"\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
"\x37\x6d\x25";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA3_384_DIGEST_SIZE;
#endif
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
"\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
"\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
"\x65\x7c\x22";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA3_384_DIGEST_SIZE;
#ifdef WOLFSSL_XILINX_CRYPT
test_sha[0] = b; /* hardware acc. can not handle "" string */
#else
test_sha[0] = a;
#endif
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2700;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2701 - i, exit);
#ifndef NO_INTM_HASH_TEST
ret = wc_Sha3_384_GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2702 - i, exit);
#endif
ret = wc_Sha3_384_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2703 - i, exit);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
ERROR_OUT(-2704 - i, exit);
#ifndef NO_INTM_HASH_TEST
if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
ERROR_OUT(-2705 - i, exit);
#endif
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
"\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
"\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2706, exit);
}
ret = wc_Sha3_384_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2707, exit);
if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
ERROR_OUT(-2708, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha3_384_Free(&sha);
return ret;
}
#endif /* WOLFSSL_NOSHA3_384 */
#ifndef WOLFSSL_NOSHA3_512
static int sha3_512_test(void)
{
wc_Sha3 sha;
byte hash[WC_SHA3_512_DIGEST_SIZE];
byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
testVector a, b, c;
testVector test_sha[3];
int ret;
int times = sizeof(test_sha) / sizeof(struct testVector), i;
a.input = "";
a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
"\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
"\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
"\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
"\x28\x1d\xcd\x26";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA3_512_DIGEST_SIZE;
b.input = "abc";
b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
"\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
"\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
"\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
"\x4e\xec\x53\xf0";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA3_512_DIGEST_SIZE;
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
"\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
"\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
"\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
"\x39\xd6\xe7\x5e";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA3_512_DIGEST_SIZE;
test_sha[0] = a;
test_sha[1] = b;
test_sha[2] = c;
ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
if (ret != 0)
return -2800;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen);
if (ret != 0)
ERROR_OUT(-2801 - i, exit);
ret = wc_Sha3_512_GetHash(&sha, hashcopy);
if (ret != 0)
ERROR_OUT(-2802 - i, exit);
ret = wc_Sha3_512_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2803 - i, exit);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
ERROR_OUT(-2804 - i, exit);
if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
ERROR_OUT(-2805 - i, exit);
}
/* BEGIN LARGE HASH TEST */ {
byte large_input[1024];
const char* large_digest =
"\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
"\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
"\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
"\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
for (i = 0; i < (int)sizeof(large_input); i++) {
large_input[i] = (byte)(i & 0xFF);
}
times = 100;
for (i = 0; i < times; ++i) {
ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
(word32)sizeof(large_input));
if (ret != 0)
ERROR_OUT(-2806, exit);
}
ret = wc_Sha3_512_Final(&sha, hash);
if (ret != 0)
ERROR_OUT(-2807, exit);
if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
ERROR_OUT(-2808, exit);
} /* END LARGE HASH TEST */
exit:
wc_Sha3_512_Free(&sha);
return ret;
}
#endif /* WOLFSSL_NOSHA3_512 */
int sha3_test(void)
{
int ret;
#ifndef WOLFSSL_NOSHA3_224
if ((ret = sha3_224_test()) != 0)
return ret;
#endif
#ifndef WOLFSSL_NOSHA3_256
if ((ret = sha3_256_test()) != 0)
return ret;
#endif
#ifndef WOLFSSL_NOSHA3_384
if ((ret = sha3_384_test()) != 0)
return ret;
#endif
#ifndef WOLFSSL_NOSHA3_512
if ((ret = sha3_512_test()) != 0)
return ret;
#endif
return 0;
}
#endif
int hash_test(void)
{
wc_HashAlg hash;
int ret, exp_ret;
int i, j;
int digestSz;
byte data[] = "0123456789abcdef0123456789abcdef0123456";
byte out[WC_MAX_DIGEST_SIZE];
byte hashOut[WC_MAX_DIGEST_SIZE];
#if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
enum wc_HashType hashType;
#endif
enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
WC_HASH_TYPE_SHA3_224,
WC_HASH_TYPE_SHA3_256,
WC_HASH_TYPE_SHA3_384,
WC_HASH_TYPE_SHA3_512 };
enum wc_HashType typesNoImpl[] = {
#ifdef NO_MD5
WC_HASH_TYPE_MD5,
#endif
#ifdef NO_SHA
WC_HASH_TYPE_SHA,
#endif
#ifndef WOLFSSL_SHA224
WC_HASH_TYPE_SHA224,
#endif
#ifdef NO_SHA256
WC_HASH_TYPE_SHA256,
#endif
#ifndef WOLFSSL_SHA384
WC_HASH_TYPE_SHA384,
#endif
#ifndef WOLFSSL_SHA512
WC_HASH_TYPE_SHA512,
#endif
#if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
WC_HASH_TYPE_SHA3_224,
#endif
#if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
WC_HASH_TYPE_SHA3_256,
#endif
#if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
WC_HASH_TYPE_SHA3_384,
#endif
#if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
WC_HASH_TYPE_SHA3_512,
#endif
WC_HASH_TYPE_NONE
};
enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_NONE };
/* Parameter Validation testing. */
ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
if (ret != BAD_FUNC_ARG)
return -2900;
ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
if (ret != BAD_FUNC_ARG)
return -2901;
ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
if (ret != BAD_FUNC_ARG)
return -2902;
ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
if (ret != BAD_FUNC_ARG)
return -2903;
ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
if (ret != BAD_FUNC_ARG)
return -2904;
ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
if (ret != BAD_FUNC_ARG)
return -2905;
ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
if (ret != BAD_FUNC_ARG)
return -2906;
/* Try invalid hash algorithms. */
for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
ret = wc_HashInit(&hash, typesBad[i]);
if (ret != BAD_FUNC_ARG)
return -2907 - i;
ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
if (ret != BAD_FUNC_ARG)
return -2917 - i;
ret = wc_HashFinal(&hash, typesBad[i], out);
if (ret != BAD_FUNC_ARG)
return -2927 - i;
wc_HashFree(&hash, typesBad[i]);
}
/* Try valid hash algorithms. */
for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
exp_ret = 0;
if (typesGood[i] == typesNoImpl[j]) {
/* Recognized but no implementation compiled in. */
exp_ret = HASH_TYPE_E;
j++;
}
ret = wc_HashInit(&hash, typesGood[i]);
if (ret != exp_ret)
return -2937 - i;
ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
if (ret != exp_ret)
return -2947 - i;
ret = wc_HashFinal(&hash, typesGood[i], out);
if (ret != exp_ret)
return -2957 - i;
wc_HashFree(&hash, typesGood[i]);
digestSz = wc_HashGetDigestSize(typesGood[i]);
if (exp_ret < 0 && digestSz != exp_ret)
return -2967 - i;
if (exp_ret == 0 && digestSz < 0)
return -2977 - i;
if (exp_ret == 0) {
ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
digestSz - 1);
if (ret != BUFFER_E)
return -2987 - i;
}
ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
if (ret != exp_ret)
return -2997 - i;
if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
return -3007 -i;
ret = wc_HashGetBlockSize(typesGood[i]);
if (exp_ret < 0 && ret != exp_ret)
return -3008 - i;
if (exp_ret == 0 && ret < 0)
return -3018 - i;
#if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
ret = wc_HashGetOID(typesGood[i]);
if (ret == BAD_FUNC_ARG ||
(exp_ret == 0 && ret == HASH_TYPE_E) ||
(exp_ret != 0 && ret != HASH_TYPE_E)) {
return -3028 - i;
}
hashType = wc_OidGetHash(ret);
if (exp_ret < 0 && ret != exp_ret)
return -3038 - i;
if (exp_ret == 0 && hashType != typesGood[i])
return -3048 - i;
#endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
}
for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
return -3058 - i;
}
#if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
#ifdef WOLFSSL_MD2
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3068;
#else
if (ret != HASH_TYPE_E)
return -3069;
#endif
hashType = wc_OidGetHash(646); /* Md2h */
#ifdef WOLFSSL_MD2
if (hashType != WC_HASH_TYPE_MD2)
return -3070;
#else
if (hashType != WC_HASH_TYPE_NONE)
return -3071;
#endif
ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
#ifndef NO_MD5
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3072;
#else
if (ret != HASH_TYPE_E)
return -3073;
#endif
ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
if (ret != BAD_FUNC_ARG)
return -3074;
ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
if (ret != BAD_FUNC_ARG)
return -3075;
hashType = wc_OidGetHash(0);
if (hashType != WC_HASH_TYPE_NONE)
return -3076;
#endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
#ifdef WOLFSSL_MD2
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3077;
#else
if (ret != HASH_TYPE_E)
return -3078;
#endif
ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
#ifdef WOLFSSL_MD2
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3079;
#else
if (ret != HASH_TYPE_E)
return -3080;
#endif
ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
#ifndef NO_MD4
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3081;
#else
if (ret != HASH_TYPE_E)
return -3082;
#endif
ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
#ifndef NO_MD4
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3083;
#else
if (ret != HASH_TYPE_E)
return -3084;
#endif
ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
#if !defined(NO_MD5) && !defined(NO_SHA)
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3085;
#else
if (ret != HASH_TYPE_E)
return -3086;
#endif
ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
#if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3091;
#else
if (ret != HASH_TYPE_E)
return -3091;
#endif
ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
#if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
return -3092;
#else
if (ret != HASH_TYPE_E)
return -3092;
#endif
ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
if (ret != BAD_FUNC_ARG)
return -3093;
ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
if (ret != BAD_FUNC_ARG)
return -3094;
#ifndef NO_ASN
#if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST)
ret = wc_GetCTC_HashOID(MD2);
if (ret == 0)
return -3095;
#endif
#ifndef NO_MD5
ret = wc_GetCTC_HashOID(WC_MD5);
if (ret == 0)
return -3096;
#endif
#ifndef NO_SHA
ret = wc_GetCTC_HashOID(WC_SHA);
if (ret == 0)
return -3097;
#endif
#ifdef WOLFSSL_SHA224
ret = wc_GetCTC_HashOID(WC_SHA224);
if (ret == 0)
return -3098;
#endif
#ifndef NO_SHA256
ret = wc_GetCTC_HashOID(WC_SHA256);
if (ret == 0)
return -3099;
#endif
#ifdef WOLFSSL_SHA384
ret = wc_GetCTC_HashOID(WC_SHA384);
if (ret == 0)
return -3100;
#endif
#ifdef WOLFSSL_SHA512
ret = wc_GetCTC_HashOID(WC_SHA512);
if (ret == 0)
return -3101;
#endif
ret = wc_GetCTC_HashOID(-1);
if (ret != 0)
return -3102;
#endif
return 0;
}
#if !defined(NO_HMAC) && !defined(NO_MD5)
int hmac_md5_test(void)
{
Hmac hmac;
byte hash[WC_MD5_DIGEST_SIZE];
const char* keys[]=
{
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
"Jefe",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
};
testVector a, b, c;
testVector test_hmac[3];
int ret;
int times = sizeof(test_hmac) / sizeof(testVector), i;
a.input = "Hi There";
a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
"\x9d";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_MD5_DIGEST_SIZE;
b.input = "what do ya want for nothing?";
b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
"\x38";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_MD5_DIGEST_SIZE;
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD";
c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
"\xf6";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_MD5_DIGEST_SIZE;
test_hmac[0] = a;
test_hmac[1] = b;
test_hmac[2] = c;
for (i = 0; i < times; ++i) {
#if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
if (i == 1) {
continue; /* cavium can't handle short keys, fips not allowed */
}
#endif
if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
return -3200;
}
ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
(word32)XSTRLEN(keys[i]));
if (ret != 0)
return -3201;
ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
(word32)test_hmac[i].inLen);
if (ret != 0)
return -3202;
ret = wc_HmacFinal(&hmac, hash);
if (ret != 0)
return -3203;
if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
return -3204 - i;
wc_HmacFree(&hmac);
}
#ifndef HAVE_FIPS
if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
return -3214;
#endif
return 0;
}
#endif /* NO_HMAC && NO_MD5 */
#if !defined(NO_HMAC) && !defined(NO_SHA)
int hmac_sha_test(void)
{
Hmac hmac;
byte hash[WC_SHA_DIGEST_SIZE];
const char* keys[]=
{
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b",
"Jefe",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA"
};
testVector a, b, c;
testVector test_hmac[3];
int ret;
int times = sizeof(test_hmac) / sizeof(testVector), i;
a.input = "Hi There";
a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
"\x8e\xf1\x46\xbe\x00";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA_DIGEST_SIZE;
b.input = "what do ya want for nothing?";
b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
"\x9c\x25\x9a\x7c\x79";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA_DIGEST_SIZE;
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD";
c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
"\x4f\x63\xf1\x75\xd3";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA_DIGEST_SIZE;
test_hmac[0] = a;
test_hmac[1] = b;
test_hmac[2] = c;
for (i = 0; i < times; ++i) {
#if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
if (i == 1)
continue; /* cavium can't handle short keys, fips not allowed */
#endif
if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
return -3300;
ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
(word32)XSTRLEN(keys[i]));
if (ret != 0)
return -3301;
ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
(word32)test_hmac[i].inLen);
if (ret != 0)
return -3302;
ret = wc_HmacFinal(&hmac, hash);
if (ret != 0)
return -3303;
if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
return -3304 - i;
wc_HmacFree(&hmac);
}
#ifndef HAVE_FIPS
if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
return -3314;
#endif
return 0;
}
#endif
#if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
int hmac_sha224_test(void)
{
Hmac hmac;
byte hash[WC_SHA224_DIGEST_SIZE];
const char* keys[]=
{
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b",
"Jefe",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA",
"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
};
testVector a, b, c, d;
testVector test_hmac[4];
int ret;
int times = sizeof(test_hmac) / sizeof(testVector), i;
a.input = "Hi There";
a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
"\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA224_DIGEST_SIZE;
b.input = "what do ya want for nothing?";
b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
"\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA224_DIGEST_SIZE;
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD";
c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
"\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA224_DIGEST_SIZE;
d.input = "Big Key Input";
d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
"\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
d.inLen = XSTRLEN(d.input);
d.outLen = WC_SHA224_DIGEST_SIZE;
test_hmac[0] = a;
test_hmac[1] = b;
test_hmac[2] = c;
test_hmac[3] = d;
for (i = 0; i < times; ++i) {
#if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
if (i == 1)
continue; /* cavium can't handle short keys, fips not allowed */
#endif
if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
return -3400;
ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
(word32)XSTRLEN(keys[i]));
if (ret != 0)
return -3401;
ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
(word32)test_hmac[i].inLen);
if (ret != 0)
return -3402;
ret = wc_HmacFinal(&hmac, hash);
if (ret != 0)
return -3403;
if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
return -3404 - i;
wc_HmacFree(&hmac);
}
#ifndef HAVE_FIPS
if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
return -3414;
#endif
return 0;
}
#endif
#if !defined(NO_HMAC) && !defined(NO_SHA256)
int hmac_sha256_test(void)
{
Hmac hmac;
byte hash[WC_SHA256_DIGEST_SIZE];
const char* keys[]=
{
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b",
"Jefe",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA",
};
testVector a, b, c, d;
testVector test_hmac[4];
int ret;
int times = sizeof(test_hmac) / sizeof(testVector), i;
a.input = "Hi There";
a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
"\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
"\xcf\xf7";
a.inLen = XSTRLEN(a.input);
a.outLen = WC_SHA256_DIGEST_SIZE;
b.input = "what do ya want for nothing?";
b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
"\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
"\x38\x43";
b.inLen = XSTRLEN(b.input);
b.outLen = WC_SHA256_DIGEST_SIZE;
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD";
c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
"\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
"\x65\xfe";
c.inLen = XSTRLEN(c.input);
c.outLen = WC_SHA256_DIGEST_SIZE;
d.input = 0;
d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
"\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
"\x3e\x46";
d.inLen = 0;
d.outLen = WC_SHA256_DIGEST_SIZE;
test_hmac[0] = a;
test_hmac[1] = b;
test_hmac[2] = c;
test_hmac[3] = d;
for (i = 0; i < times; ++i) {
#if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
if (i == 1)
continue; /* cavium can't handle short keys, fips not allowed */
#endif
#if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
if (i == 3)
continue; /* QuickAssist can't handle empty HMAC */
#endif
if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
return -3500 - i;
ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
(word32)XSTRLEN(keys[i]));
if (ret != 0)
return -3510 - i;
if (test_hmac[i].input != NULL) {
ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
(word32)test_hmac[i].inLen);
if (ret != 0)
return -3520 - i;
}
ret = wc_HmacFinal(&hmac, hash);
if (ret != 0)
return -3530 - i;
if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
return -3540 - i;
wc_HmacFree(&hmac);
}
#ifndef HAVE_FIPS
if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
return -3550;
if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
return -3551;
#endif
if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
return -3552;
return 0;
}
#endif
#if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
int hmac_blake2b_test(void)
{
Hmac hmac;
byte hash[BLAKE2B_256];
const char* keys[]=
{
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b",
"Jefe",
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA"
};
testVector a, b, c;
testVector test_hmac[3];
int ret;
int times = sizeof(test_hmac) / sizeof(testVector), i;
a.input = "Hi There";
a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
"\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
"\xac\xfd";
a.inLen = XSTRLEN(a.input);
a.outLen = BLAKE2B_256;
b.input = "what do ya want for nothing?";
b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
"\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
"\xda\x2b";
b.inLen = XSTRLEN(b.input);
b.outLen = BLAKE2B_256;
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD";
c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
"\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
"\x0f\x87";
c.inLen = XSTRLEN(c.input);
c.outLen = BLAKE2B_256;
test_hmac[0] = a;
test_hmac[1] = b;
test_hmac[2] = c;
for (i = 0; i < times; ++i) {
#if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
if (i == 1)
continue; /* cavium can't handle short keys, fips not allowed */
#endif
#if !defined(HAVE_CAVIUM_V)
/* Blake2 only supported on Cavium Nitrox III */
if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0<