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.

119 lines
3.3 KiB

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "../xmss.h"
  6. #include "../params.h"
  7. #include "../randombytes.h"
  8. #define MLEN 32
  9. #define SIGNATURES 16
  10. #ifdef XMSSMT
  11. #define XMSS_PARSE_OID xmssmt_parse_oid
  12. #define XMSS_KEYPAIR xmssmt_keypair
  13. #define XMSS_SIGN xmssmt_sign
  14. #define XMSS_SIGN_OPEN xmssmt_sign_open
  15. #define XMSS_VARIANT "XMSSMT-SHA2_20/2_256"
  16. #else
  17. #define XMSS_PARSE_OID xmss_parse_oid
  18. #define XMSS_KEYPAIR xmss_keypair
  19. #define XMSS_SIGN xmss_sign
  20. #define XMSS_SIGN_OPEN xmss_sign_open
  21. #define XMSS_VARIANT "XMSS-SHA2_10_256"
  22. #endif
  23. int main()
  24. {
  25. xmss_params params;
  26. // TODO test more different OIDs
  27. uint32_t oid = 0x01000001;
  28. int i, j;
  29. XMSS_PARSE_OID(&params, oid);
  30. unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
  31. unsigned char sk[XMSS_OID_LEN + params.sk_bytes];
  32. unsigned char *m = malloc(MLEN);
  33. unsigned char *sm = malloc(params.sig_bytes + MLEN);
  34. unsigned char *mout = malloc(params.sig_bytes + MLEN);
  35. unsigned long long smlen;
  36. unsigned long long mlen;
  37. randombytes(m, MLEN);
  38. XMSS_KEYPAIR(pk, sk, oid);
  39. printf("Testing %d %s signatures.. \n", SIGNATURES, XMSS_VARIANT);
  40. for (i = 0; i < SIGNATURES; i++) {
  41. printf(" - iteration #%d:\n", i);
  42. XMSS_SIGN(sk, sm, &smlen, m, MLEN);
  43. if (smlen != params.sig_bytes + MLEN) {
  44. printf(" X smlen incorrect [%llu != %u]!\n",
  45. smlen, params.sig_bytes);
  46. }
  47. else {
  48. printf(" smlen as expected [%llu].\n", smlen);
  49. }
  50. /* Test if signature is valid. */
  51. if (XMSS_SIGN_OPEN(mout, &mlen, sm, smlen, pk)) {
  52. printf(" X verification failed!\n");
  53. }
  54. else {
  55. printf(" verification succeeded.\n");
  56. }
  57. /* Test if the correct message was recovered. */
  58. if (mlen != MLEN) {
  59. printf(" X mlen incorrect [%llu != %u]!\n", mlen, MLEN);
  60. }
  61. else {
  62. printf(" mlen as expected [%llu].\n", mlen);
  63. }
  64. if (memcmp(m, mout, MLEN)) {
  65. printf(" X output message incorrect!\n");
  66. }
  67. else {
  68. printf(" output message as expected.\n");
  69. }
  70. /* Test if flipping bits invalidates the signature (it should). */
  71. /* Flip the first bit of the message. Should invalidate. */
  72. sm[smlen - 1] ^= 1;
  73. if (!XMSS_SIGN_OPEN(mout, &mlen, sm, smlen, pk)) {
  74. printf(" X flipping a bit of m DID NOT invalidate signature!\n");
  75. }
  76. else {
  77. printf(" flipping a bit of m invalidates signature.\n");
  78. }
  79. sm[smlen - 1] ^= 1;
  80. /* Flip one bit per hash; the signature is almost entirely hashes.
  81. This also flips a bit in the index, which is also a useful test. */
  82. for (j = 0; j < (int)(smlen - MLEN); j += params.n) {
  83. sm[j] ^= 1;
  84. if (!XMSS_SIGN_OPEN(mout, &mlen, sm, smlen, pk)) {
  85. printf(" X flipping bit %d DID NOT invalidate sig + m!\n", j);
  86. sm[j] ^= 1;
  87. break;
  88. }
  89. sm[j] ^= 1;
  90. }
  91. if (j >= (int)(smlen - MLEN)) {
  92. printf(" changing any signature hash invalidates signature.\n");
  93. }
  94. }
  95. free(m);
  96. free(sm);
  97. free(mout);
  98. return 0;
  99. }