poly1305.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef POLY1305_H
  15. #define POLY1305_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. /**
  19. * Poly1305
  20. * ========
  21. * This is an incremental interface to computing the poly1305
  22. * single shot MAC.
  23. *
  24. * Note: construct Poly1305-AES with this by taking a 16 byte
  25. * nonce and encrypting it, and then using the result as an
  26. * input to this function.
  27. */
  28. /* .. c:type:: cf_poly1305
  29. * Poly1305 incremental interface context.
  30. *
  31. * .. c:member:: cf_poly1305.h
  32. * Current accumulator.
  33. *
  34. * .. c:member:: cf_poly1305.r
  35. * Block multiplier.
  36. *
  37. * .. c:member:: cf_poly1305.s
  38. * Final XOR offset.
  39. *
  40. * .. c:member:: cf_poly1305.partial
  41. * Unprocessed input.
  42. *
  43. * .. c:member:: cf_poly1305.npartial
  44. * Number of bytes of unprocessed input.
  45. *
  46. */
  47. typedef struct
  48. {
  49. uint32_t h[17];
  50. uint32_t r[17];
  51. uint8_t s[16];
  52. uint8_t partial[16];
  53. size_t npartial;
  54. } cf_poly1305;
  55. /* .. c:function:: $DECL
  56. * Sets up `ctx` ready to compute a new MAC.
  57. *
  58. * In Poly1305-AES, `r` is the second half of the 32-byte key.
  59. * `s` is a nonce encrypted under the first half of the key.
  60. *
  61. * :param ctx: context (written)
  62. * :param r: MAC key.
  63. * :param s: preprocessed nonce.
  64. *
  65. */
  66. void cf_poly1305_init(cf_poly1305 *ctx,
  67. const uint8_t r[16],
  68. const uint8_t s[16]);
  69. /* .. c:function:: $DECL
  70. * Processes `nbytes` at `data`. Copies the data if there isn't enough to make
  71. * a full block.
  72. */
  73. void cf_poly1305_update(cf_poly1305 *ctx,
  74. const uint8_t *data,
  75. size_t nbytes);
  76. /* .. c:function:: $DECL
  77. * Finishes the operation, writing 16 bytes to `out`.
  78. *
  79. * This destroys `ctx`.
  80. */
  81. void cf_poly1305_finish(cf_poly1305 *ctx,
  82. uint8_t out[16]);
  83. #endif