1 // Copyright (c) 2013 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 5 #include "media/webm/webm_crypto_helpers.h" 6 7 #include "base/logging.h" 8 #include "base/sys_byteorder.h" 9 #include "media/base/decrypt_config.h" 10 #include "media/webm/webm_constants.h" 11 12 namespace media { 13 namespace { 14 15 // Generates a 16 byte CTR counter block. The CTR counter block format is a 16 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. 17 // |iv_size| is the size of |iv| in btyes. Returns a string of 18 // kDecryptionKeySize bytes. 19 std::string GenerateWebMCounterBlock(const uint8* iv, int iv_size) { 20 std::string counter_block(reinterpret_cast<const char*>(iv), iv_size); 21 counter_block.append(DecryptConfig::kDecryptionKeySize - iv_size, 0); 22 return counter_block; 23 } 24 25 } // namespace anonymous 26 27 scoped_ptr<DecryptConfig> WebMCreateDecryptConfig( 28 const uint8* data, int data_size, 29 const uint8* key_id, int key_id_size) { 30 if (data_size < kWebMSignalByteSize) { 31 DVLOG(1) << "Got a block from an encrypted stream with no data."; 32 return scoped_ptr<DecryptConfig>(); 33 } 34 35 uint8 signal_byte = data[0]; 36 int frame_offset = sizeof(signal_byte); 37 38 // Setting the DecryptConfig object of the buffer while leaving the 39 // initialization vector empty will tell the decryptor that the frame is 40 // unencrypted. 41 std::string counter_block; 42 43 if (signal_byte & kWebMFlagEncryptedFrame) { 44 if (data_size < kWebMSignalByteSize + kWebMIvSize) { 45 DVLOG(1) << "Got an encrypted block with not enough data " << data_size; 46 return scoped_ptr<DecryptConfig>(); 47 } 48 counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize); 49 frame_offset += kWebMIvSize; 50 } 51 52 scoped_ptr<DecryptConfig> config(new DecryptConfig( 53 std::string(reinterpret_cast<const char*>(key_id), key_id_size), 54 counter_block, 55 frame_offset, 56 std::vector<SubsampleEntry>())); 57 return config.Pass(); 58 } 59 60 } // namespace media 61