s2n_cbc_cipher_aes.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "crypto/s2n_cipher.h"
  17. #include "crypto/s2n_openssl.h"
  18. #include "error/s2n_errno.h"
  19. #include "utils/s2n_blob.h"
  20. #include "utils/s2n_safety.h"
  21. static uint8_t s2n_cbc_cipher_aes128_available()
  22. {
  23. return (EVP_aes_128_cbc() ? 1 : 0);
  24. }
  25. static uint8_t s2n_cbc_cipher_aes256_available()
  26. {
  27. return (EVP_aes_256_cbc() ? 1 : 0);
  28. }
  29. static int s2n_cbc_cipher_aes_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
  30. {
  31. POSIX_ENSURE_GTE(out->size, in->size);
  32. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  33. /* len is set by EVP_EncryptUpdate and checked post operation */
  34. int len = 0;
  35. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
  36. POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
  37. return 0;
  38. }
  39. int s2n_cbc_cipher_aes_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
  40. {
  41. POSIX_ENSURE_GTE(out->size, in->size);
  42. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  43. /* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
  44. * the decryption operation is validated. */
  45. int len = 0;
  46. POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
  47. return 0;
  48. }
  49. int s2n_cbc_cipher_aes128_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  50. {
  51. POSIX_ENSURE_EQ(in->size, 128 / 8);
  52. /* Always returns 1 */
  53. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  54. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  55. return 0;
  56. }
  57. static int s2n_cbc_cipher_aes128_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  58. {
  59. POSIX_ENSURE_EQ(in->size, 128 / 8);
  60. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  61. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  62. return 0;
  63. }
  64. static int s2n_cbc_cipher_aes256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  65. {
  66. POSIX_ENSURE_EQ(in->size, 256 / 8);
  67. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  68. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  69. return 0;
  70. }
  71. int s2n_cbc_cipher_aes256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  72. {
  73. POSIX_ENSURE_EQ(in->size, 256 / 8);
  74. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  75. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  76. return 0;
  77. }
  78. static int s2n_cbc_cipher_aes_init(struct s2n_session_key *key)
  79. {
  80. s2n_evp_ctx_init(key->evp_cipher_ctx);
  81. return 0;
  82. }
  83. static int s2n_cbc_cipher_aes_destroy_key(struct s2n_session_key *key)
  84. {
  85. EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
  86. return 0;
  87. }
  88. const struct s2n_cipher s2n_aes128 = {
  89. .key_material_size = 16,
  90. .type = S2N_CBC,
  91. .io.cbc = {
  92. .block_size = 16,
  93. .record_iv_size = 16,
  94. .decrypt = s2n_cbc_cipher_aes_decrypt,
  95. .encrypt = s2n_cbc_cipher_aes_encrypt },
  96. .is_available = s2n_cbc_cipher_aes128_available,
  97. .init = s2n_cbc_cipher_aes_init,
  98. .set_decryption_key = s2n_cbc_cipher_aes128_set_decryption_key,
  99. .set_encryption_key = s2n_cbc_cipher_aes128_set_encryption_key,
  100. .destroy_key = s2n_cbc_cipher_aes_destroy_key,
  101. };
  102. const struct s2n_cipher s2n_aes256 = {
  103. .key_material_size = 32,
  104. .type = S2N_CBC,
  105. .io.cbc = {
  106. .block_size = 16,
  107. .record_iv_size = 16,
  108. .decrypt = s2n_cbc_cipher_aes_decrypt,
  109. .encrypt = s2n_cbc_cipher_aes_encrypt },
  110. .is_available = s2n_cbc_cipher_aes256_available,
  111. .init = s2n_cbc_cipher_aes_init,
  112. .set_decryption_key = s2n_cbc_cipher_aes256_set_decryption_key,
  113. .set_encryption_key = s2n_cbc_cipher_aes256_set_encryption_key,
  114. .destroy_key = s2n_cbc_cipher_aes_destroy_key,
  115. };