nhpoly1305.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
  4. *
  5. * Copyright 2018 Google LLC
  6. */
  7. /*
  8. * "NHPoly1305" is the main component of Adiantum hashing.
  9. * Specifically, it is the calculation
  10. *
  11. * H_L ← Poly1305_{K_L}(NH_{K_N}(pad_{128}(L)))
  12. *
  13. * from the procedure in section 6.4 of the Adiantum paper [1]. It is an
  14. * ε-almost-∆-universal (ε-∆U) hash function for equal-length inputs over
  15. * Z/(2^{128}Z), where the "∆" operation is addition. It hashes 1024-byte
  16. * chunks of the input with the NH hash function [2], reducing the input length
  17. * by 32x. The resulting NH digests are evaluated as a polynomial in
  18. * GF(2^{130}-5), like in the Poly1305 MAC [3]. Note that the polynomial
  19. * evaluation by itself would suffice to achieve the ε-∆U property; NH is used
  20. * for performance since it's over twice as fast as Poly1305.
  21. *
  22. * This is *not* a cryptographic hash function; do not use it as such!
  23. *
  24. * [1] Adiantum: length-preserving encryption for entry-level processors
  25. * (https://eprint.iacr.org/2018/720.pdf)
  26. * [2] UMAC: Fast and Secure Message Authentication
  27. * (https://fastcrypto.org/umac/umac_proc.pdf)
  28. * [3] The Poly1305-AES message-authentication code
  29. * (https://cr.yp.to/mac/poly1305-20050329.pdf)
  30. */
  31. #include <asm/unaligned.h>
  32. #include <crypto/algapi.h>
  33. #include <crypto/internal/hash.h>
  34. #include <crypto/nhpoly1305.h>
  35. #include <linux/crypto.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. static void nh_generic(const u32 *key, const u8 *message, size_t message_len,
  39. __le64 hash[NH_NUM_PASSES])
  40. {
  41. u64 sums[4] = { 0, 0, 0, 0 };
  42. BUILD_BUG_ON(NH_PAIR_STRIDE != 2);
  43. BUILD_BUG_ON(NH_NUM_PASSES != 4);
  44. while (message_len) {
  45. u32 m0 = get_unaligned_le32(message + 0);
  46. u32 m1 = get_unaligned_le32(message + 4);
  47. u32 m2 = get_unaligned_le32(message + 8);
  48. u32 m3 = get_unaligned_le32(message + 12);
  49. sums[0] += (u64)(u32)(m0 + key[ 0]) * (u32)(m2 + key[ 2]);
  50. sums[1] += (u64)(u32)(m0 + key[ 4]) * (u32)(m2 + key[ 6]);
  51. sums[2] += (u64)(u32)(m0 + key[ 8]) * (u32)(m2 + key[10]);
  52. sums[3] += (u64)(u32)(m0 + key[12]) * (u32)(m2 + key[14]);
  53. sums[0] += (u64)(u32)(m1 + key[ 1]) * (u32)(m3 + key[ 3]);
  54. sums[1] += (u64)(u32)(m1 + key[ 5]) * (u32)(m3 + key[ 7]);
  55. sums[2] += (u64)(u32)(m1 + key[ 9]) * (u32)(m3 + key[11]);
  56. sums[3] += (u64)(u32)(m1 + key[13]) * (u32)(m3 + key[15]);
  57. key += NH_MESSAGE_UNIT / sizeof(key[0]);
  58. message += NH_MESSAGE_UNIT;
  59. message_len -= NH_MESSAGE_UNIT;
  60. }
  61. hash[0] = cpu_to_le64(sums[0]);
  62. hash[1] = cpu_to_le64(sums[1]);
  63. hash[2] = cpu_to_le64(sums[2]);
  64. hash[3] = cpu_to_le64(sums[3]);
  65. }
  66. /* Pass the next NH hash value through Poly1305 */
  67. static void process_nh_hash_value(struct nhpoly1305_state *state,
  68. const struct nhpoly1305_key *key)
  69. {
  70. BUILD_BUG_ON(NH_HASH_BYTES % POLY1305_BLOCK_SIZE != 0);
  71. poly1305_core_blocks(&state->poly_state, &key->poly_key, state->nh_hash,
  72. NH_HASH_BYTES / POLY1305_BLOCK_SIZE);
  73. }
  74. /*
  75. * Feed the next portion of the source data, as a whole number of 16-byte
  76. * "NH message units", through NH and Poly1305. Each NH hash is taken over
  77. * 1024 bytes, except possibly the final one which is taken over a multiple of
  78. * 16 bytes up to 1024. Also, in the case where data is passed in misaligned
  79. * chunks, we combine partial hashes; the end result is the same either way.
  80. */
  81. static void nhpoly1305_units(struct nhpoly1305_state *state,
  82. const struct nhpoly1305_key *key,
  83. const u8 *src, unsigned int srclen, nh_t nh_fn)
  84. {
  85. do {
  86. unsigned int bytes;
  87. if (state->nh_remaining == 0) {
  88. /* Starting a new NH message */
  89. bytes = min_t(unsigned int, srclen, NH_MESSAGE_BYTES);
  90. nh_fn(key->nh_key, src, bytes, state->nh_hash);
  91. state->nh_remaining = NH_MESSAGE_BYTES - bytes;
  92. } else {
  93. /* Continuing a previous NH message */
  94. __le64 tmp_hash[NH_NUM_PASSES];
  95. unsigned int pos;
  96. int i;
  97. pos = NH_MESSAGE_BYTES - state->nh_remaining;
  98. bytes = min(srclen, state->nh_remaining);
  99. nh_fn(&key->nh_key[pos / 4], src, bytes, tmp_hash);
  100. for (i = 0; i < NH_NUM_PASSES; i++)
  101. le64_add_cpu(&state->nh_hash[i],
  102. le64_to_cpu(tmp_hash[i]));
  103. state->nh_remaining -= bytes;
  104. }
  105. if (state->nh_remaining == 0)
  106. process_nh_hash_value(state, key);
  107. src += bytes;
  108. srclen -= bytes;
  109. } while (srclen);
  110. }
  111. int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
  112. const u8 *key, unsigned int keylen)
  113. {
  114. struct nhpoly1305_key *ctx = crypto_shash_ctx(tfm);
  115. int i;
  116. if (keylen != NHPOLY1305_KEY_SIZE)
  117. return -EINVAL;
  118. poly1305_core_setkey(&ctx->poly_key, key);
  119. key += POLY1305_BLOCK_SIZE;
  120. for (i = 0; i < NH_KEY_WORDS; i++)
  121. ctx->nh_key[i] = get_unaligned_le32(key + i * sizeof(u32));
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(crypto_nhpoly1305_setkey);
  125. int crypto_nhpoly1305_init(struct shash_desc *desc)
  126. {
  127. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  128. poly1305_core_init(&state->poly_state);
  129. state->buflen = 0;
  130. state->nh_remaining = 0;
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(crypto_nhpoly1305_init);
  134. int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
  135. const u8 *src, unsigned int srclen,
  136. nh_t nh_fn)
  137. {
  138. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  139. const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
  140. unsigned int bytes;
  141. if (state->buflen) {
  142. bytes = min(srclen, (int)NH_MESSAGE_UNIT - state->buflen);
  143. memcpy(&state->buffer[state->buflen], src, bytes);
  144. state->buflen += bytes;
  145. if (state->buflen < NH_MESSAGE_UNIT)
  146. return 0;
  147. nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
  148. nh_fn);
  149. state->buflen = 0;
  150. src += bytes;
  151. srclen -= bytes;
  152. }
  153. if (srclen >= NH_MESSAGE_UNIT) {
  154. bytes = round_down(srclen, NH_MESSAGE_UNIT);
  155. nhpoly1305_units(state, key, src, bytes, nh_fn);
  156. src += bytes;
  157. srclen -= bytes;
  158. }
  159. if (srclen) {
  160. memcpy(state->buffer, src, srclen);
  161. state->buflen = srclen;
  162. }
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(crypto_nhpoly1305_update_helper);
  166. int crypto_nhpoly1305_update(struct shash_desc *desc,
  167. const u8 *src, unsigned int srclen)
  168. {
  169. return crypto_nhpoly1305_update_helper(desc, src, srclen, nh_generic);
  170. }
  171. EXPORT_SYMBOL(crypto_nhpoly1305_update);
  172. int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst, nh_t nh_fn)
  173. {
  174. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  175. const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
  176. if (state->buflen) {
  177. memset(&state->buffer[state->buflen], 0,
  178. NH_MESSAGE_UNIT - state->buflen);
  179. nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
  180. nh_fn);
  181. }
  182. if (state->nh_remaining)
  183. process_nh_hash_value(state, key);
  184. poly1305_core_emit(&state->poly_state, dst);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(crypto_nhpoly1305_final_helper);
  188. int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst)
  189. {
  190. return crypto_nhpoly1305_final_helper(desc, dst, nh_generic);
  191. }
  192. EXPORT_SYMBOL(crypto_nhpoly1305_final);
  193. static struct shash_alg nhpoly1305_alg = {
  194. .base.cra_name = "nhpoly1305",
  195. .base.cra_driver_name = "nhpoly1305-generic",
  196. .base.cra_priority = 100,
  197. .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
  198. .base.cra_module = THIS_MODULE,
  199. .digestsize = POLY1305_DIGEST_SIZE,
  200. .init = crypto_nhpoly1305_init,
  201. .update = crypto_nhpoly1305_update,
  202. .final = crypto_nhpoly1305_final,
  203. .setkey = crypto_nhpoly1305_setkey,
  204. .descsize = sizeof(struct nhpoly1305_state),
  205. };
  206. static int __init nhpoly1305_mod_init(void)
  207. {
  208. return crypto_register_shash(&nhpoly1305_alg);
  209. }
  210. static void __exit nhpoly1305_mod_exit(void)
  211. {
  212. crypto_unregister_shash(&nhpoly1305_alg);
  213. }
  214. module_init(nhpoly1305_mod_init);
  215. module_exit(nhpoly1305_mod_exit);
  216. MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function");
  217. MODULE_LICENSE("GPL v2");
  218. MODULE_AUTHOR("Eric Biggers <[email protected]>");
  219. MODULE_ALIAS_CRYPTO("nhpoly1305");
  220. MODULE_ALIAS_CRYPTO("nhpoly1305-generic");