| 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 | 7 |
| 7 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 9 #include "base/bind.h" |
| 9 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 10 #include "media/base/decoder_buffer.h" | 11 #include "media/base/decoder_buffer.h" |
| 11 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 12 #include "media/base/mock_filters.h" | 13 #include "media/base/mock_filters.h" |
| 13 #include "media/crypto/aes_decryptor.h" | 14 #include "media/crypto/aes_decryptor.h" |
| 14 #include "media/webm/webm_constants.h" | 15 #include "media/webm/webm_constants.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 // Frames 0 & 1 are encrypted with the same key. Frame 2 is encrypted with a | 43 // Frames 0 & 1 are encrypted with the same key. Frame 2 is encrypted with a |
| 43 // different key. | 44 // different key. |
| 44 const WebmEncryptedData kWebmEncryptedFrames[] = { | 45 const WebmEncryptedData kWebmEncryptedFrames[] = { |
| 45 { | 46 { |
| 46 // plaintext | 47 // plaintext |
| 47 "Original data.", 14, | 48 "Original data.", 14, |
| 48 // key_id | 49 // key_id |
| 49 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 50 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 50 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | 51 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 51 0x10, 0x11, 0x12, 0x13, | 52 0x10, 0x11, 0x12, 0x13 |
| 52 }, 20, | 53 }, 20, |
| 53 // key | 54 // key |
| 54 { 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, | 55 { 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, |
| 55 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, | 56 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23 |
| 56 }, 16, | 57 }, 16, |
| 57 // encrypted_data | 58 // encrypted_data |
| 58 { 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, | 59 { 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, |
| 59 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, | 60 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, |
| 60 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, | 61 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, |
| 61 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, | 62 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, |
| 62 0x64, 0xf7, | 63 0x64, 0xf7 |
| 63 }, 34, | 64 }, 34 |
| 64 }, | 65 }, |
| 65 { | 66 { |
| 66 // plaintext | 67 // plaintext |
| 67 "Changed Original data.", 22, | 68 "Changed Original data.", 22, |
| 68 // key_id | 69 // key_id |
| 69 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 70 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 70 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | 71 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 71 0x10, 0x11, 0x12, 0x13, | 72 0x10, 0x11, 0x12, 0x13 |
| 72 }, 20, | 73 }, 20, |
| 73 // key | 74 // key |
| 74 { 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, | 75 { 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, |
| 75 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, | 76 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23 |
| 76 }, 16, | 77 }, 16, |
| 77 // encrypted_data | 78 // encrypted_data |
| 78 { 0x43, 0xe4, 0x78, 0x7a, 0x43, 0xe1, 0x49, 0xbb, | 79 { 0x43, 0xe4, 0x78, 0x7a, 0x43, 0xe1, 0x49, 0xbb, |
| 79 0x44, 0x38, 0xdf, 0xfc, 0x00, 0x00, 0x00, 0x00, | 80 0x44, 0x38, 0xdf, 0xfc, 0x00, 0x00, 0x00, 0x00, |
| 80 0x00, 0x00, 0x00, 0x00, 0xec, 0x8e, 0x87, 0x21, | 81 0x00, 0x00, 0x00, 0x00, 0xec, 0x8e, 0x87, 0x21, |
| 81 0xd3, 0xb9, 0x1c, 0x61, 0xf6, 0x5a, 0x60, 0xaa, | 82 0xd3, 0xb9, 0x1c, 0x61, 0xf6, 0x5a, 0x60, 0xaa, |
| 82 0x07, 0x0e, 0x96, 0xd0, 0x54, 0x5d, 0x35, 0x9a, | 83 0x07, 0x0e, 0x96, 0xd0, 0x54, 0x5d, 0x35, 0x9a, |
| 83 0x4a, 0xd3, | 84 0x4a, 0xd3 |
| 84 }, 42, | 85 }, 42 |
| 85 }, | 86 }, |
| 86 { | 87 { |
| 87 // plaintext | 88 // plaintext |
| 88 "Original data.", 14, | 89 "Original data.", 14, |
| 89 // key_id | 90 // key_id |
| 90 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, | 91 { 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, |
| 91 0x2c, 0x2d, 0x2e, 0x2f, 0x30, | 92 0x2c, 0x2d, 0x2e, 0x2f, 0x30 |
| 92 }, 13, | 93 }, 13, |
| 93 // key | 94 // key |
| 94 { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, | 95 { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 95 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, | 96 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40 |
| 96 }, 16, | 97 }, 16, |
| 97 // encrypted_data | 98 // encrypted_data |
| 98 { 0xd9, 0x43, 0x30, 0xfd, 0x82, 0x77, 0x62, 0x04, | 99 { 0xd9, 0x43, 0x30, 0xfd, 0x82, 0x77, 0x62, 0x04, |
| 99 0x08, 0xc2, 0x48, 0x89, 0x00, 0x00, 0x00, 0x00, | 100 0x08, 0xc2, 0x48, 0x89, 0x00, 0x00, 0x00, 0x00, |
| 100 0x00, 0x00, 0x00, 0x01, 0x48, 0x5e, 0x4a, 0x41, | 101 0x00, 0x00, 0x00, 0x01, 0x48, 0x5e, 0x4a, 0x41, |
| 101 0x2a, 0x8b, 0xf4, 0xc6, 0x47, 0x54, 0x90, 0x34, | 102 0x2a, 0x8b, 0xf4, 0xc6, 0x47, 0x54, 0x90, 0x34, |
| 102 0xf4, 0x8b, | 103 0xf4, 0x8b |
| 103 }, 34, | 104 }, 34 |
| 104 }, | 105 } |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 static const uint8 kWebmWrongKey[] = { | 108 static const uint8 kWebmWrongKey[] = { |
| 108 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72, | 109 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72, |
| 109 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e | 110 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e |
| 110 }; | 111 }; |
| 111 static const uint8 kWebmWrongSizedKey[] = { 0x20, 0x20 }; | 112 static const uint8 kWebmWrongSizedKey[] = { 0x20, 0x20 }; |
| 112 | 113 |
| 113 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except | 114 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except |
| 114 // byte 0 is changed from 0xfb to 0xfc. Bytes 0-11 of WebM encrypted data | 115 // byte 0 is changed from 0xfb to 0xfc. Bytes 0-11 of WebM encrypted data |
| 115 // contains the HMAC. | 116 // contains the HMAC. |
| 116 static const unsigned char kWebmFrame0HmacDataChanged[] = { | 117 static const uint8 kWebmFrame0HmacDataChanged[] = { |
| 117 0xfc, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, | 118 0xfc, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, |
| 118 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, | 119 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, |
| 119 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, | 120 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, |
| 120 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, | 121 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, |
| 121 0x64, 0xf7 | 122 0x64, 0xf7 |
| 122 }; | 123 }; |
| 123 | 124 |
| 124 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except | 125 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except |
| 125 // byte 12 is changed from 0xff to 0x0f. Bytes 12-19 of WebM encrypted data | 126 // byte 12 is changed from 0xff to 0x0f. Bytes 12-19 of WebM encrypted data |
| 126 // contains the IV. | 127 // contains the IV. |
| 127 static const unsigned char kWebmFrame0IvDataChanged[] = { | 128 static const uint8 kWebmFrame0IvDataChanged[] = { |
| 128 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, | 129 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, |
| 129 0xcc, 0xf8, 0xda, 0xc0, 0x0f, 0xff, 0xff, 0xff, | 130 0xcc, 0xf8, 0xda, 0xc0, 0x0f, 0xff, 0xff, 0xff, |
| 130 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, | 131 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, |
| 131 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, | 132 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, |
| 132 0x64, 0xf7 | 133 0x64, 0xf7 |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except | 136 // This is the encrypted data from frame 0 of |kWebmEncryptedFrames| except |
| 136 // byte 33 is changed from 0xf7 to 0xf8. Bytes 20+ of WebM encrypted data | 137 // byte 33 is changed from 0xf7 to 0xf8. Bytes 20+ of WebM encrypted data |
| 137 // contains the encrypted frame. | 138 // contains the encrypted frame. |
| 138 static const unsigned char kWebmFrame0FrameDataChanged[] = { | 139 static const uint8 kWebmFrame0FrameDataChanged[] = { |
| 139 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, | 140 0xfb, 0xe7, 0x1d, 0xbb, 0x4c, 0x23, 0xce, 0xba, |
| 140 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, | 141 0xcc, 0xf8, 0xda, 0xc0, 0xff, 0xff, 0xff, 0xff, |
| 141 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, | 142 0xff, 0xff, 0xff, 0xff, 0x99, 0xaa, 0xff, 0xb7, |
| 142 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, | 143 0x74, 0x02, 0x4e, 0x1c, 0x75, 0x3d, 0xee, 0xcb, |
| 143 0x64, 0xf8 | 144 0x64, 0xf8 |
| 144 }; | 145 }; |
| 145 | 146 |
| 146 static const uint8 kSubsampleOriginalData[] = "Original subsample data."; | 147 static const uint8 kSubsampleOriginalData[] = "Original subsample data."; |
| 147 static const int kSubsampleOriginalDataSize = 24; | 148 static const int kSubsampleOriginalDataSize = 24; |
| 148 | 149 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 174 // Encrypted with kSubsampleKey and kSubsampleIv but without subsamples. | 175 // Encrypted with kSubsampleKey and kSubsampleIv but without subsamples. |
| 175 static const uint8 kNoSubsampleData[] = { | 176 static const uint8 kNoSubsampleData[] = { |
| 176 0x2f, 0x03, 0x09, 0xef, 0x71, 0xaf, 0x31, 0x16, | 177 0x2f, 0x03, 0x09, 0xef, 0x71, 0xaf, 0x31, 0x16, |
| 177 0xfa, 0x9d, 0x18, 0x43, 0x1e, 0x96, 0x71, 0xb5, | 178 0xfa, 0x9d, 0x18, 0x43, 0x1e, 0x96, 0x71, 0xb5, |
| 178 0xbf, 0xf5, 0x30, 0x53, 0x9a, 0x20, 0xdf, 0x95 | 179 0xbf, 0xf5, 0x30, 0x53, 0x9a, 0x20, 0xdf, 0x95 |
| 179 }; | 180 }; |
| 180 | 181 |
| 181 static const SubsampleEntry kSubsampleEntries[] = { | 182 static const SubsampleEntry kSubsampleEntries[] = { |
| 182 { 2, 7 }, | 183 { 2, 7 }, |
| 183 { 3, 11 }, | 184 { 3, 11 }, |
| 184 { 1, 0 }, | 185 { 1, 0 } |
| 185 }; | 186 }; |
| 186 | 187 |
| 188 // Returns a 16 byte CTR counter block. The CTR counter block format is a |
| 189 // CTR IV appended with a CTR block counter. |iv| is a CTR IV. |iv_size| is |
| 190 // the size of |iv| in bytes. |
| 191 static std::string GenerateCounterBlock(const uint8* iv, int iv_size) { |
| 192 const int kDecryptionKeySize = 16; |
| 193 CHECK_GT(iv_size, 0); |
| 194 CHECK_LE(iv_size, kDecryptionKeySize); |
| 195 char counter_block_data[kDecryptionKeySize]; |
| 196 |
| 197 // Set the IV. |
| 198 memcpy(counter_block_data, iv, iv_size); |
| 199 |
| 200 // Set block counter to all 0's. |
| 201 memset(counter_block_data + iv_size, 0, kDecryptionKeySize - iv_size); |
| 202 |
| 203 return std::string(counter_block_data, kDecryptionKeySize); |
| 204 } |
| 205 |
| 206 // Creates a WebM encrypted buffer that the demuxer would pass to the |
| 207 // decryptor. |data| is the payload of a WebM encrypted Block. |key_id| is |
| 208 // initialization data from the WebM file. Every encrypted Block has |
| 209 // an HMAC and IV prepended to an encrypted frame. Current encrypted WebM |
| 210 // request for comments specification is here |
| 211 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
| 212 static scoped_refptr<DecoderBuffer> CreateWebMEncryptedBuffer( |
| 213 const uint8* data, int data_size, |
| 214 const uint8* key_id, int key_id_size) { |
| 215 scoped_refptr<DecoderBuffer> encrypted_buffer = DecoderBuffer::CopyFrom( |
| 216 data + kWebMHmacSize, data_size - kWebMHmacSize); |
| 217 CHECK(encrypted_buffer); |
| 218 |
| 219 uint64 network_iv; |
| 220 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); |
| 221 const uint64 iv = base::NetToHost64(network_iv); |
| 222 std::string webm_iv = |
| 223 GenerateCounterBlock(reinterpret_cast<const uint8*>(&iv), sizeof(iv)); |
| 224 encrypted_buffer->SetDecryptConfig( |
| 225 scoped_ptr<DecryptConfig>(new DecryptConfig( |
| 226 std::string(reinterpret_cast<const char*>(key_id), key_id_size), |
| 227 webm_iv, |
| 228 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), |
| 229 sizeof(iv), |
| 230 std::vector<SubsampleEntry>()))); |
| 231 return encrypted_buffer; |
| 232 } |
| 233 |
| 234 static scoped_refptr<DecoderBuffer> CreateSubsampleEncryptedBuffer( |
| 235 const uint8* data, int data_size, |
| 236 const uint8* key_id, int key_id_size, |
| 237 const uint8* iv, int iv_size, |
| 238 int data_offset, |
| 239 const std::vector<SubsampleEntry>& subsample_entries) { |
| 240 scoped_refptr<DecoderBuffer> encrypted_buffer = |
| 241 DecoderBuffer::CopyFrom(data, data_size); |
| 242 CHECK(encrypted_buffer); |
| 243 encrypted_buffer->SetDecryptConfig( |
| 244 scoped_ptr<DecryptConfig>(new DecryptConfig( |
| 245 std::string(reinterpret_cast<const char*>(key_id), key_id_size), |
| 246 std::string(reinterpret_cast<const char*>(iv), iv_size), |
| 247 std::string(), |
| 248 data_offset, |
| 249 subsample_entries))); |
| 250 return encrypted_buffer; |
| 251 } |
| 252 |
| 187 class AesDecryptorTest : public testing::Test { | 253 class AesDecryptorTest : public testing::Test { |
| 188 public: | 254 public: |
| 189 AesDecryptorTest() | 255 AesDecryptorTest() |
| 190 : decryptor_(&client_), | 256 : decryptor_(&client_), |
| 191 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, | 257 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, |
| 192 base::Unretained(this))) { | 258 base::Unretained(this))), |
| 259 subsample_entries_(kSubsampleEntries, |
| 260 kSubsampleEntries + arraysize(kSubsampleEntries)) { |
| 193 } | 261 } |
| 194 | 262 |
| 195 protected: | 263 protected: |
| 196 // Returns a 16 byte CTR counter block. The CTR counter block format is a | |
| 197 // CTR IV appended with a CTR block counter. |iv| is a CTR IV. |iv_size| is | |
| 198 // the size of |iv| in bytes. | |
| 199 static std::string GenerateCounterBlock(const uint8* iv, int iv_size) { | |
| 200 const int kDecryptionKeySize = 16; | |
| 201 CHECK_GT(iv_size, 0); | |
| 202 CHECK_LE(iv_size, kDecryptionKeySize); | |
| 203 char counter_block_data[kDecryptionKeySize]; | |
| 204 | |
| 205 // Set the IV. | |
| 206 memcpy(counter_block_data, iv, iv_size); | |
| 207 | |
| 208 // Set block counter to all 0's. | |
| 209 memset(counter_block_data + iv_size, 0, kDecryptionKeySize - iv_size); | |
| 210 | |
| 211 return std::string(counter_block_data, kDecryptionKeySize); | |
| 212 } | |
| 213 | |
| 214 // Creates a WebM encrypted buffer that the demuxer would pass to the | |
| 215 // decryptor. |data| is the payload of a WebM encrypted Block. |key_id| is | |
| 216 // initialization data from the WebM file. Every encrypted Block has | |
| 217 // an HMAC and IV prepended to an encrypted frame. Current encrypted WebM | |
| 218 // request for comments specification is here | |
| 219 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
| 220 scoped_refptr<DecoderBuffer> CreateWebMEncryptedBuffer( | |
| 221 const uint8* data, int data_size, | |
| 222 const uint8* key_id, int key_id_size) { | |
| 223 scoped_refptr<DecoderBuffer> encrypted_buffer = DecoderBuffer::CopyFrom( | |
| 224 data + kWebMHmacSize, data_size - kWebMHmacSize); | |
| 225 CHECK(encrypted_buffer); | |
| 226 | |
| 227 uint64 network_iv; | |
| 228 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); | |
| 229 const uint64 iv = base::NetToHost64(network_iv); | |
| 230 std::string webm_iv = | |
| 231 GenerateCounterBlock(reinterpret_cast<const uint8*>(&iv), sizeof(iv)); | |
| 232 encrypted_buffer->SetDecryptConfig( | |
| 233 scoped_ptr<DecryptConfig>(new DecryptConfig( | |
| 234 std::string(reinterpret_cast<const char*>(key_id), key_id_size), | |
| 235 webm_iv, | |
| 236 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), | |
| 237 sizeof(iv), | |
| 238 std::vector<SubsampleEntry>()))); | |
| 239 return encrypted_buffer; | |
| 240 } | |
| 241 | |
| 242 scoped_refptr<DecoderBuffer> CreateSubsampleEncryptedBuffer( | |
| 243 const uint8* data, int data_size, | |
| 244 const uint8* key_id, int key_id_size, | |
| 245 const uint8* iv, int iv_size, | |
| 246 int data_offset, | |
| 247 const std::vector<SubsampleEntry>& subsample_entries) { | |
| 248 scoped_refptr<DecoderBuffer> encrypted_buffer = | |
| 249 DecoderBuffer::CopyFrom(data, data_size); | |
| 250 CHECK(encrypted_buffer); | |
| 251 encrypted_buffer->SetDecryptConfig( | |
| 252 scoped_ptr<DecryptConfig>(new DecryptConfig( | |
| 253 std::string(reinterpret_cast<const char*>(key_id), key_id_size), | |
| 254 std::string(reinterpret_cast<const char*>(iv), iv_size), | |
| 255 std::string(), | |
| 256 data_offset, | |
| 257 subsample_entries))); | |
| 258 return encrypted_buffer; | |
| 259 } | |
| 260 | |
| 261 void GenerateKeyRequest(const uint8* key_id, int key_id_size) { | 264 void GenerateKeyRequest(const uint8* key_id, int key_id_size) { |
| 262 EXPECT_CALL(client_, KeyMessageMock(kClearKeySystem, StrNe(std::string()), | 265 EXPECT_CALL(client_, KeyMessageMock(kClearKeySystem, StrNe(std::string()), |
| 263 NotNull(), Gt(0), "")) | 266 NotNull(), Gt(0), "")) |
| 264 .WillOnce(SaveArg<1>(&session_id_string_)); | 267 .WillOnce(SaveArg<1>(&session_id_string_)); |
| 265 decryptor_.GenerateKeyRequest(kClearKeySystem, key_id, key_id_size); | 268 decryptor_.GenerateKeyRequest(kClearKeySystem, key_id, key_id_size); |
| 266 } | 269 } |
| 267 | 270 |
| 268 void AddKeyAndExpectToSucceed(const uint8* key_id, int key_id_size, | 271 void AddKeyAndExpectToSucceed(const uint8* key_id, int key_id_size, |
| 269 const uint8* key, int key_size) { | 272 const uint8* key, int key_size) { |
| 270 EXPECT_CALL(client_, KeyAdded(kClearKeySystem, session_id_string_)); | 273 EXPECT_CALL(client_, KeyAdded(kClearKeySystem, session_id_string_)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 293 ASSERT_TRUE(decrypted); | 296 ASSERT_TRUE(decrypted); |
| 294 ASSERT_EQ(plain_text_size, decrypted->GetDataSize()); | 297 ASSERT_EQ(plain_text_size, decrypted->GetDataSize()); |
| 295 EXPECT_EQ(0, memcmp(plain_text, decrypted->GetData(), plain_text_size)); | 298 EXPECT_EQ(0, memcmp(plain_text, decrypted->GetData(), plain_text_size)); |
| 296 } | 299 } |
| 297 | 300 |
| 298 void DecryptAndExpectToFail(const scoped_refptr<DecoderBuffer>& encrypted) { | 301 void DecryptAndExpectToFail(const scoped_refptr<DecoderBuffer>& encrypted) { |
| 299 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kError, IsNull())); | 302 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kError, IsNull())); |
| 300 decryptor_.Decrypt(encrypted, decrypt_cb_); | 303 decryptor_.Decrypt(encrypted, decrypt_cb_); |
| 301 } | 304 } |
| 302 | 305 |
| 303 scoped_refptr<DecoderBuffer> encrypted_data_; | |
| 304 MockDecryptorClient client_; | 306 MockDecryptorClient client_; |
| 305 AesDecryptor decryptor_; | 307 AesDecryptor decryptor_; |
| 306 std::string session_id_string_; | 308 std::string session_id_string_; |
| 307 AesDecryptor::DecryptCB decrypt_cb_; | 309 AesDecryptor::DecryptCB decrypt_cb_; |
| 310 std::vector<SubsampleEntry> subsample_entries_; |
| 308 }; | 311 }; |
| 309 | 312 |
| 310 TEST_F(AesDecryptorTest, NormalWebMDecryption) { | 313 TEST_F(AesDecryptorTest, NormalWebMDecryption) { |
| 311 const WebmEncryptedData& frame = kWebmEncryptedFrames[0]; | 314 const WebmEncryptedData& frame = kWebmEncryptedFrames[0]; |
| 312 GenerateKeyRequest(frame.key_id, frame.key_id_size); | 315 GenerateKeyRequest(frame.key_id, frame.key_id_size); |
| 313 AddKeyAndExpectToSucceed(frame.key_id, frame.key_id_size, | 316 AddKeyAndExpectToSucceed(frame.key_id, frame.key_id_size, |
| 314 frame.key, frame.key_size); | 317 frame.key, frame.key_size); |
| 315 scoped_refptr<DecoderBuffer> encrypted_data = | 318 scoped_refptr<DecoderBuffer> encrypted_data = |
| 316 CreateWebMEncryptedBuffer(frame.encrypted_data, | 319 CreateWebMEncryptedBuffer(frame.encrypted_data, |
| 317 frame.encrypted_data_size, | 320 frame.encrypted_data_size, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 | 434 |
| 432 TEST_F(AesDecryptorTest, SubsampleDecryption) { | 435 TEST_F(AesDecryptorTest, SubsampleDecryption) { |
| 433 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); | 436 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); |
| 434 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), | 437 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 435 kSubsampleKey, arraysize(kSubsampleKey)); | 438 kSubsampleKey, arraysize(kSubsampleKey)); |
| 436 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( | 439 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( |
| 437 kSubsampleData, arraysize(kSubsampleData), | 440 kSubsampleData, arraysize(kSubsampleData), |
| 438 kSubsampleKeyId, arraysize(kSubsampleKeyId), | 441 kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 439 kSubsampleIv, arraysize(kSubsampleIv), | 442 kSubsampleIv, arraysize(kSubsampleIv), |
| 440 0, | 443 0, |
| 441 std::vector<SubsampleEntry>( | 444 subsample_entries_); |
| 442 kSubsampleEntries, | |
| 443 kSubsampleEntries + arraysize(kSubsampleEntries))); | |
| 444 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( | 445 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( |
| 445 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); | 446 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); |
| 446 } | 447 } |
| 447 | 448 |
| 448 // Ensures noninterference of data offset and subsample mechanisms. We never | 449 // Ensures noninterference of data offset and subsample mechanisms. We never |
| 449 // expect to encounter this in the wild, but since the DecryptConfig doesn't | 450 // expect to encounter this in the wild, but since the DecryptConfig doesn't |
| 450 // disallow such a configuration, it should be covered. | 451 // disallow such a configuration, it should be covered. |
| 451 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) { | 452 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) { |
| 452 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); | 453 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); |
| 453 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), | 454 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 454 kSubsampleKey, arraysize(kSubsampleKey)); | 455 kSubsampleKey, arraysize(kSubsampleKey)); |
| 455 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( | 456 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( |
| 456 kPaddedSubsampleData, arraysize(kPaddedSubsampleData), | 457 kPaddedSubsampleData, arraysize(kPaddedSubsampleData), |
| 457 kSubsampleKeyId, arraysize(kSubsampleKeyId), | 458 kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 458 kSubsampleIv, arraysize(kSubsampleIv), | 459 kSubsampleIv, arraysize(kSubsampleIv), |
| 459 arraysize(kPaddedSubsampleData) - arraysize(kSubsampleData), | 460 arraysize(kPaddedSubsampleData) - arraysize(kSubsampleData), |
| 460 std::vector<SubsampleEntry>( | 461 subsample_entries_); |
| 461 kSubsampleEntries, | |
| 462 kSubsampleEntries + arraysize(kSubsampleEntries))); | |
| 463 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( | 462 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( |
| 464 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); | 463 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); |
| 465 } | 464 } |
| 466 | 465 |
| 467 // No subsample or offset. | 466 // No subsample or offset. |
| 468 TEST_F(AesDecryptorTest, NormalDecryption) { | 467 TEST_F(AesDecryptorTest, NormalDecryption) { |
| 469 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); | 468 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); |
| 470 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), | 469 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 471 kSubsampleKey, arraysize(kSubsampleKey)); | 470 kSubsampleKey, arraysize(kSubsampleKey)); |
| 472 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( | 471 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( |
| 473 kNoSubsampleData, arraysize(kNoSubsampleData), | 472 kNoSubsampleData, arraysize(kNoSubsampleData), |
| 474 kSubsampleKeyId, arraysize(kSubsampleKeyId), | 473 kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 475 kSubsampleIv, arraysize(kSubsampleIv), | 474 kSubsampleIv, arraysize(kSubsampleIv), |
| 476 0, | 475 0, |
| 477 std::vector<SubsampleEntry>()); | 476 std::vector<SubsampleEntry>()); |
| 478 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( | 477 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed( |
| 479 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); | 478 encrypted_data, kSubsampleOriginalData, kSubsampleOriginalDataSize)); |
| 480 } | 479 } |
| 481 | 480 |
| 482 TEST_F(AesDecryptorTest, IncorrectSubsampleSize) { | 481 TEST_F(AesDecryptorTest, IncorrectSubsampleSize) { |
| 483 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); | 482 GenerateKeyRequest(kSubsampleKeyId, arraysize(kSubsampleKeyId)); |
| 484 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), | 483 AddKeyAndExpectToSucceed(kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 485 kSubsampleKey, arraysize(kSubsampleKey)); | 484 kSubsampleKey, arraysize(kSubsampleKey)); |
| 486 std::vector<SubsampleEntry> entries( | 485 std::vector<SubsampleEntry> entries = subsample_entries_; |
| 487 kSubsampleEntries, | |
| 488 kSubsampleEntries + arraysize(kSubsampleEntries)); | |
| 489 entries[2].cypher_bytes += 1; | 486 entries[2].cypher_bytes += 1; |
| 490 | 487 |
| 491 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( | 488 scoped_refptr<DecoderBuffer> encrypted_data = CreateSubsampleEncryptedBuffer( |
| 492 kSubsampleData, arraysize(kSubsampleData), | 489 kSubsampleData, arraysize(kSubsampleData), |
| 493 kSubsampleKeyId, arraysize(kSubsampleKeyId), | 490 kSubsampleKeyId, arraysize(kSubsampleKeyId), |
| 494 kSubsampleIv, arraysize(kSubsampleIv), | 491 kSubsampleIv, arraysize(kSubsampleIv), |
| 495 0, | 492 0, |
| 496 entries); | 493 entries); |
| 497 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail(encrypted_data)); | 494 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail(encrypted_data)); |
| 498 } | 495 } |
| 499 | 496 |
| 500 } // namespace media | 497 } // namespace media |
| OLD | NEW |