s2n_aead_cipher_aes_gcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/aes.h>
  16. #include <openssl/evp.h>
  17. #include "crypto/s2n_cipher.h"
  18. #include "crypto/s2n_ktls_crypto.h"
  19. #include "tls/s2n_crypto.h"
  20. #include "utils/s2n_blob.h"
  21. #include "utils/s2n_safety.h"
  22. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  23. #define S2N_AEAD_AES_GCM_AVAILABLE
  24. #endif
  25. static uint8_t s2n_aead_cipher_aes128_gcm_available()
  26. {
  27. #if defined(S2N_AEAD_AES_GCM_AVAILABLE)
  28. return (EVP_aead_aes_128_gcm() ? 1 : 0);
  29. #else
  30. return (EVP_aes_128_gcm() ? 1 : 0);
  31. #endif
  32. }
  33. static uint8_t s2n_aead_cipher_aes256_gcm_available()
  34. {
  35. #if defined(S2N_AEAD_AES_GCM_AVAILABLE)
  36. return (EVP_aead_aes_256_gcm() ? 1 : 0);
  37. #else
  38. return (EVP_aes_256_gcm() ? 1 : 0);
  39. #endif
  40. }
  41. #if defined(S2N_AEAD_AES_GCM_AVAILABLE) /* BoringSSL and AWS-LC AEAD API implementation */
  42. static int s2n_aead_cipher_aes_gcm_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_REF(in);
  45. POSIX_ENSURE_REF(out);
  46. POSIX_ENSURE_REF(iv);
  47. POSIX_ENSURE_REF(key);
  48. POSIX_ENSURE_REF(aad);
  49. /* The size of the |in| blob includes the size of the data and the size of the AES-GCM tag */
  50. POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN);
  51. POSIX_ENSURE_GTE(out->size, in->size);
  52. POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN);
  53. /* Adjust input length to account for the Tag length */
  54. size_t in_len = in->size - S2N_TLS_GCM_TAG_LEN;
  55. /* out_len is set by EVP_AEAD_CTX_seal and checked post operation */
  56. size_t out_len = 0;
  57. 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);
  58. S2N_ERROR_IF((in_len + S2N_TLS_GCM_TAG_LEN) != out_len, S2N_ERR_ENCRYPT);
  59. return S2N_SUCCESS;
  60. }
  61. static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  62. {
  63. POSIX_ENSURE_REF(in);
  64. POSIX_ENSURE_REF(out);
  65. POSIX_ENSURE_REF(iv);
  66. POSIX_ENSURE_REF(key);
  67. POSIX_ENSURE_REF(aad);
  68. POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN);
  69. POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_GCM_TAG_LEN);
  70. POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN);
  71. /* out_len is set by EVP_AEAD_CTX_open and checked post operation */
  72. size_t out_len = 0;
  73. 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);
  74. S2N_ERROR_IF((in->size - S2N_TLS_GCM_TAG_LEN) != out_len, S2N_ERR_ENCRYPT);
  75. return S2N_SUCCESS;
  76. }
  77. static int s2n_aead_cipher_aes128_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  78. {
  79. POSIX_ENSURE_REF(key);
  80. POSIX_ENSURE_REF(in);
  81. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  82. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  83. return S2N_SUCCESS;
  84. }
  85. static int s2n_aead_cipher_aes256_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  86. {
  87. POSIX_ENSURE_REF(key);
  88. POSIX_ENSURE_REF(in);
  89. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  90. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  91. return S2N_SUCCESS;
  92. }
  93. static int s2n_aead_cipher_aes128_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  94. {
  95. POSIX_ENSURE_REF(key);
  96. POSIX_ENSURE_REF(in);
  97. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  98. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  99. return S2N_SUCCESS;
  100. }
  101. static int s2n_aead_cipher_aes256_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  102. {
  103. POSIX_ENSURE_REF(key);
  104. POSIX_ENSURE_REF(in);
  105. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  106. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  107. return S2N_SUCCESS;
  108. }
  109. static int s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  110. {
  111. POSIX_ENSURE_REF(key);
  112. POSIX_ENSURE_REF(in);
  113. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  114. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  115. return S2N_SUCCESS;
  116. }
  117. static int s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  118. {
  119. POSIX_ENSURE_REF(key);
  120. POSIX_ENSURE_REF(in);
  121. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  122. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  123. return S2N_SUCCESS;
  124. }
  125. static int s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  126. {
  127. POSIX_ENSURE_REF(key);
  128. POSIX_ENSURE_REF(in);
  129. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  130. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  131. return S2N_SUCCESS;
  132. }
  133. static int s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  134. {
  135. POSIX_ENSURE_REF(key);
  136. POSIX_ENSURE_REF(in);
  137. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  138. POSIX_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT);
  139. return S2N_SUCCESS;
  140. }
  141. static int s2n_aead_cipher_aes_gcm_init(struct s2n_session_key *key)
  142. {
  143. POSIX_ENSURE_REF(key);
  144. EVP_AEAD_CTX_zero(key->evp_aead_ctx);
  145. return S2N_SUCCESS;
  146. }
  147. static int s2n_aead_cipher_aes_gcm_destroy_key(struct s2n_session_key *key)
  148. {
  149. POSIX_ENSURE_REF(key);
  150. EVP_AEAD_CTX_cleanup(key->evp_aead_ctx);
  151. return S2N_SUCCESS;
  152. }
  153. #else /* Standard AES-GCM implementation */
  154. static int s2n_aead_cipher_aes_gcm_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  155. {
  156. /* The size of the |in| blob includes the size of the data and the size of the AES-GCM tag */
  157. POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN);
  158. POSIX_ENSURE_GTE(out->size, in->size);
  159. POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN);
  160. /* Initialize the IV */
  161. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  162. /* Adjust input length and buffer pointer to account for the Tag length */
  163. int in_len = in->size - S2N_TLS_GCM_TAG_LEN;
  164. uint8_t *tag_data = out->data + out->size - S2N_TLS_GCM_TAG_LEN;
  165. /* out_len is set by EVP_EncryptUpdate and checked post operation */
  166. int out_len = 0;
  167. /* Specify the AAD */
  168. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_ENCRYPT);
  169. /* Encrypt the data */
  170. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len), S2N_ERR_ENCRYPT);
  171. /* When using AES-GCM, *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_GCM_TAG_LEN into account */
  172. S2N_ERROR_IF(in_len != out_len, S2N_ERR_ENCRYPT);
  173. /* Finalize */
  174. POSIX_GUARD_OSSL(EVP_EncryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len), S2N_ERR_ENCRYPT);
  175. /* write the tag */
  176. POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_GET_TAG, S2N_TLS_GCM_TAG_LEN, tag_data), S2N_ERR_ENCRYPT);
  177. /* When using AES-GCM, EVP_EncryptFinal_ex does not write any bytes. So, we should expect *out_len = 0. */
  178. S2N_ERROR_IF(0 != out_len, S2N_ERR_ENCRYPT);
  179. return S2N_SUCCESS;
  180. }
  181. static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out)
  182. {
  183. POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN);
  184. POSIX_ENSURE_GTE(out->size, in->size);
  185. POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN);
  186. /* Initialize the IV */
  187. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  188. /* Adjust input length and buffer pointer to account for the Tag length */
  189. int in_len = in->size - S2N_TLS_GCM_TAG_LEN;
  190. uint8_t *tag_data = in->data + in->size - S2N_TLS_GCM_TAG_LEN;
  191. /* Set the TAG */
  192. POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_TAG, S2N_TLS_GCM_TAG_LEN, tag_data), S2N_ERR_DECRYPT);
  193. /* out_len is set by EVP_DecryptUpdate. While we verify the content of out_len in
  194. * s2n_aead_chacha20_poly1305_encrypt, we refrain from this here. This is to avoid
  195. * doing any branching before the ciphertext is verified. */
  196. int out_len = 0;
  197. /* Specify the AAD */
  198. POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_DECRYPT);
  199. int evp_decrypt_rc = 1;
  200. /* Decrypt the data, but don't short circuit tag verification. EVP_Decrypt* return 0 on failure, 1 for success. */
  201. evp_decrypt_rc &= EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len);
  202. /* Verify the tag */
  203. evp_decrypt_rc &= EVP_DecryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len);
  204. S2N_ERROR_IF(evp_decrypt_rc != 1, S2N_ERR_DECRYPT);
  205. return S2N_SUCCESS;
  206. }
  207. static int s2n_aead_cipher_aes128_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  208. {
  209. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  210. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  211. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL);
  212. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  213. return S2N_SUCCESS;
  214. }
  215. static int s2n_aead_cipher_aes256_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  216. {
  217. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  218. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  219. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL);
  220. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  221. return S2N_SUCCESS;
  222. }
  223. static int s2n_aead_cipher_aes128_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  224. {
  225. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN);
  226. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  227. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL);
  228. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  229. return S2N_SUCCESS;
  230. }
  231. static int s2n_aead_cipher_aes256_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  232. {
  233. POSIX_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN);
  234. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT);
  235. EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL);
  236. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  237. return S2N_SUCCESS;
  238. }
  239. static int s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  240. {
  241. POSIX_GUARD(s2n_aead_cipher_aes128_gcm_set_encryption_key(key, in));
  242. return S2N_SUCCESS;
  243. }
  244. static int s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  245. {
  246. POSIX_GUARD(s2n_aead_cipher_aes256_gcm_set_encryption_key(key, in));
  247. return S2N_SUCCESS;
  248. }
  249. static int s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  250. {
  251. POSIX_GUARD(s2n_aead_cipher_aes128_gcm_set_decryption_key(key, in));
  252. return S2N_SUCCESS;
  253. }
  254. static int s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in)
  255. {
  256. POSIX_GUARD(s2n_aead_cipher_aes256_gcm_set_decryption_key(key, in));
  257. return S2N_SUCCESS;
  258. }
  259. static int s2n_aead_cipher_aes_gcm_init(struct s2n_session_key *key)
  260. {
  261. s2n_evp_ctx_init(key->evp_cipher_ctx);
  262. return S2N_SUCCESS;
  263. }
  264. static int s2n_aead_cipher_aes_gcm_destroy_key(struct s2n_session_key *key)
  265. {
  266. EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
  267. return S2N_SUCCESS;
  268. }
  269. #endif
  270. static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_ktls_info(struct s2n_ktls_crypto_info_inputs *in,
  271. struct s2n_ktls_crypto_info *out)
  272. {
  273. RESULT_ENSURE_REF(in);
  274. RESULT_ENSURE_REF(out);
  275. s2n_ktls_crypto_info_tls12_aes_gcm_128 *crypto_info = &out->ciphers.aes_gcm_128;
  276. crypto_info->info.version = TLS_1_2_VERSION;
  277. crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_128;
  278. RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size);
  279. RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key));
  280. RESULT_ENSURE_LTE(sizeof(crypto_info->iv), in->iv.size);
  281. RESULT_CHECKED_MEMCPY(crypto_info->iv, in->iv.data, sizeof(crypto_info->iv));
  282. RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size);
  283. RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq));
  284. /* The salt is a prefix of the IV
  285. *
  286. *= https://www.rfc-editor.org/rfc/rfc4106#section-4
  287. *# The salt field is a four-octet value that is assigned at the
  288. *# beginning of the security association, and then remains constant
  289. *# for the life of the security association.
  290. */
  291. RESULT_ENSURE_LTE(sizeof(crypto_info->salt), in->iv.size);
  292. RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, sizeof(crypto_info->salt));
  293. RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info,
  294. sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_128)));
  295. return S2N_RESULT_OK;
  296. }
  297. static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_ktls_info(
  298. struct s2n_ktls_crypto_info_inputs *in, struct s2n_ktls_crypto_info *out)
  299. {
  300. RESULT_ENSURE_REF(in);
  301. RESULT_ENSURE_REF(out);
  302. s2n_ktls_crypto_info_tls12_aes_gcm_256 *crypto_info = &out->ciphers.aes_gcm_256;
  303. crypto_info->info.version = TLS_1_2_VERSION;
  304. crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_256;
  305. RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size);
  306. RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key));
  307. RESULT_ENSURE_LTE(sizeof(crypto_info->iv), in->iv.size);
  308. RESULT_CHECKED_MEMCPY(crypto_info->iv, in->iv.data, sizeof(crypto_info->iv));
  309. RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size);
  310. RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq));
  311. /* The salt is a prefix of the IV
  312. *
  313. *= https://www.rfc-editor.org/rfc/rfc4106#section-4
  314. *# The salt field is a four-octet value that is assigned at the
  315. *# beginning of the security association, and then remains constant
  316. *# for the life of the security association.
  317. */
  318. RESULT_ENSURE_LTE(sizeof(crypto_info->salt), in->iv.size);
  319. RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, sizeof(crypto_info->salt));
  320. RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info,
  321. sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_256)));
  322. return S2N_RESULT_OK;
  323. }
  324. const struct s2n_cipher s2n_aes128_gcm = {
  325. .key_material_size = S2N_TLS_AES_128_GCM_KEY_LEN,
  326. .type = S2N_AEAD,
  327. .io.aead = {
  328. .record_iv_size = S2N_TLS_GCM_EXPLICIT_IV_LEN,
  329. .fixed_iv_size = S2N_TLS_GCM_FIXED_IV_LEN,
  330. .tag_size = S2N_TLS_GCM_TAG_LEN,
  331. .decrypt = s2n_aead_cipher_aes_gcm_decrypt,
  332. .encrypt = s2n_aead_cipher_aes_gcm_encrypt },
  333. .is_available = s2n_aead_cipher_aes128_gcm_available,
  334. .init = s2n_aead_cipher_aes_gcm_init,
  335. .set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key,
  336. .set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key,
  337. .destroy_key = s2n_aead_cipher_aes_gcm_destroy_key,
  338. .set_ktls_info = s2n_aead_cipher_aes128_gcm_set_ktls_info,
  339. };
  340. const struct s2n_cipher s2n_aes256_gcm = {
  341. .key_material_size = S2N_TLS_AES_256_GCM_KEY_LEN,
  342. .type = S2N_AEAD,
  343. .io.aead = {
  344. .record_iv_size = S2N_TLS_GCM_EXPLICIT_IV_LEN,
  345. .fixed_iv_size = S2N_TLS_GCM_FIXED_IV_LEN,
  346. .tag_size = S2N_TLS_GCM_TAG_LEN,
  347. .decrypt = s2n_aead_cipher_aes_gcm_decrypt,
  348. .encrypt = s2n_aead_cipher_aes_gcm_encrypt },
  349. .is_available = s2n_aead_cipher_aes256_gcm_available,
  350. .init = s2n_aead_cipher_aes_gcm_init,
  351. .set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key,
  352. .set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key,
  353. .destroy_key = s2n_aead_cipher_aes_gcm_destroy_key,
  354. .set_ktls_info = s2n_aead_cipher_aes256_gcm_set_ktls_info,
  355. };
  356. /* TLS 1.3 GCM ciphers */
  357. const struct s2n_cipher s2n_tls13_aes128_gcm = {
  358. .key_material_size = S2N_TLS_AES_128_GCM_KEY_LEN,
  359. .type = S2N_AEAD,
  360. .io.aead = {
  361. .record_iv_size = S2N_TLS13_RECORD_IV_LEN,
  362. .fixed_iv_size = S2N_TLS13_FIXED_IV_LEN,
  363. .tag_size = S2N_TLS_GCM_TAG_LEN,
  364. .decrypt = s2n_aead_cipher_aes_gcm_decrypt,
  365. .encrypt = s2n_aead_cipher_aes_gcm_encrypt },
  366. .is_available = s2n_aead_cipher_aes128_gcm_available,
  367. .init = s2n_aead_cipher_aes_gcm_init,
  368. .set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13,
  369. .set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13,
  370. .destroy_key = s2n_aead_cipher_aes_gcm_destroy_key,
  371. };
  372. const struct s2n_cipher s2n_tls13_aes256_gcm = {
  373. .key_material_size = S2N_TLS_AES_256_GCM_KEY_LEN,
  374. .type = S2N_AEAD,
  375. .io.aead = {
  376. .record_iv_size = S2N_TLS13_RECORD_IV_LEN,
  377. .fixed_iv_size = S2N_TLS13_FIXED_IV_LEN,
  378. .tag_size = S2N_TLS_GCM_TAG_LEN,
  379. .decrypt = s2n_aead_cipher_aes_gcm_decrypt,
  380. .encrypt = s2n_aead_cipher_aes_gcm_encrypt },
  381. .is_available = s2n_aead_cipher_aes256_gcm_available,
  382. .init = s2n_aead_cipher_aes_gcm_init,
  383. .set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13,
  384. .set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13,
  385. .destroy_key = s2n_aead_cipher_aes_gcm_destroy_key,
  386. };