ref: 26cec5c13f1d9f2911e903ee5089a052adea86f0
parent: 7d855a9f13c0b303ec07b2025a35925b7a84dde9
author: Dmitry Kovalev <dkovalev@google.com>
date: Fri Mar 15 14:21:55 EDT 2013
Basic encryption feature for libvpx. New decoder control paramter VP8_SET_DECRYPT_KEY to set the decryption key. Change-Id: I6fc1f44d41f74f3b3f702778af1a6f8f5cc9439f
--- a/configure
+++ b/configure
@@ -298,6 +298,7 @@
multi_res_encoding
temporal_denoising
experimental
+ decrypt
${EXPERIMENT_LIST}
"
CMDLINE_SELECT="
@@ -347,6 +348,7 @@
multi_res_encoding
temporal_denoising
experimental
+ decrypt
"
process_cmdline() {
--- a/test/decode_test_driver.cc
+++ b/test/decode_test_driver.cc
@@ -14,18 +14,12 @@
namespace libvpx_test {
#if CONFIG_VP8_DECODER
-void Decoder::DecodeFrame(const uint8_t *cxdata, int size) {
- if (!decoder_.priv) {
- const vpx_codec_err_t res_init = vpx_codec_dec_init(&decoder_,
- &vpx_codec_vp8_dx_algo,
- &cfg_, 0);
- ASSERT_EQ(VPX_CODEC_OK, res_init) << DecodeError();
- }
+vpx_codec_err_t Decoder::DecodeFrame(const uint8_t *cxdata, int size) {
vpx_codec_err_t res_dec;
REGISTER_STATE_CHECK(res_dec = vpx_codec_decode(&decoder_,
cxdata, size, NULL, 0));
- ASSERT_EQ(VPX_CODEC_OK, res_dec) << DecodeError();
+ return res_dec;
}
void DecoderTest::RunLoop(CompressedVideoSource *video) {
@@ -34,7 +28,9 @@
// Decode frames.
for (video->Begin(); video->cxdata(); video->Next()) {
- decoder.DecodeFrame(video->cxdata(), video->frame_size());
+ vpx_codec_err_t res_dec = decoder.DecodeFrame(video->cxdata(),
+ video->frame_size());
+ ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder.DecodeError();
DxDataIterator dec_iter = decoder.GetDxData();
const vpx_image_t *img = NULL;
--- a/test/decode_test_driver.h
+++ b/test/decode_test_driver.h
@@ -44,6 +44,7 @@
Decoder(vpx_codec_dec_cfg_t cfg, unsigned long deadline)
: cfg_(cfg), deadline_(deadline) {
memset(&decoder_, 0, sizeof(decoder_));
+ Init();
}
~Decoder() {
@@ -50,7 +51,7 @@
vpx_codec_destroy(&decoder_);
}
- void DecodeFrame(const uint8_t *cxdata, int size);
+ vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, int size);
DxDataIterator GetDxData() {
return DxDataIterator(&decoder_);
@@ -65,10 +66,22 @@
ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
}
- protected:
+ void Control(int ctrl_id, const void *arg) {
+ const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
+ ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
+ }
+
const char *DecodeError() {
const char *detail = vpx_codec_error_detail(&decoder_);
return detail ? detail : vpx_codec_error(&decoder_);
+ }
+
+ protected:
+ void Init() {
+ const vpx_codec_err_t res = vpx_codec_dec_init(&decoder_,
+ &vpx_codec_vp8_dx_algo,
+ &cfg_, 0);
+ ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
}
vpx_codec_ctx_t decoder_;
--- a/test/encode_test_driver.cc
+++ b/test/encode_test_driver.cc
@@ -160,13 +160,16 @@
while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
again = true;
-
+#if CONFIG_VP8_DECODER
+ vpx_codec_err_t res_dec;
+#endif
switch (pkt->kind) {
case VPX_CODEC_CX_FRAME_PKT:
#if CONFIG_VP8_DECODER
has_cxdata = true;
- decoder.DecodeFrame((const uint8_t*)pkt->data.frame.buf,
- pkt->data.frame.sz);
+ res_dec = decoder.DecodeFrame((const uint8_t*)pkt->data.frame.buf,
+ pkt->data.frame.sz);
+ ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder.DecodeError();
#endif
ASSERT_GE(pkt->data.frame.pts, last_pts_);
last_pts_ = pkt->data.frame.pts;
--- a/test/test.mk
+++ b/test/test.mk
@@ -27,6 +27,7 @@
LIBVPX_TEST_SRCS-$(CONFIG_VP8_DECODER) += decode_test_driver.h
LIBVPX_TEST_SRCS-$(CONFIG_VP8_DECODER) += ivf_video_source.h
LIBVPX_TEST_SRCS-$(CONFIG_VP8_DECODER) += test_vector_test.cc
+
##
## WHITE BOX TESTS
##
@@ -51,6 +52,7 @@
LIBVPX_TEST_SRCS-yes += sixtap_predict_test.cc
LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += subtract_test.cc
LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += variance_test.cc
+LIBVPX_TEST_SRCS-$(CONFIG_VP8_DECODER) += vp8_decrypt_test.cc
LIBVPX_TEST_SRCS-$(CONFIG_VP8_ENCODER) += vp8_fdct4x4_test.cc
endif # VP8
--- a/test/vp8_boolcoder_test.cc
+++ b/test/vp8_boolcoder_test.cc
@@ -26,6 +26,20 @@
namespace {
const int num_tests = 10;
+
+void encrypt_buffer(uint8_t *buffer, int size, const uint8_t *key) {
+ for (int i = 0; i < size; ++i) {
+ buffer[i] ^= key[i % 32];
+ }
+}
+
+const uint8_t secret_key[32] = {
+ 234, 32, 2, 3, 4, 230, 6, 11,
+ 0, 132, 22, 23, 45, 21, 124, 255,
+ 0, 43, 52, 3, 23, 63, 99, 7,
+ 120, 8, 252, 84, 4, 83, 6, 13
+};
+
} // namespace
using libvpx_test::ACMRandom;
@@ -71,7 +85,12 @@
vp8_stop_encode(&bw);
BOOL_DECODER br;
- vp8dx_start_decode(&br, bw_buffer, buffer_size);
+
+#if CONFIG_DECRYPT
+ encrypt_buffer(bw_buffer, buffer_size, secret_key);
+#endif
+
+ vp8dx_start_decode(&br, bw_buffer, buffer_size, bw_buffer, secret_key);
bit_rnd.Reset(random_seed);
for (int i = 0; i < bits_to_test; ++i) {
if (bit_method == 2) {
--- /dev/null
+++ b/test/vp8_decrypt_test.cc
@@ -1,0 +1,65 @@
+/*
+ * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+#include "third_party/googletest/src/include/gtest/gtest.h"
+#include "test/decode_test_driver.h"
+#include "test/ivf_video_source.h"
+
+#if CONFIG_DECRYPT
+
+namespace {
+
+const uint8_t decrypt_key[32] = {
+ 255, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+} // namespace
+
+namespace libvpx_test {
+
+TEST(TestDecrypt, NullKey) {
+ vpx_codec_dec_cfg_t cfg = {0};
+ vpx_codec_ctx_t decoder = {0};
+ vpx_codec_err_t res = vpx_codec_dec_init(&decoder, &vpx_codec_vp8_dx_algo,
+ &cfg, 0);
+ ASSERT_EQ(VPX_CODEC_OK, res);
+
+ res = vpx_codec_control(&decoder, VP8_SET_DECRYPT_KEY, NULL);
+ ASSERT_EQ(VPX_CODEC_INVALID_PARAM, res);
+}
+
+TEST(TestDecrypt, DecryptWorks) {
+ libvpx_test::IVFVideoSource video("vp80-00-comprehensive-001.ivf");
+ video.Init();
+
+ vpx_codec_dec_cfg_t dec_cfg = {0};
+ Decoder decoder(dec_cfg, 0);
+
+ // Zero decrypt key (by default)
+ video.Begin();
+ vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size());
+ ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
+
+ // Non-zero decrypt key
+ video.Next();
+ decoder.Control(VP8_SET_DECRYPT_KEY, decrypt_key);
+ res = decoder.DecodeFrame(video.cxdata(), video.frame_size());
+ ASSERT_NE(VPX_CODEC_OK, res) << decoder.DecodeError();
+}
+
+} // namespace libvpx_test
+
+#endif // CONFIG_DECRYPT
--- a/vp8/decoder/dboolhuff.c
+++ b/vp8/decoder/dboolhuff.c
@@ -13,7 +13,9 @@
int vp8dx_start_decode(BOOL_DECODER *br,
const unsigned char *source,
- unsigned int source_sz)
+ unsigned int source_sz,
+ const unsigned char *origin,
+ const unsigned char *key)
{
br->user_buffer_end = source+source_sz;
br->user_buffer = source;
@@ -20,6 +22,8 @@
br->value = 0;
br->count = -8;
br->range = 255;
+ br->origin = origin;
+ br->key = key;
if (source_sz && !source)
return 1;
@@ -52,7 +56,9 @@
while(shift >= loop_end)
{
count += CHAR_BIT;
- value |= (VP8_BD_VALUE)*bufptr++ << shift;
+ value |= ((VP8_BD_VALUE)decrypt_byte(bufptr, br->origin,
+ br->key)) << shift;
+ ++bufptr;
shift -= CHAR_BIT;
}
}
--- a/vp8/decoder/dboolhuff.h
+++ b/vp8/decoder/dboolhuff.h
@@ -28,6 +28,18 @@
Even relatively modest values like 100 would work fine.*/
#define VP8_LOTS_OF_BITS (0x40000000)
+static unsigned char decrypt_byte(const unsigned char *ch,
+ const unsigned char *origin,
+ const unsigned char *key)
+{
+#if CONFIG_DECRYPT
+ const int offset = (int)(ch - origin);
+ return *ch ^ key[offset % 32]; // VP8_DECRYPT_KEY_SIZE
+#else
+ return *ch;
+#endif
+}
+
typedef struct
{
const unsigned char *user_buffer_end;
@@ -35,6 +47,8 @@
VP8_BD_VALUE value;
int count;
unsigned int range;
+ const unsigned char *origin;
+ const unsigned char *key;
} BOOL_DECODER;
DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
@@ -41,7 +55,9 @@
int vp8dx_start_decode(BOOL_DECODER *br,
const unsigned char *source,
- unsigned int source_sz);
+ unsigned int source_sz,
+ const unsigned char *origin,
+ const unsigned char *key);
void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
--- a/vp8/decoder/decodframe.c
+++ b/vp8/decoder/decodframe.c
@@ -893,7 +893,9 @@
{
if (vp8dx_start_decode(bool_decoder,
pbi->fragments.ptrs[partition_idx],
- pbi->fragments.sizes[partition_idx]))
+ pbi->fragments.sizes[partition_idx],
+ pbi->fragments.ptrs[0],
+ pbi->decrypt_key))
vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
"Failed to allocate bool decoder %d",
partition_idx);
@@ -980,10 +982,11 @@
int vp8_decode_frame(VP8D_COMP *pbi)
{
- vp8_reader *const bc = & pbi->mbc[8];
- VP8_COMMON *const pc = & pbi->common;
- MACROBLOCKD *const xd = & pbi->mb;
+ vp8_reader *const bc = &pbi->mbc[8];
+ VP8_COMMON *const pc = &pbi->common;
+ MACROBLOCKD *const xd = &pbi->mb;
const unsigned char *data = pbi->fragments.ptrs[0];
+ const unsigned char *const origin = data;
const unsigned char *data_end = data + pbi->fragments.sizes[0];
ptrdiff_t first_partition_length_in_bytes;
@@ -1016,13 +1019,21 @@
}
else
{
- pc->frame_type = (FRAME_TYPE)(data[0] & 1);
- pc->version = (data[0] >> 1) & 7;
- pc->show_frame = (data[0] >> 4) & 1;
+ const unsigned char data0 = decrypt_byte(data + 0, origin,
+ pbi->decrypt_key);
+ const unsigned char data1 = decrypt_byte(data + 1, origin,
+ pbi->decrypt_key);
+ const unsigned char data2 = decrypt_byte(data + 2, origin,
+ pbi->decrypt_key);
+
+ pc->frame_type = (FRAME_TYPE)(data0 & 1);
+ pc->version = (data0 >> 1) & 7;
+ pc->show_frame = (data0 >> 4) & 1;
first_partition_length_in_bytes =
- (data[0] | (data[1] << 8) | (data[2] << 16)) >> 5;
+ (data0 | (data1 << 8) | (data2 << 16)) >> 5;
- if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end
+ if (!pbi->ec_active &&
+ (data + first_partition_length_in_bytes > data_end
|| data + first_partition_length_in_bytes < data))
vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
"Truncated packet or corrupt partition 0 length");
@@ -1040,7 +1051,13 @@
*/
if (!pbi->ec_active || data + 3 < data_end)
{
- if (data[0] != 0x9d || data[1] != 0x01 || data[2] != 0x2a)
+ const unsigned char data0 = decrypt_byte(data + 0, origin,
+ pbi->decrypt_key);
+ const unsigned char data1 = decrypt_byte(data + 1, origin,
+ pbi->decrypt_key);
+ const unsigned char data2 = decrypt_byte(data + 2, origin,
+ pbi->decrypt_key);
+ if (data0 != 0x9d || data1 != 0x01 || data2 != 0x2a)
vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
"Invalid frame sync code");
}
@@ -1051,10 +1068,19 @@
*/
if (!pbi->ec_active || data + 6 < data_end)
{
- pc->Width = (data[3] | (data[4] << 8)) & 0x3fff;
- pc->horiz_scale = data[4] >> 6;
- pc->Height = (data[5] | (data[6] << 8)) & 0x3fff;
- pc->vert_scale = data[6] >> 6;
+ const unsigned char data3 = decrypt_byte(data + 3, origin,
+ pbi->decrypt_key);
+ const unsigned char data4 = decrypt_byte(data + 4, origin,
+ pbi->decrypt_key);
+ const unsigned char data5 = decrypt_byte(data + 5, origin,
+ pbi->decrypt_key);
+ const unsigned char data6 = decrypt_byte(data + 6, origin,
+ pbi->decrypt_key);
+
+ pc->Width = (data3 | (data4 << 8)) & 0x3fff;
+ pc->horiz_scale = data4 >> 6;
+ pc->Height = (data5 | (data6 << 8)) & 0x3fff;
+ pc->vert_scale = data6 >> 6;
}
data += 7;
@@ -1072,7 +1098,11 @@
init_frame(pbi);
- if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data)))
+ if (vp8dx_start_decode(bc,
+ data,
+ (unsigned int)(data_end - data),
+ pbi->fragments.ptrs[0],
+ pbi->decrypt_key))
vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
"Failed to allocate bool decoder 0");
if (pc->frame_type == KEY_FRAME) {
--- a/vp8/decoder/onyxd_int.h
+++ b/vp8/decoder/onyxd_int.h
@@ -122,6 +122,7 @@
int independent_partitions;
int frame_corrupt_residual;
+ const unsigned char *decrypt_key;
} VP8D_COMP;
int vp8_decode_frame(VP8D_COMP *cpi);
--- a/vp8/vp8_dx_iface.c
+++ b/vp8/vp8_dx_iface.c
@@ -29,6 +29,8 @@
#define VP8_CAP_ERROR_CONCEALMENT (CONFIG_ERROR_CONCEALMENT ? \
VPX_CODEC_CAP_ERROR_CONCEALMENT : 0)
+#define VP8_DECRYPT_KEY_SIZE 32
+
typedef vpx_codec_stream_info_t vp8_stream_info_t;
/* Structures for handling memory allocations */
@@ -73,6 +75,7 @@
int dbg_color_b_modes_flag;
int dbg_display_mv_flag;
#endif
+ unsigned char decrypt_key[VP8_DECRYPT_KEY_SIZE];
vpx_image_t img;
int img_setup;
struct frame_buffers yv12_frame_buffers;
@@ -150,6 +153,8 @@
return res;
}
+static const unsigned char fake_decrypt_key[VP8_DECRYPT_KEY_SIZE] = { 0 };
+
static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap)
{
int i;
@@ -164,6 +169,8 @@
ctx->priv->alg_priv->mmaps[0] = *mmap;
ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
+ memcpy(ctx->priv->alg_priv->decrypt_key, fake_decrypt_key,
+ VP8_DECRYPT_KEY_SIZE);
ctx->priv->init_flags = ctx->init_flags;
if (ctx->config.dec)
@@ -262,14 +269,17 @@
return VPX_CODEC_OK;
}
-static vpx_codec_err_t vp8_peek_si(const uint8_t *data,
- unsigned int data_sz,
- vpx_codec_stream_info_t *si)
+static vpx_codec_err_t vp8_peek_si_external(const uint8_t *data,
+ unsigned int data_sz,
+ vpx_codec_stream_info_t *si,
+ const unsigned char *decrypt_key)
{
vpx_codec_err_t res = VPX_CODEC_OK;
if(data + data_sz <= data)
+ {
res = VPX_CODEC_INVALID_PARAM;
+ }
else
{
/* Parse uncompresssed part of key frame header.
@@ -278,19 +288,27 @@
* 4 bytes:- including image width and height in the lowest 14 bits
* of each 2-byte value.
*/
- si->is_kf = 0;
- if (data_sz >= 10 && !(data[0] & 0x01)) /* I-Frame */
+ const uint8_t data0 = decrypt_byte(data, data, decrypt_key);
+ si->is_kf = 0;
+ if (data_sz >= 10 && !(data0 & 0x01)) /* I-Frame */
{
- const uint8_t *c = data + 3;
+ const uint8_t data3 = decrypt_byte(data + 3, data, decrypt_key);
+ const uint8_t data4 = decrypt_byte(data + 4, data, decrypt_key);
+ const uint8_t data5 = decrypt_byte(data + 5, data, decrypt_key);
+ const uint8_t data6 = decrypt_byte(data + 6, data, decrypt_key);
+ const uint8_t data7 = decrypt_byte(data + 7, data, decrypt_key);
+ const uint8_t data8 = decrypt_byte(data + 8, data, decrypt_key);
+ const uint8_t data9 = decrypt_byte(data + 9, data, decrypt_key);
+
si->is_kf = 1;
/* vet via sync code */
- if (c[0] != 0x9d || c[1] != 0x01 || c[2] != 0x2a)
+ if (data3 != 0x9d || data4 != 0x01 || data5 != 0x2a)
res = VPX_CODEC_UNSUP_BITSTREAM;
- si->w = (c[3] | (c[4] << 8)) & 0x3fff;
- si->h = (c[5] | (c[6] << 8)) & 0x3fff;
+ si->w = (data6 | (data7 << 8)) & 0x3fff;
+ si->h = (data8 | (data9 << 8)) & 0x3fff;
/*printf("w=%d, h=%d\n", si->w, si->h);*/
if (!(si->h | si->w))
@@ -297,11 +315,18 @@
res = VPX_CODEC_UNSUP_BITSTREAM;
}
else
+ {
res = VPX_CODEC_UNSUP_BITSTREAM;
+ }
}
return res;
+}
+static vpx_codec_err_t vp8_peek_si(const uint8_t *data,
+ unsigned int data_sz,
+ vpx_codec_stream_info_t *si) {
+ return vp8_peek_si_external(data, data_sz, si, fake_decrypt_key);
}
static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t *ctx,
@@ -430,8 +455,10 @@
w = ctx->si.w;
h = ctx->si.h;
- res = ctx->base.iface->dec.peek_si(ctx->fragments.ptrs[0],
- ctx->fragments.sizes[0], &ctx->si);
+ res = vp8_peek_si_external(ctx->fragments.ptrs[0],
+ ctx->fragments.sizes[0],
+ &ctx->si,
+ ctx->decrypt_key);
if((res == VPX_CODEC_UNSUP_BITSTREAM) && !ctx->si.is_kf)
{
@@ -505,6 +532,7 @@
}
res = vp8_create_decoder_instances(&ctx->yv12_frame_buffers, &oxcf);
+ ctx->yv12_frame_buffers.pbi[0]->decrypt_key = ctx->decrypt_key;
}
ctx->decoder_init = 1;
@@ -926,6 +954,20 @@
}
+
+static vpx_codec_err_t vp8_set_decrypt_key(vpx_codec_alg_priv_t *ctx,
+ int ctr_id,
+ va_list args)
+{
+ const unsigned char *data = va_arg(args, const unsigned char *);
+ if (data == NULL) {
+ return VPX_CODEC_INVALID_PARAM;
+ }
+
+ memcpy(ctx->decrypt_key, data, VP8_DECRYPT_KEY_SIZE);
+ return VPX_CODEC_OK;
+}
+
vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
{
{VP8_SET_REFERENCE, vp8_set_reference},
@@ -938,6 +980,7 @@
{VP8D_GET_LAST_REF_UPDATES, vp8_get_last_ref_updates},
{VP8D_GET_FRAME_CORRUPTED, vp8_get_frame_corrupted},
{VP8D_GET_LAST_REF_USED, vp8_get_last_ref_frame},
+ {VP8_SET_DECRYPT_KEY, vp8_set_decrypt_key},
{ -1, NULL},
};
--- a/vpx/vp8dx.h
+++ b/vpx/vp8dx.h
@@ -63,6 +63,12 @@
*/
VP8D_GET_LAST_REF_USED,
+ /** decryption key to protect encoded data buffer before decoding,
+ * pointer to 32 byte array which is copied, so the array passed
+ * does not need to be preserved
+ */
+ VP8_SET_DECRYPT_KEY,
+
VP8_DECODER_CTRL_ID_MAX
};
@@ -78,6 +84,7 @@
VPX_CTRL_USE_TYPE(VP8D_GET_LAST_REF_UPDATES, int *)
VPX_CTRL_USE_TYPE(VP8D_GET_FRAME_CORRUPTED, int *)
VPX_CTRL_USE_TYPE(VP8D_GET_LAST_REF_USED, int *)
+VPX_CTRL_USE_TYPE(VP8_SET_DECRYPT_KEY, const unsigned char *)
/*! @} - end defgroup vp8_decoder */
--
⑨