poly1305.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Poly1305 authenticator algorithm, RFC7539
  4. *
  5. * Copyright (C) 2015 Martin Willi
  6. *
  7. * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
  8. */
  9. #include <crypto/internal/poly1305.h>
  10. #include <linux/export.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/unaligned.h>
  15. #ifdef CONFIG_CRYPTO_LIB_POLY1305_ARCH
  16. #include "poly1305.h" /* $(SRCARCH)/poly1305.h */
  17. #else
  18. #define poly1305_block_init poly1305_block_init_generic
  19. #define poly1305_blocks poly1305_blocks_generic
  20. #define poly1305_emit poly1305_emit_generic
  21. #endif
  22. void poly1305_init(struct poly1305_desc_ctx *desc,
  23. const u8 key[POLY1305_KEY_SIZE])
  24. {
  25. desc->s[0] = get_unaligned_le32(key + 16);
  26. desc->s[1] = get_unaligned_le32(key + 20);
  27. desc->s[2] = get_unaligned_le32(key + 24);
  28. desc->s[3] = get_unaligned_le32(key + 28);
  29. desc->buflen = 0;
  30. poly1305_block_init(&desc->state, key);
  31. }
  32. EXPORT_SYMBOL(poly1305_init);
  33. void poly1305_update(struct poly1305_desc_ctx *desc,
  34. const u8 *src, unsigned int nbytes)
  35. {
  36. if (desc->buflen + nbytes >= POLY1305_BLOCK_SIZE) {
  37. unsigned int bulk_len;
  38. if (desc->buflen) {
  39. unsigned int l = POLY1305_BLOCK_SIZE - desc->buflen;
  40. memcpy(&desc->buf[desc->buflen], src, l);
  41. src += l;
  42. nbytes -= l;
  43. poly1305_blocks(&desc->state, desc->buf,
  44. POLY1305_BLOCK_SIZE, 1);
  45. desc->buflen = 0;
  46. }
  47. bulk_len = round_down(nbytes, POLY1305_BLOCK_SIZE);
  48. nbytes %= POLY1305_BLOCK_SIZE;
  49. if (bulk_len) {
  50. poly1305_blocks(&desc->state, src, bulk_len, 1);
  51. src += bulk_len;
  52. }
  53. }
  54. if (nbytes) {
  55. memcpy(&desc->buf[desc->buflen], src, nbytes);
  56. desc->buflen += nbytes;
  57. }
  58. }
  59. EXPORT_SYMBOL(poly1305_update);
  60. void poly1305_final(struct poly1305_desc_ctx *desc, u8 *dst)
  61. {
  62. if (unlikely(desc->buflen)) {
  63. desc->buf[desc->buflen++] = 1;
  64. memset(desc->buf + desc->buflen, 0,
  65. POLY1305_BLOCK_SIZE - desc->buflen);
  66. poly1305_blocks(&desc->state, desc->buf, POLY1305_BLOCK_SIZE,
  67. 0);
  68. }
  69. poly1305_emit(&desc->state.h, dst, desc->s);
  70. *desc = (struct poly1305_desc_ctx){};
  71. }
  72. EXPORT_SYMBOL(poly1305_final);
  73. #ifdef poly1305_mod_init_arch
  74. static int __init poly1305_mod_init(void)
  75. {
  76. poly1305_mod_init_arch();
  77. return 0;
  78. }
  79. subsys_initcall(poly1305_mod_init);
  80. static void __exit poly1305_mod_exit(void)
  81. {
  82. }
  83. module_exit(poly1305_mod_exit);
  84. #endif
  85. MODULE_LICENSE("GPL");
  86. MODULE_DESCRIPTION("Poly1305 authenticator algorithm, RFC7539");