sm3speed.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <gmssl/sm3.h>
  14. #include <gmssl/hex.h>
  15. #include <gmssl/error.h>
  16. #ifdef WIN32
  17. #include <wincrypt.h>
  18. static volatile int finish;
  19. VOID CALLBACK TimerProc_sm3(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  20. {
  21. finish = 0;
  22. }
  23. int test_sm3()
  24. {
  25. int sizebox[] = {16, 64, 256, 1024, 8192, 16384};
  26. int countbox[6] = {0};
  27. uint8_t **testhex;
  28. HCRYPTPROV hCryptProv;
  29. testhex = (uint8_t **)malloc(sizeof(uint8_t *) * 6);
  30. for (int i = 0; i < 6; i++)
  31. {
  32. testhex[i] = (uint8_t *)malloc(sizebox[i]);
  33. CryptGenRandom(hCryptProv, sizebox[i], testhex[i]);
  34. }
  35. uint8_t dgst[32];
  36. int count;
  37. for (int i = 0; i < 6; i++)
  38. {
  39. finish = 1;
  40. count = 0;
  41. printf("Doing sm3 for 3s on %d size blocks: ", sizebox[i]);
  42. UINT_PTR iTimerID = SetTimer(NULL, 0, 3000, TimerProc_sm3);
  43. while (finish)
  44. {
  45. sm3_digest(testhex[i], sizebox[i], dgst);
  46. count++;
  47. }
  48. KillTimer(NULL, iTimerID);
  49. countbox[i] = count;
  50. printf("%d sm3's in 3s\n", count);
  51. }
  52. printf("type\t\t16 bytes\t64 bytes\t256 bytes\t1024 bytes\t8192 bytes\t16384 bytes\n");
  53. printf("sm3\t");
  54. for (int i = 0; i < 6; i++)
  55. {
  56. printf("\t%.2fK", countbox[i] * sizebox[i] / 1024 / 3.00);
  57. }
  58. printf("\n");
  59. for (int i = 0; i < 6; i++)
  60. {
  61. free(testhex[i]);
  62. }
  63. free(testhex);
  64. return 1;
  65. }
  66. #else
  67. #include <signal.h>
  68. #include <sys/time.h>
  69. static volatile int finish;
  70. void sig_alm_handler_sm3(int sig_num)
  71. {
  72. if (sig_num = SIGALRM)
  73. finish = 0;
  74. }
  75. int test_sm3()
  76. {
  77. int sizebox[] = {16, 64, 256, 1024, 8192, 16384};
  78. int countbox[6] = {0};
  79. uint8_t **testhex;
  80. FILE *fs_p = fopen("/dev/urandom", "r");
  81. if (NULL == fs_p)
  82. {
  83. printf("Can not open /dev/urandom\n");
  84. return -1;
  85. }
  86. testhex = (uint8_t **)malloc(sizeof(uint8_t *) * 6);
  87. for (int i = 0; i < 6; i++)
  88. {
  89. testhex[i] = (uint8_t *)malloc(sizebox[i]);
  90. fread(testhex[i], sizebox[i], 1, fs_p);
  91. }
  92. fclose(fs_p);
  93. uint8_t dgst[32];
  94. int count;
  95. signal(SIGALRM, sig_alm_handler_sm3);
  96. struct itimerval new_value, old_value;
  97. new_value.it_value.tv_sec = 3;
  98. new_value.it_value.tv_usec = 0;
  99. new_value.it_interval.tv_sec = 0;
  100. new_value.it_interval.tv_usec = 0;
  101. for (int i = 0; i < 6; i++)
  102. {
  103. finish = 1;
  104. count = 0;
  105. printf("Doing sm3 for 3s on %d size blocks: ", sizebox[i]);
  106. setitimer(ITIMER_REAL, &new_value, &old_value);
  107. while (finish)
  108. {
  109. sm3_digest(testhex[i], sizebox[i], dgst);
  110. count++;
  111. }
  112. countbox[i] = count;
  113. printf("%d sm3's in 3s\n", count);
  114. }
  115. printf("type\t\t16 bytes\t64 bytes\t256 bytes\t1024 bytes\t8192 bytes\t16384 bytes\n");
  116. printf("sm3\t");
  117. for (int i = 0; i < 6; i++)
  118. {
  119. printf("\t%.2fK", countbox[i] * sizebox[i] / 1024 / 3.00);
  120. }
  121. printf("\n");
  122. for (int i = 0; i < 6; i++)
  123. {
  124. free(testhex[i]);
  125. }
  126. free(testhex);
  127. return 1;
  128. }
  129. #endif
  130. int sm3speed_main(void)
  131. {
  132. test_sm3();
  133. return 1;
  134. }