Skip to content

Commit

Permalink
ascon: move tests to api.c and introduce framework to split up api.c
Browse files Browse the repository at this point in the history
  • Loading branch information
julek-wolfssl committed Jan 29, 2025
1 parent cd047a3 commit 40ce6cc
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 32 deletions.
6 changes: 5 additions & 1 deletion tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>


#include <tests/api.h>

#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_TLS) && \
!defined(NO_RSA) && !defined(SINGLE_THREADED) && \
Expand Down Expand Up @@ -101723,6 +101723,10 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wc_dilithium_sig_kats),
TEST_DECL(test_wc_dilithium_verify_kats),

/* Ascon */
TEST_DECL(test_ascon_hash256),
TEST_DECL(test_ascon_aead128),

/* Signature API */
TEST_DECL(test_wc_SignatureGetSize_ecc),
TEST_DECL(test_wc_SignatureGetSize_rsa),
Expand Down
29 changes: 29 additions & 0 deletions tests/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* api.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 TESTS_API_H
#define TESTS_API_H

/* ascon */
int test_ascon_hash256(void);
int test_ascon_aead128(void);

#endif /* TESTS_API_H */
192 changes: 192 additions & 0 deletions tests/api/ascon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/* 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 <tests/unit.h>
#include <tests/api.h>
#ifdef HAVE_ASCON
#include <tests/api/ascon.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();
}
8 changes: 4 additions & 4 deletions wolfcrypt/test/ascon-kat.h → tests/api/ascon.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ascon-kat.h
/* ascon.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
Expand All @@ -19,8 +19,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#ifndef WOLFCRYPT_TEST_ASCON_KAT_H
#define WOLFCRYPT_TEST_ASCON_KAT_H
#ifndef WOLFCRYPT_TEST_ASCON_H
#define WOLFCRYPT_TEST_ASCON_H

#include <wolfssl/wolfcrypt/types.h>

Expand Down Expand Up @@ -6506,4 +6506,4 @@ static const char *ascon_aead128_kat[][5] = {
/* CT = */ "4C086D27A3B51A2333CFC7F22172A9BCAD88B8D4D77E50622D788345FA7BEE4468915D3F9422289F2349D6A3B4160397", },
};

#endif /* WOLFCRYPT_TEST_ASCON_KAT_H */
#endif /* WOLFCRYPT_TEST_ASCON_H */
4 changes: 4 additions & 0 deletions tests/api/include.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if BUILD_TESTS
tests_unit_test_SOURCES += tests/api/ascon.c
endif
EXTRA_DIST += tests/api/ascon.h
4 changes: 3 additions & 1 deletion tests/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tests_unit_test_SOURCES = \
tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) $(WOLFSENTRY_INCLUDE)
tests_unit_test_LDADD = src/libwolfssl@[email protected] $(LIB_STATIC_ADD) $(WOLFSENTRY_LIB)
tests_unit_test_DEPENDENCIES = src/libwolfssl@[email protected]
include tests/api/include.am
endif
EXTRA_DIST += tests/unit.h \
tests/test.conf \
Expand Down Expand Up @@ -72,5 +73,6 @@ EXTRA_DIST += tests/unit.h \
tests/NCONF_test.cnf \
tests/test-tls-downgrade.conf \
tests/TXT_DB.txt \
tests/utils.h
tests/utils.h \
tests/api.h
DISTCLEANFILES+= tests/.libs/unit.test
1 change: 0 additions & 1 deletion wolfcrypt/test/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ wolfcrypt_test_testwolfcrypt_SOURCES = wolfcrypt/test/test.c
wolfcrypt_test_testwolfcrypt_LDADD = src/libwolfssl@[email protected] $(LIB_STATIC_ADD)
wolfcrypt_test_testwolfcrypt_DEPENDENCIES = src/libwolfssl@[email protected]
noinst_HEADERS += wolfcrypt/test/test.h wolfcrypt/test/test_paths.h.in
noinst_HEADERS += wolfcrypt/test/ascon-kat.h
endif
endif

Expand Down
Loading

0 comments on commit 40ce6cc

Please sign in to comment.