From 2e130912cbdf8ce19b612c2f67bbcb551ec8a415 Mon Sep 17 00:00:00 2001 From: Timothe FRIGNAC Date: Mon, 28 Aug 2017 17:15:13 +0200 Subject: [PATCH] Add --key opt --- src/cfg.c | 45 +++++++++++++++++++++++++++++++++++++++++++ src/cli.c | 1 + src/encoderstate.c | 2 +- src/extras/crypto.cpp | 22 +++++++++++---------- src/extras/crypto.h | 3 ++- src/kvazaar.h | 1 + 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index c6aa43ab..25888fa8 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -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,43 @@ static int parse_tiles_specification(const char* const arg, int32_t * const ntil return 1; } +static double parse_number_or_die(const char *numstr,double min, double max) +{ + char *tail; + double d = strtod(numstr, &tail); + if (*tail) + fprintf(stderr,"Expected number\n"); + else + return d; + exit(1); + return 0; +} + +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 && (token != NULL)){ + fprintf(stderr, "\x1B[31mparsing of the array failed : too many members.\n\x1B[0m"); + return 0; + } + else if (ioptional_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); diff --git a/src/cli.c b/src/cli.c index e3b6e8b8..5ae2a207 100644 --- a/src/cli.c +++ b/src/cli.c @@ -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 }, diff --git a/src/encoderstate.c b/src/encoderstate.c index 7fcee3ee..7cdbecfe 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -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(&state->encoder_control->cfg); state->crypto_prev_pos = 0; } diff --git a/src/extras/crypto.cpp b/src/extras/crypto.cpp index 1416f3d6..eedb2613 100644 --- a/src/extras/crypto.cpp +++ b/src/extras/crypto.cpp @@ -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); diff --git a/src/extras/crypto.h b/src/extras/crypto.h index 6351178c..ef26e798 100644 --- a/src/extras/crypto.h +++ b/src/extras/crypto.h @@ -2,6 +2,7 @@ #define CRYPTO_H_ #include "global.h" +#include "../cfg.h" #include #include @@ -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, diff --git a/src/kvazaar.h b/src/kvazaar.h index fe7e78c5..38c1f916 100644 --- a/src/kvazaar.h +++ b/src/kvazaar.h @@ -320,6 +320,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. */