s2n_cbc_cipher_3des.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "error/s2n_errno.h"
  19. #include "utils/s2n_blob.h"
  20. #include "utils/s2n_safety.h"
  21. static uint8_t s2n_cbc_cipher_3des_available()
  22. {
  23. return (EVP_des_ede3_cbc() ? 1 : 0);
  24. }
  25. static int s2n_cbc_cipher_3des_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
  26. {
  27. POSIX_ENSURE_GTE(out->size, in->size);
  28. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  29. /* len is set by EVP_EncryptUpdate and checked post operation */
  30. int len = 0;
  31. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
  32. POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
  33. return 0;
  34. }
  35. static int s2n_cbc_cipher_3des_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
  36. {
  37. POSIX_ENSURE_GTE(out->size, in->size);
  38. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
  39. /* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
  40. * the decryption operation is validated. */
  41. int len = 0;
  42. POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
  43. return 0;
  44. }
  45. static int s2n_cbc_cipher_3des_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  46. {
  47. POSIX_ENSURE_EQ(in->size, 192 / 8);
  48. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  49. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  50. return 0;
  51. }
  52. static int s2n_cbc_cipher_3des_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  53. {
  54. POSIX_ENSURE_EQ(in->size, 192 / 8);
  55. EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
  56. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  57. return 0;
  58. }
  59. static int s2n_cbc_cipher_3des_init(struct s2n_session_key *key)
  60. {
  61. s2n_evp_ctx_init(key->evp_cipher_ctx);
  62. return 0;
  63. }
  64. static int s2n_cbc_cipher_3des_destroy_key(struct s2n_session_key *key)
  65. {
  66. EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
  67. return 0;
  68. }
  69. const struct s2n_cipher s2n_3des = {
  70. .key_material_size = 24,
  71. .type = S2N_CBC,
  72. .io.cbc = {
  73. .block_size = 8,
  74. .record_iv_size = 8,
  75. .decrypt = s2n_cbc_cipher_3des_decrypt,
  76. .encrypt = s2n_cbc_cipher_3des_encrypt },
  77. .is_available = s2n_cbc_cipher_3des_available,
  78. .init = s2n_cbc_cipher_3des_init,
  79. .set_decryption_key = s2n_cbc_cipher_3des_set_decryption_key,
  80. .set_encryption_key = s2n_cbc_cipher_3des_set_encryption_key,
  81. .destroy_key = s2n_cbc_cipher_3des_destroy_key,
  82. };