You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

424 lines
19 KiB

  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 accommodate 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. Otherwise, if |out| and |in| alias, input data may be
  81. * overwritten before it is read. This situation will cause an error.
  82. *
  83. * The "seal" and "open" operations return one on success and zero on error. */
  84. /* AEAD algorithms. */
  85. /* EVP_aead_aes_128_gcm is AES-128 in Galois Counter Mode. */
  86. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm(void);
  87. /* EVP_aead_aes_256_gcm is AES-256 in Galois Counter Mode. */
  88. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm(void);
  89. /* EVP_aead_chacha20_poly1305 is the AEAD built from ChaCha20 and
  90. * Poly1305 as described in RFC 7539. */
  91. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305(void);
  92. /* EVP_aead_aes_128_ctr_hmac_sha256 is AES-128 in CTR mode with HMAC-SHA256 for
  93. * authentication. The nonce is 12 bytes; the bottom 32-bits are used as the
  94. * block counter, thus the maximum plaintext size is 64GB. */
  95. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void);
  96. /* EVP_aead_aes_256_ctr_hmac_sha256 is AES-256 in CTR mode with HMAC-SHA256 for
  97. * authentication. See |EVP_aead_aes_128_ctr_hmac_sha256| for details. */
  98. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void);
  99. /* EVP_aead_aes_128_gcm_siv is AES-128 in GCM-SIV mode. See
  100. * https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02 */
  101. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void);
  102. /* EVP_aead_aes_256_gcm_siv is AES-256 in GCM-SIV mode. See
  103. * https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02 */
  104. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void);
  105. /* EVP_has_aes_hardware returns one if we enable hardware support for fast and
  106. * constant-time AES-GCM. */
  107. OPENSSL_EXPORT int EVP_has_aes_hardware(void);
  108. /* Utility functions. */
  109. /* EVP_AEAD_key_length returns the length, in bytes, of the keys used by
  110. * |aead|. */
  111. OPENSSL_EXPORT size_t EVP_AEAD_key_length(const EVP_AEAD *aead);
  112. /* EVP_AEAD_nonce_length returns the length, in bytes, of the per-message nonce
  113. * for |aead|. */
  114. OPENSSL_EXPORT size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead);
  115. /* EVP_AEAD_max_overhead returns the maximum number of additional bytes added
  116. * by the act of sealing data with |aead|. */
  117. OPENSSL_EXPORT size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead);
  118. /* EVP_AEAD_max_tag_len returns the maximum tag length when using |aead|. This
  119. * is the largest value that can be passed as |tag_len| to
  120. * |EVP_AEAD_CTX_init|. */
  121. OPENSSL_EXPORT size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead);
  122. /* AEAD operations. */
  123. /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key
  124. * and message-independent IV. */
  125. typedef struct evp_aead_ctx_st {
  126. const EVP_AEAD *aead;
  127. /* aead_state is an opaque pointer to whatever state the AEAD needs to
  128. * maintain. */
  129. void *aead_state;
  130. /* tag_len may contain the actual length of the authentication tag if it is
  131. * known at initialization time. */
  132. uint8_t tag_len;
  133. } EVP_AEAD_CTX;
  134. /* EVP_AEAD_MAX_KEY_LENGTH contains the maximum key length used by
  135. * any AEAD defined in this header. */
  136. #define EVP_AEAD_MAX_KEY_LENGTH 80
  137. /* EVP_AEAD_MAX_NONCE_LENGTH contains the maximum nonce length used by
  138. * any AEAD defined in this header. */
  139. #define EVP_AEAD_MAX_NONCE_LENGTH 16
  140. /* EVP_AEAD_MAX_OVERHEAD contains the maximum overhead used by any AEAD
  141. * defined in this header. */
  142. #define EVP_AEAD_MAX_OVERHEAD 64
  143. /* EVP_AEAD_DEFAULT_TAG_LENGTH is a magic value that can be passed to
  144. * EVP_AEAD_CTX_init to indicate that the default tag length for an AEAD should
  145. * be used. */
  146. #define EVP_AEAD_DEFAULT_TAG_LENGTH 0
  147. /* EVP_AEAD_CTX_zero sets an uninitialized |ctx| to the zero state. It must be
  148. * initialized with |EVP_AEAD_CTX_init| before use. It is safe, but not
  149. * necessary, to call |EVP_AEAD_CTX_cleanup| in this state. This may be used for
  150. * more uniform cleanup of |EVP_AEAD_CTX|. */
  151. OPENSSL_EXPORT void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx);
  152. /* EVP_AEAD_CTX_new allocates an |EVP_AEAD_CTX|, calls |EVP_AEAD_CTX_init| and
  153. * returns the |EVP_AEAD_CTX|, or NULL on error. */
  154. OPENSSL_EXPORT EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead,
  155. const uint8_t *key,
  156. size_t key_len, size_t tag_len);
  157. /* EVP_AEAD_CTX_free calls |EVP_AEAD_CTX_cleanup| and |OPENSSL_free| on
  158. * |ctx|. */
  159. OPENSSL_EXPORT void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx);
  160. /* EVP_AEAD_CTX_init initializes |ctx| for the given AEAD algorithm. The |impl|
  161. * argument is ignored and should be NULL. Authentication tags may be truncated
  162. * by passing a size as |tag_len|. A |tag_len| of zero indicates the default
  163. * tag length and this is defined as EVP_AEAD_DEFAULT_TAG_LENGTH for
  164. * readability.
  165. *
  166. * Returns 1 on success. Otherwise returns 0 and pushes to the error stack. In
  167. * the error case, you do not need to call |EVP_AEAD_CTX_cleanup|, but it's
  168. * harmless to do so. */
  169. OPENSSL_EXPORT int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  170. const uint8_t *key, size_t key_len,
  171. size_t tag_len, ENGINE *impl);
  172. /* EVP_AEAD_CTX_cleanup frees any data allocated by |ctx|. It is a no-op to
  173. * call |EVP_AEAD_CTX_cleanup| on a |EVP_AEAD_CTX| that has been |memset| to
  174. * all zeros. */
  175. OPENSSL_EXPORT void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx);
  176. /* EVP_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and
  177. * authenticates |ad_len| bytes from |ad| and writes the result to |out|. It
  178. * returns one on success and zero otherwise.
  179. *
  180. * This function may be called concurrently with itself or any other seal/open
  181. * function on the same |EVP_AEAD_CTX|.
  182. *
  183. * At most |max_out_len| bytes are written to |out| and, in order to ensure
  184. * success, |max_out_len| should be |in_len| plus the result of
  185. * |EVP_AEAD_max_overhead|. On successful return, |*out_len| is set to the
  186. * actual number of bytes written.
  187. *
  188. * The length of |nonce|, |nonce_len|, must be equal to the result of
  189. * |EVP_AEAD_nonce_length| for this AEAD.
  190. *
  191. * |EVP_AEAD_CTX_seal| never results in a partial output. If |max_out_len| is
  192. * insufficient, zero will be returned. If any error occurs, |out| will be
  193. * filled with zero bytes and |*out_len| set to zero.
  194. *
  195. * If |in| and |out| alias then |out| must be == |in|. */
  196. OPENSSL_EXPORT int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  197. size_t *out_len, size_t max_out_len,
  198. const uint8_t *nonce, size_t nonce_len,
  199. const uint8_t *in, size_t in_len,
  200. const uint8_t *ad, size_t ad_len);
  201. /* EVP_AEAD_CTX_open authenticates |in_len| bytes from |in| and |ad_len| bytes
  202. * from |ad| and decrypts at most |in_len| bytes into |out|. It returns one on
  203. * success and zero otherwise.
  204. *
  205. * This function may be called concurrently with itself or any other seal/open
  206. * function on the same |EVP_AEAD_CTX|.
  207. *
  208. * At most |in_len| bytes are written to |out|. In order to ensure success,
  209. * |max_out_len| should be at least |in_len|. On successful return, |*out_len|
  210. * is set to the the actual number of bytes written.
  211. *
  212. * The length of |nonce|, |nonce_len|, must be equal to the result of
  213. * |EVP_AEAD_nonce_length| for this AEAD.
  214. *
  215. * |EVP_AEAD_CTX_open| never results in a partial output. If |max_out_len| is
  216. * insufficient, zero will be returned. If any error occurs, |out| will be
  217. * filled with zero bytes and |*out_len| set to zero.
  218. *
  219. * If |in| and |out| alias then |out| must be == |in|. */
  220. OPENSSL_EXPORT int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  221. size_t *out_len, size_t max_out_len,
  222. const uint8_t *nonce, size_t nonce_len,
  223. const uint8_t *in, size_t in_len,
  224. const uint8_t *ad, size_t ad_len);
  225. /* EVP_AEAD_CTX_seal_scatter encrypts and authenticates |in_len| bytes from |in|
  226. * and authenticates |ad_len| bytes from |ad|. It writes |in_len| bytes of
  227. * ciphertext to |out| and the authentication tag to |out_tag|. It returns one
  228. * on success and zero otherwise.
  229. *
  230. * This function may be called concurrently with itself or any other seal/open
  231. * function on the same |EVP_AEAD_CTX|.
  232. *
  233. * Exactly |in_len| bytes are written to |out|, and up to
  234. * |EVP_AEAD_max_overhead+extra_in_len| bytes to |out_tag|. On successful
  235. * return, |*out_tag_len| is set to the actual number of bytes written to
  236. * |out_tag|.
  237. *
  238. * |extra_in| may point to an additional plaintext input buffer if the cipher
  239. * supports it. If present, |extra_in_len| additional bytes of plaintext are
  240. * encrypted and authenticated, and the ciphertext is written (before the tag)
  241. * to |out_tag|. |max_out_tag_len| must be sized to allow for the additional
  242. * |extra_in_len| bytes.
  243. *
  244. * The length of |nonce|, |nonce_len|, must be equal to the result of
  245. * |EVP_AEAD_nonce_length| for this AEAD.
  246. *
  247. * |EVP_AEAD_CTX_seal_scatter| never results in a partial output. If
  248. * |max_out_tag_len| is insufficient, zero will be returned. If any error
  249. * occurs, |out| and |out_tag| will be filled with zero bytes and |*out_tag_len|
  250. * set to zero.
  251. *
  252. * If |in| and |out| alias then |out| must be == |in|. |out_tag| may not alias
  253. * any other argument. */
  254. OPENSSL_EXPORT int EVP_AEAD_CTX_seal_scatter(
  255. const EVP_AEAD_CTX *ctx, uint8_t *out,
  256. uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
  257. const uint8_t *nonce, size_t nonce_len,
  258. const uint8_t *in, size_t in_len,
  259. const uint8_t *extra_in, size_t extra_in_len,
  260. const uint8_t *ad, size_t ad_len);
  261. /* EVP_AEAD_CTX_open_gather decrypts and authenticates |in_len| bytes from |in|
  262. * and authenticates |ad_len| bytes from |ad| using |in_tag_len| bytes of
  263. * authentication tag from |in_tag|. If successful, it writes |in_len| bytes of
  264. * plaintext to |out|. It returns one on success and zero otherwise.
  265. *
  266. * This function may be called concurrently with itself or any other seal/open
  267. * function on the same |EVP_AEAD_CTX|.
  268. *
  269. * The length of |nonce|, |nonce_len|, must be equal to the result of
  270. * |EVP_AEAD_nonce_length| for this AEAD.
  271. *
  272. * |EVP_AEAD_CTX_open_gather| never results in a partial output. If any error
  273. * occurs, |out| will be filled with zero bytes.
  274. *
  275. * If |in| and |out| alias then |out| must be == |in|. */
  276. OPENSSL_EXPORT int EVP_AEAD_CTX_open_gather(
  277. const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
  278. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
  279. size_t in_tag_len, const uint8_t *ad, size_t ad_len);
  280. /* EVP_AEAD_CTX_aead returns the underlying AEAD for |ctx|, or NULL if one has
  281. * not been set. */
  282. OPENSSL_EXPORT const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx);
  283. /* TLS-specific AEAD algorithms.
  284. *
  285. * These AEAD primitives do not meet the definition of generic AEADs. They are
  286. * all specific to TLS and should not be used outside of that context. They must
  287. * be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful, and may
  288. * not be used concurrently. Any nonces are used as IVs, so they must be
  289. * unpredictable. They only accept an |ad| parameter of length 11 (the standard
  290. * TLS one with length omitted). */
  291. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void);
  292. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void);
  293. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void);
  294. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void);
  295. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void);
  296. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void);
  297. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void);
  298. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void);
  299. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void);
  300. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_tls(void);
  301. /* EVP_aead_aes_128_gcm_tls12 is AES-128 in Galois Counter Mode using the TLS
  302. * 1.2 nonce construction. */
  303. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_tls12(void);
  304. /* EVP_aead_aes_256_gcm_tls12 is AES-256 in Galois Counter Mode using the TLS
  305. * 1.2 nonce construction. */
  306. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_tls12(void);
  307. /* SSLv3-specific AEAD algorithms.
  308. *
  309. * These AEAD primitives do not meet the definition of generic AEADs. They are
  310. * all specific to SSLv3 and should not be used outside of that context. They
  311. * must be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful,
  312. * and may not be used concurrently. They only accept an |ad| parameter of
  313. * length 9 (the standard TLS one with length and version omitted). */
  314. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void);
  315. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void);
  316. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void);
  317. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_ssl3(void);
  318. /* Obscure functions. */
  319. /* evp_aead_direction_t denotes the direction of an AEAD operation. */
  320. enum evp_aead_direction_t {
  321. evp_aead_open,
  322. evp_aead_seal,
  323. };
  324. /* EVP_AEAD_CTX_init_with_direction calls |EVP_AEAD_CTX_init| for normal
  325. * AEADs. For TLS-specific and SSL3-specific AEADs, it initializes |ctx| for a
  326. * given direction. */
  327. OPENSSL_EXPORT int EVP_AEAD_CTX_init_with_direction(
  328. EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len,
  329. size_t tag_len, enum evp_aead_direction_t dir);
  330. /* EVP_AEAD_CTX_get_iv sets |*out_len| to the length of the IV for |ctx| and
  331. * sets |*out_iv| to point to that many bytes of the current IV. This is only
  332. * meaningful for AEADs with implicit IVs (i.e. CBC mode in SSLv3 and TLS 1.0).
  333. *
  334. * It returns one on success or zero on error. */
  335. OPENSSL_EXPORT int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx,
  336. const uint8_t **out_iv, size_t *out_len);
  337. /* EVP_AEAD_CTX_tag_len computes the exact byte length of the tag written by
  338. * |EVP_AEAD_CTX_seal_scatter| and writes it to |*out_tag_len|. It returns one
  339. * on success or zero on error. |in_len| and |extra_in_len| must equal the
  340. * arguments of the same names passed to |EVP_AEAD_CTX_seal_scatter|. */
  341. OPENSSL_EXPORT int EVP_AEAD_CTX_tag_len(const EVP_AEAD_CTX *ctx,
  342. size_t *out_tag_len,
  343. const size_t in_len,
  344. const size_t extra_in_len);
  345. #if defined(__cplusplus)
  346. } /* extern C */
  347. #if !defined(BORINGSSL_NO_CXX)
  348. extern "C++" {
  349. namespace bssl {
  350. using ScopedEVP_AEAD_CTX =
  351. internal::StackAllocated<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero,
  352. EVP_AEAD_CTX_cleanup>;
  353. BORINGSSL_MAKE_DELETER(EVP_AEAD_CTX, EVP_AEAD_CTX_free)
  354. } // namespace bssl
  355. } // extern C++
  356. #endif
  357. #endif
  358. #endif /* OPENSSL_HEADER_AEAD_H */