2
0

hcrypt_ut.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. written by
  12. Haivision Systems Inc.
  13. 2011-07-11 (jdube)
  14. HaiCrypt initial implementation.
  15. *****************************************************************************/
  16. #include <string.h> /* memcpy */
  17. #include <stdio.h>
  18. #include <haicrypt.h>
  19. #include "hcrypt.h"
  20. #ifndef _WIN32
  21. /* RFC6070 PBKDF2 Tests Vectors */
  22. static struct TestVector {
  23. size_t pwd_len;
  24. const char *pwd;
  25. size_t salt_len;
  26. const unsigned char *salt;
  27. int cnt;
  28. size_t dk_len;
  29. unsigned char dk[32];
  30. } tv[] = {
  31. { /* 1 */
  32. .pwd_len = 8, .pwd = "password",
  33. .salt_len = 4, .salt = (unsigned char *)"salt",
  34. .cnt = 1,
  35. .dk_len = 20,
  36. .dk = {
  37. 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  38. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  39. 0x2f, 0xe0, 0x37, 0xa6
  40. }
  41. },
  42. { /* 2 */
  43. .pwd_len = 8, .pwd = "password",
  44. .salt_len = 4, .salt = (unsigned char *)"salt",
  45. .cnt = 2,
  46. .dk_len = 20,
  47. .dk = {
  48. 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  49. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  50. 0xd8, 0xde, 0x89, 0x57
  51. }
  52. },
  53. { /* 3 */
  54. .pwd_len = 8, .pwd = "password",
  55. .salt_len = 4, .salt = (unsigned char *)"salt",
  56. .cnt = 4096,
  57. .dk_len = 20,
  58. .dk = {
  59. 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  60. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  61. 0x65, 0xa4, 0x29, 0xc1
  62. }
  63. },
  64. { /* 4 */
  65. .pwd_len = 8, .pwd = "password",
  66. .salt_len = 4, .salt = (unsigned char *)"salt",
  67. .cnt = 16777216,
  68. .dk_len = 20,
  69. .dk = {
  70. 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
  71. 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
  72. 0x26, 0x34, 0xe9, 0x84
  73. }
  74. },
  75. { /* 5 */
  76. .pwd_len = 24, .pwd = "passwordPASSWORDpassword",
  77. .salt_len = 36, .salt = (unsigned char *)"saltSALTsaltSALTsaltSALTsaltSALTsalt",
  78. .cnt = 4096,
  79. .dk_len = 25,
  80. .dk = {
  81. 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  82. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  83. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  84. 0x38
  85. }
  86. },
  87. { /* 6 */
  88. .pwd_len = 9, .pwd = "pass\0word",
  89. .salt_len = 5, .salt = (unsigned char *)"sa\0lt",
  90. .cnt = 4096,
  91. .dk_len = 16,
  92. .dk = {
  93. 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  94. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3
  95. }
  96. },
  97. };
  98. #include <sys/time.h>
  99. static int hc_ut_pbkdf2(unsigned verbose)
  100. {
  101. int i;
  102. int nbt = sizeof(tv)/sizeof(tv[0]);
  103. int nbe = 0;
  104. unsigned char dk[32];
  105. struct timeval tstart, tstop, tdiff;
  106. for (i=0; i<nbt; i++) {
  107. if (verbose) {
  108. printf("PBKDF2 test vector %d", i+1);
  109. fflush(stdout);
  110. gettimeofday(&tstart, NULL);
  111. }
  112. hcrypt_pbkdf2_hmac_sha1(tv[i].pwd, tv[i].pwd_len,
  113. tv[i].salt, tv[i].salt_len,
  114. tv[i].cnt, tv[i].dk_len, dk);
  115. if (verbose) {
  116. gettimeofday(&tstop, NULL);
  117. timersub(&tstop, &tstart, &tdiff);
  118. }
  119. if(memcmp(dk, tv[i].dk, tv[i].dk_len)) {
  120. if (verbose) {
  121. printf(": failed in %lu.%06lu sec\n", tdiff.tv_sec, (unsigned long)tdiff.tv_usec);
  122. } else {
  123. printf("PBKDF2 test vector %d: failed\n", i+1);
  124. }
  125. nbe++;
  126. } else if (verbose) {
  127. printf(": passed in %lu.%06lu sec\n", tdiff.tv_sec, (unsigned long)tdiff.tv_usec);
  128. }
  129. }
  130. return(nbe);
  131. }
  132. int hc_ut_encrypt_ctr_speed(void)
  133. {
  134. static HaiCrypt_Secret secret = {
  135. .typ = HAICRYPT_SECTYP_PASSPHRASE,
  136. .len = 12,
  137. .str = "000000000000"
  138. };
  139. HaiCrypt_Cfg crypto_cfg;
  140. HaiCrypt_Handle hcrypto;
  141. struct timeval tstart, tstop, tdiff;
  142. unsigned char pkt[1500];
  143. int nbe = 0;
  144. int i;
  145. #ifdef HAICRYPT_USE_OPENSSL_EVP_CBC
  146. HaiCrypt_Cipher HaiCryptCipher_OpenSSL_EVP_CBC(void); /* OpenSSL EVP interface CBC mode*/
  147. #endif
  148. memset(&crypto_cfg, 0, sizeof(crypto_cfg));
  149. crypto_cfg.flags = HAICRYPT_CFG_F_CRYPTO | HAICRYPT_CFG_F_TX;
  150. crypto_cfg.xport = HAICRYPT_XPT_SRT;
  151. #ifdef HAICRYPT_USE_OPENSSL_EVP_CBC
  152. crypto_cfg.cipher = HaiCryptCipher_OpenSSL_EVP_CBC();
  153. #else
  154. crypto_cfg.cipher = HaiCryptCipher_Get_Instance();
  155. #endif
  156. crypto_cfg.key_len = (size_t)128/8;
  157. crypto_cfg.data_max_len = HAICRYPT_DEF_DATA_MAX_LENGTH; //MTU
  158. crypto_cfg.km_tx_period_ms = 0;//No HaiCrypt KM inject period, handled in SRT;
  159. crypto_cfg.km_refresh_rate_pkt = HAICRYPT_DEF_KM_REFRESH_RATE;
  160. crypto_cfg.km_pre_announce_pkt = 0x10000; //HAICRYPT_DEF_KM_PRE_ANNOUNCE;
  161. memcpy(&crypto_cfg.secret, &secret, sizeof(crypto_cfg.secret));
  162. if (HaiCrypt_Create(&crypto_cfg, &hcrypto)) {
  163. fprintf(stderr, "haicrypt: HaiCrypt_Create failed\n");
  164. return(1);
  165. }
  166. for (i=0; i<1500; i++) {
  167. pkt[i] = i & 0xff;
  168. }
  169. #define UT_NBPKTS 100000L
  170. #define UT_PKTSZ (7*188)
  171. gettimeofday(&tstart, NULL);
  172. for (i=0; i<UT_NBPKTS; i++) {
  173. if (0 > HaiCrypt_Tx_Data(hcrypto, &pkt[0], &pkt[16], UT_PKTSZ)) nbe++;
  174. if (0 == (i % 1000)) {
  175. printf("\b\b\b\b\b\b%6d", i);
  176. fflush(stdout);
  177. }
  178. }
  179. gettimeofday(&tstop, NULL);
  180. timersub(&tstop, &tstart, &tdiff);
  181. printf("\nhaicrypt: encrypted %ld packets in %lu.%06lu sec (%ld.%03ld kbps)\n",
  182. UT_NBPKTS, tdiff.tv_sec, (unsigned long)tdiff.tv_usec,
  183. (((UT_NBPKTS * UT_PKTSZ*10)/((tdiff.tv_sec*10) + (tdiff.tv_usec/100))) / 1000),
  184. (((UT_NBPKTS * UT_PKTSZ*10)/((tdiff.tv_sec*10) + (tdiff.tv_usec/100))) % 1000));
  185. HaiCrypt_Close(hcrypto);
  186. return(nbe);
  187. }
  188. int main(int argc, char *argv[])
  189. {
  190. int nbe = 0;
  191. (void)argc;
  192. (void)argv;
  193. nbe += hc_ut_encrypt_ctr_speed();
  194. nbe += hc_ut_pbkdf2(1);
  195. printf("haicrypt unit test %s: %d errors found\n", nbe ? "failed" : "passed", nbe);
  196. return(nbe);
  197. }
  198. #endif // _WIN32