secretbox.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #define hydro_secretbox_IVBYTES 20
  2. #define hydro_secretbox_SIVBYTES 20
  3. #define hydro_secretbox_MACBYTES 16
  4. void
  5. hydro_secretbox_keygen(uint8_t key[hydro_secretbox_KEYBYTES])
  6. {
  7. hydro_random_buf(key, hydro_secretbox_KEYBYTES);
  8. }
  9. static void
  10. hydro_secretbox_xor_enc(uint8_t buf[gimli_BLOCKBYTES], uint8_t *out, const uint8_t *in,
  11. size_t inlen)
  12. {
  13. size_t i;
  14. size_t leftover;
  15. for (i = 0; i < inlen / gimli_RATE; i++) {
  16. mem_xor2(&out[i * gimli_RATE], &in[i * gimli_RATE], buf, gimli_RATE);
  17. memcpy(buf, &out[i * gimli_RATE], gimli_RATE);
  18. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  19. }
  20. leftover = inlen % gimli_RATE;
  21. if (leftover != 0) {
  22. mem_xor2(&out[i * gimli_RATE], &in[i * gimli_RATE], buf, leftover);
  23. mem_cpy(buf, &out[i * gimli_RATE], leftover);
  24. }
  25. gimli_pad_u8(buf, leftover, gimli_DOMAIN_AEAD);
  26. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  27. }
  28. static void
  29. hydro_secretbox_xor_dec(uint8_t buf[gimli_BLOCKBYTES], uint8_t *out, const uint8_t *in,
  30. size_t inlen)
  31. {
  32. size_t i;
  33. size_t leftover;
  34. for (i = 0; i < inlen / gimli_RATE; i++) {
  35. mem_xor2(&out[i * gimli_RATE], &in[i * gimli_RATE], buf, gimli_RATE);
  36. memcpy(buf, &in[i * gimli_RATE], gimli_RATE);
  37. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  38. }
  39. leftover = inlen % gimli_RATE;
  40. if (leftover != 0) {
  41. mem_xor2(&out[i * gimli_RATE], &in[i * gimli_RATE], buf, leftover);
  42. mem_cpy(buf, &in[i * gimli_RATE], leftover);
  43. }
  44. gimli_pad_u8(buf, leftover, gimli_DOMAIN_AEAD);
  45. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  46. }
  47. static void
  48. hydro_secretbox_setup(uint8_t buf[gimli_BLOCKBYTES], uint64_t msg_id,
  49. const char ctx[hydro_secretbox_CONTEXTBYTES],
  50. const uint8_t key[hydro_secretbox_KEYBYTES],
  51. const uint8_t iv[hydro_secretbox_IVBYTES], uint8_t key_tag)
  52. {
  53. static const uint8_t prefix[] = { 6, 's', 'b', 'x', '2', '5', '6', 8 };
  54. uint8_t msg_id_le[8];
  55. mem_zero(buf, gimli_BLOCKBYTES);
  56. COMPILER_ASSERT(hydro_secretbox_CONTEXTBYTES == 8);
  57. COMPILER_ASSERT(sizeof prefix + hydro_secretbox_CONTEXTBYTES <= gimli_RATE);
  58. memcpy(buf, prefix, sizeof prefix);
  59. memcpy(buf + sizeof prefix, ctx, hydro_secretbox_CONTEXTBYTES);
  60. COMPILER_ASSERT(sizeof prefix + hydro_secretbox_CONTEXTBYTES == gimli_RATE);
  61. gimli_core_u8(buf, gimli_TAG_HEADER);
  62. COMPILER_ASSERT(hydro_secretbox_KEYBYTES == 2 * gimli_RATE);
  63. mem_xor(buf, key, gimli_RATE);
  64. gimli_core_u8(buf, key_tag);
  65. mem_xor(buf, key + gimli_RATE, gimli_RATE);
  66. gimli_core_u8(buf, key_tag);
  67. COMPILER_ASSERT(hydro_secretbox_IVBYTES < gimli_RATE * 2);
  68. buf[0] ^= hydro_secretbox_IVBYTES;
  69. mem_xor(&buf[1], iv, gimli_RATE - 1);
  70. gimli_core_u8(buf, gimli_TAG_HEADER);
  71. mem_xor(buf, iv + gimli_RATE - 1, hydro_secretbox_IVBYTES - (gimli_RATE - 1));
  72. STORE64_LE(msg_id_le, msg_id);
  73. COMPILER_ASSERT(hydro_secretbox_IVBYTES - gimli_RATE + 8 <= gimli_RATE);
  74. mem_xor(buf + hydro_secretbox_IVBYTES - gimli_RATE, msg_id_le, 8);
  75. gimli_core_u8(buf, gimli_TAG_HEADER);
  76. }
  77. static void
  78. hydro_secretbox_finalize(uint8_t *buf, const uint8_t key[hydro_secretbox_KEYBYTES], uint8_t tag)
  79. {
  80. COMPILER_ASSERT(hydro_secretbox_KEYBYTES == gimli_CAPACITY);
  81. mem_xor(buf + gimli_RATE, key, hydro_secretbox_KEYBYTES);
  82. gimli_core_u8(buf, tag);
  83. mem_xor(buf + gimli_RATE, key, hydro_secretbox_KEYBYTES);
  84. gimli_core_u8(buf, tag);
  85. }
  86. static int
  87. hydro_secretbox_encrypt_iv(uint8_t *c, const void *m_, size_t mlen, uint64_t msg_id,
  88. const char ctx[hydro_secretbox_CONTEXTBYTES],
  89. const uint8_t key[hydro_secretbox_KEYBYTES],
  90. const uint8_t iv[hydro_secretbox_IVBYTES])
  91. {
  92. _hydro_attr_aligned_(16) uint32_t state[gimli_BLOCKBYTES / 4];
  93. uint8_t * buf = (uint8_t *) (void *) state;
  94. const uint8_t * m = (const uint8_t *) m_;
  95. uint8_t * siv = &c[0];
  96. uint8_t * mac = &c[hydro_secretbox_SIVBYTES];
  97. uint8_t * ct = &c[hydro_secretbox_SIVBYTES + hydro_secretbox_MACBYTES];
  98. size_t i;
  99. size_t leftover;
  100. if (c == m) {
  101. memmove(c + hydro_secretbox_HEADERBYTES, m, mlen);
  102. m = c + hydro_secretbox_HEADERBYTES;
  103. }
  104. /* first pass: compute the SIV */
  105. hydro_secretbox_setup(buf, msg_id, ctx, key, iv, gimli_TAG_KEY0);
  106. for (i = 0; i < mlen / gimli_RATE; i++) {
  107. mem_xor(buf, &m[i * gimli_RATE], gimli_RATE);
  108. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  109. }
  110. leftover = mlen % gimli_RATE;
  111. if (leftover != 0) {
  112. mem_xor(buf, &m[i * gimli_RATE], leftover);
  113. }
  114. gimli_pad_u8(buf, leftover, gimli_DOMAIN_XOF);
  115. gimli_core_u8(buf, gimli_TAG_PAYLOAD);
  116. hydro_secretbox_finalize(buf, key, gimli_TAG_FINAL0);
  117. COMPILER_ASSERT(hydro_secretbox_SIVBYTES <= gimli_CAPACITY);
  118. memcpy(siv, buf + gimli_RATE, hydro_secretbox_SIVBYTES);
  119. /* second pass: encrypt the message, mix the key, squeeze an extra block for
  120. * the MAC */
  121. COMPILER_ASSERT(hydro_secretbox_SIVBYTES == hydro_secretbox_IVBYTES);
  122. hydro_secretbox_setup(buf, msg_id, ctx, key, siv, gimli_TAG_KEY);
  123. hydro_secretbox_xor_enc(buf, ct, m, mlen);
  124. hydro_secretbox_finalize(buf, key, gimli_TAG_FINAL);
  125. COMPILER_ASSERT(hydro_secretbox_MACBYTES <= gimli_CAPACITY);
  126. memcpy(mac, buf + gimli_RATE, hydro_secretbox_MACBYTES);
  127. return 0;
  128. }
  129. void
  130. hydro_secretbox_probe_create(uint8_t probe[hydro_secretbox_PROBEBYTES], const uint8_t *c,
  131. size_t c_len, const char ctx[hydro_secretbox_CONTEXTBYTES],
  132. const uint8_t key[hydro_secretbox_KEYBYTES])
  133. {
  134. const uint8_t *mac;
  135. if (c_len < hydro_secretbox_HEADERBYTES) {
  136. abort();
  137. }
  138. mac = &c[hydro_secretbox_SIVBYTES];
  139. COMPILER_ASSERT(hydro_secretbox_CONTEXTBYTES >= hydro_hash_CONTEXTBYTES);
  140. COMPILER_ASSERT(hydro_secretbox_KEYBYTES >= hydro_hash_KEYBYTES);
  141. hydro_hash_hash(probe, hydro_secretbox_PROBEBYTES, mac, hydro_secretbox_MACBYTES, ctx, key);
  142. }
  143. int
  144. hydro_secretbox_probe_verify(const uint8_t probe[hydro_secretbox_PROBEBYTES], const uint8_t *c,
  145. size_t c_len, const char ctx[hydro_secretbox_CONTEXTBYTES],
  146. const uint8_t key[hydro_secretbox_KEYBYTES])
  147. {
  148. uint8_t computed_probe[hydro_secretbox_PROBEBYTES];
  149. const uint8_t *mac;
  150. if (c_len < hydro_secretbox_HEADERBYTES) {
  151. return -1;
  152. }
  153. mac = &c[hydro_secretbox_SIVBYTES];
  154. hydro_hash_hash(computed_probe, hydro_secretbox_PROBEBYTES, mac, hydro_secretbox_MACBYTES, ctx,
  155. key);
  156. if (hydro_equal(computed_probe, probe, hydro_secretbox_PROBEBYTES) == 1) {
  157. return 0;
  158. }
  159. hydro_memzero(computed_probe, hydro_secretbox_PROBEBYTES);
  160. return -1;
  161. }
  162. int
  163. hydro_secretbox_encrypt(uint8_t *c, const void *m_, size_t mlen, uint64_t msg_id,
  164. const char ctx[hydro_secretbox_CONTEXTBYTES],
  165. const uint8_t key[hydro_secretbox_KEYBYTES])
  166. {
  167. uint8_t iv[hydro_secretbox_IVBYTES];
  168. hydro_random_buf(iv, sizeof iv);
  169. return hydro_secretbox_encrypt_iv(c, m_, mlen, msg_id, ctx, key, iv);
  170. }
  171. int
  172. hydro_secretbox_decrypt(void *m_, const uint8_t *c, size_t clen, uint64_t msg_id,
  173. const char ctx[hydro_secretbox_CONTEXTBYTES],
  174. const uint8_t key[hydro_secretbox_KEYBYTES])
  175. {
  176. _hydro_attr_aligned_(16) uint32_t state[gimli_BLOCKBYTES / 4];
  177. uint32_t pub_mac[hydro_secretbox_MACBYTES / 4];
  178. uint8_t * buf = (uint8_t *) (void *) state;
  179. const uint8_t * siv;
  180. const uint8_t * mac;
  181. const uint8_t * ct;
  182. uint8_t * m = (uint8_t *) m_;
  183. size_t mlen;
  184. uint32_t cv;
  185. if (clen < hydro_secretbox_HEADERBYTES) {
  186. return -1;
  187. }
  188. siv = &c[0];
  189. mac = &c[hydro_secretbox_SIVBYTES];
  190. ct = &c[hydro_secretbox_SIVBYTES + hydro_secretbox_MACBYTES];
  191. mlen = clen - hydro_secretbox_HEADERBYTES;
  192. memcpy(pub_mac, mac, sizeof pub_mac);
  193. COMPILER_ASSERT(hydro_secretbox_SIVBYTES == hydro_secretbox_IVBYTES);
  194. hydro_secretbox_setup(buf, msg_id, ctx, key, siv, gimli_TAG_KEY);
  195. hydro_secretbox_xor_dec(buf, m, ct, mlen);
  196. hydro_secretbox_finalize(buf, key, gimli_TAG_FINAL);
  197. COMPILER_ASSERT(hydro_secretbox_MACBYTES <= gimli_CAPACITY);
  198. COMPILER_ASSERT(gimli_RATE % 4 == 0);
  199. cv = hydro_mem_ct_cmp_u32(state + gimli_RATE / 4, pub_mac, hydro_secretbox_MACBYTES / 4);
  200. hydro_mem_ct_zero_u32(state, gimli_BLOCKBYTES / 4);
  201. if (cv != 0) {
  202. mem_zero(m, mlen);
  203. return -1;
  204. }
  205. return 0;
  206. }