tests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. uint8_t msg[1000];
  76. char hex[100 * 2 + 1];
  77. size_t i;
  78. memset(dk, 0, sizeof dk);
  79. hydro_random_buf_deterministic(key, sizeof key, dk);
  80. hydro_increment(dk, sizeof dk);
  81. hydro_hash_init(&st, ctx, key);
  82. for (i = 0; i <= sizeof msg; i++) {
  83. hydro_random_buf_deterministic(msg, i, dk);
  84. hydro_increment(dk, sizeof dk);
  85. hydro_hash_update(&st, msg, i);
  86. }
  87. hydro_hash_final(&st, h, sizeof h);
  88. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  89. assert_streq(
  90. "e5d2beb77a039965850ee76327e06b2fa6cb5121db8038b11bce4641a9c4bd843658104bdf07342570bb5fd1d7"
  91. "2c0d31a8981b47c718fddaffbd4171605c873cbaf921bb57988dd814f3a3fbef9799ff7c762705c4bf37ab2981"
  92. "5981bf0d8833d60afe14",
  93. hex);
  94. hydro_hash_hash(h, sizeof h, msg, sizeof msg, ctx, key);
  95. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  96. assert_streq(
  97. "724bd8883df73320ffd70923cb997f9a99bc670c4d78887be4975add0099fbf489b266a85d1f56743062d60a05"
  98. "590cbce47e45108367879bf4641cbaefe584e8618cbeb8c230ae956da22c7c5c4f11a8804ca576ec20fa5da239"
  99. "dde3d03a6018383c21f5",
  100. hex);
  101. hydro_hash_hash(h, hydro_hash_BYTES, msg, sizeof msg, ctx, key);
  102. hydro_bin2hex(hex, sizeof hex, h, hydro_hash_BYTES);
  103. assert_streq("7dfa45ce18210e2422fd658bf7beccb6e534e44f99ae359f4af3ba41af8ca463", hex);
  104. /* total input length is a multiple of the rate */
  105. hydro_hash_hash(h, hydro_hash_BYTES, msg, 13, ctx, key);
  106. hydro_bin2hex(hex, sizeof hex, h, hydro_hash_BYTES);
  107. assert_streq("d57a9800549bb4bab6a06fa6e16e08aad68d7d4313fb69a81b9f5d5af375dbe7", hex);
  108. }
  109. static void
  110. test_core(void)
  111. {
  112. uint8_t x[100];
  113. uint8_t y[100];
  114. uint8_t a[5] = { 1, 2, 3, 4, 5 };
  115. uint8_t b[5] = { 1, 2, 3, 4, 5 };
  116. char hex[201];
  117. const char *hexf;
  118. memset(x, 0xd0, sizeof x);
  119. hydro_memzero(x, sizeof x);
  120. assert(x[0] == 0);
  121. assert(x[sizeof x - 1] == 0);
  122. hydro_increment(x, sizeof x);
  123. assert(x[0] == 1);
  124. assert(x[sizeof x - 1] == 0);
  125. x[0] = 0xff;
  126. hydro_increment(x, sizeof x);
  127. assert(x[0] == 0);
  128. assert(x[1] == 1);
  129. assert(x[sizeof x - 1] == 0);
  130. assert(hydro_equal(a, b, sizeof a));
  131. assert(!hydro_equal(a, a, sizeof a));
  132. assert(hydro_compare(a, b, sizeof a) == 0);
  133. assert(hydro_compare(a, a, sizeof a) == 0);
  134. a[0]++;
  135. assert(hydro_compare(a, b, sizeof a) == 1);
  136. assert(hydro_compare(b, a, sizeof a) == -1);
  137. hydro_random_buf(x, sizeof x);
  138. assert(hydro_bin2hex(hex, sizeof hex, x, sizeof x) != NULL);
  139. assert(hydro_hex2bin(y, 1, hex, sizeof hex, NULL, NULL) == -1);
  140. assert(hydro_hex2bin(y, sizeof y, hex, sizeof hex, NULL, NULL) == -1);
  141. assert(hydro_hex2bin(y, sizeof y, hex, sizeof hex - 1, NULL, NULL) == sizeof x);
  142. assert(hydro_equal(x, y, sizeof x));
  143. assert(hydro_hex2bin(x, sizeof x, "452a", 4, NULL, NULL) == 2);
  144. assert(hydro_hex2bin(y, sizeof y, "#452a#", 6, "#", NULL) == 2);
  145. assert(hydro_equal(x, y, sizeof x));
  146. memcpy(hex, "#452a", sizeof "#452a");
  147. assert(hydro_hex2bin(x, sizeof x, hex, 0, NULL, &hexf) == 0);
  148. assert(hexf == hex);
  149. assert(hydro_hex2bin(x, sizeof x, hex, sizeof "#452a", NULL, &hexf) == 0);
  150. assert(hexf == hex);
  151. assert(hydro_hex2bin(x, sizeof x, hex, sizeof "#452a", "#", &hexf) == 2);
  152. assert(hexf == hex + 6);
  153. }
  154. static void
  155. test_secretbox(void)
  156. {
  157. uint8_t key[hydro_secretbox_KEYBYTES];
  158. uint8_t m[25];
  159. uint8_t m2[25];
  160. uint8_t c[hydro_secretbox_HEADERBYTES + 25];
  161. uint8_t dk[hydro_random_SEEDBYTES];
  162. uint8_t probe[hydro_secretbox_PROBEBYTES];
  163. memset(dk, 0, sizeof dk);
  164. hydro_random_buf_deterministic(m, sizeof m, dk);
  165. hydro_increment(dk, sizeof dk);
  166. hydro_random_buf_deterministic(key, sizeof key, dk);
  167. hydro_increment(dk, sizeof dk);
  168. hydro_secretbox_encrypt(c, m, sizeof m, 0, ctx, key);
  169. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == 0);
  170. assert(hydro_equal(m, m2, sizeof m));
  171. hydro_secretbox_probe_create(probe, c, sizeof c, ctx, key);
  172. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == 0);
  173. probe[0]++;
  174. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == -1);
  175. probe[0]--;
  176. key[0]++;
  177. assert(hydro_secretbox_probe_verify(probe, c, sizeof c, ctx, key) == -1);
  178. key[0]--;
  179. assert(hydro_secretbox_decrypt(m2, c, 0, 0, ctx, key) == -1);
  180. assert(hydro_secretbox_decrypt(m2, c, 1, 0, ctx, key) == -1);
  181. assert(hydro_secretbox_decrypt(m2, c, hydro_secretbox_HEADERBYTES, 0, ctx, key) == -1);
  182. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 1, ctx, key) == -1);
  183. assert(!hydro_equal(m, m2, sizeof m));
  184. key[0]++;
  185. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  186. key[0]--;
  187. c[hydro_random_uniform(sizeof c)]++;
  188. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  189. }
  190. static void
  191. test_kdf(void)
  192. {
  193. uint8_t key[hydro_kdf_KEYBYTES];
  194. uint8_t dk[hydro_random_SEEDBYTES];
  195. uint8_t subkey1[16];
  196. uint8_t subkey2[16];
  197. uint8_t subkey3[32];
  198. uint8_t subkey4[50];
  199. char subkey1_hex[16 * 2 + 1];
  200. char subkey2_hex[16 * 2 + 1];
  201. char subkey3_hex[32 * 2 + 1];
  202. char subkey4_hex[50 * 2 + 1];
  203. memset(dk, 0, sizeof dk);
  204. hydro_random_buf_deterministic(key, sizeof key, dk);
  205. hydro_kdf_derive_from_key(subkey1, sizeof subkey1, 1, ctx, key);
  206. hydro_kdf_derive_from_key(subkey2, sizeof subkey2, 2, ctx, key);
  207. hydro_kdf_derive_from_key(subkey3, sizeof subkey3, 0, ctx, key);
  208. hydro_kdf_derive_from_key(subkey4, sizeof subkey4, 0, ctx, key);
  209. hydro_bin2hex(subkey1_hex, sizeof subkey1_hex, subkey1, sizeof subkey1);
  210. hydro_bin2hex(subkey2_hex, sizeof subkey2_hex, subkey2, sizeof subkey2);
  211. hydro_bin2hex(subkey3_hex, sizeof subkey3_hex, subkey3, sizeof subkey3);
  212. hydro_bin2hex(subkey4_hex, sizeof subkey4_hex, subkey4, sizeof subkey4);
  213. assert_streq("af8019d3516d4ba6c80a7ea5a87e4d77", subkey1_hex);
  214. assert_streq("af8c4cba4e1f36c293631cc7001717dd", subkey2_hex);
  215. assert_streq("ff9345489dea1e4fe59194cea8794c9b0af9380c2d18c3ab38eeef2af95c1e26", subkey3_hex);
  216. assert_streq(
  217. "a8dd79ca19d604d1487b82d76b8d4ad4138a29dfaeeb207b99b2e5904e7855555bb94a76070fa71871df6ed911"
  218. "661d99efec",
  219. subkey4_hex);
  220. }
  221. static void
  222. test_sign(void)
  223. {
  224. uint8_t msg[500];
  225. uint8_t sig[hydro_sign_BYTES];
  226. hydro_sign_state st;
  227. hydro_sign_keypair kp;
  228. hydro_random_buf(msg, sizeof msg);
  229. hydro_sign_keygen(&kp);
  230. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  231. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == 0);
  232. sig[0]++;
  233. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  234. sig[0]--;
  235. sig[hydro_sign_BYTES - 1]++;
  236. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  237. sig[hydro_sign_BYTES - 1]--;
  238. msg[0]++;
  239. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  240. msg[0]++;
  241. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  242. hydro_sign_init(&st, ctx);
  243. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  244. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  245. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  246. hydro_sign_init(&st, ctx);
  247. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  248. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  249. hydro_sign_final_create(&st, sig, kp.sk);
  250. hydro_sign_init(&st, ctx);
  251. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  252. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  253. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  254. hydro_sign_init(&st, ctx);
  255. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  256. hydro_sign_update(&st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  257. sig[0]++;
  258. assert(hydro_sign_final_verify(&st, sig, kp.pk) == -1);
  259. hydro_sign_create(sig, msg, 0, ctx, kp.sk);
  260. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  261. assert(hydro_sign_verify(sig, msg, 0, ctx, kp.pk) == 0);
  262. }
  263. static void
  264. test_kx_n(void)
  265. {
  266. hydro_kx_keypair server_static_kp;
  267. uint8_t psk[hydro_kx_PSKBYTES];
  268. uint8_t packet1[hydro_kx_N_PACKET1BYTES];
  269. hydro_kx_session_keypair kp_client;
  270. hydro_kx_session_keypair kp_server;
  271. hydro_kx_keygen(&server_static_kp);
  272. hydro_random_buf(psk, sizeof psk);
  273. hydro_kx_n_1(&kp_client, packet1, psk, server_static_kp.pk);
  274. hydro_kx_n_2(&kp_server, packet1, psk, &server_static_kp);
  275. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  276. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  277. }
  278. static void
  279. test_kx_kk(void)
  280. {
  281. hydro_kx_state st_client;
  282. hydro_kx_keypair client_static_kp;
  283. hydro_kx_keypair server_static_kp;
  284. uint8_t packet1[hydro_kx_KK_PACKET1BYTES];
  285. uint8_t packet2[hydro_kx_KK_PACKET2BYTES];
  286. hydro_kx_session_keypair kp_client;
  287. hydro_kx_session_keypair kp_server;
  288. hydro_kx_keygen(&client_static_kp);
  289. hydro_kx_keygen(&server_static_kp);
  290. hydro_kx_kk_1(&st_client, packet1, server_static_kp.pk, &client_static_kp);
  291. hydro_kx_kk_2(&kp_server, packet2, packet1, client_static_kp.pk, &server_static_kp);
  292. hydro_kx_kk_3(&st_client, &kp_client, packet2, &client_static_kp);
  293. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  294. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  295. }
  296. static void
  297. test_kx_xx(void)
  298. {
  299. hydro_kx_state st_client;
  300. hydro_kx_state st_server;
  301. hydro_kx_keypair client_static_kp;
  302. hydro_kx_keypair server_static_kp;
  303. uint8_t psk[hydro_kx_PSKBYTES];
  304. uint8_t client_peer_pk[hydro_kx_PUBLICKEYBYTES];
  305. uint8_t server_peer_pk[hydro_kx_PUBLICKEYBYTES];
  306. uint8_t packet1[hydro_kx_XX_PACKET1BYTES];
  307. uint8_t packet2[hydro_kx_XX_PACKET2BYTES];
  308. uint8_t packet3[hydro_kx_XX_PACKET3BYTES];
  309. hydro_kx_session_keypair kp_client;
  310. hydro_kx_session_keypair kp_server;
  311. hydro_kx_keygen(&client_static_kp);
  312. hydro_kx_keygen(&server_static_kp);
  313. hydro_kx_xx_1(&st_client, packet1, NULL);
  314. hydro_kx_xx_2(&st_server, packet2, packet1, NULL, &server_static_kp);
  315. hydro_kx_xx_3(&st_client, &kp_client, packet3, NULL, packet2, NULL, &client_static_kp);
  316. hydro_kx_xx_4(&st_server, &kp_server, NULL, packet3, NULL);
  317. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  318. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  319. hydro_random_buf(psk, sizeof psk);
  320. hydro_kx_xx_1(&st_client, packet1, psk);
  321. hydro_kx_xx_2(&st_server, packet2, packet1, psk, &server_static_kp);
  322. hydro_kx_xx_3(&st_client, &kp_client, packet3, client_peer_pk, packet2, psk, &client_static_kp);
  323. hydro_kx_xx_4(&st_server, &kp_server, server_peer_pk, packet3, psk);
  324. assert(hydro_equal(kp_client.tx, kp_server.rx, hydro_kx_SESSIONKEYBYTES));
  325. assert(hydro_equal(kp_client.rx, kp_server.tx, hydro_kx_SESSIONKEYBYTES));
  326. assert(hydro_equal(client_peer_pk, server_static_kp.pk, hydro_kx_PUBLICKEYBYTES));
  327. assert(hydro_equal(server_peer_pk, client_static_kp.pk, hydro_kx_PUBLICKEYBYTES));
  328. }
  329. static void
  330. test_pwhash(void)
  331. {
  332. uint8_t master_key[hydro_pwhash_MASTERKEYBYTES];
  333. uint8_t new_master_key[hydro_pwhash_MASTERKEYBYTES];
  334. uint8_t stored[hydro_pwhash_STOREDBYTES];
  335. uint8_t h[64];
  336. uint8_t static_key[64];
  337. char h_hex[2 * 64 + 1];
  338. unsigned long long ops = 1000;
  339. memset(master_key, 'x', sizeof master_key);
  340. hydro_pwhash_deterministic(h, sizeof h, "test", sizeof "test" - 1, ctx, master_key, ops, 0, 1);
  341. hydro_bin2hex(h_hex, sizeof h_hex, h, sizeof h);
  342. if (ops == 1000) {
  343. assert_streq(
  344. "2f1a804a02f25066fd0688bf8b8e03dff3a3866958a9cf5883c459e602e232d38e3e488723f0b4a2bc61d2"
  345. "0cb36a04a4d2eb18be99bc61870d72d7de5d67f237",
  346. h_hex);
  347. }
  348. hydro_pwhash_keygen(master_key);
  349. assert(hydro_pwhash_create(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  350. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  351. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops * 2, 10, 10) ==
  352. 0);
  353. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops / 2, 10, 10) ==
  354. -1);
  355. assert(hydro_pwhash_verify(stored, "Test", sizeof "Test" - 1, master_key, ops, 0, 1) == -1);
  356. assert(hydro_pwhash_verify(stored, "test", sizeof "tes" - 1, master_key, ops, 0, 1) == -1);
  357. assert(hydro_pwhash_derive_static_key(static_key, sizeof static_key, stored, "test",
  358. sizeof "test" - 1, ctx, master_key, ops, 0, 1) == 0);
  359. assert(hydro_pwhash_derive_static_key(static_key, sizeof static_key, stored, "Test",
  360. sizeof "Test" - 1, ctx, master_key, ops, 0, 1) == -1);
  361. assert(hydro_pwhash_reencrypt(stored, master_key, master_key) == 0);
  362. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == 0);
  363. hydro_pwhash_keygen(new_master_key);
  364. assert(hydro_pwhash_reencrypt(stored, master_key, new_master_key) == 0);
  365. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, master_key, ops, 0, 1) == -1);
  366. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops, 0, 1) == 0);
  367. assert(hydro_pwhash_upgrade(stored, new_master_key, ops * 2, 0, 1) == 0);
  368. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops, 0, 1) == -1);
  369. assert(hydro_pwhash_verify(stored, "test", sizeof "test" - 1, new_master_key, ops * 2, 0, 1) ==
  370. 0);
  371. }
  372. int
  373. main(void)
  374. {
  375. #if defined(_WIN32)
  376. /*
  377. * On Windows, disable the "Abort - Retry - Ignore" GUI dialog that otherwise pops up on
  378. * assertion failure.
  379. */
  380. _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  381. #endif
  382. int ret;
  383. ret = hydro_init();
  384. assert(ret == 0);
  385. test_core();
  386. test_hash();
  387. test_kdf();
  388. test_kx_n();
  389. test_kx_kk();
  390. test_kx_xx();
  391. test_pwhash();
  392. test_randombytes();
  393. test_secretbox();
  394. test_sign();
  395. return 0;
  396. }