cbcs_decryptor.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/cbcs_decryptor.h"
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include <string>
  8. #include <vector>
  9. #include "base/containers/span.h"
  10. #include "base/logging.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/numerics/checked_math.h"
  13. #include "crypto/symmetric_key.h"
  14. #include "media/base/decoder_buffer.h"
  15. #include "media/base/decrypt_config.h"
  16. #include "media/base/encryption_pattern.h"
  17. #include "media/base/subsample_entry.h"
  18. #include "media/cdm/aes_cbc_crypto.h"
  19. namespace media {
  20. namespace {
  21. constexpr size_t kAesBlockSizeInBytes = 16;
  22. // Decrypts |input_data| into |output_data|, using the pattern specified in
  23. // |pattern|. |pattern| only applies to full blocks. Any partial block at
  24. // the end is considered unencrypted. |output_data| must have enough room to
  25. // hold |input_data|.size() bytes.
  26. bool DecryptWithPattern(const crypto::SymmetricKey& key,
  27. base::span<const uint8_t> iv,
  28. const EncryptionPattern& pattern,
  29. base::span<const uint8_t> input_data,
  30. uint8_t* output_data) {
  31. // The AES_CBC decryption is reset for each subsample.
  32. AesCbcCrypto aes_cbc_crypto;
  33. if (!aes_cbc_crypto.Initialize(key, iv))
  34. return false;
  35. // |total_blocks| is the number of blocks in the buffer, ignoring any
  36. // partial block at the end. |remaining_bytes| is the number of bytes
  37. // in the partial block at the end of the buffer, if any.
  38. size_t total_blocks = input_data.size_bytes() / kAesBlockSizeInBytes;
  39. size_t remaining_bytes = input_data.size_bytes() % kAesBlockSizeInBytes;
  40. size_t crypt_byte_block =
  41. base::strict_cast<size_t>(pattern.crypt_byte_block());
  42. size_t skip_byte_block = base::strict_cast<size_t>(pattern.skip_byte_block());
  43. // |crypt_byte_block| and |skip_byte_block| come from 4 bit values, so fail
  44. // if these are too large.
  45. if (crypt_byte_block >= 16 || skip_byte_block >= 16)
  46. return false;
  47. if (crypt_byte_block == 0 && skip_byte_block == 0) {
  48. // From ISO/IEC 23001-7:2016(E), section 9.6.1:
  49. // "When the fields default_crypt_byte_block and default_skip_byte_block
  50. // in a version 1 Track Encryption Box ('tenc') are non-zero numbers,
  51. // pattern encryption SHALL be applied."
  52. // So for the pattern 0:0, assume that all blocks are encrypted.
  53. crypt_byte_block = total_blocks;
  54. }
  55. // Apply the pattern to |input_data|.
  56. // Example (using Pattern(2,3), Ex is encrypted, Ux unencrypted)
  57. // input_data: |E1|E2|U3|U4|U5|E6|E7|U8|U9|U10|E11|
  58. // We must decrypt 2 blocks, then simply copy the next 3 blocks, and
  59. // repeat until the end. Note that the input does not have to contain
  60. // a full pattern or even |crypt_byte_block| blocks at the end.
  61. size_t blocks_processed = 0;
  62. const uint8_t* src = input_data.data();
  63. uint8_t* dest = output_data;
  64. bool is_encrypted_blocks = false;
  65. while (blocks_processed < total_blocks) {
  66. is_encrypted_blocks = !is_encrypted_blocks;
  67. size_t blocks_to_process =
  68. std::min(is_encrypted_blocks ? crypt_byte_block : skip_byte_block,
  69. total_blocks - blocks_processed);
  70. if (blocks_to_process == 0)
  71. continue;
  72. size_t bytes_to_process = blocks_to_process * kAesBlockSizeInBytes;
  73. // From ISO/IEC 23001-7:2016(E), section 10.4.2:
  74. // For a typical pattern length of 10 (e.g. 1:9) "the pattern is repeated
  75. // every 160 bytes of the protected range, until the end of the range. If
  76. // the protected range of the slice body is not a multiple of the pattern
  77. // length (e.g. 160 bytes), then the pattern sequence applies to the
  78. // included whole 16-byte Blocks and a partial 16-byte Block that may
  79. // remain where the pattern is terminated by the byte length of the range
  80. // BytesOfProtectedData, is left unencrypted."
  81. if (is_encrypted_blocks) {
  82. if (!aes_cbc_crypto.Decrypt(base::make_span(src, bytes_to_process),
  83. dest)) {
  84. return false;
  85. }
  86. } else {
  87. memcpy(dest, src, bytes_to_process);
  88. }
  89. blocks_processed += blocks_to_process;
  90. src += bytes_to_process;
  91. dest += bytes_to_process;
  92. }
  93. // Any partial block data remaining in this subsample is considered
  94. // unencrypted so simply copy it into |dest|.
  95. if (remaining_bytes > 0)
  96. memcpy(dest, src, remaining_bytes);
  97. return true;
  98. }
  99. } // namespace
  100. scoped_refptr<DecoderBuffer> DecryptCbcsBuffer(
  101. const DecoderBuffer& input,
  102. const crypto::SymmetricKey& key) {
  103. size_t sample_size = input.data_size();
  104. DCHECK(sample_size) << "No data to decrypt.";
  105. const DecryptConfig* decrypt_config = input.decrypt_config();
  106. DCHECK(decrypt_config) << "No need to call Decrypt() on unencrypted buffer.";
  107. DCHECK_EQ(EncryptionScheme::kCbcs, decrypt_config->encryption_scheme());
  108. DCHECK(decrypt_config->HasPattern());
  109. const EncryptionPattern pattern =
  110. decrypt_config->encryption_pattern().value();
  111. // Decrypted data will be the same size as |input| size.
  112. auto buffer = base::MakeRefCounted<DecoderBuffer>(sample_size);
  113. uint8_t* output_data = buffer->writable_data();
  114. buffer->set_timestamp(input.timestamp());
  115. buffer->set_duration(input.duration());
  116. buffer->set_is_key_frame(input.is_key_frame());
  117. buffer->CopySideDataFrom(input.side_data(), input.side_data_size());
  118. const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
  119. if (subsamples.empty()) {
  120. // Assume the whole buffer is encrypted.
  121. return DecryptWithPattern(
  122. key, base::as_bytes(base::make_span(decrypt_config->iv())),
  123. pattern, base::make_span(input.data(), sample_size), output_data)
  124. ? buffer
  125. : nullptr;
  126. }
  127. if (!VerifySubsamplesMatchSize(subsamples, sample_size)) {
  128. DVLOG(1) << "Subsample sizes do not equal input size";
  129. return nullptr;
  130. }
  131. const uint8_t* src = input.data();
  132. uint8_t* dest = output_data;
  133. for (const auto& subsample : subsamples) {
  134. if (subsample.clear_bytes) {
  135. DVLOG(4) << "Copying clear_bytes: " << subsample.clear_bytes;
  136. memcpy(dest, src, subsample.clear_bytes);
  137. src += subsample.clear_bytes;
  138. dest += subsample.clear_bytes;
  139. }
  140. if (subsample.cypher_bytes) {
  141. DVLOG(4) << "Processing cypher_bytes: " << subsample.cypher_bytes
  142. << ", pattern(" << pattern.crypt_byte_block() << ","
  143. << pattern.skip_byte_block() << ")";
  144. if (!DecryptWithPattern(
  145. key, base::as_bytes(base::make_span(decrypt_config->iv())),
  146. pattern, base::make_span(src, subsample.cypher_bytes), dest)) {
  147. return nullptr;
  148. }
  149. src += subsample.cypher_bytes;
  150. dest += subsample.cypher_bytes;
  151. }
  152. }
  153. return buffer;
  154. }
  155. } // namespace media