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.

121 lines
3.0 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "../xmss.h"
  4. #include "../params.h"
  5. #include "../randombytes.h"
  6. #define MLEN 3491
  7. #define SIGNATURES 50
  8. unsigned char mi[MLEN];
  9. unsigned long long smlen;
  10. unsigned long long mlen;
  11. int main()
  12. {
  13. int r;
  14. unsigned long long i, j;
  15. unsigned long errors = 0;
  16. unsigned char sk[4*XMSS_N+4];
  17. unsigned char pk[2*XMSS_N];
  18. unsigned long long signature_length = 4+XMSS_N+XMSS_WOTS_KEYSIZE+XMSS_TREEHEIGHT*XMSS_N;
  19. unsigned char mo[MLEN+signature_length];
  20. unsigned char sm[MLEN+signature_length];
  21. printf("keypair\n");
  22. xmss_keypair(pk, sk);
  23. // check pub_seed in SK
  24. for (i = 0; i < XMSS_N; i++) {
  25. if (pk[XMSS_N+i] != sk[4+2*XMSS_N+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
  26. if (pk[i] != sk[4+3*XMSS_N+i]) printf("pk.root != sk.root %llu",i);
  27. }
  28. // check index
  29. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  30. if (idx) printf("\nidx != 0 %lu\n",idx);
  31. for (i = 0; i < SIGNATURES; i++) {
  32. randombytes(mi, MLEN);
  33. printf("sign\n");
  34. xmss_sign(sk, sm, &smlen, mi, MLEN);
  35. idx = ((unsigned long)sm[0] << 24) | ((unsigned long)sm[1] << 16) | ((unsigned long)sm[2] << 8) | sm[3];
  36. printf("\nidx = %lu\n",idx);
  37. for (j = 0; j < smlen; j++) {
  38. printf("%02X", sm[j]);
  39. }
  40. printf("\n");
  41. r = memcmp(mi, sm+signature_length,MLEN);
  42. printf("%d\n", r);
  43. /* Test valid signature */
  44. printf("verify\n");
  45. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  46. printf("%d\n", r);
  47. if (r != 0) errors++;
  48. r = memcmp(mi,mo,MLEN);
  49. printf("%d\n", r);
  50. printf("%llu\n", MLEN-mlen);
  51. /* Test with modified message */
  52. sm[signature_length+10] ^= 1;
  53. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  54. printf("%d\n", r+1);
  55. if (r == 0) errors++;
  56. r = memcmp(mi,mo,MLEN);
  57. printf("%d\n", (r!=0) - 1);
  58. printf("%llu\n", mlen+1);
  59. /* Test with modified signature */
  60. /* Modified index */
  61. sm[signature_length+10] ^= 1;
  62. sm[2] ^= 1;
  63. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  64. printf("%d\n", r+1);
  65. if (r == 0) errors++;
  66. r = memcmp(mi,mo,MLEN);
  67. printf("%d\n", (r!=0) - 1);
  68. printf("%llu\n", mlen+1);
  69. /* Modified R */
  70. sm[2] ^= 1;
  71. sm[5] ^= 1;
  72. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  73. printf("%d\n", r+1);
  74. if (r == 0) errors++;
  75. r = memcmp(mi,mo,MLEN);
  76. printf("%d\n", (r!=0) - 1);
  77. printf("%llu\n", mlen+1);
  78. /* Modified OTS sig */
  79. sm[5] ^= 1;
  80. sm[240] ^= 1;
  81. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  82. printf("%d\n", r+1);
  83. if (r == 0) errors++;
  84. r = memcmp(mi,mo,MLEN);
  85. printf("%d\n", (r!=0) - 1);
  86. printf("%llu\n", mlen+1);
  87. /* Modified AUTH */
  88. sm[240] ^= 1;
  89. sm[signature_length - 10] ^= 1;
  90. r = xmss_sign_open(mo, &mlen, sm, smlen, pk);
  91. printf("%d\n", r+1);
  92. if (r == 0) errors++;
  93. r = memcmp(mi,mo,MLEN);
  94. printf("%d\n", (r!=0) - 1);
  95. printf("%llu\n", mlen+1);
  96. }
  97. printf("#errors = %lu\n", errors);
  98. return 0;
  99. }