2015-08-21 14:06:07 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-08-27 08:39:06 +01:00
|
|
|
#include <stdlib.h>
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2017-08-01 14:20:30 +01:00
|
|
|
#include "../xmss_core_fast.h"
|
2017-06-02 13:10:24 +01:00
|
|
|
#include "../params.h"
|
|
|
|
#include "../randombytes.h"
|
2015-08-21 14:06:07 +01:00
|
|
|
|
|
|
|
#define MLEN 3491
|
2015-08-27 08:39:06 +01:00
|
|
|
#define SIGNATURES 256
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
unsigned long long t1, t2;
|
|
|
|
|
|
|
|
unsigned long long cpucycles(void)
|
|
|
|
{
|
|
|
|
unsigned long long result;
|
|
|
|
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a" (result) :: "%rdx");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-21 14:06:07 +01:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
unsigned long long i;
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned int k = XMSS_BDS_K;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
unsigned long errors = 0;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned char sk[4*XMSS_N+4];
|
|
|
|
unsigned char pk[2*XMSS_N];
|
2015-08-27 08:39:06 +01:00
|
|
|
|
|
|
|
// TODO should we hide this into xmss_fast.c and just allocate a large enough chunk of memory here?
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned char stack[(XMSS_TREEHEIGHT+1)*XMSS_N];
|
2015-08-27 08:39:06 +01:00
|
|
|
unsigned int stackoffset = 0;
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned char stacklevels[XMSS_TREEHEIGHT+1];
|
|
|
|
unsigned char auth[(XMSS_TREEHEIGHT)*XMSS_N];
|
|
|
|
unsigned char keep[(XMSS_TREEHEIGHT >> 1)*XMSS_N];
|
|
|
|
treehash_inst treehash[XMSS_TREEHEIGHT-k];
|
|
|
|
unsigned char th_nodes[(XMSS_TREEHEIGHT-k)*XMSS_N];
|
|
|
|
unsigned char retain[((1 << k) - k - 1)*XMSS_N];
|
2015-08-27 08:39:06 +01:00
|
|
|
bds_state s;
|
|
|
|
bds_state *state = &s;
|
2017-06-02 13:10:24 +01:00
|
|
|
for (i = 0; i < XMSS_TREEHEIGHT-k; i++)
|
|
|
|
treehash[i].node = &th_nodes[XMSS_N*i];
|
2015-09-21 12:24:42 +01:00
|
|
|
xmss_set_bds_state(state, stack, stackoffset, stacklevels, auth, keep, treehash, retain, 0);
|
2015-08-27 08:39:06 +01:00
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned long long signature_length = 4+XMSS_N+XMSS_WOTS_KEYSIZE+XMSS_TREEHEIGHT*XMSS_N;
|
|
|
|
unsigned char mi[MLEN];
|
2015-08-21 14:06:07 +01:00
|
|
|
unsigned char mo[MLEN+signature_length];
|
|
|
|
unsigned char sm[MLEN+signature_length];
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned long long smlen;
|
|
|
|
unsigned long long mlen;
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
randombytes(mi, MLEN);
|
2015-08-21 14:06:07 +01:00
|
|
|
|
|
|
|
printf("keypair\n");
|
2016-07-11 10:15:16 +01:00
|
|
|
t1 = cpucycles();
|
2017-08-01 14:20:30 +01:00
|
|
|
xmss_core_keypair(pk, sk, state);
|
2016-07-11 10:15:16 +01:00
|
|
|
t2 = cpucycles();
|
|
|
|
printf("cycles = %llu\n", (t2-t1));
|
|
|
|
double sec = (t2-t1)/3500000;
|
|
|
|
printf("ms = %f\n", sec);
|
2015-08-21 14:06:07 +01:00
|
|
|
// check pub_seed in SK
|
2017-06-02 13:10:24 +01:00
|
|
|
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-21 14:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check index
|
|
|
|
unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
|
2016-02-02 13:06:43 +00:00
|
|
|
if (idx) printf("\nidx != 0 %lu\n",idx);
|
|
|
|
|
|
|
|
for (i = 0; i < SIGNATURES; i++) {
|
2015-08-21 14:06:07 +01:00
|
|
|
printf("sign\n");
|
2017-08-01 14:20:30 +01:00
|
|
|
xmss_core_sign(sk, state, sm, &smlen, mi, MLEN);
|
2015-08-21 14:06:07 +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-26 11:00:06 +01:00
|
|
|
r = memcmp(mi, sm+signature_length,MLEN);
|
|
|
|
printf("%d\n", r);
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
/* Test valid signature */
|
2015-08-26 11:00:06 +01:00
|
|
|
printf("verify\n");
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-08-26 11:00:06 +01:00
|
|
|
printf("%d\n", r);
|
2016-02-02 13:06:43 +00:00
|
|
|
if (r != 0) errors++;
|
2015-08-26 11:00:06 +01:00
|
|
|
r = memcmp(mi,mo,MLEN);
|
|
|
|
printf("%d\n", r);
|
|
|
|
printf("%llu\n", MLEN-mlen);
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2015-08-26 11:00:06 +01:00
|
|
|
/* Test with modified message */
|
2015-10-28 10:08:36 +00:00
|
|
|
sm[signature_length+10] ^= 1;
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-08-26 11:00:06 +01:00
|
|
|
printf("%d\n", r+1);
|
2016-02-02 13:06:43 +00:00
|
|
|
if (r == 0) errors++;
|
2015-08-26 11:00:06 +01:00
|
|
|
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;
|
2015-08-26 11:00:06 +01:00
|
|
|
sm[2] ^= 1;
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-08-26 11:00:06 +01:00
|
|
|
printf("%d\n", r+1);
|
2016-02-02 13:06:43 +00:00
|
|
|
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);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
/* Modified R */
|
|
|
|
sm[2] ^= 1;
|
|
|
|
sm[5] ^= 1;
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-10-28 10:08:36 +00:00
|
|
|
printf("%d\n", r+1);
|
2016-02-02 13:06:43 +00:00
|
|
|
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);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
/* Modified OTS sig */
|
|
|
|
sm[5] ^= 1;
|
|
|
|
sm[240] ^= 1;
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-10-28 10:08:36 +00:00
|
|
|
printf("%d\n", r+1);
|
2016-02-02 13:06:43 +00:00
|
|
|
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);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
/* Modified AUTH */
|
|
|
|
sm[240] ^= 1;
|
|
|
|
sm[signature_length - 10] ^= 1;
|
2017-08-01 14:20:30 +01:00
|
|
|
r = xmss_core_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-10-28 10:08:36 +00:00
|
|
|
printf("%d\n", r+1);
|
2016-02-02 13:06:43 +00:00
|
|
|
if (r == 0) errors++;
|
2015-08-26 11:00:06 +01:00
|
|
|
r = memcmp(mi,mo,MLEN);
|
|
|
|
printf("%d\n", (r!=0) - 1);
|
|
|
|
printf("%llu\n", mlen+1);
|
|
|
|
}
|
2015-08-21 14:06:07 +01:00
|
|
|
|
2015-10-28 10:08:36 +00:00
|
|
|
printf("#errors = %lu\n", errors);
|
|
|
|
printf("finished loop\n");
|
2015-08-21 14:06:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|