nhpoly1305.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/internal/poly1305.h>
  35. #include <crypto/nhpoly1305.h>
  36. #include <linux/crypto.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. static void nh_generic(const u32 *key, const u8 *message, size_t message_len,
  40. __le64 hash[NH_NUM_PASSES])
  41. {
  42. u64 sums[4] = { 0, 0, 0, 0 };
  43. BUILD_BUG_ON(NH_PAIR_STRIDE != 2);
  44. BUILD_BUG_ON(NH_NUM_PASSES != 4);
  45. while (message_len) {
  46. u32 m0 = get_unaligned_le32(message + 0);
  47. u32 m1 = get_unaligned_le32(message + 4);
  48. u32 m2 = get_unaligned_le32(message + 8);
  49. u32 m3 = get_unaligned_le32(message + 12);
  50. sums[0] += (u64)(u32)(m0 + key[ 0]) * (u32)(m2 + key[ 2]);
  51. sums[1] += (u64)(u32)(m0 + key[ 4]) * (u32)(m2 + key[ 6]);
  52. sums[2] += (u64)(u32)(m0 + key[ 8]) * (u32)(m2 + key[10]);
  53. sums[3] += (u64)(u32)(m0 + key[12]) * (u32)(m2 + key[14]);
  54. sums[0] += (u64)(u32)(m1 + key[ 1]) * (u32)(m3 + key[ 3]);
  55. sums[1] += (u64)(u32)(m1 + key[ 5]) * (u32)(m3 + key[ 7]);
  56. sums[2] += (u64)(u32)(m1 + key[ 9]) * (u32)(m3 + key[11]);
  57. sums[3] += (u64)(u32)(m1 + key[13]) * (u32)(m3 + key[15]);
  58. key += NH_MESSAGE_UNIT / sizeof(key[0]);
  59. message += NH_MESSAGE_UNIT;
  60. message_len -= NH_MESSAGE_UNIT;
  61. }
  62. hash[0] = cpu_to_le64(sums[0]);
  63. hash[1] = cpu_to_le64(sums[1]);
  64. hash[2] = cpu_to_le64(sums[2]);
  65. hash[3] = cpu_to_le64(sums[3]);
  66. }
  67. /* Pass the next NH hash value through Poly1305 */
  68. static void process_nh_hash_value(struct nhpoly1305_state *state,
  69. const struct nhpoly1305_key *key)
  70. {
  71. BUILD_BUG_ON(NH_HASH_BYTES % POLY1305_BLOCK_SIZE != 0);
  72. poly1305_core_blocks(&state->poly_state, &key->poly_key, state->nh_hash,
  73. NH_HASH_BYTES / POLY1305_BLOCK_SIZE, 1);
  74. }
  75. /*
  76. * Feed the next portion of the source data, as a whole number of 16-byte
  77. * "NH message units", through NH and Poly1305. Each NH hash is taken over
  78. * 1024 bytes, except possibly the final one which is taken over a multiple of
  79. * 16 bytes up to 1024. Also, in the case where data is passed in misaligned
  80. * chunks, we combine partial hashes; the end result is the same either way.
  81. */
  82. static void nhpoly1305_units(struct nhpoly1305_state *state,
  83. const struct nhpoly1305_key *key,
  84. const u8 *src, unsigned int srclen, nh_t nh_fn)
  85. {
  86. do {
  87. unsigned int bytes;
  88. if (state->nh_remaining == 0) {
  89. /* Starting a new NH message */
  90. bytes = min_t(unsigned int, srclen, NH_MESSAGE_BYTES);
  91. nh_fn(key->nh_key, src, bytes, state->nh_hash);
  92. state->nh_remaining = NH_MESSAGE_BYTES - bytes;
  93. } else {
  94. /* Continuing a previous NH message */
  95. __le64 tmp_hash[NH_NUM_PASSES];
  96. unsigned int pos;
  97. int i;
  98. pos = NH_MESSAGE_BYTES - state->nh_remaining;
  99. bytes = min(srclen, state->nh_remaining);
  100. nh_fn(&key->nh_key[pos / 4], src, bytes, tmp_hash);
  101. for (i = 0; i < NH_NUM_PASSES; i++)
  102. le64_add_cpu(&state->nh_hash[i],
  103. le64_to_cpu(tmp_hash[i]));
  104. state->nh_remaining -= bytes;
  105. }
  106. if (state->nh_remaining == 0)
  107. process_nh_hash_value(state, key);
  108. src += bytes;
  109. srclen -= bytes;
  110. } while (srclen);
  111. }
  112. int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
  113. const u8 *key, unsigned int keylen)
  114. {
  115. struct nhpoly1305_key *ctx = crypto_shash_ctx(tfm);
  116. int i;
  117. if (keylen != NHPOLY1305_KEY_SIZE)
  118. return -EINVAL;
  119. poly1305_core_setkey(&ctx->poly_key, key);
  120. key += POLY1305_BLOCK_SIZE;
  121. for (i = 0; i < NH_KEY_WORDS; i++)
  122. ctx->nh_key[i] = get_unaligned_le32(key + i * sizeof(u32));
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(crypto_nhpoly1305_setkey);
  126. int crypto_nhpoly1305_init(struct shash_desc *desc)
  127. {
  128. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  129. poly1305_core_init(&state->poly_state);
  130. state->buflen = 0;
  131. state->nh_remaining = 0;
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(crypto_nhpoly1305_init);
  135. int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
  136. const u8 *src, unsigned int srclen,
  137. nh_t nh_fn)
  138. {
  139. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  140. const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
  141. unsigned int bytes;
  142. if (state->buflen) {
  143. bytes = min(srclen, (int)NH_MESSAGE_UNIT - state->buflen);
  144. memcpy(&state->buffer[state->buflen], src, bytes);
  145. state->buflen += bytes;
  146. if (state->buflen < NH_MESSAGE_UNIT)
  147. return 0;
  148. nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
  149. nh_fn);
  150. state->buflen = 0;
  151. src += bytes;
  152. srclen -= bytes;
  153. }
  154. if (srclen >= NH_MESSAGE_UNIT) {
  155. bytes = round_down(srclen, NH_MESSAGE_UNIT);
  156. nhpoly1305_units(state, key, src, bytes, nh_fn);
  157. src += bytes;
  158. srclen -= bytes;
  159. }
  160. if (srclen) {
  161. memcpy(state->buffer, src, srclen);
  162. state->buflen = srclen;
  163. }
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(crypto_nhpoly1305_update_helper);
  167. int crypto_nhpoly1305_update(struct shash_desc *desc,
  168. const u8 *src, unsigned int srclen)
  169. {
  170. return crypto_nhpoly1305_update_helper(desc, src, srclen, nh_generic);
  171. }
  172. EXPORT_SYMBOL(crypto_nhpoly1305_update);
  173. int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst, nh_t nh_fn)
  174. {
  175. struct nhpoly1305_state *state = shash_desc_ctx(desc);
  176. const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
  177. if (state->buflen) {
  178. memset(&state->buffer[state->buflen], 0,
  179. NH_MESSAGE_UNIT - state->buflen);
  180. nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
  181. nh_fn);
  182. }
  183. if (state->nh_remaining)
  184. process_nh_hash_value(state, key);
  185. poly1305_core_emit(&state->poly_state, NULL, dst);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(crypto_nhpoly1305_final_helper);
  189. int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst)
  190. {
  191. return crypto_nhpoly1305_final_helper(desc, dst, nh_generic);
  192. }
  193. EXPORT_SYMBOL(crypto_nhpoly1305_final);
  194. static struct shash_alg nhpoly1305_alg = {
  195. .base.cra_name = "nhpoly1305",
  196. .base.cra_driver_name = "nhpoly1305-generic",
  197. .base.cra_priority = 100,
  198. .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
  199. .base.cra_module = THIS_MODULE,
  200. .digestsize = POLY1305_DIGEST_SIZE,
  201. .init = crypto_nhpoly1305_init,
  202. .update = crypto_nhpoly1305_update,
  203. .final = crypto_nhpoly1305_final,
  204. .setkey = crypto_nhpoly1305_setkey,
  205. .descsize = sizeof(struct nhpoly1305_state),
  206. };
  207. static int __init nhpoly1305_mod_init(void)
  208. {
  209. return crypto_register_shash(&nhpoly1305_alg);
  210. }
  211. static void __exit nhpoly1305_mod_exit(void)
  212. {
  213. crypto_unregister_shash(&nhpoly1305_alg);
  214. }
  215. subsys_initcall(nhpoly1305_mod_init);
  216. module_exit(nhpoly1305_mod_exit);
  217. MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function");
  218. MODULE_LICENSE("GPL v2");
  219. MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
  220. MODULE_ALIAS_CRYPTO("nhpoly1305");
  221. MODULE_ALIAS_CRYPTO("nhpoly1305-generic");