OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 5 #ifndef MEDIA_CRYPTO_HMAC_AES_DECRYPTOR_H_ |
6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 6 #define MEDIA_CRYPTO_HMAC_AES_DECRYPTOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "media/base/decoder_buffer.h" |
14 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
15 | 16 |
16 namespace crypto { | 17 namespace crypto { |
17 class SymmetricKey; | 18 class SymmetricKey; |
18 } | 19 } |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 class DecoderBuffer; | 23 //class DecoderBuffer; |
23 | 24 |
24 // Decrypts AES encrypted buffer into unencrypted buffer. | 25 // Checks the integrity of the encrypted data and decrypts the AES encrypted |
25 class MEDIA_EXPORT AesDecryptor { | 26 // buffer into an unencrypted buffer. |
| 27 class MEDIA_EXPORT HmacAesDecryptor { |
26 public: | 28 public: |
27 AesDecryptor(); | 29 // The size is from the WebM encrypted specification. Current WebM |
28 ~AesDecryptor(); | 30 // encrypted request for comments specification is here |
| 31 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
| 32 static const int kSha1DigestSize = 20; |
| 33 static const int kKeySize = 16; |
| 34 static const char kHmacSeed[]; |
| 35 static const char kEncryptionSeed[]; |
29 | 36 |
| 37 HmacAesDecryptor(); |
| 38 ~HmacAesDecryptor(); |
| 39 |
| 40 // Decryptor implementation. |
30 // Add a |key_id| and |key| pair to the key system. The key is not limited to | 41 // Add a |key_id| and |key| pair to the key system. The key is not limited to |
31 // a decryption key. It can be any data that the key system accepts, such as | 42 // a decryption key. It can be any data that the key system accepts, such as |
32 // a license. If multiple calls of this function set different keys for the | 43 // a license. If multiple calls of this function set different keys for the |
33 // same |key_id|, the older key will be replaced by the newer key. | 44 // same |key_id|, the older key will be replaced by the newer key. |
34 void AddKey(const uint8* key_id, int key_id_size, | 45 void AddKey(const uint8* key_id, int key_id_size, |
35 const uint8* key, int key_size); | 46 const uint8* key, int key_size); |
36 | 47 |
37 // Decrypt |input| buffer. The |input| should not be NULL. | 48 // Check and Decrypt |input| buffer. The |input| should not be NULL. |
38 // Return a DecoderBuffer with the decrypted data if decryption succeeded. | 49 // Return a DecoderBuffer with the decrypted data if the check and |
39 // Return NULL if decryption failed. | 50 // decryption succeeded. Return NULL if check or decryption failed. |
| 51 // TODO(fgalligan): Do we need to differentiate between a check failure |
| 52 // and a decryption failure? |
40 scoped_refptr<DecoderBuffer> Decrypt( | 53 scoped_refptr<DecoderBuffer> Decrypt( |
41 const scoped_refptr<DecoderBuffer>& input); | 54 const scoped_refptr<DecoderBuffer>& input); |
42 | 55 |
43 private: | 56 private: |
44 // KeyMap owns the crypto::SymmetricKey* and must delete them when they are | 57 // Helper class that manages the HMAC and encryption keys. |
| 58 class HmacEncryptionKeys { |
| 59 public: |
| 60 explicit HmacEncryptionKeys(const std::string& secret); |
| 61 ~HmacEncryptionKeys(); |
| 62 |
| 63 // Creates the HMAC and encryption key. |
| 64 bool Init(); |
| 65 |
| 66 std::string hmac_key() { return hmac_key_; } |
| 67 crypto::SymmetricKey* encryption_key() { return encryption_key_.get(); } |
| 68 |
| 69 private: |
| 70 // The base secret that is used to derive the HMAC and encryption keys. |
| 71 const std::string secret_; |
| 72 |
| 73 // The key used to perform the intergrity check. |
| 74 std::string hmac_key_; |
| 75 |
| 76 // The key used to decrypt the data. |
| 77 scoped_ptr<crypto::SymmetricKey> encryption_key_; |
| 78 }; |
| 79 |
| 80 // KeysMap owns the HmacEncryptionKeys* and must delete them when they are |
45 // not needed any more. | 81 // not needed any more. |
46 typedef base::hash_map<std::string, crypto::SymmetricKey*> KeyMap; | 82 typedef base::hash_map<std::string, HmacEncryptionKeys*> KeysMap; |
47 KeyMap key_map_; | 83 KeysMap keys_map_; |
48 base::Lock lock_; | 84 base::Lock lock_; |
49 | 85 |
50 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); | 86 DISALLOW_COPY_AND_ASSIGN(HmacAesDecryptor); |
51 }; | 87 }; |
52 | 88 |
53 } // namespace media | 89 } // namespace media |
54 | 90 |
55 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 91 #endif // MEDIA_CRYPTO_HMAC_AES_DECRYPTOR_H_ |
OLD | NEW |