Skip to content

Commit

Permalink
Add --key opt
Browse files Browse the repository at this point in the history
  • Loading branch information
TimotF committed Aug 28, 2017
1 parent 409d211 commit 2e13091
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
45 changes: 45 additions & 0 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ int kvz_config_init(kvz_config *cfg)

cfg->slices = KVZ_SLICES_NONE;

cfg->optional_key = NULL;

return 1;
}

Expand All @@ -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);

Expand Down Expand Up @@ -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){
coeff_key[i] = (uint8_t)parse_number_or_die(token,min,max);
i++;
token = strtok(NULL, delim);
}
if(i>=size && (token != NULL)){
fprintf(stderr, "\x1B[31mparsing of the array failed : too many members.\n\x1B[0m");
return 0;
}
else if (i<size){
fprintf(stderr, "\x1B[31mparsing of the array failed : too few members.\n\x1B[0m");
return 0;
}
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;
Expand Down Expand Up @@ -952,6 +992,11 @@ int kvz_config_parse(kvz_config *cfg, const char *name, const char *value)

return 1;
}
else if OPT("key"){
int size_key = 16;
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);
Expand Down
1 change: 1 addition & 0 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion src/encoderstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 12 additions & 10 deletions src/extras/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/extras/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CRYPTO_H_

#include "global.h"
#include "../cfg.h"

#include <stdio.h>
#include <math.h>
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/kvazaar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down

0 comments on commit 2e13091

Please sign in to comment.