Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef API_TEST_FAKE_FRAME_DECRYPTOR_H_ |
| 12 | #define API_TEST_FAKE_FRAME_DECRYPTOR_H_ |
| 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "api/crypto/frame_decryptor_interface.h" |
| 20 | #include "api/media_types.h" |
| 21 | #include "rtc_base/ref_counted_object.h" |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // The FakeFrameDecryptor is a TEST ONLY fake implementation of the |
| 26 | // FrameDecryptorInterface. It is constructed with a simple single digit key and |
| 27 | // a fixed postfix byte. This is just to validate that the core code works |
| 28 | // as expected. |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 29 | class FakeFrameDecryptor final |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 30 | : public rtc::RefCountedObject<FrameDecryptorInterface> { |
| 31 | public: |
| 32 | // Provide a key (0,255) and some postfix byte (0,255) this should match the |
| 33 | // byte you expect from the FakeFrameEncryptor. |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 34 | explicit FakeFrameDecryptor(uint8_t fake_key = 0xAA, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 35 | uint8_t expected_postfix_byte = 255); |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 36 | // Fake decryption that just xors the payload with the 1 byte key and checks |
| 37 | // the postfix byte. This will always fail if fail_decryption_ is set to true. |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame^] | 38 | Result Decrypt(cricket::MediaType media_type, |
| 39 | const std::vector<uint32_t>& csrcs, |
| 40 | rtc::ArrayView<const uint8_t> additional_data, |
| 41 | rtc::ArrayView<const uint8_t> encrypted_frame, |
| 42 | rtc::ArrayView<uint8_t> frame) override; |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 43 | // Always returns 1 less than the size of the encrypted frame. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 44 | size_t GetMaxPlaintextByteSize(cricket::MediaType media_type, |
| 45 | size_t encrypted_frame_size) override; |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 46 | // Sets the fake key to use for encryption. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 47 | void SetFakeKey(uint8_t fake_key); |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 48 | // Returns the fake key used for encryption. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 49 | uint8_t GetFakeKey() const; |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 50 | // Set the Postfix byte that is expected in the encrypted payload. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 51 | void SetExpectedPostfixByte(uint8_t expected_postfix_byte); |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 52 | // Returns the postfix byte that will be checked for in the encrypted payload. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 53 | uint8_t GetExpectedPostfixByte() const; |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 54 | // If set to true will force all encryption to fail. |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 55 | void SetFailDecryption(bool fail_decryption); |
Benjamin Wright | 150a907 | 2018-10-26 15:43:06 -0700 | [diff] [blame] | 56 | // Simple error codes for tests to validate against. |
| 57 | enum class FakeDecryptStatus : int { |
| 58 | OK = 0, |
| 59 | FORCED_FAILURE = 1, |
| 60 | INVALID_POSTFIX = 2 |
| 61 | }; |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | uint8_t fake_key_ = 0; |
| 65 | uint8_t expected_postfix_byte_ = 0; |
| 66 | bool fail_decryption_ = false; |
| 67 | }; |
| 68 | |
| 69 | } // namespace webrtc |
| 70 | |
| 71 | #endif // API_TEST_FAKE_FRAME_DECRYPTOR_H_ |