Skip to content

Commit

Permalink
kats working
Browse files Browse the repository at this point in the history
  • Loading branch information
ruipedro16 committed Feb 15, 2024
1 parent 52adf41 commit 49b6d62
Show file tree
Hide file tree
Showing 8 changed files with 624 additions and 111 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test-ref-jasmin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ jobs:
strategy:
matrix:
directory:
# - fors
# - wots
# - wotsx1
- merkle
- sign
- kats
steps:
- uses: actions/checkout@v4
- name: Compile tests
Expand Down
2 changes: 2 additions & 0 deletions etc/preprocessor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

print("compiling with -lea\n\n")

data = []

subprocess.run(
["make", "-C", "../ref-jasmin/test/sign", "clean"],
stdout=subprocess.DEVNULL,
Expand Down
57 changes: 48 additions & 9 deletions ref-jasmin/PQCgenKAT_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "api.h"
#include "rng.h"

#include "macros.h"

#define MAX_MARKER_LEN 50

#define KAT_SUCCESS 0
Expand All @@ -25,14 +27,51 @@ uint8_t *__jasmin_syscall_randombytes__(uint8_t *x, uint64_t xlen) {
return x;
}


extern int crypto_sign_keypair_jazz(uint8_t *pk, uint8_t *sk);
extern int crypto_sign_jazz(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen, const uint8_t *sk);
extern int crypto_sign_open_jazz(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, const uint8_t *pk);
extern int crypto_sign_signature_jazz(uint8_t *sig, size_t *siglen, const uint8_t *m, size_t mlen, const uint8_t *sk);
extern int crypto_sign_verify_jazz(const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, const uint8_t *pk);

#define crypto_sign_keypair crypto_sign_keypair_jazz
#define crypto_sign crypto_sign_jazz
#define crypto_sign_open crypto_sign_open_jazz

int crypto_sign(unsigned char *sm, unsigned long long *smlen,
const unsigned char *m, unsigned long long mlen,
const unsigned char *sk)
{
size_t siglen;

crypto_sign_signature_jazz(sm, &siglen, m, (size_t)mlen, sk);

memmove(sm + SPX_BYTES, m, mlen);
*smlen = siglen + mlen;

return 0;
}

int crypto_sign_open(unsigned char *m, unsigned long long *mlen,
const unsigned char *sm, unsigned long long smlen,
const unsigned char *pk)
{
/* The API caller does not necessarily know what size a signature should be
but SPHINCS+ signatures are always exactly SPX_BYTES. */
if (smlen < SPX_BYTES) {
memset(m, 0, smlen);
*mlen = 0;
return -1;
}

*mlen = smlen - SPX_BYTES;

if (crypto_sign_verify_jazz(sm, SPX_BYTES, sm + SPX_BYTES, (size_t)*mlen, pk)) {
memset(m, 0, smlen);
*mlen = 0;
return -1;
}

/* If verification was successful, move the message to the right place. */
memmove(m, sm + SPX_BYTES, *mlen);

return 0;
}

int FindMarker(FILE *infile, const char *marker);
int ReadHex(FILE *infile, unsigned char *A, int Length, char *str);
Expand Down Expand Up @@ -137,25 +176,25 @@ int main(void) {
fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES);

if ((ret_val = crypto_sign(sm, &smlen, m, mlen, sk)) != 0) {
printf("crypto_sign returned <%d>\n", ret_val);
printf("[%s]: crypto_sign returned <%d>\n", xstr(PARAMS), ret_val);
return KAT_CRYPTO_FAILURE;
}
fprintf(fp_rsp, "smlen = %llu\n", smlen);
fprintBstr(fp_rsp, "sm = ", sm, smlen);
fprintf(fp_rsp, "\n");

if ((ret_val = crypto_sign_open(m1, &mlen1, sm, smlen, pk)) != 0) {
printf("crypto_sign_open returned <%d>\n", ret_val);
printf("[%s]: crypto_sign_open returned <%d>\n", xstr(PARAMS), ret_val);
return KAT_CRYPTO_FAILURE;
}

if (mlen != mlen1) {
printf("crypto_sign_open returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen1, mlen);
printf("[%s] crypto_sign_open returned bad 'mlen': Got <%llu>, expected <%llu>\n", xstr(PARAMS), mlen1, mlen);
return KAT_CRYPTO_FAILURE;
}

if (memcmp(m, m1, mlen)) {
printf("crypto_sign_open returned bad 'm' value\n");
printf("[%s] crypto_sign_open returned bad 'm' value\n", xstr(PARAMS));
return KAT_CRYPTO_FAILURE;
}

Expand Down
6 changes: 3 additions & 3 deletions ref-jasmin/test/kat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ CFLAGS ?= -w -g #-O3 -Wall -Wextra -Wpedantic -Werror -std=c99 \
# NOTE: -Wvla was removed for CFLAGS due to the usage of SPX_VLA

JASMIN ?= jasminc
JFLAGS ?= -nowarning -g# ${JADDFLAGS}# -wunusedvar -wduplicatevar
JFLAGS ?= -nowarning ${JADDFLAGS}
JPP ?= ../scripts/jpp
PREPROCESSOR ?= ../scripts/preprocessor

# ------------------------------------------------------------------------------

HASH_LIST := shake
PARAMS_LIST := 128f # 128s 192f 192s 256f 256s
PARAMS_LIST := 128f 192f 256f# 128s 192s 256s
THASH_LIST := simple robust

TLIST := $(foreach H,$(HASH_LIST), $(foreach P,$(PARAMS_LIST), $(foreach T,$(THASH_LIST), $(H)_$(P)_$(T))))
Expand Down Expand Up @@ -57,7 +57,7 @@ bin/PQC_sign_kat_jasmin_%.s: bin/PQC_sign_kat_jasmin_%.jpp
$(TESTS):
bin/PQC_sign_kat_jasmin_%: bin/PQC_sign_kat_jasmin_%.s | bin/
$(CC) $(CFLAGS) -o $@ -DPARAMS=sphincs-$(GET_HASH)-$(GET_PARAM) -DTHASH=$(GET_THASH) \
-I../common -I../../ -I../../params ../../PQCgenKAT_sign.c $< ../../rng.c -lcrypto
-I../common -I../../ -I../../params PQCgenKAT_sign.c rng.c $< -lcrypto

bin/PQC_sign_kat_jasmin_%.out: bin/PQC_sign_kat_jasmin_%
@./$<
Expand Down
Loading

0 comments on commit 49b6d62

Please sign in to comment.