xmss-KAT-generator/test/test_xmss_core.c

121 lines
3.0 KiB
C
Raw Normal View History

2015-08-11 11:08:27 +01:00
#include <stdio.h>
#include <string.h>
#include "../xmss_core.h"
#include "../params.h"
#include "../randombytes.h"
2015-08-11 11:08:27 +01:00
#define MLEN 3491
#define SIGNATURES 5
2015-08-11 11:08:27 +01:00
unsigned char mi[MLEN];
unsigned long long smlen;
unsigned long long mlen;
int main()
{
int r;
unsigned long long i, j;
2015-10-28 10:08:36 +00:00
unsigned long errors = 0;
unsigned char sk[4*XMSS_N+4];
unsigned char pk[2*XMSS_N];
2015-10-28 14:49:46 +00:00
unsigned long long signature_length = 4+XMSS_N+XMSS_WOTS_KEYSIZE+XMSS_TREEHEIGHT*XMSS_N;
2015-08-11 11:08:27 +01:00
unsigned char mo[MLEN+signature_length];
unsigned char sm[MLEN+signature_length];
printf("keypair\n");
xmss_core_keypair(pk, sk);
2015-08-11 11:08:27 +01:00
// check pub_seed in SK
for (i = 0; i < XMSS_N; i++) {
if (pk[XMSS_N+i] != sk[4+2*XMSS_N+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
if (pk[i] != sk[4+3*XMSS_N+i]) printf("pk.root != sk.root %llu",i);
2015-08-11 11:08:27 +01:00
}
// check index
2015-08-12 13:37:49 +01:00
unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
if (idx) printf("\nidx != 0 %lu\n",idx);
for (i = 0; i < SIGNATURES; i++) {
randombytes(mi, MLEN);
2015-08-12 13:37:49 +01:00
printf("sign\n");
xmss_core_sign(sk, sm, &smlen, mi, MLEN);
2015-08-12 13:37:49 +01:00
idx = ((unsigned long)sm[0] << 24) | ((unsigned long)sm[1] << 16) | ((unsigned long)sm[2] << 8) | sm[3];
printf("\nidx = %lu\n",idx);
2015-08-11 11:08:27 +01:00
for (j = 0; j < smlen; j++) {
printf("%02X", sm[j]);
}
printf("\n");
r = memcmp(mi, sm+signature_length,MLEN);
printf("%d\n", r);
2015-08-11 11:08:27 +01:00
/* Test valid signature */
printf("verify\n");
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
printf("%d\n", r);
if (r != 0) errors++;
r = memcmp(mi,mo,MLEN);
printf("%d\n", r);
printf("%llu\n", MLEN-mlen);
2015-08-11 11:08:27 +01:00
/* Test with modified message */
2015-10-28 10:08:36 +00:00
sm[signature_length+10] ^= 1;
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
printf("%d\n", r+1);
if (r == 0) errors++;
r = memcmp(mi,mo,MLEN);
printf("%d\n", (r!=0) - 1);
printf("%llu\n", mlen+1);
/* Test with modified signature */
2015-10-28 10:08:36 +00:00
/* Modified index */
2015-10-28 14:49:46 +00:00
sm[signature_length+10] ^= 1;
sm[2] ^= 1;
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
printf("%d\n", r+1);
if (r == 0) errors++;
2015-10-28 10:08:36 +00:00
r = memcmp(mi,mo,MLEN);
printf("%d\n", (r!=0) - 1);
printf("%llu\n", mlen+1);
2015-10-28 10:08:36 +00:00
/* Modified R */
sm[2] ^= 1;
sm[5] ^= 1;
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
2015-10-28 10:08:36 +00:00
printf("%d\n", r+1);
if (r == 0) errors++;
2015-10-28 10:08:36 +00:00
r = memcmp(mi,mo,MLEN);
printf("%d\n", (r!=0) - 1);
printf("%llu\n", mlen+1);
2015-10-28 10:08:36 +00:00
/* Modified OTS sig */
sm[5] ^= 1;
sm[240] ^= 1;
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
2015-10-28 10:08:36 +00:00
printf("%d\n", r+1);
if (r == 0) errors++;
2015-10-28 10:08:36 +00:00
r = memcmp(mi,mo,MLEN);
printf("%d\n", (r!=0) - 1);
printf("%llu\n", mlen+1);
2015-10-28 10:08:36 +00:00
/* Modified AUTH */
sm[240] ^= 1;
sm[signature_length - 10] ^= 1;
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
2015-10-28 10:08:36 +00:00
printf("%d\n", r+1);
if (r == 0) errors++;
r = memcmp(mi,mo,MLEN);
printf("%d\n", (r!=0) - 1);
printf("%llu\n", mlen+1);
}
2015-08-11 11:08:27 +01:00
2015-10-28 10:08:36 +00:00
printf("#errors = %lu\n", errors);
2015-08-11 11:08:27 +01:00
return 0;
}