Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

159 righe
4.2 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "../xmss.h"
  5. #include "../params.h"
  6. #include "../randombytes.h"
  7. #define XMSS_MLEN 32
  8. #ifndef XMSS_SIGNATURES
  9. #define XMSS_SIGNATURES 16
  10. #endif
  11. #ifdef XMSSMT
  12. #define XMSS_PARSE_OID xmssmt_parse_oid
  13. #define XMSS_STR_TO_OID xmssmt_str_to_oid
  14. #define XMSS_KEYPAIR xmssmt_keypair
  15. #define XMSS_SIGN xmssmt_sign
  16. #define XMSS_SIGN_OPEN xmssmt_sign_open
  17. #else
  18. #define XMSS_PARSE_OID xmss_parse_oid
  19. #define XMSS_STR_TO_OID xmss_str_to_oid
  20. #define XMSS_KEYPAIR xmss_keypair
  21. #define XMSS_SIGN xmss_sign
  22. #define XMSS_SIGN_OPEN xmss_sign_open
  23. #endif
  24. #ifndef XMSS_VARIANT
  25. #ifdef XMSSMT
  26. #define XMSS_VARIANT "XMSSMT-SHA2_20/2_256"
  27. #else
  28. #define XMSS_VARIANT "XMSS-SHA2_10_256"
  29. #endif
  30. #endif
  31. static unsigned long long cpucycles(void)
  32. {
  33. unsigned long long result;
  34. __asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
  35. : "=a" (result) :: "%rdx");
  36. return result;
  37. }
  38. static int cmp_llu(const void *a, const void*b)
  39. {
  40. if (*(unsigned long long *)a < *(unsigned long long *)b) return -1;
  41. if (*(unsigned long long *)a > *(unsigned long long *)b) return 1;
  42. return 0;
  43. }
  44. static unsigned long long median(unsigned long long *l, size_t llen)
  45. {
  46. qsort(l, llen, sizeof(unsigned long long), cmp_llu);
  47. if (llen % 2) return l[llen / 2];
  48. else return (l[llen/2 - 1] + l[llen/2]) / 2;
  49. }
  50. static unsigned long long average(unsigned long long *t, size_t tlen)
  51. {
  52. unsigned long long acc=0;
  53. size_t i;
  54. for(i = 0; i < tlen; i++) {
  55. acc += t[i];
  56. }
  57. return acc/(tlen);
  58. }
  59. static void print_results(unsigned long long *t, size_t tlen)
  60. {
  61. size_t i;
  62. for (i = 0; i < tlen-1; i++) {
  63. t[i] = t[i+1] - t[i];
  64. }
  65. printf("\tmedian : %llu cycles\n", median(t, tlen));
  66. printf("\taverage : %llu cycles\n", average(t, tlen-1));
  67. printf("\n");
  68. }
  69. int main()
  70. {
  71. /* Make stdout buffer more responsive. */
  72. setbuf(stdout, NULL);
  73. xmss_params params;
  74. uint32_t oid;
  75. int ret = 0;
  76. int i;
  77. // TODO test more different variants
  78. if (XMSS_STR_TO_OID(&oid, XMSS_VARIANT)) {
  79. #ifdef XMSSMT
  80. printf("XMSSMT variant %s not recognized!\n", XMSS_VARIANT);
  81. #else
  82. printf("XMSS variant %s not recognized!\n", XMSS_VARIANT);
  83. #endif
  84. return -1;
  85. }
  86. XMSS_PARSE_OID(&params, oid);
  87. unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
  88. unsigned char sk[XMSS_OID_LEN + params.sk_bytes];
  89. unsigned char *m = malloc(XMSS_MLEN);
  90. unsigned char *sm = malloc(params.sig_bytes + XMSS_MLEN);
  91. unsigned char *mout = malloc(params.sig_bytes + XMSS_MLEN);
  92. unsigned long long smlen;
  93. unsigned long long mlen;
  94. unsigned long long t0, t1;
  95. unsigned long long *t = malloc(sizeof(unsigned long long) * XMSS_SIGNATURES);
  96. struct timespec start, stop;
  97. double result;
  98. randombytes(m, XMSS_MLEN);
  99. printf("Benchmarking variant %s\n", XMSS_VARIANT);
  100. printf("Generating keypair.. ");
  101. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  102. t0 = cpucycles();
  103. XMSS_KEYPAIR(pk, sk, oid);
  104. t1 = cpucycles();
  105. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
  106. result = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3;
  107. printf("took %lf us (%.2lf sec), %llu cycles\n", result, result / 1e6, t1 - t0);
  108. printf("Creating %d signatures..\n", XMSS_SIGNATURES);
  109. for (i = 0; i < XMSS_SIGNATURES; i++) {
  110. t[i] = cpucycles();
  111. XMSS_SIGN(sk, sm, &smlen, m, XMSS_MLEN);
  112. }
  113. print_results(t, XMSS_SIGNATURES);
  114. printf("Verifying %d signatures..\n", XMSS_SIGNATURES);
  115. for (i = 0; i < XMSS_SIGNATURES; i++) {
  116. t[i] = cpucycles();
  117. ret |= XMSS_SIGN_OPEN(mout, &mlen, sm, smlen, pk);
  118. }
  119. print_results(t, XMSS_SIGNATURES);
  120. if (ret) {
  121. printf("DETECTED VERIFICATION ERRORS!\n");
  122. }
  123. printf("Signature size: %d (%.2f KiB)\n", params.sig_bytes, params.sig_bytes / 1024.0);
  124. printf("Public key size: %d (%.2f KiB)\n", params.pk_bytes, params.pk_bytes / 1024.0);
  125. printf("Secret key size: %llu (%.2f KiB)\n", params.sk_bytes, params.sk_bytes / 1024.0);
  126. free(m);
  127. free(sm);
  128. free(mout);
  129. free(t);
  130. return ret;
  131. }