tests.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../hydrogen.h"
  5. static const char *ctx = "libtests";
  6. static void test_randombytes(void)
  7. {
  8. uint8_t dk[randombytes_SEEDBYTES];
  9. uint8_t tmp[10000];
  10. unsigned long b = 0U;
  11. unsigned long bp;
  12. uint32_t x;
  13. size_t i, j;
  14. for (i = 0; i < 10000; i++) {
  15. x = randombytes_random();
  16. for (j = 0; j < sizeof x; j++) {
  17. b += (x >> j) & 1;
  18. }
  19. }
  20. assert(b > 18000 && b < 22000);
  21. b = 0;
  22. randombytes_buf(tmp, sizeof tmp);
  23. for (i = 0; i < 10000; i++) {
  24. for (j = 0; j < sizeof tmp[0]; j++) {
  25. b += (tmp[i] >> j) & 1;
  26. }
  27. }
  28. assert(b > 4500 && b < 5500);
  29. memcpy(dk, tmp, sizeof dk);
  30. b = 0;
  31. randombytes_buf_deterministic(tmp, 10000, dk);
  32. for (i = 0; i < 10000; i++) {
  33. for (j = 0; j < sizeof tmp[0]; j++) {
  34. b += (tmp[i] >> j) & 1;
  35. }
  36. }
  37. assert(b > 4500 && b < 5500);
  38. bp = b;
  39. b = 0;
  40. randombytes_buf_deterministic(tmp, 10000, dk);
  41. for (i = 0; i < 10000; i++) {
  42. for (j = 0; j < sizeof tmp[0]; j++) {
  43. b += (tmp[i] >> j) & 1;
  44. }
  45. }
  46. assert(b == bp);
  47. for (i = 0; i < 1000; i++) {
  48. for (j = 1; j < 100; j++) {
  49. x = randombytes_uniform((uint32_t) j);
  50. assert(x < j);
  51. }
  52. }
  53. }
  54. static void test_hash(void)
  55. {
  56. hydro_hash_state st;
  57. uint8_t dk[randombytes_SEEDBYTES];
  58. uint8_t h[100];
  59. uint8_t key[hydro_hash_KEYBYTES_MAX];
  60. uint8_t msg[1000];
  61. char hex[100 * 2 + 1];
  62. size_t i;
  63. memset(dk, 0, sizeof dk);
  64. randombytes_buf_deterministic(key, sizeof key, dk);
  65. hydro_increment(dk, sizeof dk);
  66. hydro_hash_init(&st, ctx, key, sizeof key, sizeof h);
  67. for (i = 0; i <= sizeof msg; i++) {
  68. randombytes_buf_deterministic(msg, i, dk);
  69. hydro_increment(dk, sizeof dk);
  70. hydro_hash_update(&st, msg, i);
  71. }
  72. hydro_hash_final(&st, h, sizeof h);
  73. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  74. assert(hydro_equal("7137d87ad55fdf061789e1e8bebf7572525d4d08f7f4371a960b02c"
  75. "6242724a71cd2a88d50c32bc9e118044a2d539c01b5cccc3b52e67f"
  76. "c5eae283de5dcbd501a933c4b9c5aaddbaf81693ec485811459e6ed"
  77. "b5257a7573a573525d9f1874e71556d9ad3",
  78. hex, sizeof hex));
  79. hydro_hash_hash(h, sizeof h, msg, sizeof msg, ctx, key, sizeof key);
  80. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  81. assert(hydro_equal("b453c8627089d4b38ac87a5a89503c5a5740eb4206be37d4c5c50e9"
  82. "56b3d56f0bc96b14a87061cce9bb69b89f57a0d13dab0ad7b498513"
  83. "9492d8cd8eaea4e1c59f971a8cc817e62485c2a0c19faa7b3296700"
  84. "9a16a405a679970637f8f11536db65c416f",
  85. hex, sizeof hex));
  86. hydro_hash_hash(h, hydro_hash_BYTES, msg, sizeof msg, ctx, key, sizeof key);
  87. hydro_bin2hex(hex, sizeof hex, h, hydro_hash_BYTES);
  88. assert(hydro_equal(
  89. "f13a6f6e56c7799bdf6cb2f7787419a703cd617110b67d951ed04b3ddde7fde8", hex,
  90. strlen(hex) + 1));
  91. }
  92. static void test_hash128(void)
  93. {
  94. hydro_hash128_state st;
  95. uint8_t dk[randombytes_SEEDBYTES];
  96. uint8_t h[hydro_hash128_BYTES];
  97. uint8_t key[hydro_hash128_KEYBYTES];
  98. uint8_t msg[1000];
  99. char hex[hydro_hash128_BYTES * 2 + 1];
  100. size_t i;
  101. memset(dk, 0, sizeof dk);
  102. randombytes_buf_deterministic(key, sizeof key, dk);
  103. hydro_increment(dk, sizeof dk);
  104. hydro_hash128_init(&st, ctx, key);
  105. for (i = 0; i <= sizeof msg; i++) {
  106. randombytes_buf_deterministic(msg, i, dk);
  107. hydro_increment(dk, sizeof dk);
  108. hydro_hash128_update(&st, msg, i);
  109. }
  110. hydro_hash128_final(&st, h);
  111. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  112. assert(hydro_equal("3d266a5ae418ba26607d611d49942567", hex, sizeof hex));
  113. hydro_hash128_hash(h, msg, sizeof msg, ctx, key);
  114. hydro_bin2hex(hex, sizeof hex, h, sizeof h);
  115. assert(hydro_equal("d4a32e2f38ab334dd4d4e252bb46d76b", hex, sizeof hex));
  116. }
  117. static void test_core(void)
  118. {
  119. uint8_t x[100];
  120. uint8_t y[100];
  121. uint8_t a[5] = { 1, 2, 3, 4, 5 };
  122. uint8_t b[5] = { 1, 2, 3, 4, 5 };
  123. char hex[201];
  124. memset(x, 0xd0, sizeof x);
  125. hydro_memzero(x, sizeof x);
  126. assert(x[0] == 0);
  127. assert(x[sizeof x - 1] == 0);
  128. hydro_increment(x, sizeof x);
  129. assert(x[0] == 1);
  130. assert(x[sizeof x - 1] == 0);
  131. x[0] = 0xff;
  132. hydro_increment(x, sizeof x);
  133. assert(x[0] == 0);
  134. assert(x[1] == 1);
  135. assert(x[sizeof x - 1] == 0);
  136. assert(hydro_equal(a, b, sizeof a));
  137. assert(!hydro_equal(a, a, sizeof a));
  138. assert(hydro_compare(a, b, sizeof a) == 0);
  139. assert(hydro_compare(a, a, sizeof a) == 0);
  140. a[0]++;
  141. assert(hydro_compare(a, b, sizeof a) == 1);
  142. assert(hydro_compare(b, a, sizeof a) == -1);
  143. randombytes_buf(x, sizeof x);
  144. assert(hydro_bin2hex(hex, sizeof hex, x, sizeof x) != NULL);
  145. assert(hydro_hex2bin(y, 1, hex, sizeof hex, NULL, NULL, NULL) == -1);
  146. assert(hydro_hex2bin(y, sizeof y, hex, sizeof hex, NULL, NULL, NULL) == 0);
  147. assert(hydro_equal(x, y, sizeof x));
  148. }
  149. static void test_secretbox(void)
  150. {
  151. uint8_t key[hydro_secretbox_KEYBYTES];
  152. uint8_t m[25];
  153. uint8_t m2[25];
  154. uint8_t c[hydro_secretbox_HEADERBYTES + 25];
  155. uint8_t dk[randombytes_SEEDBYTES];
  156. memset(dk, 0, sizeof dk);
  157. randombytes_buf_deterministic(m, sizeof m, dk);
  158. hydro_increment(dk, sizeof dk);
  159. randombytes_buf_deterministic(key, sizeof key, dk);
  160. hydro_increment(dk, sizeof dk);
  161. hydro_secretbox_encrypt(c, m, sizeof m, 0, ctx, key);
  162. assert(hydro_secretbox_decrypt(m2, c, 0, 0, ctx, key) == -1);
  163. assert(hydro_secretbox_decrypt(m2, c, 1, 0, ctx, key) == -1);
  164. assert(hydro_secretbox_decrypt(
  165. m2, c, hydro_secretbox_HEADERBYTES, 0, ctx, key) == -1);
  166. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == 0);
  167. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 1, ctx, key) == -1);
  168. assert(hydro_equal(m, m2, sizeof m));
  169. key[0]++;
  170. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  171. key[0]--;
  172. c[randombytes_uniform(sizeof c)]++;
  173. assert(hydro_secretbox_decrypt(m2, c, sizeof c, 0, ctx, key) == -1);
  174. }
  175. static void test_kdf(void)
  176. {
  177. uint8_t key[hydro_kdf_KEYBYTES];
  178. uint8_t dk[randombytes_SEEDBYTES];
  179. uint8_t subkey1[16];
  180. uint8_t subkey2[16];
  181. uint8_t subkey3[32];
  182. uint8_t subkey4[50];
  183. char subkey1_hex[16 * 2 + 1];
  184. char subkey2_hex[16 * 2 + 1];
  185. char subkey3_hex[32 * 2 + 1];
  186. char subkey4_hex[50 * 2 + 1];
  187. memset(dk, 0, sizeof dk);
  188. randombytes_buf_deterministic(key, sizeof key, dk);
  189. hydro_kdf_derive_from_key(subkey1, sizeof subkey1, 1, ctx, key);
  190. hydro_kdf_derive_from_key(subkey2, sizeof subkey2, 2, ctx, key);
  191. hydro_kdf_derive_from_key(subkey3, sizeof subkey3, 0, ctx, key);
  192. hydro_kdf_derive_from_key(subkey4, sizeof subkey4, 0, ctx, key);
  193. hydro_bin2hex(subkey1_hex, sizeof subkey1_hex, subkey1, sizeof subkey1);
  194. hydro_bin2hex(subkey2_hex, sizeof subkey2_hex, subkey2, sizeof subkey2);
  195. hydro_bin2hex(subkey3_hex, sizeof subkey3_hex, subkey3, sizeof subkey3);
  196. hydro_bin2hex(subkey4_hex, sizeof subkey4_hex, subkey4, sizeof subkey4);
  197. assert(hydro_equal(
  198. "b6dadc6c3594b305fac7160e89fb628e", subkey1_hex, sizeof subkey1_hex));
  199. assert(hydro_equal(
  200. "baf03b412086d54067cac64f583bcd16", subkey2_hex, sizeof subkey2_hex));
  201. assert(hydro_equal(
  202. "d583dc8833f19dbd544f057d0cebaed5507306a134361119d4e4eb172d903be3",
  203. subkey3_hex, sizeof subkey3_hex));
  204. assert(hydro_equal("ef289ae126182038ae57ab4c07f0eab94676f85f5462cffd2586fa6"
  205. "ae881c2eacd863c8f3335abb70ced9d5360462d693ec1",
  206. subkey4_hex, sizeof subkey4_hex));
  207. }
  208. static void test_sign(void)
  209. {
  210. uint8_t msg[500];
  211. uint8_t sig[hydro_sign_BYTES];
  212. hydro_sign_state st;
  213. hydro_sign_keypair kp;
  214. randombytes_buf(msg, sizeof msg);
  215. hydro_sign_keygen(&kp);
  216. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  217. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == 0);
  218. sig[0]++;
  219. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  220. sig[0]--;
  221. sig[hydro_sign_BYTES - 1]++;
  222. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  223. sig[hydro_sign_BYTES - 1]--;
  224. msg[0]++;
  225. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  226. msg[0]++;
  227. hydro_sign_create(sig, msg, sizeof msg, ctx, kp.sk);
  228. hydro_sign_init(&st, ctx);
  229. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  230. hydro_sign_update(
  231. &st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  232. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  233. hydro_sign_init(&st, ctx);
  234. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  235. hydro_sign_update(
  236. &st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  237. hydro_sign_final_create(&st, sig, kp.sk);
  238. hydro_sign_init(&st, ctx);
  239. hydro_sign_update(&st, msg, (sizeof msg) / 3);
  240. hydro_sign_update(
  241. &st, msg + (sizeof msg) / 3, (sizeof msg) - (sizeof msg) / 3);
  242. assert(hydro_sign_final_verify(&st, sig, kp.pk) == 0);
  243. sig[0]++;
  244. assert(hydro_sign_final_verify(&st, sig, kp.pk) == -1);
  245. hydro_sign_create(sig, msg, 0, ctx, kp.sk);
  246. assert(hydro_sign_verify(sig, msg, sizeof msg, ctx, kp.pk) == -1);
  247. assert(hydro_sign_verify(sig, msg, 0, ctx, kp.pk) == 0);
  248. }
  249. int main(void)
  250. {
  251. int ret;
  252. ret = hydro_init();
  253. assert(ret == 0);
  254. test_core();
  255. test_hash();
  256. test_hash128();
  257. test_kdf();
  258. test_randombytes();
  259. test_secretbox();
  260. test_sign();
  261. return 0;
  262. }