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.
5 #include "media/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/webm/webm_constants.h"
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);
25 } // namespace anonymous
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
>();
35 uint8 signal_byte
= data
[0];
36 int frame_offset
= sizeof(signal_byte
);
38 // Setting the DecryptConfig object of the buffer while leaving the
39 // initialization vector empty will tell the decryptor that the frame is
41 std::string counter_block
;
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
>();
48 counter_block
= GenerateWebMCounterBlock(data
+ frame_offset
, kWebMIvSize
);
49 frame_offset
+= kWebMIvSize
;
52 scoped_ptr
<DecryptConfig
> config(new DecryptConfig(
53 std::string(reinterpret_cast<const char*>(key_id
), key_id_size
),
56 std::vector
<SubsampleEntry
>()));