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.

103 lines
2.7 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. #include "../utils.h"
  9. #define XMSS_MLEN 32
  10. // #ifndef XMSS_SIGNATURES
  11. // #define XMSS_SIGNATURES 16
  12. // #endif
  13. #ifdef XMSSMT
  14. #define XMSS_PARSE_OID xmssmt_parse_oid
  15. #define XMSS_STR_TO_OID xmssmt_str_to_oid
  16. #define XMSS_KEYPAIR xmssmt_keypair
  17. #define XMSS_SIGN xmssmt_sign
  18. #define XMSS_SIGN_OPEN xmssmt_sign_open
  19. #define XMSS_VARIANT "XMSSMT-SHA2_20/2_256"
  20. #define XMSS_SIGNATURES (1 << 20)
  21. #else
  22. #define XMSS_PARSE_OID xmss_parse_oid
  23. #define XMSS_STR_TO_OID xmss_str_to_oid
  24. #define XMSS_KEYPAIR xmss_keypair
  25. #define XMSS_SIGN xmss_sign
  26. #define XMSS_SIGN_OPEN xmss_sign_open
  27. #define XMSS_VARIANT "XMSS-SHA2_10_256"
  28. #define XMSS_SIGNATURES (1 << 10)
  29. #endif
  30. int main()
  31. {
  32. xmss_params params;
  33. uint32_t oid;
  34. int ret = 0;
  35. int return_code = 0;
  36. int i;
  37. // TODO test more different variants
  38. XMSS_STR_TO_OID(&oid, XMSS_VARIANT);
  39. XMSS_PARSE_OID(&params, oid);
  40. unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
  41. unsigned char sk[XMSS_OID_LEN + params.sk_bytes];
  42. unsigned char *m = malloc(XMSS_MLEN);
  43. unsigned char *sm = malloc(params.sig_bytes + XMSS_MLEN);
  44. unsigned char *mout = malloc(params.sig_bytes + XMSS_MLEN);
  45. unsigned long long smlen;
  46. unsigned long long idx;
  47. unsigned long long j;
  48. randombytes(m, XMSS_MLEN);
  49. XMSS_KEYPAIR(pk, sk, oid);
  50. printf("Testing %d %s signatures.. \n", XMSS_SIGNATURES, XMSS_VARIANT);
  51. for (i = 0; i < XMSS_SIGNATURES; i++) {
  52. if( (i & 1023) == 0)
  53. printf(" - iteration #%d:\n", i);
  54. return_code = XMSS_SIGN(sk, sm, &smlen, m, XMSS_MLEN);
  55. if (return_code != 0) {
  56. printf(" Error! Return code was %d\n",return_code);
  57. ret = -1;
  58. }
  59. }
  60. if(ret == 0)
  61. printf("As expected, return code was 0\n");
  62. for (; i < (XMSS_SIGNATURES) + 2; i++) {
  63. printf(" - iteration #%d:\n", i);
  64. return_code = XMSS_SIGN(sk, sm, &smlen, m, XMSS_MLEN);
  65. if (return_code == 0) {
  66. printf(" Error! Return code was %d\n",return_code);
  67. ret = -1;
  68. }
  69. else {
  70. printf("Return code as expected [%d].\n", return_code);
  71. }
  72. idx = (unsigned long)bytes_to_ull(sk, params.index_bytes);
  73. printf("Index: %llu\n", idx);
  74. printf("Secret key: %llu\n", idx);
  75. for (j = 0; j < XMSS_OID_LEN + params.sk_bytes;j++)
  76. printf("%d ", sk[j]);
  77. printf("\n");
  78. }
  79. free(m);
  80. free(sm);
  81. free(mout);
  82. return ret;
  83. }