aead.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_AEAD_H
  15. #define OPENSSL_HEADER_AEAD_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /* Authenticated Encryption with Additional Data.
  21. *
  22. * AEAD couples confidentiality and integrity in a single primitive. AEAD
  23. * algorithms take a key and then can seal and open individual messages. Each
  24. * message has a unique, per-message nonce and, optionally, additional data
  25. * which is authenticated but not included in the ciphertext.
  26. *
  27. * The |EVP_AEAD_CTX_init| function initialises an |EVP_AEAD_CTX| structure and
  28. * performs any precomputation needed to use |aead| with |key|. The length of
  29. * the key, |key_len|, is given in bytes.
  30. *
  31. * The |tag_len| argument contains the length of the tags, in bytes, and allows
  32. * for the processing of truncated authenticators. A zero value indicates that
  33. * the default tag length should be used and this is defined as
  34. * |EVP_AEAD_DEFAULT_TAG_LENGTH| in order to make the code clear. Using
  35. * truncated tags increases an attacker's chance of creating a valid forgery.
  36. * Be aware that the attacker's chance may increase more than exponentially as
  37. * would naively be expected.
  38. *
  39. * When no longer needed, the initialised |EVP_AEAD_CTX| structure must be
  40. * passed to |EVP_AEAD_CTX_cleanup|, which will deallocate any memory used.
  41. *
  42. * With an |EVP_AEAD_CTX| in hand, one can seal and open messages. These
  43. * operations are intended to meet the standard notions of privacy and
  44. * authenticity for authenticated encryption. For formal definitions see
  45. * Bellare and Namprempre, "Authenticated encryption: relations among notions
  46. * and analysis of the generic composition paradigm," Lecture Notes in Computer
  47. * Science B<1976> (2000), 531–545,
  48. * http://www-cse.ucsd.edu/~mihir/papers/oem.html.
  49. *
  50. * When sealing messages, a nonce must be given. The length of the nonce is
  51. * fixed by the AEAD in use and is returned by |EVP_AEAD_nonce_length|. *The
  52. * nonce must be unique for all messages with the same key*. This is critically
  53. * important - nonce reuse may completely undermine the security of the AEAD.
  54. * Nonces may be predictable and public, so long as they are unique. Uniqueness
  55. * may be achieved with a simple counter or, if large enough, may be generated
  56. * randomly. The nonce must be passed into the "open" operation by the receiver
  57. * so must either be implicit (e.g. a counter), or must be transmitted along
  58. * with the sealed message.
  59. *
  60. * The "seal" and "open" operations are atomic - an entire message must be
  61. * encrypted or decrypted in a single call. Large messages may have to be split
  62. * up in order to accomodate this. When doing so, be mindful of the need not to
  63. * repeat nonces and the possibility that an attacker could duplicate, reorder
  64. * or drop message chunks. For example, using a single key for a given (large)
  65. * message and sealing chunks with nonces counting from zero would be secure as
  66. * long as the number of chunks was securely transmitted. (Otherwise an
  67. * attacker could truncate the message by dropping chunks from the end.)
  68. *
  69. * The number of chunks could be transmitted by prefixing it to the plaintext,
  70. * for example. This also assumes that no other message would ever use the same
  71. * key otherwise the rule that nonces must be unique for a given key would be
  72. * violated.
  73. *
  74. * The "seal" and "open" operations also permit additional data to be
  75. * authenticated via the |ad| parameter. This data is not included in the
  76. * ciphertext and must be identical for both the "seal" and "open" call. This
  77. * permits implicit context to be authenticated but may be empty if not needed.
  78. *
  79. * The "seal" and "open" operations may work in-place if the |out| and |in|
  80. * arguments are equal. They may also be used to shift the data left inside the
  81. * same buffer if |out| is less than |in|. However, |out| may not point inside
  82. * the input data otherwise the input may be overwritten before it has been
  83. * read. This situation will cause an error.
  84. *
  85. * The "seal" and "open" operations return one on success and zero on error. */
  86. /* AEAD algorithms. */
  87. /* EVP_aead_aes_128_gcm is AES-128 in Galois Counter Mode. */
  88. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm(void);
  89. /* EVP_aead_aes_256_gcm is AES-256 in Galois Counter Mode. */
  90. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm(void);
  91. /* EVP_aead_chacha20_poly1305 is the AEAD built from ChaCha20 and
  92. * Poly1305 as described in RFC 7539. */
  93. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305(void);
  94. /* EVP_aead_chacha20_poly1305_old is an AEAD built from ChaCha20 and
  95. * Poly1305 that is used in the experimental ChaCha20-Poly1305 TLS cipher
  96. * suites. */
  97. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305_old(void);
  98. /* EVP_aead_aes_128_key_wrap is AES-128 Key Wrap mode. This should never be
  99. * used except to interoperate with existing systems that use this mode.
  100. *
  101. * If the nonce is empty then the default nonce will be used, otherwise it must
  102. * be eight bytes long. The input must be a multiple of eight bytes long. No
  103. * additional data can be given to this mode. */
  104. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_key_wrap(void);
  105. /* EVP_aead_aes_256_key_wrap is AES-256 in Key Wrap mode. This should never be
  106. * used except to interoperate with existing systems that use this mode.
  107. *
  108. * See |EVP_aead_aes_128_key_wrap| for details. */
  109. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_key_wrap(void);
  110. /* EVP_aead_aes_128_ctr_hmac_sha256 is AES-128 in CTR mode with HMAC-SHA256 for
  111. * authentication. The nonce is 12 bytes; the bottom 32-bits are used as the
  112. * block counter, thus the maximum plaintext size is 64GB. */
  113. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void);
  114. /* EVP_aead_aes_256_ctr_hmac_sha256 is AES-256 in CTR mode with HMAC-SHA256 for
  115. * authentication. See |EVP_aead_aes_128_ctr_hmac_sha256| for details. */
  116. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void);
  117. /* EVP_has_aes_hardware returns one if we enable hardware support for fast and
  118. * constant-time AES-GCM. */
  119. OPENSSL_EXPORT int EVP_has_aes_hardware(void);
  120. /* TLS-specific AEAD algorithms.
  121. *
  122. * These AEAD primitives do not meet the definition of generic AEADs. They are
  123. * all specific to TLS and should not be used outside of that context. They must
  124. * be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful, and may
  125. * not be used concurrently. Any nonces are used as IVs, so they must be
  126. * unpredictable. They only accept an |ad| parameter of length 11 (the standard
  127. * TLS one with length omitted). */
  128. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_rc4_md5_tls(void);
  129. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_rc4_sha1_tls(void);
  130. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void);
  131. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void);
  132. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void);
  133. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void);
  134. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void);
  135. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void);
  136. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void);
  137. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void);
  138. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void);
  139. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_tls(void);
  140. /* SSLv3-specific AEAD algorithms.
  141. *
  142. * These AEAD primitives do not meet the definition of generic AEADs. They are
  143. * all specific to SSLv3 and should not be used outside of that context. They
  144. * must be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful,
  145. * and may not be used concurrently. They only accept an |ad| parameter of
  146. * length 9 (the standard TLS one with length and version omitted). */
  147. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_rc4_md5_ssl3(void);
  148. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_rc4_sha1_ssl3(void);
  149. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void);
  150. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void);
  151. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void);
  152. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_ssl3(void);
  153. /* Utility functions. */
  154. /* EVP_AEAD_key_length returns the length, in bytes, of the keys used by
  155. * |aead|. */
  156. OPENSSL_EXPORT size_t EVP_AEAD_key_length(const EVP_AEAD *aead);
  157. /* EVP_AEAD_nonce_length returns the length, in bytes, of the per-message nonce
  158. * for |aead|. */
  159. OPENSSL_EXPORT size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead);
  160. /* EVP_AEAD_max_overhead returns the maximum number of additional bytes added
  161. * by the act of sealing data with |aead|. */
  162. OPENSSL_EXPORT size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead);
  163. /* EVP_AEAD_max_tag_len returns the maximum tag length when using |aead|. This
  164. * is the largest value that can be passed as |tag_len| to
  165. * |EVP_AEAD_CTX_init|. */
  166. OPENSSL_EXPORT size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead);
  167. /* AEAD operations. */
  168. /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key
  169. * and message-independent IV. */
  170. typedef struct evp_aead_ctx_st {
  171. const EVP_AEAD *aead;
  172. /* aead_state is an opaque pointer to whatever state the AEAD needs to
  173. * maintain. */
  174. void *aead_state;
  175. } EVP_AEAD_CTX;
  176. /* EVP_AEAD_MAX_KEY_LENGTH contains the maximum key length used by
  177. * any AEAD defined in this header. */
  178. #define EVP_AEAD_MAX_KEY_LENGTH 80
  179. /* EVP_AEAD_MAX_NONCE_LENGTH contains the maximum nonce length used by
  180. * any AEAD defined in this header. */
  181. #define EVP_AEAD_MAX_NONCE_LENGTH 16
  182. /* EVP_AEAD_MAX_OVERHEAD contains the maximum overhead used by any AEAD
  183. * defined in this header. */
  184. #define EVP_AEAD_MAX_OVERHEAD 64
  185. /* EVP_AEAD_DEFAULT_TAG_LENGTH is a magic value that can be passed to
  186. * EVP_AEAD_CTX_init to indicate that the default tag length for an AEAD should
  187. * be used. */
  188. #define EVP_AEAD_DEFAULT_TAG_LENGTH 0
  189. /* evp_aead_direction_t denotes the direction of an AEAD operation. */
  190. enum evp_aead_direction_t {
  191. evp_aead_open,
  192. evp_aead_seal,
  193. };
  194. /* EVP_AEAD_CTX_zero sets an uninitialized |ctx| to the zero state. It must be
  195. * initialized with |EVP_AEAD_CTX_init| before use. It is safe, but not
  196. * necessary, to call |EVP_AEAD_CTX_cleanup| in this state. This may be used for
  197. * more uniform cleanup of |EVP_AEAD_CTX|. */
  198. OPENSSL_EXPORT void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx);
  199. /* EVP_AEAD_CTX_init initializes |ctx| for the given AEAD algorithm. The |impl|
  200. * argument is ignored and should be NULL. Authentication tags may be truncated
  201. * by passing a size as |tag_len|. A |tag_len| of zero indicates the default
  202. * tag length and this is defined as EVP_AEAD_DEFAULT_TAG_LENGTH for
  203. * readability.
  204. *
  205. * Returns 1 on success. Otherwise returns 0 and pushes to the error stack. In
  206. * the error case, you do not need to call |EVP_AEAD_CTX_cleanup|, but it's
  207. * harmless to do so. */
  208. OPENSSL_EXPORT int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  209. const uint8_t *key, size_t key_len,
  210. size_t tag_len, ENGINE *impl);
  211. /* EVP_AEAD_CTX_init_with_direction calls |EVP_AEAD_CTX_init| for normal
  212. * AEADs. For TLS-specific and SSL3-specific AEADs, it initializes |ctx| for a
  213. * given direction. */
  214. OPENSSL_EXPORT int EVP_AEAD_CTX_init_with_direction(
  215. EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len,
  216. size_t tag_len, enum evp_aead_direction_t dir);
  217. /* EVP_AEAD_CTX_cleanup frees any data allocated by |ctx|. It is a no-op to
  218. * call |EVP_AEAD_CTX_cleanup| on a |EVP_AEAD_CTX| that has been |memset| to
  219. * all zeros. */
  220. OPENSSL_EXPORT void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx);
  221. /* EVP_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and
  222. * authenticates |ad_len| bytes from |ad| and writes the result to |out|. It
  223. * returns one on success and zero otherwise.
  224. *
  225. * This function may be called (with the same |EVP_AEAD_CTX|) concurrently with
  226. * itself or |EVP_AEAD_CTX_open|.
  227. *
  228. * At most |max_out_len| bytes are written to |out| and, in order to ensure
  229. * success, |max_out_len| should be |in_len| plus the result of
  230. * |EVP_AEAD_max_overhead|. On successful return, |*out_len| is set to the
  231. * actual number of bytes written.
  232. *
  233. * The length of |nonce|, |nonce_len|, must be equal to the result of
  234. * |EVP_AEAD_nonce_length| for this AEAD.
  235. *
  236. * |EVP_AEAD_CTX_seal| never results in a partial output. If |max_out_len| is
  237. * insufficient, zero will be returned. (In this case, |*out_len| is set to
  238. * zero.)
  239. *
  240. * If |in| and |out| alias then |out| must be <= |in|. */
  241. OPENSSL_EXPORT int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  242. size_t *out_len, size_t max_out_len,
  243. const uint8_t *nonce, size_t nonce_len,
  244. const uint8_t *in, size_t in_len,
  245. const uint8_t *ad, size_t ad_len);
  246. /* EVP_AEAD_CTX_open authenticates |in_len| bytes from |in| and |ad_len| bytes
  247. * from |ad| and decrypts at most |in_len| bytes into |out|. It returns one on
  248. * success and zero otherwise.
  249. *
  250. * This function may be called (with the same |EVP_AEAD_CTX|) concurrently with
  251. * itself or |EVP_AEAD_CTX_seal|.
  252. *
  253. * At most |in_len| bytes are written to |out|. In order to ensure success,
  254. * |max_out_len| should be at least |in_len|. On successful return, |*out_len|
  255. * is set to the the actual number of bytes written.
  256. *
  257. * The length of |nonce|, |nonce_len|, must be equal to the result of
  258. * |EVP_AEAD_nonce_length| for this AEAD.
  259. *
  260. * |EVP_AEAD_CTX_open| never results in a partial output. If |max_out_len| is
  261. * insufficient, zero will be returned. (In this case, |*out_len| is set to
  262. * zero.)
  263. *
  264. * If |in| and |out| alias then |out| must be <= |in|. */
  265. OPENSSL_EXPORT int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  266. size_t *out_len, size_t max_out_len,
  267. const uint8_t *nonce, size_t nonce_len,
  268. const uint8_t *in, size_t in_len,
  269. const uint8_t *ad, size_t ad_len);
  270. /* Obscure functions. */
  271. /* EVP_AEAD_CTX_get_rc4_state sets |*out_key| to point to an RC4 key structure.
  272. * It returns one on success or zero if |ctx| doesn't have an RC4 key. */
  273. OPENSSL_EXPORT int EVP_AEAD_CTX_get_rc4_state(const EVP_AEAD_CTX *ctx,
  274. const RC4_KEY **out_key);
  275. /* EVP_AEAD_CTX_get_iv sets |*out_len| to the length of the IV for |ctx| and
  276. * sets |*out_iv| to point to that many bytes of the current IV. This is only
  277. * meaningful for AEADs with implicit IVs (i.e. CBC mode in SSLv3 and TLS 1.0).
  278. *
  279. * It returns one on success or zero on error. */
  280. OPENSSL_EXPORT int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx,
  281. const uint8_t **out_iv, size_t *out_len);
  282. /* Deprecated functions. */
  283. /* EVP_aead_chacha20_poly1305_rfc7539 calls |EVP_aead_chacha20_poly1305|.
  284. *
  285. * TODO(davidben): Remove this. */
  286. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305_rfc7539(void);
  287. #if defined(__cplusplus)
  288. } /* extern C */
  289. #endif
  290. #endif /* OPENSSL_HEADER_AEAD_H */