Merge pull request #177 from TimotF/master

Add --key opt for encryption usage
This commit is contained in:
Arttu Ylä-Outinen 2017-09-01 11:23:23 +03:00 committed by GitHub
commit f8c5bb18a4
7 changed files with 82 additions and 14 deletions

View file

@ -123,6 +123,8 @@ int kvz_config_init(kvz_config *cfg)
cfg->slices = KVZ_SLICES_NONE;
cfg->optional_key = NULL;
return 1;
}
@ -134,6 +136,7 @@ int kvz_config_destroy(kvz_config *cfg)
FREE_POINTER(cfg->tiles_height_split);
FREE_POINTER(cfg->slice_addresses_in_ts);
FREE_POINTER(cfg->roi.dqps);
FREE_POINTER(cfg->optional_key);
}
free(cfg);
@ -230,6 +233,54 @@ static int parse_tiles_specification(const char* const arg, int32_t * const ntil
return 1;
}
static int parse_uint8(const char *numstr,uint8_t* number,int min, int max)
{
char *tail;
int d = strtol(numstr, &tail, 10);
if (*tail || d < min || d > max){
fprintf(stderr, "Expected number between %d and %d\n", min, max);
if(number)
*number = 0;
return 0;
} else{
if (number)
*number = (uint8_t) d;
return 1;
}
}
static int parse_array(const char *array, uint8_t *coeff_key, int size,
int min, int max)
{
char *key = strdup(array);
const char delim[] = ",;:";
char *token;
int i = 0;
token = strtok(key, delim);
while(token!=NULL&&i<size){
if (!parse_uint8(token, &coeff_key[i], min, max))
{
free(key);
return 0;
}
i++;
token = strtok(NULL, delim);
}
if(i>=size && (token != NULL)){
fprintf(stderr, "parsing failed : too many members.\n");
free(key);
return 0;
}
else if (i<size){
fprintf(stderr, "parsing failed : too few members.\n");
free(key);
return 0;
}
free(key);
return 1;
}
static int parse_slice_specification(const char* const arg, int32_t * const nslices, int32_t** const array) {
const char* current_arg = NULL;
int32_t current_value;
@ -952,6 +1003,12 @@ int kvz_config_parse(kvz_config *cfg, const char *name, const char *value)
return 1;
}
else if OPT("key"){
int size_key = 16;
FREE_POINTER(cfg->optional_key);
cfg->optional_key = (uint8_t *)malloc(sizeof(uint8_t)*size_key);
return parse_array(value, cfg->optional_key, size_key, 0, 255);
}
else if OPT("me-early-termination"){
int8_t mode = 0;
int result = parse_enum(value, me_early_termination_names, &mode);

View file

@ -107,6 +107,7 @@ static const struct option long_options[] = {
{ "hash", required_argument, NULL, 0 },
{"cu-split-termination",required_argument, NULL, 0 },
{ "crypto", required_argument, NULL, 0 },
{ "key", required_argument, NULL, 0 },
{ "me-early-termination",required_argument, NULL, 0 },
{ "lossless", no_argument, NULL, 0 },
{ "no-lossless", no_argument, NULL, 0 },

View file

@ -617,6 +617,12 @@ encoder_control_t* kvz_encoder_control_init(const kvz_config *const cfg)
encoder->cfg.vps_period = -1;
}
if(encoder->cfg.optional_key){
encoder->cfg.optional_key = MALLOC(uint8_t,16);
if (!encoder->cfg.optional_key) goto init_failed;
memcpy(encoder->cfg.optional_key, cfg->optional_key, 16);
}
return encoder;
init_failed:
@ -647,6 +653,7 @@ void kvz_encoder_control_free(encoder_control_t *const encoder)
FREE_POINTER(encoder->tiles_tile_id);
FREE_POINTER(encoder->cfg.roi.dqps);
FREE_POINTER(encoder->cfg.optional_key);
kvz_scalinglist_destroy(&encoder->scaling_list);

View file

@ -712,7 +712,7 @@ static void encoder_state_encode_leaf(encoder_state_t * const state)
state->ref_qp = state->frame->QP;
if (cfg->crypto_features) {
state->crypto_hdl = kvz_crypto_create();
state->crypto_hdl = kvz_crypto_create(cfg);
state->crypto_prev_pos = 0;
}

View file

@ -26,22 +26,24 @@ struct crypto_handle_t {
};
static const int init_val[32] = {
201, 75, 219, 152, 6, 245, 237, 107,
179, 194, 81, 29, 66, 98, 198, 0,
16, 213, 27, 56, 255, 127, 242, 112,
97, 126, 197, 204, 25, 59, 38, 30,
};
static uint8_t default_IV[16] = {201, 75, 219, 152, 6, 245, 237, 107, 179, 194, 81, 29, 66, 98, 198, 0};
static uint8_t default_key[16] = {16, 213, 27, 56, 255, 127, 242, 112, 97, 126, 197, 204, 25, 59, 38, 30};
crypto_handle_t* kvz_crypto_create()
crypto_handle_t* kvz_crypto_create(const kvz_config *cfg)
{
crypto_handle_t* hdl = (crypto_handle_t*)calloc(1, sizeof(crypto_handle_t));
uint8_t *key;
if(cfg->optional_key!=NULL)
key = cfg->optional_key;
else
key = default_key;
for (int i = 0; i < 16; i++) {
hdl->iv [i] = init_val[i];
hdl->counter[i] = init_val[i + 5];
hdl->key[i] = init_val[i + 16];
hdl->iv [i] = default_IV[i];
hdl->counter[i] = (i<11)? default_IV[5+i] : key[i-11];
hdl->key[i] = key[i];
}
hdl->cipher = new cipher_t(hdl->key, CryptoPP::AES::DEFAULT_KEYLENGTH, hdl->iv);

View file

@ -2,6 +2,7 @@
#define CRYPTO_H_
#include "global.h"
#include "../cfg.h"
#include <stdio.h>
#include <math.h>
@ -20,7 +21,7 @@ extern "C" {
typedef struct crypto_handle_t crypto_handle_t;
STUBBED crypto_handle_t* kvz_crypto_create();
STUBBED crypto_handle_t* kvz_crypto_create(const kvz_config *cfg);
STUBBED void kvz_crypto_decrypt(crypto_handle_t* hdl,
const uint8_t *in_stream,
int size_bits,
@ -45,7 +46,7 @@ STUBBED unsigned kvz_crypto_get_key(crypto_handle_t *hdl, int num_bits);
// Provide them in the header so we can avoid compiling the cpp file, which
// means we don't need a C++ compiler when crypto is not enabled.
static INLINE crypto_handle_t* kvz_crypto_create()
static INLINE crypto_handle_t* kvz_crypto_create(const kvz_config *cfg)
{
return NULL;
}

View file

@ -151,7 +151,6 @@ enum kvz_crypto_features {
KVZ_CRYPTO_TRANSF_COEFF_SIGNS = (1 << 3),
KVZ_CRYPTO_INTRA_MODE = (1 << 4),
KVZ_CRYPTO_ON = (1 << 5) - 1,
};
/**
@ -320,6 +319,7 @@ typedef struct kvz_config
enum kvz_cu_split_termination cu_split_termination; /*!< \since 3.8.0 \brief Mode of cu split termination. */
enum kvz_crypto_features crypto_features; /*!< \since 3.7.0 */
uint8_t *optional_key;
enum kvz_me_early_termination me_early_termination; /*!< \since 3.8.0 \brief Mode of me early termination. */