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.

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