DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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/formats/webm/webm_constants.h"
10 
11 namespace shaka {
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_t* 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 bool WebMCreateDecryptConfig(const uint8_t* data,
28  int data_size,
29  const uint8_t* key_id,
30  int key_id_size,
31  scoped_ptr<DecryptConfig>* decrypt_config,
32  int* data_offset) {
33  if (data_size < kWebMSignalByteSize) {
34  DVLOG(1) << "Got a block from an encrypted stream with no data.";
35  return false;
36  }
37 
38  uint8_t signal_byte = data[0];
39  int frame_offset = sizeof(signal_byte);
40 
41  // Setting the DecryptConfig object of the buffer while leaving the
42  // initialization vector empty will tell the decryptor that the frame is
43  // unencrypted.
44  std::string counter_block;
45 
46  if (signal_byte & kWebMFlagEncryptedFrame) {
47  if (data_size < kWebMSignalByteSize + kWebMIvSize) {
48  DVLOG(1) << "Got an encrypted block with not enough data " << data_size;
49  return false;
50  }
51  counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
52  frame_offset += kWebMIvSize;
53  }
54 
55  decrypt_config->reset(new DecryptConfig(
56  std::vector<uint8_t>(key_id, key_id + key_id_size),
57  std::vector<uint8_t>(counter_block.begin(), counter_block.end()),
58  std::vector<SubsampleEntry>()));
59  *data_offset = frame_offset;
60 
61  return true;
62 }
63 
64 } // namespace media
65 } // namespace shaka
static const size_t kDecryptionKeySize
Keys are always 128 bits.