aes_cbc_crypto.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/cdm/aes_cbc_crypto.h"
  5. #include "base/logging.h"
  6. #include "base/numerics/safe_conversions.h"
  7. #include "crypto/openssl_util.h"
  8. #include "crypto/symmetric_key.h"
  9. #include "third_party/boringssl/src/include/openssl/aes.h"
  10. #include "third_party/boringssl/src/include/openssl/crypto.h"
  11. #include "third_party/boringssl/src/include/openssl/err.h"
  12. #include "third_party/boringssl/src/include/openssl/evp.h"
  13. // Notes on using OpenSSL:
  14. // https://www.openssl.org/docs/man1.1.0/crypto/EVP_DecryptUpdate.html
  15. // The documentation for EVP_DecryptUpdate() only states
  16. // "EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex()
  17. // are the corresponding decryption operations. EVP_DecryptFinal() will
  18. // return an error code if padding is enabled and the final block is not
  19. // correctly formatted. The parameters and restrictions are identical
  20. // to the encryption operations except that if padding is enabled ..."
  21. // As this implementation does not use padding, the last part should not be
  22. // an issue. However, there is no mention whether data can be decrypted
  23. // block-by-block or if all the data must be unencrypted at once.
  24. //
  25. // The documentation for EVP_EncryptUpdate() (same page as above) states
  26. // "EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes
  27. // the encrypted version to out. This function can be called multiple times
  28. // to encrypt successive blocks of data."
  29. // Given that the EVP_Decrypt* methods have the same restrictions, the code
  30. // below assumes that EVP_DecryptUpdate() can be called on a block-by-block
  31. // basis. A test in aes_cbc_crypto_unittest.cc verifies this.
  32. namespace media {
  33. AesCbcCrypto::AesCbcCrypto() {
  34. // Ensure the crypto library is initialized. CRYPTO_library_init may be
  35. // safely called concurrently.
  36. CRYPTO_library_init();
  37. EVP_CIPHER_CTX_init(&ctx_);
  38. }
  39. AesCbcCrypto::~AesCbcCrypto() {
  40. EVP_CIPHER_CTX_cleanup(&ctx_);
  41. }
  42. bool AesCbcCrypto::Initialize(const crypto::SymmetricKey& key,
  43. base::span<const uint8_t> iv) {
  44. crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  45. // This uses AES-CBC-128, so the key must be 128 bits.
  46. const EVP_CIPHER* cipher = EVP_aes_128_cbc();
  47. const uint8_t* key_data = reinterpret_cast<const uint8_t*>(key.key().data());
  48. if (key.key().length() != EVP_CIPHER_key_length(cipher)) {
  49. DVLOG(1) << "Key length is incorrect.";
  50. return false;
  51. }
  52. // |iv| must also be 128 bits.
  53. if (iv.size_bytes() != EVP_CIPHER_iv_length(cipher)) {
  54. DVLOG(1) << "IV length is incorrect.";
  55. return false;
  56. }
  57. if (!EVP_DecryptInit_ex(&ctx_, cipher, nullptr, key_data, iv.data())) {
  58. DVLOG(1) << "EVP_DecryptInit_ex() failed.";
  59. return false;
  60. }
  61. if (!EVP_CIPHER_CTX_set_padding(&ctx_, 0)) {
  62. DVLOG(1) << "EVP_CIPHER_CTX_set_padding() failed.";
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool AesCbcCrypto::Decrypt(base::span<const uint8_t> encrypted_data,
  68. uint8_t* decrypted_data) {
  69. crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  70. if (encrypted_data.size_bytes() % EVP_CIPHER_CTX_block_size(&ctx_) != 0) {
  71. DVLOG(1) << "Encrypted bytes not a multiple of block size.";
  72. return false;
  73. }
  74. int out_length;
  75. if (!EVP_DecryptUpdate(&ctx_, decrypted_data, &out_length,
  76. encrypted_data.data(), encrypted_data.size_bytes())) {
  77. DVLOG(1) << "EVP_DecryptUpdate() failed.";
  78. return false;
  79. }
  80. return encrypted_data.size_bytes() == base::checked_cast<size_t>(out_length);
  81. }
  82. } // namespace media