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 <stdint.h>
  4. #include "../xmss.h"
  5. #include "../params.h"
  6. #include "../randombytes.h"
  7. #define MLEN 3491
  8. #define SIGNATURES 5
  9. unsigned char mi[MLEN];
  10. unsigned long long smlen;
  11. unsigned long long mlen;
  12. int main()
  13. {
  14. xmss_params params;
  15. // TODO test more different OIDs
  16. uint32_t oid = 0x01000001;
  17. xmssmt_parse_oid(&params, oid);
  18. int r;
  19. unsigned long long i,j;
  20. unsigned char sk[XMSS_OID_LEN + params.privatekey_bytes];
  21. unsigned char pk[XMSS_OID_LEN + params.publickey_bytes];
  22. unsigned char mo[MLEN+params.bytes];
  23. unsigned char sm[MLEN+params.bytes];
  24. printf("keypair\n");
  25. xmssmt_keypair(pk, sk, oid);
  26. // check pub_seed in SK
  27. for (i = 0; i < params.n; i++) {
  28. if (pk[XMSS_OID_LEN+params.n+i] != sk[XMSS_OID_LEN+params.index_len+2*params.n+i]) printf("pk.pub_seed != sk.pub_seed %llu",i);
  29. if (pk[XMSS_OID_LEN+i] != sk[XMSS_OID_LEN+params.index_len+3*params.n+i]) printf("pk.root != sk.root %llu",i);
  30. }
  31. printf("pk checked\n");
  32. // check index
  33. unsigned long long idx = 0;
  34. for (i = 0; i < params.index_len; i++) {
  35. idx |= ((unsigned long long)sk[i + XMSS_OID_LEN]) << 8*(params.index_len - 1 - i);
  36. }
  37. if (idx) printf("\nidx != 0: %llu\n",idx);
  38. for (i = 0; i < SIGNATURES; i++) {
  39. randombytes(mi, MLEN);
  40. printf("sign\n");
  41. xmssmt_sign(sk, sm, &smlen, mi, MLEN);
  42. idx = 0;
  43. for (j = 0; j < params.index_len; j++) {
  44. idx += ((unsigned long long)sm[j]) << 8*(params.index_len - 1 - j);
  45. }
  46. printf("\nidx = %llu\n",idx);
  47. r = memcmp(mi, sm+params.bytes,MLEN);
  48. printf("%d\n", r);
  49. for (j = 0; j < smlen; j++) {
  50. printf("%02X", sm[j]);
  51. }
  52. printf("\n");
  53. /* Test valid signature */
  54. printf("verify\n");
  55. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  56. printf("%d\n", r);
  57. r = memcmp(mi,mo,MLEN);
  58. printf("%d\n", r);
  59. printf("%llu\n", MLEN-mlen);
  60. /* Test with modified message */
  61. sm[52] ^= 1;
  62. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  63. printf("%d\n", r+1);
  64. r = memcmp(mi,mo,MLEN);
  65. printf("%d\n", (r!=0) - 1);
  66. printf("%llu\n", mlen+1);
  67. /* Test with modified signature */
  68. sm[260] ^= 1;
  69. sm[52] ^= 1;
  70. sm[2] ^= 1;
  71. r = xmssmt_sign_open(mo, &mlen, sm, smlen, pk);
  72. printf("%d\n", r+1);
  73. r = memcmp(mi,mo,MLEN);
  74. printf("%d\n", (r!=0) - 1);
  75. printf("%llu\n", mlen+1);
  76. }
  77. return 0;
  78. }