poly1305.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* poly1305.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/poly1305.h
  23. */
  24. #ifndef WOLF_CRYPT_POLY1305_H
  25. #define WOLF_CRYPT_POLY1305_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef HAVE_POLY1305
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* auto detect between 32bit / 64bit */
  32. #if defined(__SIZEOF_INT128__) && defined(__LP64__)
  33. #define WC_HAS_SIZEOF_INT128_64BIT
  34. #endif
  35. #if defined(_MSC_VER) && defined(_M_X64)
  36. #define WC_HAS_MSVC_64BIT
  37. #endif
  38. #if (defined(__GNUC__) && defined(__LP64__) && \
  39. ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))))
  40. #define WC_HAS_GCC_4_4_64BIT
  41. #endif
  42. #ifdef USE_INTEL_SPEEDUP
  43. #elif (defined(WC_HAS_SIZEOF_INT128_64BIT) || defined(WC_HAS_MSVC_64BIT) || \
  44. defined(WC_HAS_GCC_4_4_64BIT))
  45. #define POLY130564
  46. #else
  47. #define POLY130532
  48. #endif
  49. enum {
  50. POLY1305 = 7,
  51. POLY1305_BLOCK_SIZE = 16,
  52. POLY1305_DIGEST_SIZE = 16,
  53. };
  54. #define WC_POLY1305_PAD_SZ 16
  55. #define WC_POLY1305_MAC_SZ 16
  56. /* Poly1305 state */
  57. typedef struct Poly1305 {
  58. #ifdef USE_INTEL_SPEEDUP
  59. word64 r[3];
  60. word64 h[3];
  61. word64 pad[2];
  62. word64 hh[20];
  63. word32 r1[8];
  64. word32 r2[8];
  65. word32 r3[8];
  66. word32 r4[8];
  67. word64 hm[16];
  68. unsigned char buffer[8*POLY1305_BLOCK_SIZE];
  69. size_t leftover;
  70. unsigned char finished;
  71. unsigned char started;
  72. #else
  73. #if defined(WOLFSSL_ARMASM) && defined(__aarch64__)
  74. ALIGN128 word32 r[5];
  75. ALIGN128 word32 r_2[5]; /* r^2 */
  76. ALIGN128 word32 r_4[5]; /* r^4 */
  77. ALIGN128 word32 h[5];
  78. word32 pad[4];
  79. word64 leftover;
  80. #else
  81. #if defined(POLY130564)
  82. word64 r[3];
  83. word64 h[3];
  84. word64 pad[2];
  85. #else
  86. word32 r[5];
  87. word32 h[5];
  88. word32 pad[4];
  89. #endif
  90. size_t leftover;
  91. #endif /* WOLFSSL_ARMASM */
  92. unsigned char buffer[POLY1305_BLOCK_SIZE];
  93. unsigned char finished;
  94. #endif
  95. } Poly1305;
  96. /* does init */
  97. WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key,
  98. word32 kySz);
  99. WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte* m, word32 bytes);
  100. WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag);
  101. /* AEAD Functions */
  102. WOLFSSL_API int wc_Poly1305_Pad(Poly1305* ctx, word32 lenToPad);
  103. WOLFSSL_API int wc_Poly1305_EncodeSizes(Poly1305* ctx, word32 aadSz,
  104. word32 dataSz);
  105. #ifdef WORD64_AVAILABLE
  106. WOLFSSL_API int wc_Poly1305_EncodeSizes64(Poly1305* ctx, word64 aadSz,
  107. word64 dataSz);
  108. #endif
  109. WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, const byte* additional,
  110. word32 addSz, const byte* input, word32 sz, byte* tag, word32 tagSz);
  111. #if defined(__aarch64__ ) && defined(WOLFSSL_ARMASM)
  112. void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
  113. size_t bytes);
  114. void poly1305_block(Poly1305* ctx, const unsigned char *m);
  115. #endif
  116. #ifdef __cplusplus
  117. } /* extern "C" */
  118. #endif
  119. #endif /* HAVE_POLY1305 */
  120. #endif /* WOLF_CRYPT_POLY1305_H */