No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

111 líneas
3.2 KiB

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