1 // Copyright (c) 2012 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 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_
6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_
10 #include "base/basictypes.h"
11 #include "base/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/string_piece.h"
15 #include "base/synchronization/lock.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/media_export.h"
25 class DecryptorClient
;
27 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
28 // encryption must be CTR with a key size of 128bits.
29 class MEDIA_EXPORT AesDecryptor
: public Decryptor
{
31 // The AesDecryptor does not take ownership of the |client|. The |client|
32 // must be valid throughout the lifetime of the AesDecryptor.
33 explicit AesDecryptor(DecryptorClient
* client
);
34 virtual ~AesDecryptor();
36 // Decryptor implementation.
37 virtual bool GenerateKeyRequest(const std::string
& key_system
,
38 const std::string
& type
,
39 const uint8
* init_data
,
40 int init_data_length
) OVERRIDE
;
41 virtual void AddKey(const std::string
& key_system
,
44 const uint8
* init_data
,
46 const std::string
& session_id
) OVERRIDE
;
47 virtual void CancelKeyRequest(const std::string
& key_system
,
48 const std::string
& session_id
) OVERRIDE
;
49 virtual void Decrypt(StreamType stream_type
,
50 const scoped_refptr
<DecoderBuffer
>& encrypted
,
51 const DecryptCB
& decrypt_cb
) OVERRIDE
;
52 virtual void CancelDecrypt(StreamType stream_type
) OVERRIDE
;
53 virtual void InitializeAudioDecoder(scoped_ptr
<AudioDecoderConfig
> config
,
54 const DecoderInitCB
& init_cb
,
55 const KeyAddedCB
& key_added_cb
) OVERRIDE
;
56 virtual void InitializeVideoDecoder(scoped_ptr
<VideoDecoderConfig
> config
,
57 const DecoderInitCB
& init_cb
,
58 const KeyAddedCB
& key_added_cb
) OVERRIDE
;
59 virtual void DecryptAndDecodeAudio(
60 const scoped_refptr
<DecoderBuffer
>& encrypted
,
61 const AudioDecodeCB
& audio_decode_cb
) OVERRIDE
;
62 virtual void DecryptAndDecodeVideo(
63 const scoped_refptr
<DecoderBuffer
>& encrypted
,
64 const VideoDecodeCB
& video_decode_cb
) OVERRIDE
;
65 virtual void ResetDecoder(StreamType stream_type
) OVERRIDE
;
66 virtual void DeinitializeDecoder(StreamType stream_type
) OVERRIDE
;
69 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
70 // as there are no decryptors that are performing an integrity check.
71 // Helper class that manages the decryption key.
74 explicit DecryptionKey(const std::string
& secret
);
77 // Creates the encryption key.
80 crypto::SymmetricKey
* decryption_key() { return decryption_key_
.get(); }
83 // The base secret that is used to create the decryption key.
84 const std::string secret_
;
86 // The key used to decrypt the data.
87 scoped_ptr
<crypto::SymmetricKey
> decryption_key_
;
89 DISALLOW_COPY_AND_ASSIGN(DecryptionKey
);
92 // Sets |key| for |key_id|. The AesDecryptor takes the ownership of the |key|.
93 void SetKey(const std::string
& key_id
, scoped_ptr
<DecryptionKey
> key
);
95 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
96 // the key. Returns NULL if no key is associated with |key_id|.
97 DecryptionKey
* GetKey(const std::string
& key_id
) const;
99 // KeyMap owns the DecryptionKey* and must delete them when they are
100 // not needed any more.
101 typedef base::hash_map
<std::string
, DecryptionKey
*> KeyMap
;
103 // Since only Decrypt() is called off the renderer thread, we only need to
104 // protect |key_map_|, the only member variable that is shared between
105 // Decrypt() and other methods.
106 KeyMap key_map_
; // Protected by the |key_map_lock_|.
107 mutable base::Lock key_map_lock_
; // Protects the |key_map_|.
109 // Make session ID unique per renderer by making it static.
110 // TODO(xhwang): Make session ID more strictly defined if needed:
111 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0
112 static uint32 next_session_id_
;
114 DecryptorClient
* const client_
;
116 DISALLOW_COPY_AND_ASSIGN(AesDecryptor
);
121 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_