| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/sys_byteorder.h" | |
| 11 #include "media/base/decoder_buffer.h" | 10 #include "media/base/decoder_buffer.h" |
| 12 #include "media/base/decrypt_config.h" | 11 #include "media/base/decrypt_config.h" |
| 13 #include "media/base/mock_filters.h" | 12 #include "media/base/mock_filters.h" |
| 14 #include "media/crypto/aes_decryptor.h" | 13 #include "media/crypto/aes_decryptor.h" |
| 15 #include "media/webm/webm_constants.h" | 14 #include "media/webm/webm_constants.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 using ::testing::_; | 18 using ::testing::_; |
| 20 using ::testing::Gt; | 19 using ::testing::Gt; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 "Original data.", 14, | 84 "Original data.", 14, |
| 86 // key_id | 85 // key_id |
| 87 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, | 86 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, |
| 88 0x2c, 0x2d, 0x2e, 0x2f, 0x30 | 87 0x2c, 0x2d, 0x2e, 0x2f, 0x30 |
| 89 }, 13, | 88 }, 13, |
| 90 // key | 89 // key |
| 91 { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, | 90 { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 92 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40 | 91 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40 |
| 93 }, 16, | 92 }, 16, |
| 94 // encrypted_data | 93 // encrypted_data |
| 95 { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 94 { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 96 0x01, 0x9c, 0x71, 0x26, 0x57, 0x3e, 0x25, 0x37, | 95 0x00, 0x9c, 0x71, 0x26, 0x57, 0x3e, 0x25, 0x37, |
| 97 0xf7, 0x31, 0x81, 0x19, 0x64, 0xce, 0xbc | 96 0xf7, 0x31, 0x81, 0x19, 0x64, 0xce, 0xbc |
| 98 }, 23 | 97 }, 23 |
| 99 }, | 98 }, |
| 100 { | 99 { |
| 101 // plaintext | 100 // plaintext |
| 102 "Changed Original data.", 22, | 101 "Changed Original data.", 22, |
| 103 // key_id | 102 // key_id |
| 104 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, | 103 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, |
| 105 0x2c, 0x2d, 0x2e, 0x2f, 0x30 | 104 0x2c, 0x2d, 0x2e, 0x2f, 0x30 |
| 106 }, 13, | 105 }, 13, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 156 |
| 158 static const SubsampleEntry kSubsampleEntries[] = { | 157 static const SubsampleEntry kSubsampleEntries[] = { |
| 159 { 2, 7 }, | 158 { 2, 7 }, |
| 160 { 3, 11 }, | 159 { 3, 11 }, |
| 161 { 1, 0 } | 160 { 1, 0 } |
| 162 }; | 161 }; |
| 163 | 162 |
| 164 // Returns a 16 byte CTR counter block. The CTR counter block format is a | 163 // Returns a 16 byte CTR counter block. The CTR counter block format is a |
| 165 // CTR IV appended with a CTR block counter. |iv| is a CTR IV. |iv_size| is | 164 // CTR IV appended with a CTR block counter. |iv| is a CTR IV. |iv_size| is |
| 166 // the size of |iv| in bytes. | 165 // the size of |iv| in bytes. |
| 167 static std::string GenerateCounterBlock(const uint8* iv, int iv_size) { | 166 static std::string GenerateCounterBlock(const uint8* iv, size_t iv_size) { |
| 168 const int kDecryptionKeySize = 16; | 167 CHECK_LE(iv_size, static_cast<size_t>(DecryptConfig::kDecryptionKeySize)); |
| 169 CHECK_GT(iv_size, 0); | |
| 170 CHECK_LE(iv_size, kDecryptionKeySize); | |
| 171 | 168 |
| 172 std::string counter_block(reinterpret_cast<const char*>(iv), iv_size); | 169 std::string counter_block(DecryptConfig::kDecryptionKeySize, 0); |
| 173 counter_block.append(kDecryptionKeySize - iv_size, 0); | 170 memcpy(&counter_block[0], reinterpret_cast<const char*>(iv), iv_size); |
| 174 return counter_block; | 171 return counter_block; |
| 175 } | 172 } |
| 176 | 173 |
| 177 // Creates a WebM encrypted buffer that the demuxer would pass to the | 174 // Creates a WebM encrypted buffer that the demuxer would pass to the |
| 178 // decryptor. |data| is the payload of a WebM encrypted Block. |key_id| is | 175 // decryptor. |data| is the payload of a WebM encrypted Block. |key_id| is |
| 179 // initialization data from the WebM file. Every encrypted Block has | 176 // initialization data from the WebM file. Every encrypted Block has |
| 180 // a signal byte prepended to a frame. If the frame is encrypted then an IV is | 177 // a signal byte prepended to a frame. If the frame is encrypted then an IV is |
| 181 // prepended to the Block. Current encrypted WebM request for comments | 178 // prepended to the Block. Current encrypted WebM request for comments |
| 182 // specification is here | 179 // specification is here |
| 183 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | 180 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
| 184 static scoped_refptr<DecoderBuffer> CreateWebMEncryptedBuffer( | 181 static scoped_refptr<DecoderBuffer> CreateWebMEncryptedBuffer( |
| 185 const uint8* data, int data_size, | 182 const uint8* data, int data_size, |
| 186 const uint8* key_id, int key_id_size) { | 183 const uint8* key_id, int key_id_size) { |
| 187 scoped_refptr<DecoderBuffer> encrypted_buffer = DecoderBuffer::CopyFrom( | 184 scoped_refptr<DecoderBuffer> encrypted_buffer = DecoderBuffer::CopyFrom( |
| 188 data, data_size); | 185 data, data_size); |
| 189 CHECK(encrypted_buffer); | 186 CHECK(encrypted_buffer); |
| 190 | 187 |
| 191 uint8 signal_byte = data[0]; | 188 uint8 signal_byte = data[0]; |
| 192 int data_offset = sizeof(signal_byte); | 189 int data_offset = sizeof(signal_byte); |
| 193 | 190 |
| 194 // Setting the DecryptConfig object of the buffer while leaving the | 191 // Setting the DecryptConfig object of the buffer while leaving the |
| 195 // initialization vector empty will tell the decryptor that the frame is | 192 // initialization vector empty will tell the decryptor that the frame is |
| 196 // unencrypted. | 193 // unencrypted. |
| 197 std::string counter_block_str; | 194 std::string counter_block_str; |
| 198 | 195 |
| 199 if (signal_byte & kWebMFlagEncryptedFrame) { | 196 if (signal_byte & kWebMFlagEncryptedFrame) { |
| 200 uint64 network_iv; | 197 counter_block_str = GenerateCounterBlock(data + data_offset, kWebMIvSize); |
| 201 memcpy(&network_iv, data + data_offset, sizeof(network_iv)); | 198 data_offset += kWebMIvSize; |
| 202 const uint64 iv = base::NetToHost64(network_iv); | |
| 203 counter_block_str = | |
| 204 GenerateCounterBlock(reinterpret_cast<const uint8*>(&iv), sizeof(iv)); | |
| 205 data_offset += sizeof(iv); | |
| 206 } | 199 } |
| 207 | 200 |
| 208 encrypted_buffer->SetDecryptConfig( | 201 encrypted_buffer->SetDecryptConfig( |
| 209 scoped_ptr<DecryptConfig>(new DecryptConfig( | 202 scoped_ptr<DecryptConfig>(new DecryptConfig( |
| 210 std::string(reinterpret_cast<const char*>(key_id), key_id_size), | 203 std::string(reinterpret_cast<const char*>(key_id), key_id_size), |
| 211 counter_block_str, | 204 counter_block_str, |
| 212 data_offset, | 205 data_offset, |
| 213 std::vector<SubsampleEntry>()))); | 206 std::vector<SubsampleEntry>()))); |
| 214 return encrypted_buffer; | 207 return encrypted_buffer; |
| 215 } | 208 } |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( | 579 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( |
| 587 kSubsampleData, arraysize(kSubsampleData), | 580 kSubsampleData, arraysize(kSubsampleData), |
| 588 kSubsampleKeyId, arraysize(kSubsampleKeyId), | 581 kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 589 kSubsampleIv, arraysize(kSubsampleIv), | 582 kSubsampleIv, arraysize(kSubsampleIv), |
| 590 0, | 583 0, |
| 591 entries); | 584 entries); |
| 592 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail(encrypted_data)); | 585 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail(encrypted_data)); |
| 593 } | 586 } |
| 594 | 587 |
| 595 } // namespace media | 588 } // namespace media |
| OLD | NEW |