OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "media/cast/transport/utility/transport_encryption_handler.h" | 5 #include "media/cast/common/transport_encryption_handler.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "crypto/encryptor.h" | 8 #include "crypto/encryptor.h" |
9 #include "crypto/symmetric_key.h" | 9 #include "crypto/symmetric_key.h" |
10 #include "media/cast/transport/cast_transport_defines.h" | 10 #include "media/cast/net/cast_transport_defines.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Crypto. |
| 15 const size_t kAesBlockSize = 16; |
| 16 const size_t kAesKeySize = 16; |
| 17 |
| 18 std::string GetAesNonce(uint32 frame_id, const std::string& iv_mask) { |
| 19 std::string aes_nonce(kAesBlockSize, 0); |
| 20 |
| 21 // Serializing frame_id in big-endian order (aes_nonce[8] is the most |
| 22 // significant byte of frame_id). |
| 23 aes_nonce[11] = frame_id & 0xff; |
| 24 aes_nonce[10] = (frame_id >> 8) & 0xff; |
| 25 aes_nonce[9] = (frame_id >> 16) & 0xff; |
| 26 aes_nonce[8] = (frame_id >> 24) & 0xff; |
| 27 |
| 28 for (size_t i = 0; i < kAesBlockSize; ++i) { |
| 29 aes_nonce[i] ^= iv_mask[i]; |
| 30 } |
| 31 return aes_nonce; |
| 32 } |
| 33 |
| 34 } // namespace |
11 | 35 |
12 namespace media { | 36 namespace media { |
13 namespace cast { | 37 namespace cast { |
14 namespace transport { | |
15 | 38 |
16 TransportEncryptionHandler::TransportEncryptionHandler() | 39 TransportEncryptionHandler::TransportEncryptionHandler() |
17 : key_(), encryptor_(), iv_mask_(), is_activated_(false) {} | 40 : key_(), encryptor_(), iv_mask_(), is_activated_(false) {} |
18 | 41 |
19 TransportEncryptionHandler::~TransportEncryptionHandler() {} | 42 TransportEncryptionHandler::~TransportEncryptionHandler() {} |
20 | 43 |
21 bool TransportEncryptionHandler::Initialize(std::string aes_key, | 44 bool TransportEncryptionHandler::Initialize(std::string aes_key, |
22 std::string aes_iv_mask) { | 45 std::string aes_iv_mask) { |
23 is_activated_ = false; | 46 is_activated_ = false; |
24 if (aes_iv_mask.size() == kAesKeySize && aes_key.size() == kAesKeySize) { | 47 if (aes_iv_mask.size() == kAesKeySize && aes_key.size() == kAesKeySize) { |
(...skipping 28 matching lines...) Expand all Loading... |
53 } | 76 } |
54 return true; | 77 return true; |
55 } | 78 } |
56 | 79 |
57 bool TransportEncryptionHandler::Decrypt(uint32 frame_id, | 80 bool TransportEncryptionHandler::Decrypt(uint32 frame_id, |
58 const base::StringPiece& ciphertext, | 81 const base::StringPiece& ciphertext, |
59 std::string* plaintext) { | 82 std::string* plaintext) { |
60 if (!is_activated_) { | 83 if (!is_activated_) { |
61 return false; | 84 return false; |
62 } | 85 } |
63 if (!encryptor_->SetCounter(transport::GetAesNonce(frame_id, iv_mask_))) { | 86 if (!encryptor_->SetCounter(GetAesNonce(frame_id, iv_mask_))) { |
64 NOTREACHED() << "Failed to set counter"; | 87 NOTREACHED() << "Failed to set counter"; |
65 return false; | 88 return false; |
66 } | 89 } |
67 if (!encryptor_->Decrypt(ciphertext, plaintext)) { | 90 if (!encryptor_->Decrypt(ciphertext, plaintext)) { |
68 VLOG(1) << "Decryption error"; | 91 VLOG(1) << "Decryption error"; |
69 return false; | 92 return false; |
70 } | 93 } |
71 return true; | 94 return true; |
72 } | 95 } |
73 | 96 |
74 } // namespace transport | |
75 } // namespace cast | 97 } // namespace cast |
76 } // namespace media | 98 } // namespace media |
OLD | NEW |