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.

93 line
2.2 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 5
  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 char sk[(XMSS_INDEX_LEN+4*XMSS_N)];
  16. unsigned char pk[2*XMSS_N];
  17. unsigned long long signature_length = XMSS_INDEX_LEN + XMSS_N + (XMSS_D*XMSS_WOTS_KEYSIZE) + XMSS_FULLHEIGHT*XMSS_N;
  18. unsigned char mo[MLEN+signature_length];
  19. unsigned char sm[MLEN+signature_length];
  20. printf("keypair\n");
  21. xmssmt_keypair(pk, sk);
  22. // check pub_seed in SK
  23. for (i = 0; i < XMSS_N; i++) {
  24. if (pk[XMSS_N+i] != sk[XMSS_INDEX_LEN+2*XMSS_N+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
  25. if (pk[i] != sk[XMSS_INDEX_LEN+3*XMSS_N+i]) printf("pk.root != sk.root %llu",i);
  26. }
  27. printf("pk checked\n");
  28. unsigned int idx_len = XMSS_INDEX_LEN;
  29. // check index
  30. unsigned long long idx = 0;
  31. for (i = 0; i < idx_len; i++) {
  32. idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
  33. }
  34. if (idx) printf("\nidx != 0: %llu\n",idx);
  35. for (i = 0; i < SIGNATURES; i++) {
  36. randombytes(mi, MLEN);
  37. printf("sign\n");
  38. xmssmt_sign(sk, sm, &smlen, mi, MLEN);
  39. idx = 0;
  40. for (j = 0; j < idx_len; j++) {
  41. idx += ((unsigned long long)sm[j]) << 8*(idx_len - 1 - j);
  42. }
  43. printf("\nidx = %llu\n",idx);
  44. r = memcmp(mi, sm+signature_length,MLEN);
  45. printf("%d\n", r);
  46. for (j = 0; j < smlen; j++) {
  47. printf("%02X", sm[j]);
  48. }
  49. printf("\n");
  50. /* Test valid signature */
  51. printf("verify\n");
  52. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  53. printf("%d\n", r);
  54. r = memcmp(mi,mo,MLEN);
  55. printf("%d\n", r);
  56. printf("%llu\n", MLEN-mlen);
  57. /* Test with modified message */
  58. sm[52] ^= 1;
  59. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  60. printf("%d\n", r+1);
  61. r = memcmp(mi,mo,MLEN);
  62. printf("%d\n", (r!=0) - 1);
  63. printf("%llu\n", mlen+1);
  64. /* Test with modified signature */
  65. sm[260] ^= 1;
  66. sm[52] ^= 1;
  67. sm[2] ^= 1;
  68. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  69. printf("%d\n", r+1);
  70. r = memcmp(mi,mo,MLEN);
  71. printf("%d\n", (r!=0) - 1);
  72. printf("%llu\n", mlen+1);
  73. }
  74. return 0;
  75. }