1ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian/*
2ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *
4ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  Use of this source code is governed by a BSD-style license
5ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  that can be found in the LICENSE file in the root of the source
6ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  tree. An additional intellectual property rights grant can be found
7ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  in the file PATENTS.  All contributing project authors may
8ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian *  be found in the AUTHORS file in the root of the source tree.
9ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian */
10ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
11ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include <cstdio>
12ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include <cstdlib>
13ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include <string>
14ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include <vector>
15ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include "third_party/googletest/src/include/gtest/gtest.h"
16ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include "test/codec_factory.h"
17ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include "test/ivf_video_source.h"
18ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
19ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramaniannamespace {
20ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian// In a real use the 'decrypt_state' parameter will be a pointer to a struct
21ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian// with whatever internal state the decryptor uses. For testing we'll just
22ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian// xor with a constant key, and decrypt_state will point to the start of
23ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian// the original buffer.
247bc9febe8749e98a3812a0dc4380ceae75c29450Johannconst uint8_t test_key[16] = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
257bc9febe8749e98a3812a0dc4380ceae75c29450Johann                               0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0 };
26ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
27ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanianvoid encrypt_buffer(const uint8_t *src, uint8_t *dst, size_t size,
28ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian                    ptrdiff_t offset) {
29ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  for (size_t i = 0; i < size; ++i) {
30ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    dst[i] = src[i] ^ test_key[(offset + i) & 15];
31ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  }
32ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian}
33ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
347bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid test_decrypt_cb(void *decrypt_state, const uint8_t *input, uint8_t *output,
357bc9febe8749e98a3812a0dc4380ceae75c29450Johann                     int count) {
36ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  encrypt_buffer(input, output, count,
37ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian                 input - reinterpret_cast<uint8_t *>(decrypt_state));
38ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian}
39ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
40ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian}  // namespace
41ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
42ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramaniannamespace libvpx_test {
43ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
44ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh VenkatasubramanianTEST(TestDecrypt, DecryptWorksVp9) {
45ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  libvpx_test::IVFVideoSource video("vp90-2-05-resize.ivf");
46ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  video.Init();
47ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
48da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian  vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
49ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  VP9Decoder decoder(dec_cfg, 0);
50ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
51ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  video.Begin();
52ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
53ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  // no decryption
54ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size());
55ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
56ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
57ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  // decrypt frame
58ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  video.Next();
59ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
60ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  std::vector<uint8_t> encrypted(video.frame_size());
61ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  encrypt_buffer(video.cxdata(), &encrypted[0], video.frame_size(), 0);
62ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  vpx_decrypt_init di = { test_decrypt_cb, &encrypted[0] };
63ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  decoder.Control(VPXD_SET_DECRYPTOR, &di);
64ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
65ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  res = decoder.DecodeFrame(&encrypted[0], encrypted.size());
66ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
67ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian}
68ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian
69ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian}  // namespace libvpx_test
70