您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

157 行
4.2 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "../xmss_fast.h"
  5. #define MLEN 3491
  6. #define SIGNATURES 256
  7. unsigned char mi[MLEN];
  8. unsigned long long smlen;
  9. unsigned long long mlen;
  10. unsigned long long t1, t2;
  11. unsigned long long cpucycles(void)
  12. {
  13. unsigned long long result;
  14. asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a" (result) :: "%rdx");
  15. return result;
  16. }
  17. int main()
  18. {
  19. int r;
  20. unsigned long long i;
  21. unsigned int n = 32;
  22. unsigned int h = 20;
  23. unsigned int w = 16;
  24. unsigned int k = 2;
  25. unsigned long errors = 0;
  26. unsigned char sk[4*n+4];
  27. unsigned char pk[2*n];
  28. xmss_params p;
  29. xmss_params *params = &p;
  30. xmss_set_params(params, n, h, w, k);
  31. // TODO should we hide this into xmss_fast.c and just allocate a large enough chunk of memory here?
  32. unsigned char stack[(h+1)*n];
  33. unsigned int stackoffset = 0;
  34. unsigned char stacklevels[h+1];
  35. unsigned char auth[(h)*n];
  36. unsigned char keep[(h >> 1)*n];
  37. treehash_inst treehash[h-k];
  38. unsigned char th_nodes[(h-k)*n];
  39. unsigned char retain[((1 << k) - k - 1)*n];
  40. bds_state s;
  41. bds_state *state = &s;
  42. for (i = 0; i < h-k; i++)
  43. treehash[i].node = &th_nodes[n*i];
  44. xmss_set_bds_state(state, stack, stackoffset, stacklevels, auth, keep, treehash, retain, 0);
  45. unsigned long long signature_length = 4+n+params->wots_par.keysize+h*n;
  46. unsigned char mo[MLEN+signature_length];
  47. unsigned char sm[MLEN+signature_length];
  48. FILE *urandom = fopen("/dev/urandom", "r");
  49. for (i = 0; i < MLEN; i++) mi[i] = fgetc(urandom);
  50. printf("keypair\n");
  51. t1 = cpucycles();
  52. xmss_keypair(pk, sk, state, params);
  53. t2 = cpucycles();
  54. printf("cycles = %llu\n", (t2-t1));
  55. double sec = (t2-t1)/3500000;
  56. printf("ms = %f\n", sec);
  57. int read;
  58. read = fgetc(stdin);
  59. // check pub_seed in SK
  60. for (i = 0; i < n; i++) {
  61. if (pk[n+i] != sk[4+2*n+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
  62. if (pk[i] != sk[4+3*n+i]) printf("pk.root != sk.root %llu",i);
  63. }
  64. // check index
  65. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  66. if (idx) printf("\nidx != 0 %lu\n",idx);
  67. for (i = 0; i < SIGNATURES; i++) {
  68. printf("sign\n");
  69. xmss_sign(sk, state, sm, &smlen, mi, MLEN, params);
  70. idx = ((unsigned long)sm[0] << 24) | ((unsigned long)sm[1] << 16) | ((unsigned long)sm[2] << 8) | sm[3];
  71. printf("\nidx = %lu\n",idx);
  72. r = memcmp(mi, sm+signature_length,MLEN);
  73. printf("%d\n", r);
  74. /* Test valid signature */
  75. printf("verify\n");
  76. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  77. printf("%d\n", r);
  78. if (r != 0) errors++;
  79. r = memcmp(mi,mo,MLEN);
  80. printf("%d\n", r);
  81. printf("%llu\n", MLEN-mlen);
  82. /* Test with modified message */
  83. sm[signature_length+10] ^= 1;
  84. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  85. printf("%d\n", r+1);
  86. if (r == 0) errors++;
  87. r = memcmp(mi,mo,MLEN);
  88. printf("%d\n", (r!=0) - 1);
  89. printf("%llu\n", mlen+1);
  90. /* Test with modified signature */
  91. /* Modified index */
  92. sm[signature_length+10] ^= 1;
  93. sm[2] ^= 1;
  94. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  95. printf("%d\n", r+1);
  96. if (r == 0) errors++;
  97. r = memcmp(mi,mo,MLEN);
  98. printf("%d\n", (r!=0) - 1);
  99. printf("%llu\n", mlen+1);
  100. /* Modified R */
  101. sm[2] ^= 1;
  102. sm[5] ^= 1;
  103. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  104. printf("%d\n", r+1);
  105. if (r == 0) errors++;
  106. r = memcmp(mi,mo,MLEN);
  107. printf("%d\n", (r!=0) - 1);
  108. printf("%llu\n", mlen+1);
  109. /* Modified OTS sig */
  110. sm[5] ^= 1;
  111. sm[240] ^= 1;
  112. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  113. printf("%d\n", r+1);
  114. if (r == 0) errors++;
  115. r = memcmp(mi,mo,MLEN);
  116. printf("%d\n", (r!=0) - 1);
  117. printf("%llu\n", mlen+1);
  118. /* Modified AUTH */
  119. sm[240] ^= 1;
  120. sm[signature_length - 10] ^= 1;
  121. r = xmss_sign_open(mo, &mlen, sm, smlen, pk, params);
  122. printf("%d\n", r+1);
  123. if (r == 0) errors++;
  124. r = memcmp(mi,mo,MLEN);
  125. printf("%d\n", (r!=0) - 1);
  126. printf("%llu\n", mlen+1);
  127. }
  128. printf("#errors = %lu\n", errors);
  129. printf("finished loop\n");
  130. fclose(urandom);
  131. printf("closed urandom\n");
  132. return 0;
  133. }