2015-09-21 12:24:42 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../xmss_fast.h"
|
2017-06-02 13:10:24 +01:00
|
|
|
#include "../params.h"
|
|
|
|
#include "../randombytes.h"
|
2015-09-21 12:24:42 +01:00
|
|
|
|
|
|
|
#define MLEN 3491
|
2017-06-02 13:10:24 +01:00
|
|
|
#define SIGNATURES 128
|
2015-09-21 12:24:42 +01:00
|
|
|
|
|
|
|
unsigned char mi[MLEN];
|
|
|
|
unsigned long long smlen;
|
|
|
|
unsigned long long mlen;
|
|
|
|
|
2017-06-02 13:10:24 +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-09-21 12:24:42 +01:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
unsigned long long i,j;
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned int n = XMSS_N;
|
|
|
|
unsigned int h = XMSS_FULLHEIGHT;
|
|
|
|
unsigned int d = XMSS_D;
|
|
|
|
unsigned int k = XMSS_BDS_K;
|
2015-09-21 12:24:42 +01:00
|
|
|
|
2015-10-05 20:59:50 +01:00
|
|
|
unsigned int tree_h = h / d;
|
|
|
|
|
|
|
|
// stack needs to be larger than regular (H-K-1), since we re-use for 'next'
|
2016-02-01 16:21:09 +00:00
|
|
|
unsigned char stack[(2*d-1) * (tree_h + 1)*n];
|
|
|
|
unsigned char stacklevels[(2*d-1) * (tree_h + 1)*n];
|
|
|
|
unsigned char auth[(2*d-1) * tree_h*n];
|
|
|
|
unsigned char keep[(2*d-1) * (tree_h >> 1)*n];
|
|
|
|
treehash_inst treehash[(2*d-1) * (tree_h-k)];
|
|
|
|
unsigned char th_nodes[(2*d-1) * (tree_h-k)*n];
|
|
|
|
unsigned char retain[(2*d-1) * ((1 << k) - k - 1)*n];
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned char wots_sigs[d * XMSS_WOTS_KEYSIZE];
|
2016-02-01 16:21:09 +00:00
|
|
|
// first d are 'regular' states, second d are 'next'; top tree has no 'next'
|
|
|
|
bds_state states[2*d-1];
|
2015-09-21 12:24:42 +01:00
|
|
|
|
2016-02-01 16:21:09 +00:00
|
|
|
for (i = 0; i < 2*d-1; i++) {
|
2016-02-02 13:06:43 +00:00
|
|
|
for (j = 0; j < tree_h-k; j++)
|
2015-10-05 20:59:50 +01:00
|
|
|
treehash[i*(tree_h-k) + j].node = th_nodes + (i*(tree_h-k) + j) * n;
|
2015-09-21 12:24:42 +01:00
|
|
|
xmss_set_bds_state(states + i,
|
2015-10-05 20:59:50 +01:00
|
|
|
stack + i*(tree_h + 1)*n, 0, stacklevels + i*(tree_h + 1),
|
|
|
|
auth + i*tree_h*n,
|
|
|
|
keep + i*(tree_h >> 1)*n,
|
|
|
|
treehash + i*(tree_h-k),
|
2015-09-21 12:24:42 +01:00
|
|
|
retain + i*((1 << k) - k - 1)*n,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned char sk[(XMSS_INDEX_LEN+4*n)];
|
2015-09-21 12:24:42 +01:00
|
|
|
unsigned char pk[2*n];
|
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned long long signature_length = XMSS_INDEX_LEN + n + (d*XMSS_WOTS_KEYSIZE) + h*n;
|
2015-09-21 12:24:42 +01:00
|
|
|
unsigned char mo[MLEN+signature_length];
|
|
|
|
unsigned char sm[MLEN+signature_length];
|
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
randombytes(mi, MLEN);
|
2015-09-21 12:24:42 +01:00
|
|
|
|
|
|
|
printf("keypair\n");
|
2017-06-02 13:10:24 +01:00
|
|
|
xmssmt_keypair(pk, sk, states, wots_sigs);
|
2015-09-21 12:24:42 +01:00
|
|
|
// check pub_seed in SK
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2017-06-02 13:10:24 +01:00
|
|
|
if (pk[n+i] != sk[XMSS_INDEX_LEN+2*n+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
|
|
|
|
if (pk[i] != sk[XMSS_INDEX_LEN+3*n+i]) printf("pk.root != sk.root %llu",i);
|
2015-09-21 12:24:42 +01:00
|
|
|
}
|
|
|
|
printf("pk checked\n");
|
|
|
|
|
2017-06-02 13:10:24 +01:00
|
|
|
unsigned int idx_len = XMSS_INDEX_LEN;
|
2015-09-21 12:24:42 +01:00
|
|
|
// check index
|
|
|
|
unsigned long long idx = 0;
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i = 0; i < idx_len; i++) {
|
2015-09-21 12:24:42 +01:00
|
|
|
idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
|
|
|
|
}
|
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
if (idx) printf("\nidx != 0: %llu\n",idx);
|
2015-09-21 12:24:42 +01:00
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i = 0; i < SIGNATURES; i++) {
|
2015-09-21 12:24:42 +01:00
|
|
|
printf("sign\n");
|
2017-06-02 13:10:24 +01:00
|
|
|
t1 = cpucycles();
|
|
|
|
xmssmt_sign(sk, states, wots_sigs, sm, &smlen, mi, MLEN);
|
|
|
|
t2 = cpucycles();
|
|
|
|
printf("signing cycles = %llu\n", (t2-t1));
|
2015-09-21 12:24:42 +01:00
|
|
|
|
|
|
|
idx = 0;
|
2016-02-02 13:06:43 +00:00
|
|
|
for (j = 0; j < idx_len; j++) {
|
2015-09-21 12:24:42 +01:00
|
|
|
idx += ((unsigned long long)sm[j]) << 8*(idx_len - 1 - j);
|
|
|
|
}
|
|
|
|
printf("\nidx = %llu\n",idx);
|
|
|
|
r = memcmp(mi, sm+signature_length,MLEN);
|
|
|
|
printf("%d\n", r);
|
|
|
|
|
|
|
|
/* Test valid signature */
|
|
|
|
printf("verify\n");
|
2017-06-02 13:10:24 +01:00
|
|
|
t1 = cpucycles();
|
|
|
|
r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
|
|
|
|
t2 = cpucycles();
|
|
|
|
printf("verification cycles = %llu\n", (t2-t1));
|
2015-09-21 12:24:42 +01:00
|
|
|
printf("%d\n", r);
|
|
|
|
r = memcmp(mi,mo,MLEN);
|
|
|
|
printf("%d\n", r);
|
|
|
|
printf("%llu\n", MLEN-mlen);
|
|
|
|
|
|
|
|
/* Test with modified message */
|
|
|
|
sm[52] ^= 1;
|
2017-06-02 13:10:24 +01:00
|
|
|
r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-09-21 12:24:42 +01:00
|
|
|
printf("%d\n", r+1);
|
|
|
|
r = memcmp(mi,mo,MLEN);
|
|
|
|
printf("%d\n", (r!=0) - 1);
|
|
|
|
printf("%llu\n", mlen+1);
|
|
|
|
|
|
|
|
/* Test with modified signature */
|
|
|
|
sm[260] ^= 1;
|
|
|
|
sm[52] ^= 1;
|
|
|
|
sm[2] ^= 1;
|
2017-06-02 13:10:24 +01:00
|
|
|
r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
|
2015-09-21 12:24:42 +01:00
|
|
|
printf("%d\n", r+1);
|
|
|
|
r = memcmp(mi,mo,MLEN);
|
|
|
|
printf("%d\n", (r!=0) - 1);
|
|
|
|
printf("%llu\n", mlen+1);
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|