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.2 KiB

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