crypto_aead_aes256gcm.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef crypto_aead_aes256gcm_H
  2. #define crypto_aead_aes256gcm_H
  3. /*
  4. * WARNING: Despite being the most popular AEAD construction due to its
  5. * use in TLS, safely using AES-GCM in a different context is tricky.
  6. *
  7. * No more than ~ 350 GB of input data should be encrypted with a given key.
  8. * This is for ~ 16 KB messages -- Actual figures vary according to
  9. * message sizes.
  10. *
  11. * In addition, nonces are short and repeated nonces would totally destroy
  12. * the security of this scheme.
  13. *
  14. * Nonces should thus come from atomic counters, which can be difficult to
  15. * set up in a distributed environment.
  16. *
  17. * Unless you absolutely need AES-GCM, use crypto_aead_xchacha20poly1305_ietf_*()
  18. * instead. It doesn't have any of these limitations.
  19. * Or, if you don't need to authenticate additional data, just stick to
  20. * crypto_secretbox().
  21. */
  22. #include <stddef.h>
  23. #include "export.h"
  24. #ifdef __cplusplus
  25. # ifdef __GNUC__
  26. # pragma GCC diagnostic ignored "-Wlong-long"
  27. # endif
  28. extern "C" {
  29. #endif
  30. SODIUM_EXPORT
  31. int crypto_aead_aes256gcm_is_available(void);
  32. #define crypto_aead_aes256gcm_KEYBYTES 32U
  33. SODIUM_EXPORT
  34. size_t crypto_aead_aes256gcm_keybytes(void);
  35. #define crypto_aead_aes256gcm_NSECBYTES 0U
  36. SODIUM_EXPORT
  37. size_t crypto_aead_aes256gcm_nsecbytes(void);
  38. #define crypto_aead_aes256gcm_NPUBBYTES 12U
  39. SODIUM_EXPORT
  40. size_t crypto_aead_aes256gcm_npubbytes(void);
  41. #define crypto_aead_aes256gcm_ABYTES 16U
  42. SODIUM_EXPORT
  43. size_t crypto_aead_aes256gcm_abytes(void);
  44. #define crypto_aead_aes256gcm_MESSAGEBYTES_MAX \
  45. SODIUM_MIN(SODIUM_SIZE_MAX - crypto_aead_aes256gcm_ABYTES, \
  46. (16ULL * ((1ULL << 32) - 2ULL)))
  47. SODIUM_EXPORT
  48. size_t crypto_aead_aes256gcm_messagebytes_max(void);
  49. typedef struct CRYPTO_ALIGN(16) crypto_aead_aes256gcm_state_ {
  50. unsigned char opaque[512];
  51. } crypto_aead_aes256gcm_state;
  52. SODIUM_EXPORT
  53. size_t crypto_aead_aes256gcm_statebytes(void);
  54. SODIUM_EXPORT
  55. int crypto_aead_aes256gcm_encrypt(unsigned char *c,
  56. unsigned long long *clen_p,
  57. const unsigned char *m,
  58. unsigned long long mlen,
  59. const unsigned char *ad,
  60. unsigned long long adlen,
  61. const unsigned char *nsec,
  62. const unsigned char *npub,
  63. const unsigned char *k)
  64. __attribute__ ((nonnull(1, 8, 9)));
  65. SODIUM_EXPORT
  66. int crypto_aead_aes256gcm_decrypt(unsigned char *m,
  67. unsigned long long *mlen_p,
  68. unsigned char *nsec,
  69. const unsigned char *c,
  70. unsigned long long clen,
  71. const unsigned char *ad,
  72. unsigned long long adlen,
  73. const unsigned char *npub,
  74. const unsigned char *k)
  75. __attribute__ ((warn_unused_result)) __attribute__ ((nonnull(4, 8, 9)));
  76. SODIUM_EXPORT
  77. int crypto_aead_aes256gcm_encrypt_detached(unsigned char *c,
  78. unsigned char *mac,
  79. unsigned long long *maclen_p,
  80. const unsigned char *m,
  81. unsigned long long mlen,
  82. const unsigned char *ad,
  83. unsigned long long adlen,
  84. const unsigned char *nsec,
  85. const unsigned char *npub,
  86. const unsigned char *k)
  87. __attribute__ ((nonnull(1, 2, 9, 10)));
  88. SODIUM_EXPORT
  89. int crypto_aead_aes256gcm_decrypt_detached(unsigned char *m,
  90. unsigned char *nsec,
  91. const unsigned char *c,
  92. unsigned long long clen,
  93. const unsigned char *mac,
  94. const unsigned char *ad,
  95. unsigned long long adlen,
  96. const unsigned char *npub,
  97. const unsigned char *k)
  98. __attribute__ ((warn_unused_result)) __attribute__ ((nonnull(3, 5, 8, 9)));
  99. /* -- Precomputation interface -- */
  100. SODIUM_EXPORT
  101. int crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_,
  102. const unsigned char *k)
  103. __attribute__ ((nonnull));
  104. SODIUM_EXPORT
  105. int crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c,
  106. unsigned long long *clen_p,
  107. const unsigned char *m,
  108. unsigned long long mlen,
  109. const unsigned char *ad,
  110. unsigned long long adlen,
  111. const unsigned char *nsec,
  112. const unsigned char *npub,
  113. const crypto_aead_aes256gcm_state *ctx_)
  114. __attribute__ ((nonnull(1, 8, 9)));
  115. SODIUM_EXPORT
  116. int crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m,
  117. unsigned long long *mlen_p,
  118. unsigned char *nsec,
  119. const unsigned char *c,
  120. unsigned long long clen,
  121. const unsigned char *ad,
  122. unsigned long long adlen,
  123. const unsigned char *npub,
  124. const crypto_aead_aes256gcm_state *ctx_)
  125. __attribute__ ((warn_unused_result)) __attribute__ ((nonnull(4, 8, 9)));
  126. SODIUM_EXPORT
  127. int crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c,
  128. unsigned char *mac,
  129. unsigned long long *maclen_p,
  130. const unsigned char *m,
  131. unsigned long long mlen,
  132. const unsigned char *ad,
  133. unsigned long long adlen,
  134. const unsigned char *nsec,
  135. const unsigned char *npub,
  136. const crypto_aead_aes256gcm_state *ctx_)
  137. __attribute__ ((nonnull(1, 2, 9, 10)));
  138. SODIUM_EXPORT
  139. int crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m,
  140. unsigned char *nsec,
  141. const unsigned char *c,
  142. unsigned long long clen,
  143. const unsigned char *mac,
  144. const unsigned char *ad,
  145. unsigned long long adlen,
  146. const unsigned char *npub,
  147. const crypto_aead_aes256gcm_state *ctx_)
  148. __attribute__ ((warn_unused_result)) __attribute__ ((nonnull(3, 5, 8, 9)));
  149. SODIUM_EXPORT
  150. void crypto_aead_aes256gcm_keygen(unsigned char k[crypto_aead_aes256gcm_KEYBYTES])
  151. __attribute__ ((nonnull));
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif