DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
webm_crypto_helpers.cc
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.
4 
5 #include "packager/media/formats/webm/webm_crypto_helpers.h"
6 
7 #include "packager/base/logging.h"
8 #include "packager/base/sys_byteorder.h"
9 #include "packager/media/base/decrypt_config.h"
10 #include "packager/media/formats/webm/webm_constants.h"
11 
12 namespace edash_packager {
13 namespace media {
14 namespace {
15 
16 // Generates a 16 byte CTR counter block. The CTR counter block format is a
17 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV.
18 // |iv_size| is the size of |iv| in btyes. Returns a string of
19 // kDecryptionKeySize bytes.
20 std::string GenerateWebMCounterBlock(const uint8_t* iv, int iv_size) {
21  std::string counter_block(reinterpret_cast<const char*>(iv), iv_size);
22  counter_block.append(DecryptConfig::kDecryptionKeySize - iv_size, 0);
23  return counter_block;
24 }
25 
26 } // namespace anonymous
27 
28 bool WebMCreateDecryptConfig(const uint8_t* data,
29  int data_size,
30  const uint8_t* key_id,
31  int key_id_size,
32  scoped_ptr<DecryptConfig>* decrypt_config,
33  int* data_offset) {
34  if (data_size < kWebMSignalByteSize) {
35  DVLOG(1) << "Got a block from an encrypted stream with no data.";
36  return false;
37  }
38 
39  uint8_t signal_byte = data[0];
40  int frame_offset = sizeof(signal_byte);
41 
42  // Setting the DecryptConfig object of the buffer while leaving the
43  // initialization vector empty will tell the decryptor that the frame is
44  // unencrypted.
45  std::string counter_block;
46 
47  if (signal_byte & kWebMFlagEncryptedFrame) {
48  if (data_size < kWebMSignalByteSize + kWebMIvSize) {
49  DVLOG(1) << "Got an encrypted block with not enough data " << data_size;
50  return false;
51  }
52  counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
53  frame_offset += kWebMIvSize;
54  }
55 
56  decrypt_config->reset(new DecryptConfig(
57  std::vector<uint8_t>(key_id, key_id + key_id_size),
58  std::vector<uint8_t>(counter_block.begin(), counter_block.end()),
59  frame_offset, std::vector<SubsampleEntry>()));
60  *data_offset = frame_offset;
61 
62  return true;
63 }
64 
65 } // namespace media
66 } // namespace edash_packager
static const size_t kDecryptionKeySize
Keys are always 128 bits.