You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.5 KiB

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