Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

134 рядки
3.6 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "../xmss_fast.h"
  4. #include "../params.h"
  5. #include "../randombytes.h"
  6. #define MLEN 3491
  7. #define SIGNATURES 128
  8. unsigned char mi[MLEN];
  9. unsigned long long smlen;
  10. unsigned long long mlen;
  11. unsigned long long t1, t2;
  12. unsigned long long cpucycles(void)
  13. {
  14. unsigned long long result;
  15. asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a" (result) :: "%rdx");
  16. return result;
  17. }
  18. int main()
  19. {
  20. int r;
  21. unsigned long long i,j;
  22. unsigned int n = XMSS_N;
  23. unsigned int h = XMSS_FULLHEIGHT;
  24. unsigned int d = XMSS_D;
  25. unsigned int k = XMSS_BDS_K;
  26. unsigned int tree_h = h / d;
  27. // stack needs to be larger than regular (H-K-1), since we re-use for 'next'
  28. unsigned char stack[(2*d-1) * (tree_h + 1)*n];
  29. unsigned char stacklevels[(2*d-1) * (tree_h + 1)*n];
  30. unsigned char auth[(2*d-1) * tree_h*n];
  31. unsigned char keep[(2*d-1) * (tree_h >> 1)*n];
  32. treehash_inst treehash[(2*d-1) * (tree_h-k)];
  33. unsigned char th_nodes[(2*d-1) * (tree_h-k)*n];
  34. unsigned char retain[(2*d-1) * ((1 << k) - k - 1)*n];
  35. unsigned char wots_sigs[d * XMSS_WOTS_KEYSIZE];
  36. // first d are 'regular' states, second d are 'next'; top tree has no 'next'
  37. bds_state states[2*d-1];
  38. for (i = 0; i < 2*d-1; i++) {
  39. for (j = 0; j < tree_h-k; j++)
  40. treehash[i*(tree_h-k) + j].node = th_nodes + (i*(tree_h-k) + j) * n;
  41. xmss_set_bds_state(states + i,
  42. stack + i*(tree_h + 1)*n, 0, stacklevels + i*(tree_h + 1),
  43. auth + i*tree_h*n,
  44. keep + i*(tree_h >> 1)*n,
  45. treehash + i*(tree_h-k),
  46. retain + i*((1 << k) - k - 1)*n,
  47. 0
  48. );
  49. }
  50. unsigned char sk[(XMSS_INDEX_LEN+4*n)];
  51. unsigned char pk[2*n];
  52. unsigned long long signature_length = XMSS_INDEX_LEN + n + (d*XMSS_WOTS_KEYSIZE) + h*n;
  53. unsigned char mo[MLEN+signature_length];
  54. unsigned char sm[MLEN+signature_length];
  55. randombytes(mi, MLEN);
  56. printf("keypair\n");
  57. xmssmt_keypair(pk, sk, states, wots_sigs);
  58. // check pub_seed in SK
  59. for (i = 0; i < n; i++) {
  60. if (pk[n+i] != sk[XMSS_INDEX_LEN+2*n+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
  61. if (pk[i] != sk[XMSS_INDEX_LEN+3*n+i]) printf("pk.root != sk.root %llu",i);
  62. }
  63. printf("pk checked\n");
  64. unsigned int idx_len = XMSS_INDEX_LEN;
  65. // check index
  66. unsigned long long idx = 0;
  67. for (i = 0; i < idx_len; i++) {
  68. idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
  69. }
  70. if (idx) printf("\nidx != 0: %llu\n",idx);
  71. for (i = 0; i < SIGNATURES; i++) {
  72. printf("sign\n");
  73. t1 = cpucycles();
  74. xmssmt_sign(sk, states, wots_sigs, sm, &smlen, mi, MLEN);
  75. t2 = cpucycles();
  76. printf("signing cycles = %llu\n", (t2-t1));
  77. idx = 0;
  78. for (j = 0; j < idx_len; j++) {
  79. idx += ((unsigned long long)sm[j]) << 8*(idx_len - 1 - j);
  80. }
  81. printf("\nidx = %llu\n",idx);
  82. r = memcmp(mi, sm+signature_length,MLEN);
  83. printf("%d\n", r);
  84. /* Test valid signature */
  85. printf("verify\n");
  86. t1 = cpucycles();
  87. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  88. t2 = cpucycles();
  89. printf("verification cycles = %llu\n", (t2-t1));
  90. printf("%d\n", r);
  91. r = memcmp(mi,mo,MLEN);
  92. printf("%d\n", r);
  93. printf("%llu\n", MLEN-mlen);
  94. /* Test with modified message */
  95. sm[52] ^= 1;
  96. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  97. printf("%d\n", r+1);
  98. r = memcmp(mi,mo,MLEN);
  99. printf("%d\n", (r!=0) - 1);
  100. printf("%llu\n", mlen+1);
  101. /* Test with modified signature */
  102. sm[260] ^= 1;
  103. sm[52] ^= 1;
  104. sm[2] ^= 1;
  105. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  106. printf("%d\n", r+1);
  107. r = memcmp(mi,mo,MLEN);
  108. printf("%d\n", (r!=0) - 1);
  109. printf("%llu\n", mlen+1);
  110. }
  111. return 0;
  112. }