mirror of https://github.com/wolfSSL/wolfssl.git
Optimize test suite organization and utilities
- Create modular test directory structure (core, hash, cipher, pk, misc) - Add shared test utilities and macros in core/ - Move tests into functional categories - Maintain full test coverage with error cases - Keep conditional compilation support (#ifndef NO_*) - Improve code organization and reusability Co-Authored-By: kaleb@wolfssl.com <kaleb@wolfssl.com>pull/8461/head
parent
3e38bdcd2c
commit
e27c00d3f8
37
tests/api.c
37
tests/api.c
|
@ -290,17 +290,32 @@
|
|||
#include <tests/api/api.h>
|
||||
|
||||
/* Gather test declarations to include them in the testCases array */
|
||||
#include <tests/api/test_md5.h>
|
||||
#include <tests/api/test_sha.h>
|
||||
#include <tests/api/test_sha256.h>
|
||||
#include <tests/api/test_sha512.h>
|
||||
#include <tests/api/test_sha3.h>
|
||||
#include <tests/api/test_blake2.h>
|
||||
#include <tests/api/test_sm3.h>
|
||||
#include <tests/api/test_ripemd.h>
|
||||
#include <tests/api/test_hash.h>
|
||||
#include <tests/api/test_ascon.h>
|
||||
#include <tests/api/test_dtls.h>
|
||||
/* Core utilities */
|
||||
#include <tests/api/core/test_utils.h>
|
||||
#include <tests/api/core/test_data.h>
|
||||
#include <tests/api/core/test_setup.h>
|
||||
|
||||
/* Hash tests */
|
||||
#include <tests/api/hash/test_md5.h>
|
||||
#include <tests/api/hash/test_sha.h>
|
||||
#include <tests/api/hash/test_sha256.h>
|
||||
#include <tests/api/hash/test_sha512.h>
|
||||
#include <tests/api/hash/test_sha3.h>
|
||||
#include <tests/api/hash/test_blake2.h>
|
||||
#include <tests/api/hash/test_sm3.h>
|
||||
#include <tests/api/hash/test_ripemd.h>
|
||||
#include <tests/api/hash/test_hash.h>
|
||||
/* Cipher tests */
|
||||
#include <tests/api/cipher/test_aes.h>
|
||||
#include <tests/api/cipher/test_des3.h>
|
||||
#include <tests/api/cipher/test_camellia.h>
|
||||
/* Public key tests */
|
||||
#include <tests/api/pk/test_rsa.h>
|
||||
#include <tests/api/pk/test_ecc.h>
|
||||
#include <tests/api/pk/test_dsa.h>
|
||||
/* Misc tests */
|
||||
#include <tests/api/misc/test_ascon.h>
|
||||
#include <tests/api/misc/test_dtls.h>
|
||||
|
||||
#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_TLS) && \
|
||||
!defined(NO_RSA) && !defined(SINGLE_THREADED) && \
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# Cipher Algorithm Tests
|
||||
|
||||
This directory contains tests for cipher algorithms including:
|
||||
- AES
|
||||
- DES3
|
||||
- Camellia
|
||||
- ChaCha20
|
|
@ -0,0 +1,138 @@
|
|||
/* test_aes.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_aes.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* AES Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_AesInit(void)
|
||||
{
|
||||
#ifndef NO_AES
|
||||
byte key[32];
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
byte plain[AES_BLOCK_SIZE];
|
||||
byte cipher[AES_BLOCK_SIZE];
|
||||
|
||||
return TEST_CRYPTO_OPERATION("AES",
|
||||
wc_AesInit,
|
||||
wc_AesSetKey,
|
||||
wc_AesCbcEncrypt,
|
||||
wc_AesFree,
|
||||
plain, sizeof(plain), cipher);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_AesSetKey(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_AES
|
||||
Aes aes;
|
||||
byte key[32];
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_AesSetKey(&aes, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION), 0);
|
||||
ExpectIntEQ(wc_AesSetKey(&aes, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_AesSetKey(NULL, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_AesSetKey(&aes, NULL, AES_BLOCK_SIZE, iv, AES_ENCRYPTION),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_AesFree(&aes);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_AesCbcEncrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_AES
|
||||
Aes aes;
|
||||
byte key[32];
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
byte plain[AES_BLOCK_SIZE];
|
||||
byte cipher[AES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_AesSetKey(&aes, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION), 0);
|
||||
ExpectIntEQ(wc_AesCbcEncrypt(&aes, cipher, plain, AES_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_AesCbcEncrypt(NULL, cipher, plain, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, plain, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_AesCbcEncrypt(&aes, cipher, NULL, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_AesFree(&aes);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_AesCbcDecrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_AES
|
||||
Aes aes;
|
||||
byte key[32];
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
byte plain[AES_BLOCK_SIZE];
|
||||
byte cipher[AES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_AesSetKey(&aes, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION), 0);
|
||||
ExpectIntEQ(wc_AesCbcDecrypt(&aes, plain, cipher, AES_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_AesCbcDecrypt(NULL, plain, cipher, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, cipher, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_AesCbcDecrypt(&aes, plain, NULL, AES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_AesFree(&aes);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_AesFree(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_AES
|
||||
Aes aes;
|
||||
wc_AesInit(&aes, NULL, INVALID_DEVID);
|
||||
wc_AesFree(&aes);
|
||||
wc_AesFree(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
/* test_aes.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_AES_H
|
||||
#define WOLFCRYPT_TEST_AES_H
|
||||
|
||||
int test_wc_AesInit(void);
|
||||
int test_wc_AesSetKey(void);
|
||||
int test_wc_AesCbcEncrypt(void);
|
||||
int test_wc_AesCbcDecrypt(void);
|
||||
int test_wc_AesGcmSetKey(void);
|
||||
int test_wc_AesGcmEncrypt(void);
|
||||
int test_wc_AesGcmDecrypt(void);
|
||||
int test_wc_AesCtrEncrypt(void);
|
||||
int test_wc_AesFree(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_AES_H */
|
|
@ -0,0 +1,139 @@
|
|||
/* test_camellia.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/camellia.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_camellia.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Camellia Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_CamelliaInit(void)
|
||||
{
|
||||
#ifdef HAVE_CAMELLIA
|
||||
byte key[32];
|
||||
byte iv[CAMELLIA_BLOCK_SIZE];
|
||||
byte plain[CAMELLIA_BLOCK_SIZE];
|
||||
byte cipher[CAMELLIA_BLOCK_SIZE];
|
||||
|
||||
return TEST_CRYPTO_OPERATION("CAMELLIA",
|
||||
wc_CamelliaInit,
|
||||
wc_CamelliaSetKey,
|
||||
wc_CamelliaCbcEncrypt,
|
||||
wc_CamelliaFree,
|
||||
plain, sizeof(plain), cipher);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_CamelliaSetKey(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_CAMELLIA
|
||||
Camellia camellia;
|
||||
byte key[32];
|
||||
byte iv[CAMELLIA_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_CamelliaInit(&camellia, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, 16, iv), 0);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, 24, iv), 0);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, 32, iv), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_CamelliaSetKey(NULL, key, 16, iv), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, NULL, 16, iv), BAD_FUNC_ARG);
|
||||
|
||||
wc_CamelliaFree(&camellia);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_CamelliaCbcEncrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_CAMELLIA
|
||||
Camellia camellia;
|
||||
byte key[32];
|
||||
byte iv[CAMELLIA_BLOCK_SIZE];
|
||||
byte plain[CAMELLIA_BLOCK_SIZE];
|
||||
byte cipher[CAMELLIA_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_CamelliaInit(&camellia, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, 16, iv), 0);
|
||||
ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, cipher, plain,
|
||||
CAMELLIA_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, cipher, plain,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plain,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, cipher, NULL,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
|
||||
wc_CamelliaFree(&camellia);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_CamelliaCbcDecrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_CAMELLIA
|
||||
Camellia camellia;
|
||||
byte key[32];
|
||||
byte iv[CAMELLIA_BLOCK_SIZE];
|
||||
byte plain[CAMELLIA_BLOCK_SIZE];
|
||||
byte cipher[CAMELLIA_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_CamelliaInit(&camellia, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, 16, iv), 0);
|
||||
ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, plain, cipher,
|
||||
CAMELLIA_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, plain, cipher,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, cipher,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, plain, NULL,
|
||||
CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
|
||||
|
||||
wc_CamelliaFree(&camellia);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_CamelliaFree(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_CAMELLIA
|
||||
Camellia camellia;
|
||||
wc_CamelliaInit(&camellia, NULL, INVALID_DEVID);
|
||||
wc_CamelliaFree(&camellia);
|
||||
wc_CamelliaFree(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/* test_camellia.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_CAMELLIA_H
|
||||
#define WOLFCRYPT_TEST_CAMELLIA_H
|
||||
|
||||
int test_wc_CamelliaInit(void);
|
||||
int test_wc_CamelliaSetKey(void);
|
||||
int test_wc_CamelliaCbcEncrypt(void);
|
||||
int test_wc_CamelliaCbcDecrypt(void);
|
||||
int test_wc_CamelliaFree(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_CAMELLIA_H */
|
|
@ -0,0 +1,136 @@
|
|||
/* test_des3.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/des3.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_des3.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* DES3 Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_Des3Init(void)
|
||||
{
|
||||
#ifndef NO_DES3
|
||||
byte key[24];
|
||||
byte iv[DES_BLOCK_SIZE];
|
||||
byte plain[DES_BLOCK_SIZE];
|
||||
byte cipher[DES_BLOCK_SIZE];
|
||||
|
||||
return TEST_CRYPTO_OPERATION("DES3",
|
||||
wc_Des3Init,
|
||||
wc_Des3SetKey,
|
||||
wc_Des3CbcEncrypt,
|
||||
wc_Des3Free,
|
||||
plain, sizeof(plain), cipher);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_Des3SetKey(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DES3
|
||||
Des3 des3;
|
||||
byte key[24];
|
||||
byte iv[DES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_Des3Init(&des3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_Des3SetKey(&des3, key, iv, DES_ENCRYPTION), 0);
|
||||
ExpectIntEQ(wc_Des3SetKey(&des3, key, iv, DES_DECRYPTION), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Des3SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_Des3SetKey(&des3, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
|
||||
|
||||
wc_Des3Free(&des3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_Des3CbcEncrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DES3
|
||||
Des3 des3;
|
||||
byte key[24];
|
||||
byte iv[DES_BLOCK_SIZE];
|
||||
byte plain[DES_BLOCK_SIZE];
|
||||
byte cipher[DES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_Des3Init(&des3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_Des3SetKey(&des3, key, iv, DES_ENCRYPTION), 0);
|
||||
ExpectIntEQ(wc_Des3CbcEncrypt(&des3, cipher, plain, DES_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Des3CbcEncrypt(NULL, cipher, plain, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_Des3CbcEncrypt(&des3, NULL, plain, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_Des3CbcEncrypt(&des3, cipher, NULL, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_Des3Free(&des3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_Des3CbcDecrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DES3
|
||||
Des3 des3;
|
||||
byte key[24];
|
||||
byte iv[DES_BLOCK_SIZE];
|
||||
byte plain[DES_BLOCK_SIZE];
|
||||
byte cipher[DES_BLOCK_SIZE];
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_Des3Init(&des3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_Des3SetKey(&des3, key, iv, DES_DECRYPTION), 0);
|
||||
ExpectIntEQ(wc_Des3CbcDecrypt(&des3, plain, cipher, DES_BLOCK_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Des3CbcDecrypt(NULL, plain, cipher, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_Des3CbcDecrypt(&des3, NULL, cipher, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_Des3CbcDecrypt(&des3, plain, NULL, DES_BLOCK_SIZE),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_Des3Free(&des3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_Des3Free(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DES3
|
||||
Des3 des3;
|
||||
wc_Des3Init(&des3, NULL, INVALID_DEVID);
|
||||
wc_Des3Free(&des3);
|
||||
wc_Des3Free(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/* test_des3.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_DES3_H
|
||||
#define WOLFCRYPT_TEST_DES3_H
|
||||
|
||||
int test_wc_Des3Init(void);
|
||||
int test_wc_Des3SetKey(void);
|
||||
int test_wc_Des3CbcEncrypt(void);
|
||||
int test_wc_Des3CbcDecrypt(void);
|
||||
int test_wc_Des3Free(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_DES3_H */
|
|
@ -0,0 +1,7 @@
|
|||
# Core Test Utilities
|
||||
|
||||
This directory contains shared test utilities and common code used across the wolfSSL test suite:
|
||||
|
||||
- `test_utils.h` - Common test macros and functions
|
||||
- `test_data.h` - Shared test vectors and data
|
||||
- `test_setup.h` - Common setup/teardown utilities
|
|
@ -0,0 +1,42 @@
|
|||
/* test_data.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_TEST_DATA_H
|
||||
#define WOLFSSL_TEST_DATA_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
/* Test vector structure */
|
||||
typedef struct TestVector {
|
||||
const byte* input;
|
||||
const byte* expected;
|
||||
word32 inLen;
|
||||
word32 outLen;
|
||||
int flags; /* Optional flags for test case */
|
||||
const char* desc; /* Optional test case description */
|
||||
} TestVector;
|
||||
|
||||
/* Common test data sizes */
|
||||
#define TEST_DATA_SIZE_1K 1024
|
||||
#define TEST_DATA_SIZE_4K 4096
|
||||
#define TEST_DATA_SIZE_16K 16384
|
||||
|
||||
/* Test data generation utilities */
|
||||
byte* GetTestBuffer(word32 size);
|
||||
void FreeTestBuffer(byte* buffer);
|
||||
|
||||
/* Test vector access functions */
|
||||
const TestVector* GetSharedTestVectors(void);
|
||||
word32 GetSharedTestVectorCount(void);
|
||||
|
||||
#endif /* WOLFSSL_TEST_DATA_H */
|
|
@ -0,0 +1,38 @@
|
|||
/* test_setup.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_TEST_SETUP_H
|
||||
#define WOLFSSL_TEST_SETUP_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
|
||||
/* Test environment setup/teardown */
|
||||
#define TEST_CASE_SETUP(name) \
|
||||
static int name##_Setup(void)
|
||||
|
||||
#define TEST_CASE_TEARDOWN(name) \
|
||||
static int name##_Teardown(void)
|
||||
|
||||
/* Test suite setup/teardown */
|
||||
#define TEST_SUITE_SETUP(name) \
|
||||
static int name##_SuiteSetup(void)
|
||||
|
||||
#define TEST_SUITE_TEARDOWN(name) \
|
||||
static int name##_SuiteTeardown(void)
|
||||
|
||||
/* Common setup utilities */
|
||||
int SetupTestEnvironment(void);
|
||||
void CleanupTestEnvironment(void);
|
||||
|
||||
#endif /* WOLFSSL_TEST_SETUP_H */
|
|
@ -0,0 +1,45 @@
|
|||
/* test_utils.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_TEST_UTILS_H
|
||||
#define WOLFSSL_TEST_UTILS_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
|
||||
/* Common test macros for crypto operations */
|
||||
#define TEST_CRYPTO_OPERATION(name, init_fn, update_fn, final_fn, free_fn, data, len, hash) \
|
||||
EXPECT_DECLS; \
|
||||
ExpectIntEQ(init_fn(), 0); \
|
||||
ExpectIntEQ(update_fn(data, len), 0); \
|
||||
ExpectIntEQ(final_fn(hash), 0); \
|
||||
if (free_fn) ExpectIntEQ(free_fn(), 0); \
|
||||
return EXPECT_RESULT()
|
||||
|
||||
/* Common test setup/teardown macros */
|
||||
#define TEST_SETUP(name) \
|
||||
EXPECT_DECLS; \
|
||||
if (name##_Setup) ExpectIntEQ(name##_Setup(), 0)
|
||||
|
||||
#define TEST_TEARDOWN(name) \
|
||||
if (name##_Teardown) ExpectIntEQ(name##_Teardown(), 0); \
|
||||
return EXPECT_RESULT()
|
||||
|
||||
/* Common test result checking macros */
|
||||
#define TEST_ASSERT_SUCCESS(fn) \
|
||||
ExpectIntEQ(fn, 0)
|
||||
|
||||
#define TEST_ASSERT_FAIL(fn, err) \
|
||||
ExpectIntEQ(fn, err)
|
||||
|
||||
#endif /* WOLFSSL_TEST_UTILS_H */
|
|
@ -0,0 +1,8 @@
|
|||
# Hash Algorithm Tests
|
||||
|
||||
This directory contains tests for hash algorithms including:
|
||||
- SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512)
|
||||
- SHA-3
|
||||
- MD5
|
||||
- BLAKE2
|
||||
- RIPEMD
|
|
@ -0,0 +1,114 @@
|
|||
/* test_blake2.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/blake2.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_blake2.h>
|
||||
|
||||
/*
|
||||
* Unit test for the wc_InitBlake2b()
|
||||
*/
|
||||
int test_wc_InitBlake2b(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_BLAKE2
|
||||
Blake2b blake;
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
|
||||
/* Test bad arg. */
|
||||
ExpectIntEQ(wc_InitBlake2b(NULL, 64), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b(NULL, 128), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b(&blake, 128), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b(NULL, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b(&blake, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_InitBlake2b*/
|
||||
|
||||
/*
|
||||
* Unit test for the wc_InitBlake2b_WithKey()
|
||||
*/
|
||||
int test_wc_InitBlake2b_WithKey(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_BLAKE2
|
||||
Blake2b blake;
|
||||
word32 digestSz = BLAKE2B_KEYBYTES;
|
||||
byte key[BLAKE2B_KEYBYTES];
|
||||
word32 keylen = BLAKE2B_KEYBYTES;
|
||||
|
||||
XMEMSET(key, 0, sizeof(key));
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END wc_InitBlake2b_WithKey*/
|
||||
|
||||
/*
|
||||
* Unit test for the wc_InitBlake2s_WithKey()
|
||||
*/
|
||||
int test_wc_InitBlake2s_WithKey(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_BLAKE2S
|
||||
Blake2s blake;
|
||||
word32 digestSz = BLAKE2S_KEYBYTES;
|
||||
byte *key = (byte*)"01234567890123456789012345678901";
|
||||
word32 keylen = BLAKE2S_KEYBYTES;
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END wc_InitBlake2s_WithKey*/
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/* test_sm3.h
|
||||
/* test_blake2.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
|
@ -19,15 +19,11 @@
|
|||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_SM3_H
|
||||
#define WOLFCRYPT_TEST_SM3_H
|
||||
#ifndef WOLFCRYPT_TEST_BLAKE2_H
|
||||
#define WOLFCRYPT_TEST_BLAKE2_H
|
||||
|
||||
int test_wc_InitSm3Free(void);
|
||||
int test_wc_Sm3UpdateFinal(void);
|
||||
int test_wc_Sm3GetHash(void);
|
||||
int test_wc_Sm3Copy(void);
|
||||
int test_wc_Sm3FinalRaw(void);
|
||||
int test_wc_Sm3GetSetFlags(void);
|
||||
int test_wc_Sm3Hash(void);
|
||||
int test_wc_InitBlake2b(void);
|
||||
int test_wc_InitBlake2b_WithKey(void);
|
||||
int test_wc_InitBlake2s_WithKey(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_SM3_H */
|
||||
#endif /* WOLFCRYPT_TEST_BLAKE2_H */
|
|
@ -0,0 +1,230 @@
|
|||
/* test_hash.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_hash.h>
|
||||
|
||||
int test_wc_HashInit(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
int i; /* 0 indicates tests passed, 1 indicates failure */
|
||||
|
||||
wc_HashAlg hash;
|
||||
|
||||
/* enum for holding supported algorithms, #ifndef's restrict if disabled */
|
||||
enum wc_HashType enumArray[] = {
|
||||
#ifndef NO_MD5
|
||||
WC_HASH_TYPE_MD5,
|
||||
#endif
|
||||
#ifndef NO_SHA
|
||||
WC_HASH_TYPE_SHA,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA224
|
||||
WC_HASH_TYPE_SHA224,
|
||||
#endif
|
||||
#ifndef NO_SHA256
|
||||
WC_HASH_TYPE_SHA256,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA384
|
||||
WC_HASH_TYPE_SHA384,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA512
|
||||
WC_HASH_TYPE_SHA512,
|
||||
#endif
|
||||
};
|
||||
/* dynamically finds the length */
|
||||
int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
|
||||
|
||||
/* For loop to test various arguments... */
|
||||
for (i = 0; i < enumlen; i++) {
|
||||
/* check for bad args */
|
||||
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
|
||||
wc_HashFree(&hash, enumArray[i]);
|
||||
|
||||
/* check for null ptr */
|
||||
ExpectIntEQ(wc_HashInit(NULL, enumArray[i]),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
} /* end of for loop */
|
||||
|
||||
return EXPECT_RESULT();
|
||||
} /* end of test_wc_HashInit */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_HashSetFlags()
|
||||
*/
|
||||
int test_wc_HashSetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_HASH_FLAGS
|
||||
wc_HashAlg hash;
|
||||
word32 flags = 0;
|
||||
int i, j;
|
||||
int notSupportedLen;
|
||||
|
||||
/* enum for holding supported algorithms, #ifndef's restrict if disabled */
|
||||
enum wc_HashType enumArray[] = {
|
||||
#ifndef NO_MD5
|
||||
WC_HASH_TYPE_MD5,
|
||||
#endif
|
||||
#ifndef NO_SHA
|
||||
WC_HASH_TYPE_SHA,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA224
|
||||
WC_HASH_TYPE_SHA224,
|
||||
#endif
|
||||
#ifndef NO_SHA256
|
||||
WC_HASH_TYPE_SHA256,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA384
|
||||
WC_HASH_TYPE_SHA384,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA512
|
||||
WC_HASH_TYPE_SHA512,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA3
|
||||
WC_HASH_TYPE_SHA3_224,
|
||||
#endif
|
||||
};
|
||||
enum wc_HashType notSupported[] = {
|
||||
WC_HASH_TYPE_MD5_SHA,
|
||||
WC_HASH_TYPE_MD2,
|
||||
WC_HASH_TYPE_MD4,
|
||||
WC_HASH_TYPE_BLAKE2B,
|
||||
WC_HASH_TYPE_BLAKE2S,
|
||||
WC_HASH_TYPE_NONE,
|
||||
};
|
||||
|
||||
/* dynamically finds the length */
|
||||
int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
|
||||
|
||||
/* For loop to test various arguments... */
|
||||
for (i = 0; i < enumlen; i++) {
|
||||
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
|
||||
ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
|
||||
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
|
||||
ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_HashFree(&hash, enumArray[i]);
|
||||
|
||||
}
|
||||
/* For loop to test not supported cases */
|
||||
notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
|
||||
for (j = 0; j < notSupportedLen; j++) {
|
||||
ExpectIntEQ(wc_HashInit(&hash, notSupported[j]),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_HashFree(&hash, notSupported[j]),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
}
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_HashSetFlags */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_HashGetFlags()
|
||||
*/
|
||||
int test_wc_HashGetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_HASH_FLAGS
|
||||
wc_HashAlg hash;
|
||||
word32 flags = 0;
|
||||
int i, j;
|
||||
|
||||
/* enum for holding supported algorithms, #ifndef's restrict if disabled */
|
||||
enum wc_HashType enumArray[] = {
|
||||
#ifndef NO_MD5
|
||||
WC_HASH_TYPE_MD5,
|
||||
#endif
|
||||
#ifndef NO_SHA
|
||||
WC_HASH_TYPE_SHA,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA224
|
||||
WC_HASH_TYPE_SHA224,
|
||||
#endif
|
||||
#ifndef NO_SHA256
|
||||
WC_HASH_TYPE_SHA256,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA384
|
||||
WC_HASH_TYPE_SHA384,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA512
|
||||
WC_HASH_TYPE_SHA512,
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHA3
|
||||
WC_HASH_TYPE_SHA3_224,
|
||||
#endif
|
||||
};
|
||||
enum wc_HashType notSupported[] = {
|
||||
WC_HASH_TYPE_MD5_SHA,
|
||||
WC_HASH_TYPE_MD2,
|
||||
WC_HASH_TYPE_MD4,
|
||||
WC_HASH_TYPE_BLAKE2B,
|
||||
WC_HASH_TYPE_BLAKE2S,
|
||||
WC_HASH_TYPE_NONE,
|
||||
};
|
||||
int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
|
||||
int notSupportedLen;
|
||||
|
||||
/* For loop to test various arguments... */
|
||||
for (i = 0; i < enumlen; i++) {
|
||||
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
|
||||
ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
|
||||
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
|
||||
ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_HashFree(&hash, enumArray[i]);
|
||||
}
|
||||
/* For loop to test not supported cases */
|
||||
notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
|
||||
for (j = 0; j < notSupportedLen; j++) {
|
||||
ExpectIntEQ(wc_HashInit(&hash, notSupported[j]),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_HashFree(&hash, notSupported[j]),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
}
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_HashGetFlags */
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/* test_dtls.h
|
||||
/* test_hash.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
|
@ -19,11 +19,11 @@
|
|||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifndef TESTS_API_DTLS_H
|
||||
#define TESTS_API_DTLS_H
|
||||
#ifndef WOLFCRYPT_TEST_HASH_H
|
||||
#define WOLFCRYPT_TEST_HASH_H
|
||||
|
||||
int test_dtls12_basic_connection_id(void);
|
||||
int test_dtls13_basic_connection_id(void);
|
||||
int test_wolfSSL_dtls_cid_parse(void);
|
||||
int test_wc_HashInit(void);
|
||||
int test_wc_HashSetFlags(void);
|
||||
int test_wc_HashGetFlags(void);
|
||||
|
||||
#endif /* TESTS_API_DTLS_H */
|
||||
#endif /* WOLFCRYPT_TEST_HASH_H */
|
|
@ -0,0 +1,144 @@
|
|||
/* test_md5.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/md5.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_md5.h>
|
||||
|
||||
/*
|
||||
* Unit test for the wc_InitMd5()
|
||||
*/
|
||||
int test_wc_InitMd5(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_MD5
|
||||
wc_Md5 md5;
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitMd5(&md5), 0);
|
||||
/* Test bad arg. */
|
||||
ExpectIntEQ(wc_InitMd5(NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Md5Free(&md5);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_InitMd5 */
|
||||
|
||||
|
||||
/*
|
||||
* Testing wc_UpdateMd5()
|
||||
*/
|
||||
int test_wc_Md5Update(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_MD5
|
||||
wc_Md5 md5;
|
||||
byte hash[WC_MD5_DIGEST_SIZE];
|
||||
testVector a, b, c;
|
||||
|
||||
ExpectIntEQ(wc_InitMd5(&md5), 0);
|
||||
|
||||
/* Input */
|
||||
a.input = "a";
|
||||
a.inLen = XSTRLEN(a.input);
|
||||
ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
|
||||
ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
|
||||
|
||||
/* Update input. */
|
||||
a.input = "abc";
|
||||
a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
|
||||
"\x72";
|
||||
a.inLen = XSTRLEN(a.input);
|
||||
a.outLen = XSTRLEN(a.output);
|
||||
ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
|
||||
ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
|
||||
|
||||
/* Pass in bad values. */
|
||||
b.input = NULL;
|
||||
b.inLen = 0;
|
||||
ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
|
||||
c.input = NULL;
|
||||
c.inLen = WC_MD5_DIGEST_SIZE;
|
||||
ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Md5Free(&md5);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Md5Update() */
|
||||
|
||||
/*
|
||||
* Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
|
||||
*/
|
||||
int test_wc_Md5Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_MD5
|
||||
/* Instantiate */
|
||||
wc_Md5 md5;
|
||||
byte* hash_test[3];
|
||||
byte hash1[WC_MD5_DIGEST_SIZE];
|
||||
byte hash2[2*WC_MD5_DIGEST_SIZE];
|
||||
byte hash3[5*WC_MD5_DIGEST_SIZE];
|
||||
int times, i;
|
||||
|
||||
/* Initialize */
|
||||
ExpectIntEQ(wc_InitMd5(&md5), 0);
|
||||
|
||||
hash_test[0] = hash1;
|
||||
hash_test[1] = hash2;
|
||||
hash_test[2] = hash3;
|
||||
times = sizeof(hash_test)/sizeof(byte*);
|
||||
for (i = 0; i < times; i++) {
|
||||
ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
|
||||
}
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Md5Final(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Md5Final(NULL, hash1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Md5Final(&md5, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Md5Free(&md5);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/* test_ascon.h
|
||||
/* test_md5.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
|
@ -19,10 +19,11 @@
|
|||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifndef TESTS_API_TEST_ASCON_H
|
||||
#define TESTS_API_TEST_ASCON_H
|
||||
#ifndef WOLFCRYPT_TEST_MD5_H
|
||||
#define WOLFCRYPT_TEST_MD5_H
|
||||
|
||||
int test_ascon_hash256(void);
|
||||
int test_ascon_aead128(void);
|
||||
int test_wc_InitMd5(void);
|
||||
int test_wc_Md5Update(void);
|
||||
int test_wc_Md5Final(void);
|
||||
|
||||
#endif /* TESTS_API_TEST_ASCON_H */
|
||||
#endif /* WOLFCRYPT_TEST_MD5_H */
|
|
@ -0,0 +1,139 @@
|
|||
/* test_ripemd.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/ripemd.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_ripemd.h>
|
||||
|
||||
/*
|
||||
* Testing wc_InitRipeMd()
|
||||
*/
|
||||
int test_wc_InitRipeMd(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_RIPEMD
|
||||
RipeMd ripemd;
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
|
||||
/* Test bad arg. */
|
||||
ExpectIntEQ(wc_InitRipeMd(NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
|
||||
} /* END test_wc_InitRipeMd */
|
||||
|
||||
/*
|
||||
* Testing wc_RipeMdUpdate()
|
||||
*/
|
||||
int test_wc_RipeMdUpdate(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_RIPEMD
|
||||
RipeMd ripemd;
|
||||
byte hash[RIPEMD_DIGEST_SIZE];
|
||||
testVector a, b, c;
|
||||
|
||||
ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
|
||||
|
||||
/* Input */
|
||||
a.input = "a";
|
||||
a.inLen = XSTRLEN(a.input);
|
||||
ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
|
||||
ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
|
||||
|
||||
/* Update input. */
|
||||
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 = XSTRLEN(a.output);
|
||||
ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
|
||||
ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
|
||||
|
||||
/* Pass in bad values. */
|
||||
b.input = NULL;
|
||||
b.inLen = 0;
|
||||
ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
|
||||
c.input = NULL;
|
||||
c.inLen = RIPEMD_DIGEST_SIZE;
|
||||
ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_RipeMdUdpate */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_RipeMdFinal()
|
||||
*/
|
||||
int test_wc_RipeMdFinal(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_RIPEMD
|
||||
RipeMd ripemd;
|
||||
byte* hash_test[3];
|
||||
byte hash1[RIPEMD_DIGEST_SIZE];
|
||||
byte hash2[2*RIPEMD_DIGEST_SIZE];
|
||||
byte hash3[5*RIPEMD_DIGEST_SIZE];
|
||||
int times, i;
|
||||
|
||||
/* Initialize */
|
||||
ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
|
||||
|
||||
hash_test[0] = hash1;
|
||||
hash_test[1] = hash2;
|
||||
hash_test[2] = hash3;
|
||||
times = sizeof(hash_test) / sizeof(byte*);
|
||||
/* Testing oversized buffers. */
|
||||
for (i = 0; i < times; i++) {
|
||||
ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
|
||||
}
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_RipeMdFinal */
|
||||
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/* test_ripemd.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_RIPEMD_H
|
||||
#define WOLFCRYPT_TEST_RIPEMD_H
|
||||
|
||||
int test_wc_InitRipeMd(void);
|
||||
int test_wc_RipeMdUpdate(void);
|
||||
int test_wc_RipeMdFinal(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_RIPEMD_H */
|
|
@ -0,0 +1,146 @@
|
|||
/* test_sha.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/sha.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_sha.h>
|
||||
|
||||
/*
|
||||
* Unit test for the wc_InitSha()
|
||||
*/
|
||||
int test_wc_InitSha(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA
|
||||
wc_Sha sha;
|
||||
|
||||
/* Test good arg. */
|
||||
ExpectIntEQ(wc_InitSha(&sha), 0);
|
||||
/* Test bad arg. */
|
||||
ExpectIntEQ(wc_InitSha(NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_ShaFree(&sha);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_InitSha */
|
||||
|
||||
/*
|
||||
* Tesing wc_ShaUpdate()
|
||||
*/
|
||||
int test_wc_ShaUpdate(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA
|
||||
wc_Sha sha;
|
||||
byte hash[WC_SHA_DIGEST_SIZE];
|
||||
testVector a, b, c;
|
||||
|
||||
ExpectIntEQ(wc_InitSha(&sha), 0);
|
||||
|
||||
/* Input. */
|
||||
a.input = "a";
|
||||
a.inLen = XSTRLEN(a.input);
|
||||
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
|
||||
ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
|
||||
|
||||
/* Update input. */
|
||||
a.input = "abc";
|
||||
a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
|
||||
"\x6C\x9C\xD0\xD8\x9D";
|
||||
a.inLen = XSTRLEN(a.input);
|
||||
a.outLen = XSTRLEN(a.output);
|
||||
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
|
||||
ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
|
||||
|
||||
/* Try passing in bad values. */
|
||||
b.input = NULL;
|
||||
b.inLen = 0;
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
|
||||
c.input = NULL;
|
||||
c.inLen = WC_SHA_DIGEST_SIZE;
|
||||
ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_ShaFree(&sha);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_ShaUpdate() */
|
||||
|
||||
/*
|
||||
* Unit test on wc_ShaFinal
|
||||
*/
|
||||
int test_wc_ShaFinal(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA
|
||||
wc_Sha sha;
|
||||
byte* hash_test[3];
|
||||
byte hash1[WC_SHA_DIGEST_SIZE];
|
||||
byte hash2[2*WC_SHA_DIGEST_SIZE];
|
||||
byte hash3[5*WC_SHA_DIGEST_SIZE];
|
||||
int times, i;
|
||||
|
||||
/* Initialize*/
|
||||
ExpectIntEQ(wc_InitSha(&sha), 0);
|
||||
|
||||
hash_test[0] = hash1;
|
||||
hash_test[1] = hash2;
|
||||
hash_test[2] = hash3;
|
||||
times = sizeof(hash_test)/sizeof(byte*);
|
||||
for (i = 0; i < times; i++) {
|
||||
ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
|
||||
}
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_ShaFinal(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_ShaFinal(NULL, hash1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_ShaFinal(&sha, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_ShaFree(&sha);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_ShaFinal */
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/* test_sha.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_SHA_H
|
||||
#define WOLFCRYPT_TEST_SHA_H
|
||||
|
||||
int test_wc_InitSha(void);
|
||||
int test_wc_ShaUpdate(void);
|
||||
int test_wc_ShaFinal(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_SHA_H */
|
|
@ -0,0 +1,468 @@
|
|||
/* test_sha256.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/sha256.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_sha256.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* SHA-256
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* Unit test for wc_InitSha256()
|
||||
*/
|
||||
int test_wc_InitSha256(void)
|
||||
{
|
||||
#ifndef NO_SHA256
|
||||
byte hash[WC_SHA256_DIGEST_SIZE];
|
||||
byte data[] = "test data";
|
||||
return TEST_CRYPTO_OPERATION("SHA256",
|
||||
wc_InitSha256,
|
||||
wc_Sha256Update,
|
||||
wc_Sha256Final,
|
||||
wc_Sha256Free,
|
||||
data, sizeof(data), hash);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
} /* END test_wc_InitSha256 */
|
||||
|
||||
/*
|
||||
* Unit test for wc_Sha256Update()
|
||||
*/
|
||||
int test_wc_Sha256Update(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256 sha256;
|
||||
byte hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* test_input = "abc";
|
||||
const byte expected[] = {
|
||||
0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
|
||||
0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
|
||||
0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
|
||||
0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD
|
||||
};
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, expected, WC_SHA256_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256Update(NULL, (byte*)test_input, XSTRLEN(test_input)),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, NULL, WC_SHA256_DIGEST_SIZE),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Test edge cases */
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, 0), 0);
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256Update */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha256Final()
|
||||
*/
|
||||
int test_wc_Sha256Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256 sha256;
|
||||
byte hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256Final(&sha256, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256Final */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha256FinalRaw()
|
||||
*/
|
||||
int test_wc_Sha256FinalRaw(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && \
|
||||
!defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
|
||||
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
|
||||
!defined(WOLFSSL_NO_HASH_RAW)
|
||||
wc_Sha256 sha256;
|
||||
byte hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256FinalRaw */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha256GetFlags()
|
||||
*/
|
||||
int test_wc_Sha256GetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sha256 sha256;
|
||||
word32 flags = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
|
||||
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256GetFlags(NULL, &flags), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256GetFlags(&sha256, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256GetFlags */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha256Free()
|
||||
*/
|
||||
int test_wc_Sha256Free(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256Free(NULL);
|
||||
/* Set result to SUCCESS. */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256Free */
|
||||
/*
|
||||
* Unit test function for wc_Sha256GetHash()
|
||||
*/
|
||||
int test_wc_Sha256GetHash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256 sha256;
|
||||
byte hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha256GetHash(&sha256, hash), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256GetHash(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256GetHash */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha256Copy()
|
||||
*/
|
||||
int test_wc_Sha256Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256 sha256, temp;
|
||||
byte hash1[WC_SHA256_DIGEST_SIZE], hash2[WC_SHA256_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
XMEMSET(&sha256, 0, sizeof(sha256));
|
||||
XMEMSET(&temp, 0, sizeof(temp));
|
||||
|
||||
ExpectIntEQ(wc_InitSha256(&sha256), 0);
|
||||
ExpectIntEQ(wc_InitSha256(&temp), 0);
|
||||
ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
|
||||
/* Test copy operation */
|
||||
ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
|
||||
|
||||
/* Verify both generate same hash */
|
||||
ExpectIntEQ(wc_Sha256Final(&sha256, hash1), 0);
|
||||
ExpectIntEQ(wc_Sha256Final(&temp, hash2), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash1, hash2, WC_SHA256_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha256Copy(NULL, &temp), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha256Free(&sha256);
|
||||
wc_Sha256Free(&temp);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha256Copy */
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* SHA-224
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* Testing wc_InitSha224();
|
||||
*/
|
||||
int test_wc_InitSha224(void)
|
||||
{
|
||||
#ifdef WOLFSSL_SHA224
|
||||
byte hash[WC_SHA224_DIGEST_SIZE];
|
||||
byte data[] = "test data";
|
||||
return TEST_CRYPTO_OPERATION("SHA224",
|
||||
wc_InitSha224,
|
||||
wc_Sha224Update,
|
||||
wc_Sha224Final,
|
||||
wc_Sha224Free,
|
||||
data, sizeof(data), hash);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
} /* END test_wc_InitSha224 */
|
||||
|
||||
/*
|
||||
* Unit test on wc_Sha224Update
|
||||
*/
|
||||
int test_wc_Sha224Update(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHA224
|
||||
wc_Sha224 sha224;
|
||||
byte hash[WC_SHA224_DIGEST_SIZE];
|
||||
const char* test_input = "abc";
|
||||
const byte expected[] = {
|
||||
0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
|
||||
0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
|
||||
0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
|
||||
0xe3, 0x6c, 0x9d, 0xa7
|
||||
};
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, expected, WC_SHA224_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha224Update(NULL, (byte*)test_input, XSTRLEN(test_input)),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, NULL, WC_SHA224_DIGEST_SIZE),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Test edge cases */
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)test_input, 0), 0);
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224Update */
|
||||
|
||||
/*
|
||||
* Unit test for wc_Sha224Final();
|
||||
*/
|
||||
int test_wc_Sha224Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHA224
|
||||
wc_Sha224 sha224;
|
||||
byte hash[WC_SHA224_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha224Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha224Final(&sha224, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224Final */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha224SetFlags()
|
||||
*/
|
||||
int test_wc_Sha224SetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sha224 sha224;
|
||||
word32 flags = WC_HASH_FLAG_WILLCOPY;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
|
||||
|
||||
/* Verify flags were set */
|
||||
flags = 0;
|
||||
ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
|
||||
ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha224SetFlags(NULL, flags), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224SetFlags */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha224GetFlags()
|
||||
*/
|
||||
int test_wc_Sha224GetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sha224 sha224;
|
||||
word32 flags = 0;
|
||||
|
||||
/* Initialize */
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
|
||||
ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
|
||||
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224GetFlags */
|
||||
/*
|
||||
* Unit test function for wc_Sha224Free()
|
||||
*/
|
||||
int test_wc_Sha224Free(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHA224
|
||||
wc_Sha224Free(NULL);
|
||||
/* Set result to SUCCESS. */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
|
||||
} /* END test_wc_Sha224Free */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha224GetHash()
|
||||
*/
|
||||
int test_wc_Sha224GetHash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHA224
|
||||
wc_Sha224 sha224;
|
||||
byte hash[WC_SHA224_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
ExpectIntEQ(wc_Sha224GetHash(&sha224, hash), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha224GetHash(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224GetHash */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha224Copy()
|
||||
*/
|
||||
int test_wc_Sha224Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHA224
|
||||
wc_Sha224 sha224, temp;
|
||||
byte hash1[WC_SHA224_DIGEST_SIZE], hash2[WC_SHA224_DIGEST_SIZE];
|
||||
const char* test_input = "test";
|
||||
|
||||
/* Test normal operation */
|
||||
XMEMSET(&sha224, 0, sizeof(sha224));
|
||||
XMEMSET(&temp, 0, sizeof(temp));
|
||||
|
||||
ExpectIntEQ(wc_InitSha224(&sha224), 0);
|
||||
ExpectIntEQ(wc_InitSha224(&temp), 0);
|
||||
ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)test_input, XSTRLEN(test_input)), 0);
|
||||
|
||||
/* Test copy operation */
|
||||
ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
|
||||
|
||||
/* Verify both generate same hash */
|
||||
ExpectIntEQ(wc_Sha224Final(&sha224, hash1), 0);
|
||||
ExpectIntEQ(wc_Sha224Final(&temp, hash2), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash1, hash2, WC_SHA224_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_Sha224Copy(NULL, &temp), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha224Free(&sha224);
|
||||
wc_Sha224Free(&temp);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha224Copy */
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* test_sha256.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_SHA256_H
|
||||
#define WOLFCRYPT_TEST_SHA256_H
|
||||
|
||||
int test_wc_InitSha256(void);
|
||||
int test_wc_Sha256Update(void);
|
||||
int test_wc_Sha256Final(void);
|
||||
int test_wc_Sha256FinalRaw(void);
|
||||
int test_wc_Sha256GetFlags(void);
|
||||
int test_wc_Sha256Free(void);
|
||||
int test_wc_Sha256GetHash(void);
|
||||
int test_wc_Sha256Copy(void);
|
||||
|
||||
int test_wc_InitSha224(void);
|
||||
int test_wc_Sha224Update(void);
|
||||
int test_wc_Sha224Final(void);
|
||||
int test_wc_Sha224SetFlags(void);
|
||||
int test_wc_Sha224GetFlags(void);
|
||||
int test_wc_Sha224Free(void);
|
||||
int test_wc_Sha224GetHash(void);
|
||||
int test_wc_Sha224Copy(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_SHA256_H */
|
|
@ -0,0 +1,730 @@
|
|||
/* test_sha3.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/sha3.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_sha3.h>
|
||||
|
||||
/*******************************************************************************
|
||||
* SHA-3
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
|
||||
* wc_InitSha3_512
|
||||
*/
|
||||
int test_wc_InitSha3(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3)
|
||||
wc_Sha3 sha3;
|
||||
|
||||
(void)sha3;
|
||||
|
||||
#if !defined(WOLFSSL_NOSHA3_224)
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
#endif /* NOSHA3_224 */
|
||||
#if !defined(WOLFSSL_NOSHA3_256)
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
#endif /* NOSHA3_256 */
|
||||
#if !defined(WOLFSSL_NOSHA3_384)
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
#endif /* NOSHA3_384 */
|
||||
#if !defined(WOLFSSL_NOSHA3_512)
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
#endif /* NOSHA3_512 */
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_InitSha3 */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_Update()
|
||||
*/
|
||||
int test_wc_Sha3_Update(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
|
||||
!defined(WOLFSSL_AFALG_XILINX)
|
||||
wc_Sha3 sha3;
|
||||
byte msg[] = "Everybody's working for the weekend.";
|
||||
byte msg2[] = "Everybody gets Friday off.";
|
||||
byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
|
||||
"\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
|
||||
"\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
|
||||
"\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
|
||||
"\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
|
||||
word32 msglen = sizeof(msg) - 1;
|
||||
word32 msg2len = sizeof(msg2);
|
||||
word32 msgCmplen = sizeof(msgCmp);
|
||||
|
||||
#if !defined(WOLFSSL_NOSHA3_224)
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
|
||||
ExpectTrue(sha3.i == msglen);
|
||||
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
|
||||
|
||||
/* Pass bad args. */
|
||||
ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
#endif /* SHA3_224 */
|
||||
|
||||
#if !defined(WOLFSSL_NOSHA3_256)
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
|
||||
ExpectTrue(sha3.i == msglen);
|
||||
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
|
||||
|
||||
/* Pass bad args. */
|
||||
ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
#endif /* SHA3_256 */
|
||||
|
||||
#if !defined(WOLFSSL_NOSHA3_384)
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
|
||||
ExpectTrue(sha3.i == msglen);
|
||||
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
|
||||
|
||||
/* Pass bad args. */
|
||||
ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
#endif /* SHA3_384 */
|
||||
|
||||
#if !defined(WOLFSSL_NOSHA3_512)
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
|
||||
ExpectTrue(sha3.i == msglen);
|
||||
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
|
||||
|
||||
/* Pass bad args. */
|
||||
ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
#endif /* SHA3_512 */
|
||||
#endif /* WOLFSSL_SHA3 */
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_Update */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_224_Final()
|
||||
*/
|
||||
int test_wc_Sha3_224_Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
|
||||
wc_Sha3 sha3;
|
||||
const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
|
||||
"nopnopq";
|
||||
const char* expOut = "\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";
|
||||
byte hash[WC_SHA3_224_DIGEST_SIZE];
|
||||
byte hashRet[WC_SHA3_224_DIGEST_SIZE];
|
||||
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashRet, 0, sizeof(hashRet));
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_224_Final */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_256_Final()
|
||||
*/
|
||||
int test_wc_Sha3_256_Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
|
||||
wc_Sha3 sha3;
|
||||
const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
|
||||
"nopnopq";
|
||||
const char* expOut = "\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";
|
||||
byte hash[WC_SHA3_256_DIGEST_SIZE];
|
||||
byte hashRet[WC_SHA3_256_DIGEST_SIZE];
|
||||
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashRet, 0, sizeof(hashRet));
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_256_Final */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_384_Final()
|
||||
*/
|
||||
int test_wc_Sha3_384_Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
|
||||
wc_Sha3 sha3;
|
||||
const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
|
||||
"nopnopq";
|
||||
const char* expOut = "\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";
|
||||
byte hash[WC_SHA3_384_DIGEST_SIZE];
|
||||
byte hashRet[WC_SHA3_384_DIGEST_SIZE];
|
||||
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashRet, 0, sizeof(hashRet));
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_384_Final */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_512_Final()
|
||||
*/
|
||||
int test_wc_Sha3_512_Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
|
||||
!defined(WOLFSSL_NOSHA3_384)
|
||||
wc_Sha3 sha3;
|
||||
const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
|
||||
"nopnopq";
|
||||
const char* expOut = "\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";
|
||||
byte hash[WC_SHA3_512_DIGEST_SIZE];
|
||||
byte hashRet[WC_SHA3_512_DIGEST_SIZE];
|
||||
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashRet, 0, sizeof(hashRet));
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_512_Final */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_224_Copy()
|
||||
*/
|
||||
int test_wc_Sha3_224_Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
|
||||
wc_Sha3 sha3, sha3Cpy;
|
||||
const char* msg = TEST_STRING;
|
||||
word32 msglen = (word32)TEST_STRING_SZ;
|
||||
byte hash[WC_SHA3_224_DIGEST_SIZE];
|
||||
byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
|
||||
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashCpy, 0, sizeof(hashCpy));
|
||||
XMEMSET(&sha3, 0, sizeof(wc_Sha3));
|
||||
XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
wc_Sha3_224_Free(&sha3Cpy);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_224_Copy */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_256_Copy()
|
||||
*/
|
||||
int test_wc_Sha3_256_Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
|
||||
wc_Sha3 sha3, sha3Cpy;
|
||||
const char* msg = TEST_STRING;
|
||||
word32 msglen = (word32)TEST_STRING_SZ;
|
||||
byte hash[WC_SHA3_256_DIGEST_SIZE];
|
||||
byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
|
||||
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashCpy, 0, sizeof(hashCpy));
|
||||
XMEMSET(&sha3, 0, sizeof(wc_Sha3));
|
||||
XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
wc_Sha3_256_Free(&sha3Cpy);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_256_Copy */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_384_Copy()
|
||||
*/
|
||||
int test_wc_Sha3_384_Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
|
||||
wc_Sha3 sha3, sha3Cpy;
|
||||
const char* msg = TEST_STRING;
|
||||
word32 msglen = (word32)TEST_STRING_SZ;
|
||||
byte hash[WC_SHA3_384_DIGEST_SIZE];
|
||||
byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
|
||||
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashCpy, 0, sizeof(hashCpy));
|
||||
XMEMSET(&sha3, 0, sizeof(wc_Sha3));
|
||||
XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_384_Free(&sha3);
|
||||
wc_Sha3_384_Free(&sha3Cpy);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_384_Copy */
|
||||
|
||||
/*
|
||||
* Testing wc_Sha3_512_Copy()
|
||||
*/
|
||||
int test_wc_Sha3_512_Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
|
||||
wc_Sha3 sha3, sha3Cpy;
|
||||
const char* msg = TEST_STRING;
|
||||
word32 msglen = (word32)TEST_STRING_SZ;
|
||||
byte hash[WC_SHA3_512_DIGEST_SIZE];
|
||||
byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
|
||||
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashCpy, 0, sizeof(hashCpy));
|
||||
XMEMSET(&sha3, 0, sizeof(wc_Sha3));
|
||||
XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
|
||||
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
|
||||
ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Sha3_512_Free(&sha3);
|
||||
wc_Sha3_512_Free(&sha3Cpy);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_512_Copy */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Sha3_GetFlags()
|
||||
*/
|
||||
int test_wc_Sha3_GetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sha3 sha3;
|
||||
word32 flags = 0;
|
||||
|
||||
/* Initialize */
|
||||
ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
|
||||
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
|
||||
wc_Sha3_224_Free(&sha3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sha3_GetFlags */
|
||||
|
||||
/*******************************************************************************
|
||||
* SHAKE-256
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_InitShake256(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
wc_Shake shake;
|
||||
|
||||
ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Shake256_Free(&shake);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
|
||||
int test_wc_Shake256_Update(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
wc_Shake shake;
|
||||
byte msg[] = "Everybody's working for the weekend.";
|
||||
byte msg2[] = "Everybody gets Friday off.";
|
||||
byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
|
||||
"\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
|
||||
"\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
|
||||
"\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
|
||||
"\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
|
||||
word32 msglen = sizeof(msg) - 1;
|
||||
word32 msg2len = sizeof(msg2);
|
||||
word32 msgCmplen = sizeof(msgCmp);
|
||||
|
||||
ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
|
||||
ExpectTrue(shake.i == msglen);
|
||||
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
|
||||
|
||||
/* Pass bad args. */
|
||||
ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
wc_Shake256_Free(&shake);
|
||||
|
||||
ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
|
||||
ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
|
||||
wc_Shake256_Free(&shake);
|
||||
#endif /* WOLFSSL_SHAKE256 */
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_Shake256_Final(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
wc_Shake shake;
|
||||
const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
|
||||
"nopnopq";
|
||||
const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
|
||||
"\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
|
||||
"\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
|
||||
"\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
|
||||
"\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
|
||||
"\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
|
||||
"\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
|
||||
"\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
|
||||
"\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
|
||||
byte hash[114];
|
||||
|
||||
/* Init stack variables. */
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
|
||||
ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
|
||||
0);
|
||||
ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
|
||||
ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Shake256_Free(&shake);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
/*
|
||||
* Testing wc_Shake256_Copy()
|
||||
*/
|
||||
int test_wc_Shake256_Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
wc_Shake shake, shakeCpy;
|
||||
const char* msg = TEST_STRING;
|
||||
word32 msglen = (word32)TEST_STRING_SZ;
|
||||
byte hash[144];
|
||||
byte hashCpy[144];
|
||||
word32 hashLen = sizeof(hash);
|
||||
word32 hashLenCpy = sizeof(hashCpy);
|
||||
|
||||
XMEMSET(hash, 0, sizeof(hash));
|
||||
XMEMSET(hashCpy, 0, sizeof(hashCpy));
|
||||
|
||||
ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
|
||||
ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
|
||||
|
||||
ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
|
||||
ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
|
||||
ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
|
||||
ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
|
||||
ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
|
||||
|
||||
/* Test bad args. */
|
||||
ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
wc_Shake256_Free(&shake);
|
||||
wc_Shake256_Free(&shakeCpy);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Shake256_Copy */
|
||||
|
||||
/*
|
||||
* Unit test function for wc_Shake256Hash()
|
||||
*/
|
||||
int test_wc_Shake256Hash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
const byte data[] = { /* Hello World */
|
||||
0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
|
||||
0x72,0x6c,0x64
|
||||
};
|
||||
word32 len = sizeof(data);
|
||||
byte hash[144];
|
||||
word32 hashLen = sizeof(hash);
|
||||
|
||||
ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Shake256Hash */
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* test_sha3.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_SHA3_H
|
||||
#define WOLFCRYPT_TEST_SHA3_H
|
||||
|
||||
int test_wc_InitSha3(void);
|
||||
int test_wc_Sha3_Update(void);
|
||||
int test_wc_Sha3_224_Final(void);
|
||||
int test_wc_Sha3_256_Final(void);
|
||||
int test_wc_Sha3_384_Final(void);
|
||||
int test_wc_Sha3_512_Final(void);
|
||||
int test_wc_Sha3_224_Copy(void);
|
||||
int test_wc_Sha3_256_Copy(void);
|
||||
int test_wc_Sha3_384_Copy(void);
|
||||
int test_wc_Sha3_512_Copy(void);
|
||||
int test_wc_Sha3_GetFlags(void);
|
||||
|
||||
int test_wc_InitShake256(void);
|
||||
int test_wc_Shake256_Update(void);
|
||||
int test_wc_Shake256_Final(void);
|
||||
int test_wc_Shake256_Copy(void);
|
||||
int test_wc_Shake256Hash(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_SHA3_H */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,61 @@
|
|||
/* test_sha512.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_SHA512_H
|
||||
#define WOLFCRYPT_TEST_SHA512_H
|
||||
|
||||
int test_wc_InitSha512(void);
|
||||
int test_wc_Sha512Update(void);
|
||||
int test_wc_Sha512Final(void);
|
||||
int test_wc_Sha512FinalRaw(void);
|
||||
int test_wc_Sha512GetFlags(void);
|
||||
int test_wc_Sha512Free(void);
|
||||
int test_wc_Sha512GetHash(void);
|
||||
int test_wc_Sha512Copy(void);
|
||||
|
||||
int test_wc_InitSha512_224(void);
|
||||
int test_wc_Sha512_224Update(void);
|
||||
int test_wc_Sha512_224Final(void);
|
||||
int test_wc_Sha512_224FinalRaw(void);
|
||||
int test_wc_Sha512_224GetFlags(void);
|
||||
int test_wc_Sha512_224Free(void);
|
||||
int test_wc_Sha512_224GetHash(void);
|
||||
int test_wc_Sha512_224Copy(void);
|
||||
|
||||
int test_wc_InitSha512_256(void);
|
||||
int test_wc_Sha512_256Update(void);
|
||||
int test_wc_Sha512_256Final(void);
|
||||
int test_wc_Sha512_256FinalRaw(void);
|
||||
int test_wc_Sha512_256GetFlags(void);
|
||||
int test_wc_Sha512_256Free(void);
|
||||
int test_wc_Sha512_256GetHash(void);
|
||||
int test_wc_Sha512_256Copy(void);
|
||||
|
||||
int test_wc_InitSha384(void);
|
||||
int test_wc_Sha384Update(void);
|
||||
int test_wc_Sha384Final(void);
|
||||
int test_wc_Sha384FinalRaw(void);
|
||||
int test_wc_Sha384GetFlags(void);
|
||||
int test_wc_Sha384Free(void);
|
||||
int test_wc_Sha384GetHash(void);
|
||||
int test_wc_Sha384Copy(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_SHA512_H */
|
|
@ -0,0 +1,7 @@
|
|||
# Miscellaneous Crypto Tests
|
||||
|
||||
This directory contains tests for other cryptographic operations including:
|
||||
- HMAC
|
||||
- CMAC
|
||||
- Random number generation
|
||||
- Key derivation functions
|
|
@ -0,0 +1,8 @@
|
|||
# Public Key Algorithm Tests
|
||||
|
||||
This directory contains tests for public key algorithms including:
|
||||
- RSA
|
||||
- ECC
|
||||
- DSA
|
||||
- Ed25519
|
||||
- X25519
|
|
@ -0,0 +1,132 @@
|
|||
/* test_dsa.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/dsa.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_dsa.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* DSA Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_InitDsaKey(void)
|
||||
{
|
||||
#ifndef NO_DSA
|
||||
DsaKey key;
|
||||
return TEST_CRYPTO_OPERATION("DSA",
|
||||
wc_InitDsaKey,
|
||||
NULL,
|
||||
NULL,
|
||||
wc_FreeDsaKey,
|
||||
NULL, 0, NULL);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_DsaSign(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DSA
|
||||
DsaKey key;
|
||||
WC_RNG rng;
|
||||
byte hash[SHA_DIGEST_SIZE];
|
||||
byte sig[DSA_MAX_SIG_SIZE];
|
||||
word32 sigLen = sizeof(sig);
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitDsaKey(&key), 0);
|
||||
ExpectIntEQ(wc_InitRng(&rng), 0);
|
||||
ExpectIntEQ(wc_DsaSign(hash, sig, &key, &rng), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_DsaSign(NULL, sig, &key, &rng), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaSign(hash, sig, NULL, &rng), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaSign(hash, sig, &key, NULL), BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeDsaKey(&key);
|
||||
wc_FreeRng(&rng);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_DsaVerify(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DSA
|
||||
DsaKey key;
|
||||
byte hash[SHA_DIGEST_SIZE];
|
||||
byte sig[DSA_MAX_SIG_SIZE];
|
||||
word32 sigLen = sizeof(sig);
|
||||
int stat = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitDsaKey(&key), 0);
|
||||
ExpectIntEQ(wc_DsaVerify(hash, sig, &key, &stat), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_DsaVerify(NULL, sig, &key, &stat), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &stat), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaVerify(hash, sig, NULL, &stat), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaVerify(hash, sig, &key, NULL), BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeDsaKey(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_DsaPublicPrivateKeyDecode(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DSA
|
||||
DsaKey key;
|
||||
byte* tmp = NULL;
|
||||
word32 idx = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitDsaKey(&key), 0);
|
||||
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, 0), BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeDsaKey(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_DsaFree(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_DSA
|
||||
DsaKey key;
|
||||
wc_InitDsaKey(&key);
|
||||
wc_FreeDsaKey(&key);
|
||||
wc_FreeDsaKey(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/* test_dsa.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_DSA_H
|
||||
#define WOLFCRYPT_TEST_DSA_H
|
||||
|
||||
int test_wc_InitDsaKey(void);
|
||||
int test_wc_DsaSign(void);
|
||||
int test_wc_DsaVerify(void);
|
||||
int test_wc_DsaPublicPrivateKeyDecode(void);
|
||||
int test_wc_DsaFree(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_DSA_H */
|
|
@ -0,0 +1,178 @@
|
|||
/* test_ecc.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/ecc.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_ecc.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* ECC Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_ecc_init(void)
|
||||
{
|
||||
#ifdef HAVE_ECC
|
||||
ecc_key key;
|
||||
return TEST_CRYPTO_OPERATION("ECC",
|
||||
wc_ecc_init,
|
||||
NULL,
|
||||
NULL,
|
||||
wc_ecc_free,
|
||||
NULL, 0, NULL);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_ecc_sign_hash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ECC
|
||||
ecc_key key;
|
||||
WC_RNG rng;
|
||||
byte hash[32];
|
||||
byte sig[ECC_MAX_SIG_SIZE];
|
||||
word32 sigLen = sizeof(sig);
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_ecc_init(&key), 0);
|
||||
ExpectIntEQ(wc_InitRng(&rng), 0);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigLen, &rng, &key),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_ecc_sign_hash(NULL, sizeof(hash), sig, &sigLen, &rng, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, 0, sig, &sigLen, &rng, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, sizeof(hash), NULL, &sigLen, &rng, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, sizeof(hash), sig, NULL, &rng, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigLen, NULL, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigLen, &rng, NULL),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_ecc_free(&key);
|
||||
wc_FreeRng(&rng);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_ecc_verify_hash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ECC
|
||||
ecc_key key;
|
||||
byte hash[32];
|
||||
byte sig[ECC_MAX_SIG_SIZE];
|
||||
word32 sigLen = sizeof(sig);
|
||||
int stat = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_ecc_init(&key), 0);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, sigLen, hash, sizeof(hash), &stat, &key),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_ecc_verify_hash(NULL, sigLen, hash, sizeof(hash), &stat, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, 0, hash, sizeof(hash), &stat, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, sigLen, NULL, sizeof(hash), &stat, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, sigLen, hash, 0, &stat, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, sigLen, hash, sizeof(hash), NULL, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_verify_hash(sig, sigLen, hash, sizeof(hash), &stat, NULL),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_ecc_free(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_ecc_make_key(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ECC
|
||||
WC_RNG rng;
|
||||
ecc_key key;
|
||||
int size = 32;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitRng(&rng), 0);
|
||||
ExpectIntEQ(wc_ecc_init(&key), 0);
|
||||
ExpectIntEQ(wc_ecc_make_key(&rng, size, &key), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_ecc_make_key(NULL, size, &key), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_make_key(&rng, 0, &key), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_make_key(&rng, size, NULL), BAD_FUNC_ARG);
|
||||
|
||||
wc_ecc_free(&key);
|
||||
wc_FreeRng(&rng);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_ecc_shared_secret(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ECC
|
||||
ecc_key key1, key2;
|
||||
byte secret[ECC_MAXSIZE];
|
||||
word32 secretLen = sizeof(secret);
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_ecc_init(&key1), 0);
|
||||
ExpectIntEQ(wc_ecc_init(&key2), 0);
|
||||
ExpectIntEQ(wc_ecc_shared_secret(&key1, &key2, secret, &secretLen),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_ecc_shared_secret(NULL, &key2, secret, &secretLen),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_shared_secret(&key1, NULL, secret, &secretLen),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_shared_secret(&key1, &key2, NULL, &secretLen),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_ecc_shared_secret(&key1, &key2, secret, NULL),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_ecc_free(&key1);
|
||||
wc_ecc_free(&key2);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_ecc_free(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ECC
|
||||
ecc_key key;
|
||||
wc_ecc_init(&key);
|
||||
wc_ecc_free(&key);
|
||||
wc_ecc_free(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/* test_ecc.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_ECC_H
|
||||
#define WOLFCRYPT_TEST_ECC_H
|
||||
|
||||
int test_wc_ecc_init(void);
|
||||
int test_wc_ecc_sign_hash(void);
|
||||
int test_wc_ecc_verify_hash(void);
|
||||
int test_wc_ecc_make_key(void);
|
||||
int test_wc_ecc_shared_secret(void);
|
||||
int test_wc_ecc_free(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_ECC_H */
|
|
@ -0,0 +1,163 @@
|
|||
/* test_rsa.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/rsa.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_rsa.h>
|
||||
#include "../core/test_utils.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* RSA Tests
|
||||
******************************************************************************/
|
||||
|
||||
int test_wc_InitRsaKey(void)
|
||||
{
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
return TEST_CRYPTO_OPERATION("RSA",
|
||||
wc_InitRsaKey,
|
||||
NULL,
|
||||
NULL,
|
||||
wc_FreeRsaKey,
|
||||
NULL, 0, NULL);
|
||||
#else
|
||||
return EXPECT_RESULT();
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_wc_RsaPrivateKeyDecode(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
byte* tmp = NULL;
|
||||
word32 idx = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitRsaKey(&key, NULL), 0);
|
||||
ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, 0), BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeRsaKey(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_RsaPublicKeyDecode(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
byte* tmp = NULL;
|
||||
word32 idx = 0;
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitRsaKey(&key, NULL), 0);
|
||||
ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &key, 0), BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, 0), BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeRsaKey(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_RsaPublicEncrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
WC_RNG rng;
|
||||
byte in[] = "Test data";
|
||||
byte out[256];
|
||||
word32 outLen = sizeof(out);
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitRsaKey(&key, NULL), 0);
|
||||
ExpectIntEQ(wc_InitRng(&rng), 0);
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(in, sizeof(in), out, outLen, &key, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(NULL, sizeof(in), out, outLen, &key, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(in, 0, out, outLen, &key, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(in, sizeof(in), NULL, outLen, &key, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(in, sizeof(in), out, 0, &key, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPublicEncrypt(in, sizeof(in), out, outLen, NULL, &rng),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeRsaKey(&key);
|
||||
wc_FreeRng(&rng);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_RsaPrivateDecrypt(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
byte in[256];
|
||||
byte out[256];
|
||||
word32 outLen = sizeof(out);
|
||||
|
||||
/* Test normal operation */
|
||||
ExpectIntEQ(wc_InitRsaKey(&key, NULL), 0);
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(in, sizeof(in), out, outLen, &key),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
/* Test error cases */
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(NULL, sizeof(in), out, outLen, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(in, 0, out, outLen, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(in, sizeof(in), NULL, outLen, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(in, sizeof(in), out, 0, &key),
|
||||
BAD_FUNC_ARG);
|
||||
ExpectIntEQ(wc_RsaPrivateDecrypt(in, sizeof(in), out, outLen, NULL),
|
||||
BAD_FUNC_ARG);
|
||||
|
||||
wc_FreeRsaKey(&key);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wc_RsaFree(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifndef NO_RSA
|
||||
RsaKey key;
|
||||
wc_InitRsaKey(&key, NULL);
|
||||
wc_FreeRsaKey(&key);
|
||||
wc_FreeRsaKey(NULL); /* Test NULL case */
|
||||
ExpectTrue(1);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/* test_rsa.h
|
||||
*
|
||||
* Copyright (C) 2006-2025 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.
|
||||
*/
|
||||
|
||||
#ifndef WOLFCRYPT_TEST_RSA_H
|
||||
#define WOLFCRYPT_TEST_RSA_H
|
||||
|
||||
int test_wc_InitRsaKey(void);
|
||||
int test_wc_RsaPrivateKeyDecode(void);
|
||||
int test_wc_RsaPublicKeyDecode(void);
|
||||
int test_wc_RsaPublicEncrypt(void);
|
||||
int test_wc_RsaPrivateDecrypt(void);
|
||||
int test_wc_RsaFree(void);
|
||||
|
||||
#endif /* WOLFCRYPT_TEST_RSA_H */
|
|
@ -1,194 +0,0 @@
|
|||
/* test_ascon.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/ascon.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/test_ascon.h>
|
||||
|
||||
#ifdef HAVE_ASCON
|
||||
#include <tests/api/test_ascon_kats.h>
|
||||
#endif
|
||||
|
||||
int test_ascon_hash256(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ASCON
|
||||
byte msg[1024];
|
||||
byte mdOut[ASCON_HASH256_SZ];
|
||||
const size_t test_rounds = sizeof(msg) + 1; /* +1 to test 0-len msg */
|
||||
wc_AsconHash256* asconHash = NULL;
|
||||
word32 i;
|
||||
|
||||
ExpectIntEQ(XELEM_CNT(ascon_hash256_output), test_rounds);
|
||||
|
||||
/* init msg buffer */
|
||||
for (i = 0; i < sizeof(msg); i++)
|
||||
msg[i] = (byte)i;
|
||||
|
||||
ExpectNotNull(asconHash = wc_AsconHash256_New());
|
||||
|
||||
for (i = 0; i < test_rounds && EXPECT_SUCCESS(); i++) {
|
||||
XMEMSET(mdOut, 0, sizeof(mdOut));
|
||||
ExpectIntEQ(wc_AsconHash256_Init(asconHash), 0);
|
||||
ExpectIntEQ(wc_AsconHash256_Update(asconHash, msg, i), 0);
|
||||
ExpectIntEQ(wc_AsconHash256_Final(asconHash, mdOut), 0);
|
||||
ExpectBufEQ(mdOut, ascon_hash256_output[i], ASCON_HASH256_SZ);
|
||||
wc_AsconHash256_Clear(asconHash);
|
||||
}
|
||||
|
||||
/* Test separated update */
|
||||
for (i = 0; i < test_rounds && EXPECT_SUCCESS(); i++) {
|
||||
word32 half_i = i / 2;
|
||||
XMEMSET(mdOut, 0, sizeof(mdOut));
|
||||
ExpectIntEQ(wc_AsconHash256_Init(asconHash), 0);
|
||||
ExpectIntEQ(wc_AsconHash256_Update(asconHash, msg, half_i), 0);
|
||||
ExpectIntEQ(wc_AsconHash256_Update(asconHash, msg + half_i,
|
||||
i - half_i), 0);
|
||||
ExpectIntEQ(wc_AsconHash256_Final(asconHash, mdOut), 0);
|
||||
ExpectBufEQ(mdOut, ascon_hash256_output[i], ASCON_HASH256_SZ);
|
||||
wc_AsconHash256_Clear(asconHash);
|
||||
}
|
||||
|
||||
wc_AsconHash256_Free(asconHash);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_ascon_aead128(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef HAVE_ASCON
|
||||
word32 i;
|
||||
wc_AsconAEAD128* asconAEAD = NULL;
|
||||
|
||||
ExpectNotNull(asconAEAD = wc_AsconAEAD128_New());
|
||||
|
||||
for (i = 0; i < XELEM_CNT(ascon_aead128_kat); i++) {
|
||||
byte key[ASCON_AEAD128_KEY_SZ];
|
||||
byte nonce[ASCON_AEAD128_NONCE_SZ];
|
||||
byte pt[32]; /* longest plaintext we test is 32 bytes */
|
||||
word32 ptSz;
|
||||
byte ad[32]; /* longest AD we test is 32 bytes */
|
||||
word32 adSz;
|
||||
byte ct[48]; /* longest ciphertext we test is 32 bytes + 16 bytes tag */
|
||||
word32 ctSz;
|
||||
word32 j;
|
||||
byte tag[ASCON_AEAD128_TAG_SZ];
|
||||
byte buf[32]; /* longest buffer we test is 32 bytes */
|
||||
|
||||
XMEMSET(key, 0, sizeof(key));
|
||||
XMEMSET(nonce, 0, sizeof(nonce));
|
||||
XMEMSET(pt, 0, sizeof(pt));
|
||||
XMEMSET(ad, 0, sizeof(ad));
|
||||
XMEMSET(ct, 0, sizeof(ct));
|
||||
XMEMSET(tag, 0, sizeof(tag));
|
||||
|
||||
/* Convert HEX strings to byte stream */
|
||||
for (j = 0; ascon_aead128_kat[i][0][j] != '\0'; j += 2) {
|
||||
key[j/2] = HexCharToByte(ascon_aead128_kat[i][0][j]) << 4 |
|
||||
HexCharToByte(ascon_aead128_kat[i][0][j+1]);
|
||||
}
|
||||
for (j = 0; ascon_aead128_kat[i][1][j] != '\0'; j += 2) {
|
||||
nonce[j/2] = HexCharToByte(ascon_aead128_kat[i][1][j]) << 4 |
|
||||
HexCharToByte(ascon_aead128_kat[i][1][j+1]);
|
||||
}
|
||||
for (j = 0; ascon_aead128_kat[i][2][j] != '\0'; j += 2) {
|
||||
pt[j/2] = HexCharToByte(ascon_aead128_kat[i][2][j]) << 4 |
|
||||
HexCharToByte(ascon_aead128_kat[i][2][j+1]);
|
||||
}
|
||||
ptSz = j/2;
|
||||
for (j = 0; ascon_aead128_kat[i][3][j] != '\0'; j += 2) {
|
||||
ad[j/2] = HexCharToByte(ascon_aead128_kat[i][3][j]) << 4 |
|
||||
HexCharToByte(ascon_aead128_kat[i][3][j+1]);
|
||||
}
|
||||
adSz = j/2;
|
||||
for (j = 0; ascon_aead128_kat[i][4][j] != '\0'; j += 2) {
|
||||
ct[j/2] = HexCharToByte(ascon_aead128_kat[i][4][j]) << 4 |
|
||||
HexCharToByte(ascon_aead128_kat[i][4][j+1]);
|
||||
}
|
||||
ctSz = j/2 - ASCON_AEAD128_TAG_SZ;
|
||||
|
||||
for (j = 0; j < 4; j++) {
|
||||
ExpectIntEQ(wc_AsconAEAD128_Init(asconAEAD), 0);
|
||||
ExpectIntEQ(wc_AsconAEAD128_SetKey(asconAEAD, key), 0);
|
||||
ExpectIntEQ(wc_AsconAEAD128_SetNonce(asconAEAD, nonce), 0);
|
||||
ExpectIntEQ(wc_AsconAEAD128_SetAD(asconAEAD, ad, adSz), 0);
|
||||
if (j == 0) {
|
||||
/* Encryption test */
|
||||
ExpectIntEQ(wc_AsconAEAD128_EncryptUpdate(asconAEAD, buf, pt,
|
||||
ptSz), 0);
|
||||
ExpectBufEQ(buf, ct, ptSz);
|
||||
ExpectIntEQ(wc_AsconAEAD128_EncryptFinal(asconAEAD, tag), 0);
|
||||
ExpectBufEQ(tag, ct + ptSz, ASCON_AEAD128_TAG_SZ);
|
||||
}
|
||||
else if (j == 1) {
|
||||
/* Decryption test */
|
||||
ExpectIntEQ(wc_AsconAEAD128_DecryptUpdate(asconAEAD, buf, ct,
|
||||
ctSz), 0);
|
||||
ExpectBufEQ(buf, pt, ctSz);
|
||||
ExpectIntEQ(wc_AsconAEAD128_DecryptFinal(asconAEAD, ct + ctSz),
|
||||
0);
|
||||
}
|
||||
else if (j == 2) {
|
||||
/* Split encryption test */
|
||||
ExpectIntEQ(wc_AsconAEAD128_EncryptUpdate(asconAEAD, buf, pt,
|
||||
ptSz / 2), 0);
|
||||
ExpectIntEQ(wc_AsconAEAD128_EncryptUpdate(asconAEAD,
|
||||
buf + (ptSz/2), pt + (ptSz/2), ptSz - (ptSz/2)), 0);
|
||||
ExpectBufEQ(buf, ct, ptSz);
|
||||
ExpectIntEQ(wc_AsconAEAD128_EncryptFinal(asconAEAD, tag), 0);
|
||||
ExpectBufEQ(tag, ct + ptSz, ASCON_AEAD128_TAG_SZ);
|
||||
}
|
||||
else if (j == 3) {
|
||||
/* Split decryption test */
|
||||
ExpectIntEQ(wc_AsconAEAD128_DecryptUpdate(asconAEAD, buf, ct,
|
||||
ctSz / 2), 0);
|
||||
ExpectIntEQ(wc_AsconAEAD128_DecryptUpdate(asconAEAD,
|
||||
buf + (ctSz/2), ct + (ctSz/2), ctSz - (ctSz/2)), 0);
|
||||
ExpectBufEQ(buf, pt, ctSz);
|
||||
ExpectIntEQ(wc_AsconAEAD128_DecryptFinal(asconAEAD, ct + ctSz),
|
||||
0);
|
||||
}
|
||||
wc_AsconAEAD128_Clear(asconAEAD);
|
||||
}
|
||||
}
|
||||
|
||||
wc_AsconAEAD128_Free(asconAEAD);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,585 +0,0 @@
|
|||
/* test_dtls.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
*/
|
||||
|
||||
#include <tests/unit.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/internal.h>
|
||||
|
||||
#include <tests/utils.h>
|
||||
#include <tests/api/test_dtls.h>
|
||||
|
||||
int test_dtls12_basic_connection_id(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS_CID)
|
||||
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
|
||||
unsigned char server_cid[] = { 0, 1, 2, 3, 4, 5 };
|
||||
unsigned char readBuf[40];
|
||||
const char* params[] = {
|
||||
#ifndef NO_RSA
|
||||
#ifndef NO_SHA256
|
||||
#if defined(WOLFSSL_AES_128) && defined(WOLFSSL_STATIC_RSA)
|
||||
"AES128-SHA256",
|
||||
#ifdef HAVE_AESCCM
|
||||
"AES128-CCM8",
|
||||
#endif
|
||||
"DHE-RSA-AES128-SHA256",
|
||||
"ECDHE-RSA-AES128-SHA256",
|
||||
#ifdef HAVE_AESGCM
|
||||
"DHE-RSA-AES128-GCM-SHA256",
|
||||
"ECDHE-RSA-AES128-GCM-SHA256",
|
||||
#endif
|
||||
#endif /* WOLFSSL_AES_128 && WOLFSSL_STATIC_RSA */
|
||||
#endif /* NO_SHA256 */
|
||||
#endif /* NO_RSA */
|
||||
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(HAVE_FIPS)
|
||||
"DHE-RSA-CHACHA20-POLY1305",
|
||||
"DHE-RSA-CHACHA20-POLY1305-OLD",
|
||||
"ECDHE-RSA-CHACHA20-POLY1305",
|
||||
"ECDHE-RSA-CHACHA20-POLY1305-OLD",
|
||||
#endif
|
||||
#ifndef NO_PSK
|
||||
"DHE-PSK-AES128-CBC-SHA256",
|
||||
"DHE-PSK-AES256-GCM-SHA384",
|
||||
#ifdef HAVE_NULL_CIPHER
|
||||
"DHE-PSK-NULL-SHA256",
|
||||
#endif
|
||||
"DHE-PSK-AES128-CCM",
|
||||
#endif
|
||||
};
|
||||
size_t i;
|
||||
struct {
|
||||
byte drop:1;
|
||||
byte changeCID:1;
|
||||
} run_params[] = {
|
||||
{ .drop = 0, .changeCID = 0 },
|
||||
{ .drop = 1, .changeCID = 0 },
|
||||
{ .drop = 0, .changeCID = 1 },
|
||||
};
|
||||
|
||||
/* We check if the side included the CID in their output */
|
||||
#define CLIENT_CID() mymemmem(test_ctx.s_buff, test_ctx.s_len, \
|
||||
client_cid, sizeof(client_cid))
|
||||
#define SERVER_CID() mymemmem(test_ctx.c_buff, test_ctx.c_len, \
|
||||
server_cid, sizeof(server_cid))
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < XELEM_CNT(params) && EXPECT_SUCCESS(); i++) {
|
||||
size_t j;
|
||||
for (j = 0; j < XELEM_CNT(run_params); j++) {
|
||||
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
|
||||
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
|
||||
struct test_memio_ctx test_ctx;
|
||||
|
||||
printf("Testing %s run #%ld ... ", params[i], (long int)j);
|
||||
|
||||
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
|
||||
|
||||
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
|
||||
&ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method),
|
||||
0);
|
||||
|
||||
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, params[i]), 1);
|
||||
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, params[i]), 1);
|
||||
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_c), 1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, server_cid,
|
||||
sizeof(server_cid)), 1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_s), 1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_s, client_cid,
|
||||
sizeof(client_cid)), 1);
|
||||
|
||||
#ifndef NO_PSK
|
||||
if (XSTRSTR(params[i], "-PSK-") != NULL) {
|
||||
wolfSSL_set_psk_client_callback(ssl_c, my_psk_client_cb);
|
||||
wolfSSL_set_psk_server_callback(ssl_s, my_psk_server_cb);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SECURE_RENEGOTIATION
|
||||
ExpectIntEQ(wolfSSL_UseSecureRenegotiation(ssl_c), 1);
|
||||
ExpectIntEQ(wolfSSL_UseSecureRenegotiation(ssl_s), 1);
|
||||
#endif
|
||||
|
||||
/* CH1 */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(CLIENT_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), 1);
|
||||
ExpectNull(CLIENT_CID());
|
||||
}
|
||||
/* HVR */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(SERVER_CID());
|
||||
/* No point dropping HVR */
|
||||
/* CH2 */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(CLIENT_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), 1);
|
||||
ExpectNull(CLIENT_CID());
|
||||
}
|
||||
/* Server first flight */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(SERVER_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_s), 1);
|
||||
ExpectNull(SERVER_CID());
|
||||
}
|
||||
/* Client second flight */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), 1);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
}
|
||||
/* Server second flight */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
}
|
||||
/* Client complete connection */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
|
||||
ExpectNull(CLIENT_CID());
|
||||
|
||||
/* Write some data */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Read the data */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
/* Write short data */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i], 1), 1);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i], 1), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Read the short data */
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), 1);
|
||||
ExpectIntEQ(readBuf[0], params[i][0]);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), 1);
|
||||
ExpectIntEQ(readBuf[0], params[i][0]);
|
||||
/* Write some data but with wrong CID */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
/* Use Expect so we don't access CLIENT_CID() if it is NULL */
|
||||
ExpectTrue(((char*)CLIENT_CID())[0] = -1);
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Use Expect so we don't access SERVER_CID() if it is NULL */
|
||||
ExpectTrue(((char*)SERVER_CID())[0] = -1);
|
||||
/* Try to read the data but it shouldn't be there */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
|
||||
#ifdef HAVE_SECURE_RENEGOTIATION
|
||||
/* do two SCR's */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_Rehandshake(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
|
||||
/* SCR's after the first one have extra internal logic */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_Rehandshake(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
|
||||
|
||||
if (run_params[j].changeCID) {
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, client_cid,
|
||||
sizeof(client_cid)), 0);
|
||||
/* Forcefully change the CID */
|
||||
ssl_c->dtlsCidInfo->rx->id[0] = -1;
|
||||
/* We need to init the rehandshake from the client, otherwise
|
||||
* we won't be able to test changing the CID. It would be
|
||||
* rejected by the record CID matching code. */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_Rehandshake(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1),
|
||||
WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_SSL_renegotiate_pending(ssl_c), 1);
|
||||
/* Server first flight */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
|
||||
/* We expect the server to reject the CID change. */
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), DTLS_CID_ERROR);
|
||||
goto loop_exit;
|
||||
}
|
||||
/* Server init'd SCR */
|
||||
/* Server request */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
ExpectIntEQ(wolfSSL_SSL_renegotiate_pending(ssl_s), 1);
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
}
|
||||
/* Init SCR on client side with the server's request */
|
||||
/* CH no HVR on SCR */
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_SSL_renegotiate_pending(ssl_c), 1);
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), 1);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
}
|
||||
/* Server first flight */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
}
|
||||
/* Client second flight */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), 1);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
}
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
/* Server second flight */
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), APP_DATA_READY);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
if (!run_params[j].drop) {
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i],
|
||||
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
|
||||
}
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
if (run_params[j].drop) {
|
||||
test_ctx.c_len = test_ctx.s_len = 0;
|
||||
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_s), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
}
|
||||
/* Test loading old epoch */
|
||||
/* Client complete connection */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
if (!run_params[j].drop) {
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
}
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
|
||||
ExpectNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_SSL_renegotiate_pending(ssl_c), 0);
|
||||
ExpectIntEQ(wolfSSL_SSL_renegotiate_pending(ssl_s), 0);
|
||||
#endif
|
||||
/* Close connection */
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
wolfSSL_SetLoggingPrefix("client");
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_c), 1);
|
||||
wolfSSL_SetLoggingPrefix("server");
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_s), 1);
|
||||
|
||||
#ifdef HAVE_SECURE_RENEGOTIATION
|
||||
loop_exit:
|
||||
#endif
|
||||
wolfSSL_SetLoggingPrefix(NULL);
|
||||
wolfSSL_free(ssl_c);
|
||||
wolfSSL_CTX_free(ctx_c);
|
||||
wolfSSL_free(ssl_s);
|
||||
wolfSSL_CTX_free(ctx_s);
|
||||
|
||||
if (EXPECT_SUCCESS())
|
||||
printf("ok\n");
|
||||
else
|
||||
printf("failed\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#undef CLIENT_CID
|
||||
#undef SERVER_CID
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_dtls13_basic_connection_id(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
|
||||
&& defined(WOLFSSL_DTLS_CID)
|
||||
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
|
||||
unsigned char server_cid[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
unsigned char readBuf[50];
|
||||
const char* params[] = {
|
||||
#ifndef NO_SHA256
|
||||
#ifdef WOLFSSL_AES_128
|
||||
#ifdef HAVE_AESGCM
|
||||
"TLS13-AES128-GCM-SHA256",
|
||||
#endif
|
||||
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
|
||||
"TLS13-CHACHA20-POLY1305-SHA256",
|
||||
#endif
|
||||
#ifdef HAVE_AESCCM
|
||||
"TLS13-AES128-CCM-8-SHA256",
|
||||
"TLS13-AES128-CCM-SHA256",
|
||||
#endif
|
||||
#endif
|
||||
#ifdef HAVE_NULL_CIPHER
|
||||
"TLS13-SHA256-SHA256",
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
size_t i;
|
||||
|
||||
/* We check if the side included the CID in their output */
|
||||
#define CLIENT_CID() mymemmem(test_ctx.s_buff, test_ctx.s_len, \
|
||||
client_cid, sizeof(client_cid))
|
||||
#define SERVER_CID() mymemmem(test_ctx.c_buff, test_ctx.c_len, \
|
||||
server_cid, sizeof(server_cid))
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < XELEM_CNT(params) && EXPECT_SUCCESS(); i++) {
|
||||
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
|
||||
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
|
||||
struct test_memio_ctx test_ctx;
|
||||
|
||||
printf("Testing %s ... ", params[i]);
|
||||
|
||||
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
|
||||
|
||||
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
|
||||
wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
|
||||
|
||||
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, params[i]), WOLFSSL_SUCCESS);
|
||||
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, params[i]), WOLFSSL_SUCCESS);
|
||||
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_c), 1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, server_cid, sizeof(server_cid)),
|
||||
1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_s), 1);
|
||||
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_s, client_cid, sizeof(client_cid)),
|
||||
1);
|
||||
|
||||
/* CH1 */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(CLIENT_CID());
|
||||
/* HRR */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(SERVER_CID());
|
||||
/* CH2 */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNull(CLIENT_CID());
|
||||
/* Server first flight */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Client second flight */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
/* Server process flight */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
|
||||
/* Client process flight */
|
||||
ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
|
||||
|
||||
/* Write some data */
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i], (int)XSTRLEN(params[i])),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i], (int)XSTRLEN(params[i])),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Read the data */
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectStrEQ(readBuf, params[i]);
|
||||
/* Write short data */
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i], 1), 1);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i], 1), 1);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Read the short data */
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), 1);
|
||||
ExpectIntEQ(readBuf[0], params[i][0]);
|
||||
XMEMSET(readBuf, 0, sizeof(readBuf));
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), 1);
|
||||
ExpectIntEQ(readBuf[0], params[i][0]);
|
||||
/* Write some data but with wrong CID */
|
||||
ExpectIntEQ(wolfSSL_write(ssl_c, params[i], (int)XSTRLEN(params[i])),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
/* Use Expect so we don't access CLIENT_CID() if it is NULL */
|
||||
ExpectTrue(((char*)CLIENT_CID())[0] = -1);
|
||||
ExpectIntEQ(wolfSSL_write(ssl_s, params[i], (int)XSTRLEN(params[i])),
|
||||
XSTRLEN(params[i]));
|
||||
ExpectNotNull(SERVER_CID());
|
||||
/* Use Expect so we don't access SERVER_CID() if it is NULL */
|
||||
ExpectTrue(((char*)SERVER_CID())[0] = -1);
|
||||
/* Try to read the data but it shouldn't be there */
|
||||
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
|
||||
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
|
||||
|
||||
/* Close connection */
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE);
|
||||
ExpectNotNull(CLIENT_CID());
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
|
||||
ExpectNotNull(SERVER_CID());
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_c), 1);
|
||||
ExpectIntEQ(wolfSSL_shutdown(ssl_s), 1);
|
||||
|
||||
if (EXPECT_SUCCESS())
|
||||
printf("ok\n");
|
||||
else
|
||||
printf("failed\n");
|
||||
|
||||
wolfSSL_free(ssl_c);
|
||||
wolfSSL_CTX_free(ctx_c);
|
||||
wolfSSL_free(ssl_s);
|
||||
wolfSSL_CTX_free(ctx_s);
|
||||
}
|
||||
|
||||
#undef CLIENT_CID
|
||||
#undef SERVER_CID
|
||||
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
||||
|
||||
int test_wolfSSL_dtls_cid_parse(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_DTLS_CID)
|
||||
/* Taken from Wireshark. Right-click -> copy -> ... as escaped string */
|
||||
/* Plaintext ServerHelloDone. No CID. */
|
||||
byte noCid[] =
|
||||
"\x16\xfe\xfd\x00\x00\x00\x00\x00\x00\x00\x04\x00\x0c\x0e\x00\x00" \
|
||||
"\x00\x00\x04\x00\x00\x00\x00\x00\x00";
|
||||
/* 1.2 app data containing CID */
|
||||
byte cid12[] =
|
||||
"\x19\xfe\xfd\x00\x01\x00\x00\x00\x00\x00\x01\x77\xa3\x79\x34\xb3" \
|
||||
"\xf1\x1f\x34\x00\x1f\xdb\x8c\x28\x25\x9f\xe1\x02\x26\x77\x1c\x3a" \
|
||||
"\x50\x1b\x50\x99\xd0\xb5\x20\xd8\x2c\x2e\xaa\x36\x36\xe0\xb7\xb7" \
|
||||
"\xf7\x7d\xff\xb0";
|
||||
#ifdef WOLFSSL_DTLS13
|
||||
/* 1.3 app data containing CID */
|
||||
byte cid13[] =
|
||||
"\x3f\x70\x64\x04\xc6\xfb\x97\x21\xd9\x28\x27\x00\x17\xc1\x01\x86" \
|
||||
"\xe7\x23\x2c\xad\x65\x83\xa8\xf4\xbf\xbf\x7b\x25\x16\x80\x19\xc3" \
|
||||
"\x81\xda\xf5\x3f";
|
||||
#endif
|
||||
|
||||
ExpectPtrEq(wolfSSL_dtls_cid_parse(noCid, sizeof(noCid), 8), NULL);
|
||||
ExpectPtrEq(wolfSSL_dtls_cid_parse(cid12, sizeof(cid12), 8), cid12 + 11);
|
||||
#ifdef WOLFSSL_DTLS13
|
||||
ExpectPtrEq(wolfSSL_dtls_cid_parse(cid13, sizeof(cid13), 8), cid13 + 1);
|
||||
#endif
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
}
|
|
@ -1,309 +0,0 @@
|
|||
/* test_sm3.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 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
|
||||
|
||||
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/sm3.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <tests/unit.h>
|
||||
#include <tests/api/api.h>
|
||||
#include <tests/api/test_sm3.h>
|
||||
|
||||
/*
|
||||
* Testing wc_InitSm3(), wc_Sm3Free()
|
||||
*/
|
||||
int test_wc_InitSm3Free(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SM3
|
||||
wc_Sm3 sm3;
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid Parameters */
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
|
||||
wc_Sm3Free(NULL);
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_InitSm3 */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3Update(), wc_Sm3Final()
|
||||
*/
|
||||
int test_wc_Sm3UpdateFinal(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SM3
|
||||
wc_Sm3 sm3;
|
||||
byte data[WC_SM3_BLOCK_SIZE * 4];
|
||||
byte hash[WC_SM3_DIGEST_SIZE];
|
||||
byte calcHash[WC_SM3_DIGEST_SIZE];
|
||||
byte expHash[WC_SM3_DIGEST_SIZE] = {
|
||||
0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
|
||||
0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
|
||||
0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
|
||||
0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
|
||||
};
|
||||
word32 chunk;
|
||||
word32 i;
|
||||
|
||||
XMEMSET(data, 0, sizeof(data));
|
||||
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Update(NULL, data, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
|
||||
/* Ensure too many bytes for lengths. */
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Final(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Final(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
|
||||
ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
|
||||
|
||||
/* Chunk tests. */
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
|
||||
for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
|
||||
for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
|
||||
}
|
||||
if (i < (word32)sizeof(data)) {
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
|
||||
0);
|
||||
}
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
|
||||
ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
/* Not testing when the low 32-bit length overflows. */
|
||||
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3Update */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3GetHash()
|
||||
*/
|
||||
int test_wc_Sm3GetHash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#ifdef WOLFSSL_SM3
|
||||
wc_Sm3 sm3;
|
||||
byte hash[WC_SM3_DIGEST_SIZE];
|
||||
byte calcHash[WC_SM3_DIGEST_SIZE];
|
||||
byte data[WC_SM3_BLOCK_SIZE];
|
||||
|
||||
XMEMSET(data, 0, sizeof(data));
|
||||
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3GetHash(NULL, hash), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid Parameters */
|
||||
ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
|
||||
ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
|
||||
|
||||
/* With update. */
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
|
||||
ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
|
||||
ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
|
||||
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3Update */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3Copy()
|
||||
*/
|
||||
int test_wc_Sm3Copy(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sm3 sm3;
|
||||
wc_Sm3 sm3Copy;
|
||||
byte hash[WC_SM3_DIGEST_SIZE];
|
||||
byte hashCopy[WC_SM3_DIGEST_SIZE];
|
||||
byte data[WC_SM3_BLOCK_SIZE + 1];
|
||||
int i;
|
||||
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Copy(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid Parameters */
|
||||
ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
|
||||
|
||||
/* Ensure all parts of data updated during hashing are copied. */
|
||||
for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
|
||||
ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
|
||||
ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
|
||||
ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
|
||||
ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
wc_Sm3Free(&sm3Copy);
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3Copy */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3FinalRaw()
|
||||
*/
|
||||
int test_wc_Sm3FinalRaw(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
|
||||
!defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
|
||||
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
|
||||
!defined(WOLFSSL_NO_HASH_RAW)
|
||||
wc_Sm3 sm3;
|
||||
byte hash1[WC_SM3_DIGEST_SIZE];
|
||||
byte hash2[WC_SM3_DIGEST_SIZE];
|
||||
byte hash3[WC_SM3_DIGEST_SIZE];
|
||||
byte* hash_test[3] = { hash1, hash2, hash3 };
|
||||
int times;
|
||||
int i;
|
||||
|
||||
XMEMSET(&sm3, 0, sizeof(sm3));
|
||||
|
||||
/* Initialize */
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
|
||||
/* Invalid Parameters */
|
||||
ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
times = sizeof(hash_test) / sizeof(byte*);
|
||||
for (i = 0; i < times; i++) {
|
||||
ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
|
||||
}
|
||||
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3FinalRaw */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
|
||||
*/
|
||||
int test_wc_Sm3GetSetFlags(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
|
||||
wc_Sm3 sm3;
|
||||
wc_Sm3 sm3Copy;
|
||||
word32 flags = 0;
|
||||
|
||||
ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
|
||||
ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
|
||||
|
||||
ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
|
||||
ExpectIntEQ(flags, 0);
|
||||
ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
|
||||
ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
|
||||
ExpectIntEQ(flags, 0);
|
||||
ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
|
||||
ExpectIntEQ(flags, 0);
|
||||
ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
|
||||
ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
|
||||
ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
|
||||
|
||||
ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
|
||||
ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
|
||||
ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
|
||||
|
||||
wc_Sm3Free(&sm3Copy);
|
||||
wc_Sm3Free(&sm3);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3Update */
|
||||
|
||||
/*
|
||||
* Testing wc_Sm3Hash()
|
||||
*/
|
||||
int test_wc_Sm3Hash(void)
|
||||
{
|
||||
EXPECT_DECLS;
|
||||
#if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
|
||||
byte data[WC_SM3_BLOCK_SIZE];
|
||||
byte hash[WC_SM3_DIGEST_SIZE];
|
||||
|
||||
/* Invalid parameters. */
|
||||
ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL),
|
||||
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
|
||||
|
||||
/* Valid parameters. */
|
||||
ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
|
||||
#endif
|
||||
return EXPECT_RESULT();
|
||||
} /* END test_wc_Sm3Hash */
|
||||
|
Loading…
Reference in New Issue