tests.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "hydrogen.h"
  8. static const char *ctx = "libtests";
  9. static int
  10. streq(const char *expected, const char *found)
  11. {
  12. if (strcmp(expected, found) != 0) {
  13. fprintf(stderr, "Found: [%s]\n", found);
  14. return 0;
  15. }
  16. return 1;
  17. }
  18. #define assert_streq(EXPECTED, FOUND) assert(streq((EXPECTED), (FOUND)))
  19. static void
  20. test_randombytes(void)
  21. {
  22. uint8_t dk[hydro_random_SEEDBYTES];
  23. uint8_t tmp[10000];
  24. unsigned long b = 0U;
  25. unsigned long bp;
  26. uint32_t x;
  27. size_t i, j;
  28. for (i = 0; i < 10000; i++) {
  29. x = hydro_random_u32();
  30. for (j = 0; j < sizeof x; j++) {
  31. b += (x >> j) & 1;
  32. }
  33. }
  34. assert(b > 18000 && b < 22000);
  35. b = 0;
  36. hydro_random_buf(tmp, sizeof tmp);
  37. for (i = 0; i < 10000; i++) {
  38. for (j = 0; j < sizeof tmp[0]; j++) {
  39. b += (tmp[i] >> j) & 1;
  40. }
  41. }
  42. assert(b > 4500 && b < 5500);
  43. memcpy(dk, tmp, sizeof dk);
  44. b = 0;
  45. hydro_random_buf_deterministic(tmp, 10000, dk);
  46. for (i = 0; i < 10000; i++) {
  47. for (j = 0; j < sizeof tmp[0]; j++) {
  48. b += (tmp[i] >> j) & 1;
  49. }
  50. }
  51. assert(b > 4500 && b < 5500);
  52. bp = b;
  53. b = 0;
  54. hydro_random_buf_deterministic(tmp, 10000, dk);
  55. for (i = 0; i < 10000; i++) {
  56. for (j = 0; j < sizeof tmp[0]; j++) {
  57. b += (tmp[i] >> j) & 1;
  58. }
  59. }
  60. assert(b == bp);
  61. for (i = 0; i < 1000; i++) {
  62. for (j = 1; j < 100; j++) {
  63. x = hydro_random_uniform((uint32_t) j);
  64. assert(x < j);
  65. }
  66. }
  67. }
  68. static void
  69. test_hash(void)
  70. {
  71. hydro_hash_state st;
  72. uint8_t dk[hydro_random_SEEDBYTES];
  73. uint8_t h[100];
  74. uint8_t key[hydro_hash_KEYBYTES];
  75. #ifdef __TRUSTINSOFT_ANALYZER__
  76. uint8_t msg[32];
  77. #else
  78. uint8_t msg[1000];
  79. #endif
  80. char hex[100 * 2 + 1];
  81. size_t i;
  82. memset(dk, 0, sizeof dk);
  83. hydro_random_buf_deterministic(key, sizeof key, dk);
  84. hydro_increment(dk, sizeof dk);
  85. hydro_hash_init(&st, ctx, key);
  86. for (i = 0; i <= sizeof msg; i++) {
  87. hydro_random_buf_deterministic(msg, i, dk);
  88. hydro_increment(dk, sizeof dk);
  89. hydro_hash_update(&st, msg, i);
  90. }
  91. hydro_hash_final(&st, h, sizeof h);
  92. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  93. #ifndef __TRUSTINSOFT_ANALYZER__
  94. assert_streq(
  95. "e5d2beb77a039965850ee76327e06b2fa6cb5121db8038b11bce4641a9c4bd843658104bdf07342570bb5fd1d7"
  96. "2c0d31a8981b47c718fddaffbd4171605c873cbaf921bb57988dd814f3a3fbef9799ff7c762705c4bf37ab2981"
  97. "5981bf0d8833d60afe14",
  98. hex);
  99. #endif
  100. hydro_hash_hash(h, sizeof h, msg, sizeof msg, ctx, key);
  101. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  102. #ifndef __TRUSTINSOFT_ANALYZER__
  103. assert_streq(
  104. "724bd8883df73320ffd70923cb997f9a99bc670c4d78887be4975add0099fbf489b266a85d1f56743062d60a05"
  105. "590cbce47e45108367879bf4641cbaefe584e8618cbeb8c230ae956da22c7c5c4f11a8804ca576ec20fa5da239"
  106. "dde3d03a6018383c21f5",
  107. hex);
  108. #endif
  109. hydro_hash_hash(h, hydro_hash_BYTES, msg, sizeof msg, ctx, key);
  110. hydro_bin2hex(hex, sizeof hex, h, hydro_hash_BYTES);
  111. #ifndef __TRUSTINSOFT_ANALYZER__
  112. assert_streq("7dfa45ce18210e2422fd658bf7beccb6e534e44f99ae359f4af3ba41af8ca463", hex);
  113. #endif
  114. /* total input length is a multiple of the rate */
  115. hydro_hash_hash(h, hydro_hash_BYTES, msg, 13, ctx, key);
  116. hydro_bin2hex(hex, sizeof hex, h, hydro_hash_BYTES);
  117. #ifndef __TRUSTINSOFT_ANALYZER__
  118. assert_streq("d57a9800549bb4bab6a06fa6e16e08aad68d7d4313fb69a81b9f5d5af375dbe7", hex);
  119. #endif
  120. }
  121. static void
  122. test_core(void)
  123. {
  124. uint8_t x[100];
  125. uint8_t y[100];
  126. uint8_t a[5] = { 1, 2, 3, 4, 5 };
  127. uint8_t b[5] = { 1, 2, 3, 4, 5 };
  128. char hex[201];
  129. const char *hexf;
  130. memset(x, 0xd0, sizeof x);
  131. hydro_memzero(x, sizeof x);
  132. assert(x[0] == 0);
  133. assert(x[sizeof x - 1] == 0);
  134. hydro_increment(x, sizeof x);
  135. assert(x[0] == 1);
  136. assert(x[sizeof x - 1] == 0);
  137. x[0] = 0xff;
  138. hydro_increment(x, sizeof x);
  139. assert(x[0] == 0);
  140. assert(x[1] == 1);
  141. assert(x[sizeof x - 1] == 0);
  142. assert(hydro_equal(a, b, sizeof a));
  143. assert(!hydro_equal(a, a, sizeof a));
  144. assert(hydro_compare(a, b, sizeof a) == 0);
  145. assert(hydro_compare(a, a, sizeof a) == 0);
  146. a[0]++;
  147. assert(hydro_compare(a, b, sizeof a) == 1);
  148. assert(hydro_compare(b, a, sizeof a) == -1);
  149. hydro_random_buf(x, sizeof x);
  150. assert(hydro_bin2hex(hex, sizeof hex, x, sizeof x) != NULL);
  151. assert(hydro_hex2bin(y, 1, hex, sizeof hex, NULL, NULL) == -1);
  152. assert(hydro_hex2bin(y, sizeof y, hex, sizeof hex, NULL, NULL) == -1);
  153. assert(hydro_hex2bin(y, sizeof y, hex, sizeof hex - 1, NULL, NULL) == sizeof x);
  154. assert(hydro_equal(x, y, sizeof x));
  155. assert(hydro_hex2bin(x, sizeof x, "452a", 4, NULL, NULL) == 2);
  156. assert(hydro_hex2bin(y, sizeof y, "#452a#", 6, "#", NULL) == 2);
  157. assert(hydro_equal(x, y, sizeof x));
  158. memcpy(hex, "#452a", sizeof "#452a");
  159. assert(hydro_hex2bin(x, sizeof x, hex, 0, NULL, &hexf) == 0);
  160. assert(hexf == hex);
  161. assert(hydro_hex2bin(x, sizeof x, hex, sizeof "#452a", NULL, &hexf) == 0);
  162. assert(hexf == hex);
  163. assert(hydro_hex2bin(x, sizeof x, hex, sizeof "#452a", "#", &hexf) == 2);
  164. assert(hexf == hex + 6);
  165. }
  166. static void
  167. test_secretbox(void)
  168. {
  169. uint8_t key[hydro_secretbox_KEYBYTES];
  170. uint8_t m[25];
  171. uint8_t m2[25];
  172. uint8_t c[hydro_secretbox_HEADERBYTES + 25];
  173. uint8_t dk[hydro_random_SEEDBYTES];
  174. uint8_t probe[hydro_secretbox_PROBEBYTES];
  175. memset(dk, 0, sizeof dk);
  176. hydro_random_buf_deterministic(m, sizeof m, dk);
  177. hydro_increment(dk, sizeof dk);
  178. hydro_random_buf_deterministic(key, sizeof key, dk);
  179. hydro_increment(dk, sizeof dk);
  180. hydro_secretbox_encrypt(c, m, sizeof m, 0, ctx, key);
  181. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == 0);
  182. assert(hydro_equal(m, m2, sizeof m));
  183. hydro_secretbox_probe_create(probe, c, sizeof c, ctx, key);
  184. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == 0);
  185. probe[0]++;
  186. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == -1);
  187. probe[0]--;
  188. key[0]++;
  189. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == -1);
  190. key[0]--;
  191. assert(hydro_secretbox_decrypt(m2, c, 0, 0, ctx, key) == -1);
  192. assert(hydro_secretbox_decrypt(m2, c, 1, 0, ctx, key) == -1);
  193. assert(hydro_secretbox_decrypt(m2, c, hydro_secretbox_HEADERBYTES, 0, ctx, key) == -1);
  194. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 1, ctx, key) == -1);
  195. assert(!hydro_equal(m, m2, sizeof m));
  196. key[0]++;
  197. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  198. key[0]--;
  199. c[hydro_random_uniform(sizeof c)]++;
  200. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  201. }
  202. static void
  203. test_kdf(void)
  204. {
  205. uint8_t key[hydro_kdf_KEYBYTES];
  206. uint8_t dk[hydro_random_SEEDBYTES];
  207. uint8_t subkey1[16];
  208. uint8_t subkey2[16];
  209. uint8_t subkey3[32];
  210. uint8_t subkey4[50];
  211. char subkey1_hex[16 * 2 + 1];
  212. char subkey2_hex[16 * 2 + 1];
  213. char subkey3_hex[32 * 2 + 1];
  214. char subkey4_hex[50 * 2 + 1];
  215. memset(dk, 0, sizeof dk);
  216. hydro_random_buf_deterministic(key, sizeof key, dk);
  217. hydro_kdf_derive_from_key(subkey1, sizeof subkey1, 1, ctx, key);
  218. hydro_kdf_derive_from_key(subkey2, sizeof subkey2, 2, ctx, key);
  219. hydro_kdf_derive_from_key(subkey3, sizeof subkey3, 0, ctx, key);
  220. hydro_kdf_derive_from_key(subkey4, sizeof subkey4, 0, ctx, key);
  221. hydro_bin2hex(subkey1_hex, sizeof subkey1_hex, subkey1, sizeof subkey1);
  222. hydro_bin2hex(subkey2_hex, sizeof subkey2_hex, subkey2, sizeof subkey2);
  223. hydro_bin2hex(subkey3_hex, sizeof subkey3_hex, subkey3, sizeof subkey3);
  224. hydro_bin2hex(subkey4_hex, sizeof subkey4_hex, subkey4, sizeof subkey4);
  225. assert_streq("af8019d3516d4ba6c80a7ea5a87e4d77", subkey1_hex);
  226. assert_streq("af8c4cba4e1f36c293631cc7001717dd", subkey2_hex);
  227. assert_streq("ff9345489dea1e4fe59194cea8794c9b0af9380c2d18c3ab38eeef2af95c1e26", subkey3_hex);
  228. assert_streq(
  229. "a8dd79ca19d604d1487b82d76b8d4ad4138a29dfaeeb207b99b2e5904e7855555bb94a76070fa71871df6ed911"
  230. "661d99efec",
  231. subkey4_hex);
  232. }
  233. static void
  234. test_sign(void)
  235. {
  236. #ifdef __TRUSTINSOFT_ANALYZER__
  237. uint8_t msg[32];
  238. #else
  239. uint8_t msg[500];
  240. #endif
  241. uint8_t sig[hydro_sign_BYTES];
  242. hydro_sign_state st;
  243. hydro_sign_keypair kp;
  244. hydro_random_buf(msg, sizeof msg);
  245. hydro_sign_keygen(&kp);
  246. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  247. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == 0);
  248. sig[0]++;
  249. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  250. sig[0]--;
  251. sig[hydro_sign_BYTES - 1]++;
  252. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  253. sig[hydro_sign_BYTES - 1]--;
  254. msg[0]++;
  255. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  256. msg[0]++;
  257. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  258. hydro_sign_init(&st, ctx);
  259. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  260. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  261. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  262. hydro_sign_init(&st, ctx);
  263. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  264. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  265. hydro_sign_final_create(&st, sig, kp.sk);
  266. hydro_sign_init(&st, ctx);
  267. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  268. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  269. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  270. hydro_sign_init(&st, ctx);
  271. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  272. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  273. sig[0]++;
  274. assert(hydro_sign_final_verify(&st, sig, kp.pk) == -1);
  275. hydro_sign_create(sig, msg, 0, ctx, kp.sk);
  276. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  277. assert(hydro_sign_verify(sig, msg, 0, ctx, kp.pk) == 0);
  278. }
  279. static void
  280. test_kx_n(void)
  281. {
  282. hydro_kx_keypair server_static_kp;
  283. uint8_t psk[hydro_kx_PSKBYTES];
  284. uint8_t packet1[hydro_kx_N_PACKET1BYTES];
  285. hydro_kx_session_keypair kp_client;
  286. hydro_kx_session_keypair kp_server;
  287. hydro_kx_keygen(&server_static_kp);
  288. hydro_random_buf(psk, sizeof psk);
  289. hydro_kx_n_1(&kp_client, packet1, psk, server_static_kp.pk);
  290. hydro_kx_n_2(&kp_server, packet1, psk, &server_static_kp);
  291. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  292. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  293. }
  294. static void
  295. test_kx_kk(void)
  296. {
  297. hydro_kx_state st_client;
  298. hydro_kx_keypair client_static_kp;
  299. hydro_kx_keypair server_static_kp;
  300. uint8_t packet1[hydro_kx_KK_PACKET1BYTES];
  301. uint8_t packet2[hydro_kx_KK_PACKET2BYTES];
  302. hydro_kx_session_keypair kp_client;
  303. hydro_kx_session_keypair kp_server;
  304. hydro_kx_keygen(&client_static_kp);
  305. hydro_kx_keygen(&server_static_kp);
  306. hydro_kx_kk_1(&st_client, packet1, server_static_kp.pk, &client_static_kp);
  307. hydro_kx_kk_2(&kp_server, packet2, packet1, client_static_kp.pk, &server_static_kp);
  308. hydro_kx_kk_3(&st_client, &kp_client, packet2, &client_static_kp);
  309. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  310. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  311. }
  312. static void
  313. test_kx_xx(void)
  314. {
  315. hydro_kx_state st_client;
  316. hydro_kx_state st_server;
  317. hydro_kx_keypair client_static_kp;
  318. hydro_kx_keypair server_static_kp;
  319. uint8_t psk[hydro_kx_PSKBYTES];
  320. uint8_t client_peer_pk[hydro_kx_PUBLICKEYBYTES];
  321. uint8_t server_peer_pk[hydro_kx_PUBLICKEYBYTES];
  322. uint8_t packet1[hydro_kx_XX_PACKET1BYTES];
  323. uint8_t packet2[hydro_kx_XX_PACKET2BYTES];
  324. uint8_t packet3[hydro_kx_XX_PACKET3BYTES];
  325. hydro_kx_session_keypair kp_client;
  326. hydro_kx_session_keypair kp_server;
  327. hydro_kx_keygen(&client_static_kp);
  328. hydro_kx_keygen(&server_static_kp);
  329. hydro_kx_xx_1(&st_client, packet1, NULL);
  330. hydro_kx_xx_2(&st_server, packet2, packet1, NULL, &server_static_kp);
  331. hydro_kx_xx_3(&st_client, &kp_client, packet3, NULL, packet2, NULL, &client_static_kp);
  332. hydro_kx_xx_4(&st_server, &kp_server, NULL, packet3, NULL);
  333. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  334. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  335. hydro_random_buf(psk, sizeof psk);
  336. hydro_kx_xx_1(&st_client, packet1, psk);
  337. hydro_kx_xx_2(&st_server, packet2, packet1, psk, &server_static_kp);
  338. hydro_kx_xx_3(&st_client, &kp_client, packet3, client_peer_pk, packet2, psk, &client_static_kp);
  339. hydro_kx_xx_4(&st_server, &kp_server, server_peer_pk, packet3, psk);
  340. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  341. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  342. assert(hydro_equal(client_peer_pk, server_static_kp.pk, hydro_kx_PUBLICKEYBYTES));
  343. assert(hydro_equal(server_peer_pk, client_static_kp.pk, hydro_kx_PUBLICKEYBYTES));
  344. }
  345. static void
  346. test_kx_nk(void)
  347. {
  348. hydro_kx_state st_client;
  349. hydro_kx_keypair server_static_kp;
  350. uint8_t psk[hydro_kx_PSKBYTES];
  351. uint8_t packet1[hydro_kx_NK_PACKET1BYTES];
  352. uint8_t packet2[hydro_kx_NK_PACKET2BYTES];
  353. hydro_kx_session_keypair kp_client;
  354. hydro_kx_session_keypair kp_server;
  355. hydro_kx_keygen(&server_static_kp);
  356. hydro_kx_nk_1(&st_client, packet1, NULL, server_static_kp.pk);
  357. hydro_kx_nk_2(&kp_server, packet2, packet1, NULL, &server_static_kp);
  358. hydro_kx_nk_3(&st_client, &kp_client, packet2);
  359. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  360. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  361. hydro_random_buf(psk, sizeof psk);
  362. hydro_kx_nk_1(&st_client, packet1, psk, server_static_kp.pk);
  363. hydro_kx_nk_2(&kp_server, packet2, packet1, psk, &server_static_kp);
  364. hydro_kx_nk_3(&st_client, &kp_client, packet2);
  365. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  366. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  367. }
  368. static void
  369. test_pwhash(void)
  370. {
  371. uint8_t master_key[hydro_pwhash_MASTERKEYBYTES];
  372. uint8_t new_master_key[hydro_pwhash_MASTERKEYBYTES];
  373. uint8_t stored[hydro_pwhash_STOREDBYTES];
  374. uint8_t h[64];
  375. uint8_t static_key[64];
  376. char h_hex[2 * 64 + 1];
  377. unsigned long long ops = 1000;
  378. memset(master_key, 'x', sizeof master_key);
  379. hydro_pwhash_deterministic(h, sizeof h, "test", sizeof "test" - 1, ctx, master_key, ops, 0, 1);
  380. hydro_bin2hex(h_hex, sizeof h_hex, h, sizeof h);
  381. if (ops == 1000) {
  382. assert_streq(
  383. "2f1a804a02f25066fd0688bf8b8e03dff3a3866958a9cf5883c459e602e232d38e3e488723f0b4a2bc61d2"
  384. "0cb36a04a4d2eb18be99bc61870d72d7de5d67f237",
  385. h_hex);
  386. }
  387. hydro_pwhash_keygen(master_key);
  388. assert(hydro_pwhash_create(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  389. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  390. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops * 2, 10, 10) ==
  391. 0);
  392. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops / 2, 10, 10) ==
  393. -1);
  394. assert(hydro_pwhash_verify(stored, "Test", sizeof "Test" - 1, master_key, ops, 0, 1) == -1);
  395. assert(hydro_pwhash_verify(stored, "test", sizeof "tes" - 1, master_key, ops, 0, 1) == -1);
  396. assert(hydro_pwhash_derive_static_key(static_key, sizeof static_key, stored, "test",
  397. sizeof "test" - 1, ctx, master_key, ops, 0, 1) == 0);
  398. assert(hydro_pwhash_derive_static_key(static_key, sizeof static_key, stored, "Test",
  399. sizeof "Test" - 1, ctx, master_key, ops, 0, 1) == -1);
  400. assert(hydro_pwhash_reencrypt(stored, master_key, master_key) == 0);
  401. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  402. hydro_pwhash_keygen(new_master_key);
  403. assert(hydro_pwhash_reencrypt(stored, master_key, new_master_key) == 0);
  404. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == -1);
  405. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops, 0, 1) == 0);
  406. assert(hydro_pwhash_upgrade(stored, new_master_key, ops * 2, 0, 1) == 0);
  407. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops, 0, 1) == -1);
  408. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops * 2, 0, 1) ==
  409. 0);
  410. }
  411. int
  412. main(void)
  413. {
  414. #ifdef _MSC_VER
  415. /*
  416. * On Windows, disable the "Abort - Retry - Ignore" GUI dialog that otherwise pops up on
  417. * assertion failure.
  418. */
  419. _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  420. #endif
  421. int ret;
  422. ret = hydro_init();
  423. assert(ret == 0);
  424. test_core();
  425. test_hash();
  426. test_kdf();
  427. test_kx_n();
  428. test_kx_kk();
  429. test_kx_xx();
  430. test_kx_nk();
  431. test_pwhash();
  432. test_randombytes();
  433. test_secretbox();
  434. test_sign();
  435. return 0;
  436. }