Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / webm / webm_crypto_helpers.cc
blobbd473bc23edbc4f9c8f3da99340524fc6963f539
1 // Copyright 2014 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.
5 #include "media/formats/webm/webm_crypto_helpers.h"
7 #include "base/logging.h"
8 #include "base/sys_byteorder.h"
9 #include "media/base/decrypt_config.h"
10 #include "media/formats/webm/webm_constants.h"
12 namespace media {
13 namespace {
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;
25 } // namespace anonymous
27 bool WebMCreateDecryptConfig(const uint8* data, int data_size,
28 const uint8* key_id, int key_id_size,
29 scoped_ptr<DecryptConfig>* decrypt_config,
30 int* data_offset) {
31 if (data_size < kWebMSignalByteSize) {
32 DVLOG(1) << "Got a block from an encrypted stream with no data.";
33 return false;
36 uint8 signal_byte = data[0];
37 int frame_offset = sizeof(signal_byte);
39 // Setting the DecryptConfig object of the buffer while leaving the
40 // initialization vector empty will tell the decryptor that the frame is
41 // unencrypted.
42 std::string counter_block;
44 if (signal_byte & kWebMFlagEncryptedFrame) {
45 if (data_size < kWebMSignalByteSize + kWebMIvSize) {
46 DVLOG(1) << "Got an encrypted block with not enough data " << data_size;
47 return false;
49 counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
50 frame_offset += kWebMIvSize;
53 decrypt_config->reset(new DecryptConfig(
54 std::string(reinterpret_cast<const char*>(key_id), key_id_size),
55 counter_block,
56 std::vector<SubsampleEntry>()));
57 *data_offset = frame_offset;
59 return true;
62 } // namespace media