poly1305.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for the Poly1305 algorithm
  4. */
  5. #ifndef _CRYPTO_POLY1305_H
  6. #define _CRYPTO_POLY1305_H
  7. #include <linux/types.h>
  8. #include <linux/crypto.h>
  9. #define POLY1305_BLOCK_SIZE 16
  10. #define POLY1305_KEY_SIZE 32
  11. #define POLY1305_DIGEST_SIZE 16
  12. /* The poly1305_key and poly1305_state types are mostly opaque and
  13. * implementation-defined. Limbs might be in base 2^64 or base 2^26, or
  14. * different yet. The union type provided keeps these 64-bit aligned for the
  15. * case in which this is implemented using 64x64 multiplies.
  16. */
  17. struct poly1305_key {
  18. union {
  19. u32 r[5];
  20. u64 r64[3];
  21. };
  22. };
  23. struct poly1305_core_key {
  24. struct poly1305_key key;
  25. struct poly1305_key precomputed_s;
  26. };
  27. struct poly1305_state {
  28. union {
  29. u32 h[5];
  30. u64 h64[3];
  31. };
  32. };
  33. struct poly1305_desc_ctx {
  34. /* partial buffer */
  35. u8 buf[POLY1305_BLOCK_SIZE];
  36. /* bytes used in partial buffer */
  37. unsigned int buflen;
  38. /* how many keys have been set in r[] */
  39. unsigned short rset;
  40. /* whether s[] has been set */
  41. bool sset;
  42. /* finalize key */
  43. u32 s[4];
  44. /* accumulator */
  45. struct poly1305_state h;
  46. /* key */
  47. union {
  48. struct poly1305_key opaque_r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
  49. struct poly1305_core_key core_r;
  50. };
  51. };
  52. void poly1305_init_arch(struct poly1305_desc_ctx *desc,
  53. const u8 key[POLY1305_KEY_SIZE]);
  54. void poly1305_init_generic(struct poly1305_desc_ctx *desc,
  55. const u8 key[POLY1305_KEY_SIZE]);
  56. static inline void poly1305_init(struct poly1305_desc_ctx *desc, const u8 *key)
  57. {
  58. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
  59. poly1305_init_arch(desc, key);
  60. else
  61. poly1305_init_generic(desc, key);
  62. }
  63. void poly1305_update_arch(struct poly1305_desc_ctx *desc, const u8 *src,
  64. unsigned int nbytes);
  65. void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
  66. unsigned int nbytes);
  67. static inline void poly1305_update(struct poly1305_desc_ctx *desc,
  68. const u8 *src, unsigned int nbytes)
  69. {
  70. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
  71. poly1305_update_arch(desc, src, nbytes);
  72. else
  73. poly1305_update_generic(desc, src, nbytes);
  74. }
  75. void poly1305_final_arch(struct poly1305_desc_ctx *desc, u8 *digest);
  76. void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *digest);
  77. static inline void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest)
  78. {
  79. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
  80. poly1305_final_arch(desc, digest);
  81. else
  82. poly1305_final_generic(desc, digest);
  83. }
  84. #endif