s2n_aead_cipher_chacha20_poly1305.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <openssl/evp.h>
  16. #include "crypto/s2n_cipher.h"
  17. #include "crypto/s2n_openssl.h"
  18. #include "tls/s2n_crypto.h"
  19. #include "utils/s2n_blob.h"
  20. #include "utils/s2n_safety.h"
  21. /* We support two different backing implementations of ChaCha20-Poly1305: one
  22. * implementation for OpenSSL (>= 1.1.0, see
  23. * https://www.openssl.org/news/cl110.txt) and one implementation for BoringSSL
  24. * and AWS-LC. LibreSSL supports ChaCha20-Poly1305, but the interface is
  25. * different.
  26. * Note, the order in the if/elif below matters because both BoringSSL and
  27. * AWS-LC define OPENSSL_VERSION_NUMBER. */
  28. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  29. #define S2N_CHACHA20_POLY1305_AVAILABLE_BSSL_AWSLC
  30. #elif (S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0) && !defined(LIBRESSL_VERSION_NUMBER))
  31. #define S2N_CHACHA20_POLY1305_AVAILABLE_OSSL
  32. #endif
  33. static uint8_t s2n_aead_chacha20_poly1305_available(void)
  34. {
  35. #if defined(S2N_CHACHA20_POLY1305_AVAILABLE_OSSL) || defined(S2N_CHACHA20_POLY1305_AVAILABLE_BSSL_AWSLC)
  36. return 1;
  37. #else
  38. return 0;
  39. #endif
  40. }
  41. #if defined(S2N_CHACHA20_POLY1305_AVAILABLE_OSSL) /* OpenSSL implementation */
  42. static int s2n_aead_chacha20_poly1305_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  43. {
  44. POSIX_ENSURE_GTE(in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  45. /* The size of the |in| blob includes the size of the data and the size of the ChaCha20-Poly1305 tag */
  46. POSIX_ENSURE_GTE(out->size, in->size);
  47. POSIX_ENSURE_EQ(iv->size, S2N_TLS_CHACHA20_POLY1305_IV_LEN);
  48. /* Initialize the IV */
  49. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  50. /* Adjust input length and buffer pointer to account for the Tag length */
  51. int in_len = in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
  52. uint8_t *tag_data = out->data + out->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
  53. /* out_len is set by EVP_EncryptUpdate and checked post operation */
  54. int out_len = 0;
  55. /* Specify the AAD */
  56. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_ENCRYPT);
  57. /* Encrypt the data */
  58. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len), S2N_ERR_ENCRYPT);
  59. /* For OpenSSL 1.1.0 and 1.1.1, when using ChaCha20-Poly1305, *out_len is the number of bytes written by EVP_EncryptUpdate. Since the tag is not written during this call, we do not take S2N_TLS_CHACHA20_POLY1305_TAG_LEN into account */
  60. S2N_ERROR_IF(in_len != out_len, S2N_ERR_ENCRYPT);
  61. /* Finalize */
  62. POSIX_GUARD_OSSL(EVP_EncryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len), S2N_ERR_ENCRYPT);
  63. /* Write the tag */
  64. POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_GET_TAG, S2N_TLS_CHACHA20_POLY1305_TAG_LEN, tag_data), S2N_ERR_ENCRYPT);
  65. /* For OpenSSL 1.1.0 and 1.1.1, when using ChaCha20-Poly1305, EVP_EncryptFinal_ex does not write any bytes. So, we should expect *out_len = 0. */
  66. S2N_ERROR_IF(0 != out_len, S2N_ERR_ENCRYPT);
  67. return 0;
  68. }
  69. static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  70. {
  71. POSIX_ENSURE_GTE(in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  72. POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  73. POSIX_ENSURE_EQ(iv->size, S2N_TLS_CHACHA20_POLY1305_IV_LEN);
  74. /* Initialize the IV */
  75. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  76. /* Adjust input length and buffer pointer to account for the Tag length */
  77. int in_len = in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
  78. uint8_t *tag_data = in->data + in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
  79. /* Set the TAG */
  80. POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_TAG, S2N_TLS_CHACHA20_POLY1305_TAG_LEN, tag_data), S2N_ERR_DECRYPT);
  81. /* out_len is set by EVP_DecryptUpdate. While we verify the content of out_len in
  82. * s2n_aead_chacha20_poly1305_encrypt, we refrain from this here. This is to avoid
  83. * doing any branching before the ciphertext is verified. */
  84. int out_len = 0;
  85. /* Specify the AAD */
  86. POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_DECRYPT);
  87. int evp_decrypt_rc = 1;
  88. /* Decrypt the data, but don't short circuit tag verification. EVP_Decrypt* return 0 on failure, 1 for success. */
  89. evp_decrypt_rc &= EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len);
  90. /* Verify the tag */
  91. evp_decrypt_rc &= EVP_DecryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len);
  92. S2N_ERROR_IF(evp_decrypt_rc != 1, S2N_ERR_DECRYPT);
  93. return 0;
  94. }
  95. static int s2n_aead_chacha20_poly1305_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  96. {
  97. POSIX_ENSURE_EQ(in->size, S2N_TLS_CHACHA20_POLY1305_KEY_LEN);
  98. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  99. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_SET_IVLEN, S2N_TLS_CHACHA20_POLY1305_IV_LEN, NULL);
  100. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  101. return 0;
  102. }
  103. static int s2n_aead_chacha20_poly1305_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  104. {
  105. POSIX_ENSURE_EQ(in->size, S2N_TLS_CHACHA20_POLY1305_KEY_LEN);
  106. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  107. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_SET_IVLEN, S2N_TLS_CHACHA20_POLY1305_IV_LEN, NULL);
  108. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  109. return 0;
  110. }
  111. static int s2n_aead_chacha20_poly1305_init(struct s2n_session_key *key)
  112. {
  113. s2n_evp_ctx_init(key->evp_cipher_ctx);
  114. return 0;
  115. }
  116. static int s2n_aead_chacha20_poly1305_destroy_key(struct s2n_session_key *key)
  117. {
  118. EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
  119. return 0;
  120. }
  121. #elif defined(S2N_CHACHA20_POLY1305_AVAILABLE_BSSL_AWSLC) /* BoringSSL and AWS-LC implementation */
  122. static int s2n_aead_chacha20_poly1305_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  123. {
  124. POSIX_ENSURE_GTE(in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  125. /* The size of the |in| blob includes the size of the data and the size of the ChaCha20-Poly1305 tag */
  126. POSIX_ENSURE_GTE(out->size, in->size);
  127. POSIX_ENSURE_EQ(iv->size, S2N_TLS_CHACHA20_POLY1305_IV_LEN);
  128. /* Adjust input length to account for the Tag length */
  129. size_t in_len = in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
  130. /* out_len is set by EVP_AEAD_CTX_seal and checked post operation */
  131. size_t out_len = 0;
  132. POSIX_GUARD_OSSL(EVP_AEAD_CTX_seal(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in_len, aad->data, aad->size), S2N_ERR_ENCRYPT);
  133. S2N_ERROR_IF((in_len + S2N_TLS_CHACHA20_POLY1305_TAG_LEN) != out_len, S2N_ERR_ENCRYPT);
  134. return 0;
  135. }
  136. static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  137. {
  138. POSIX_ENSURE_GTE(in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  139. POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
  140. POSIX_ENSURE_EQ(iv->size, S2N_TLS_CHACHA20_POLY1305_IV_LEN);
  141. /* out_len is set by EVP_AEAD_CTX_open and checked post operation */
  142. size_t out_len = 0;
  143. POSIX_GUARD_OSSL(EVP_AEAD_CTX_open(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in->size, aad->data, aad->size), S2N_ERR_DECRYPT);
  144. S2N_ERROR_IF((in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN) != out_len, S2N_ERR_ENCRYPT);
  145. return 0;
  146. }
  147. static int s2n_aead_chacha20_poly1305_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  148. {
  149. POSIX_ENSURE_EQ(in->size, S2N_TLS_CHACHA20_POLY1305_KEY_LEN);
  150. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_chacha20_poly1305(), in->data, in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  151. return 0;
  152. }
  153. static int s2n_aead_chacha20_poly1305_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  154. {
  155. POSIX_ENSURE_EQ(in->size, S2N_TLS_CHACHA20_POLY1305_KEY_LEN);
  156. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_chacha20_poly1305(), in->data, in->size, S2N_TLS_CHACHA20_POLY1305_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  157. return 0;
  158. }
  159. static int s2n_aead_chacha20_poly1305_init(struct s2n_session_key *key)
  160. {
  161. EVP_AEAD_CTX_zero(key->evp_aead_ctx);
  162. return 0;
  163. }
  164. static int s2n_aead_chacha20_poly1305_destroy_key(struct s2n_session_key *key)
  165. {
  166. EVP_AEAD_CTX_cleanup(key->evp_aead_ctx);
  167. return 0;
  168. }
  169. #else /* No ChaCha20-Poly1305 implementation exists for chosen cryptographic provider (E.g Openssl 1.0.x) */
  170. static int s2n_aead_chacha20_poly1305_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  171. {
  172. POSIX_BAIL(S2N_ERR_ENCRYPT);
  173. }
  174. static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  175. {
  176. POSIX_BAIL(S2N_ERR_DECRYPT);
  177. }
  178. static int s2n_aead_chacha20_poly1305_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  179. {
  180. POSIX_BAIL(S2N_ERR_KEY_INIT);
  181. }
  182. static int s2n_aead_chacha20_poly1305_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  183. {
  184. POSIX_BAIL(S2N_ERR_KEY_INIT);
  185. }
  186. static int s2n_aead_chacha20_poly1305_init(struct s2n_session_key *key)
  187. {
  188. POSIX_BAIL(S2N_ERR_KEY_INIT);
  189. }
  190. static int s2n_aead_chacha20_poly1305_destroy_key(struct s2n_session_key *key)
  191. {
  192. POSIX_BAIL(S2N_ERR_KEY_DESTROY);
  193. }
  194. #endif
  195. const struct s2n_cipher s2n_chacha20_poly1305 = {
  196. .key_material_size = S2N_TLS_CHACHA20_POLY1305_KEY_LEN,
  197. .type = S2N_AEAD,
  198. .io.aead = {
  199. .record_iv_size = S2N_TLS_CHACHA20_POLY1305_EXPLICIT_IV_LEN,
  200. .fixed_iv_size = S2N_TLS_CHACHA20_POLY1305_FIXED_IV_LEN,
  201. .tag_size = S2N_TLS_CHACHA20_POLY1305_TAG_LEN,
  202. .decrypt = s2n_aead_chacha20_poly1305_decrypt,
  203. .encrypt = s2n_aead_chacha20_poly1305_encrypt },
  204. .is_available = s2n_aead_chacha20_poly1305_available,
  205. .init = s2n_aead_chacha20_poly1305_init,
  206. .set_encryption_key = s2n_aead_chacha20_poly1305_set_encryption_key,
  207. .set_decryption_key = s2n_aead_chacha20_poly1305_set_decryption_key,
  208. .destroy_key = s2n_aead_chacha20_poly1305_destroy_key,
  209. };