Home | History | Annotate | Download | only in webm
      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 "media/formats/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/formats/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 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;
     34   }
     35 
     36   uint8 signal_byte = data[0];
     37   int frame_offset = sizeof(signal_byte);
     38 
     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;
     43 
     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;
     48     }
     49     counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
     50     frame_offset += kWebMIvSize;
     51   }
     52 
     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;
     58 
     59   return true;
     60 }
     61 
     62 }  // namespace media
     63