onetimeauth_poly1305.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "onetimeauth_poly1305.h"
  2. #include "crypto_onetimeauth_poly1305.h"
  3. #include "private/common.h"
  4. #include "private/implementations.h"
  5. #include "randombytes.h"
  6. #include "runtime.h"
  7. #include "donna/poly1305_donna.h"
  8. #if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)
  9. # include "sse2/poly1305_sse2.h"
  10. #endif
  11. static const crypto_onetimeauth_poly1305_implementation *implementation =
  12. &crypto_onetimeauth_poly1305_donna_implementation;
  13. int
  14. crypto_onetimeauth_poly1305(unsigned char *out, const unsigned char *in,
  15. unsigned long long inlen, const unsigned char *k)
  16. {
  17. return implementation->onetimeauth(out, in, inlen, k);
  18. }
  19. int
  20. crypto_onetimeauth_poly1305_verify(const unsigned char *h,
  21. const unsigned char *in,
  22. unsigned long long inlen,
  23. const unsigned char *k)
  24. {
  25. return implementation->onetimeauth_verify(h, in, inlen, k);
  26. }
  27. int
  28. crypto_onetimeauth_poly1305_init(crypto_onetimeauth_poly1305_state *state,
  29. const unsigned char *key)
  30. {
  31. return implementation->onetimeauth_init(state, key);
  32. }
  33. int
  34. crypto_onetimeauth_poly1305_update(crypto_onetimeauth_poly1305_state *state,
  35. const unsigned char *in,
  36. unsigned long long inlen)
  37. {
  38. return implementation->onetimeauth_update(state, in, inlen);
  39. }
  40. int
  41. crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
  42. unsigned char *out)
  43. {
  44. return implementation->onetimeauth_final(state, out);
  45. }
  46. size_t
  47. crypto_onetimeauth_poly1305_bytes(void)
  48. {
  49. return crypto_onetimeauth_poly1305_BYTES;
  50. }
  51. size_t
  52. crypto_onetimeauth_poly1305_keybytes(void)
  53. {
  54. return crypto_onetimeauth_poly1305_KEYBYTES;
  55. }
  56. size_t
  57. crypto_onetimeauth_poly1305_statebytes(void)
  58. {
  59. return sizeof(crypto_onetimeauth_poly1305_state);
  60. }
  61. void
  62. crypto_onetimeauth_poly1305_keygen(
  63. unsigned char k[crypto_onetimeauth_poly1305_KEYBYTES])
  64. {
  65. randombytes_buf(k, crypto_onetimeauth_poly1305_KEYBYTES);
  66. }
  67. int
  68. _crypto_onetimeauth_poly1305_pick_best_implementation(void)
  69. {
  70. implementation = &crypto_onetimeauth_poly1305_donna_implementation;
  71. #if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)
  72. if (sodium_runtime_has_sse2()) {
  73. implementation = &crypto_onetimeauth_poly1305_sse2_implementation;
  74. }
  75. #endif
  76. return 0;
  77. }