mirror of
https://github.com/ultravideo/uvg266.git
synced 2024-11-23 18:14:06 +00:00
Rewrite crypto to conform to kvazaar code style
This commit is contained in:
parent
6829865190
commit
22155950c1
48
src/cabac.c
48
src/cabac.c
|
@ -297,7 +297,7 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
//m_pcBinIf->encodeBinsEP(Suffix, r_param);
|
||||
if(r_param==1) {
|
||||
if(!(( base_level ==2 )&& (codeNumber==4 || codeNumber==5) ) ) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 1;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 1, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 1);
|
||||
|
@ -309,21 +309,21 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
else
|
||||
if(r_param==2) {
|
||||
if( base_level ==1) {
|
||||
uint32_t key =ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 3;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 2, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 2);
|
||||
} else
|
||||
if( base_level ==2) {
|
||||
if(codeNumber<=7 || codeNumber>=12) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 3;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 2, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 2);
|
||||
}
|
||||
else
|
||||
if(codeNumber<10) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = (( (Suffix&1) + ( state->tile->m_prev_pos^key )) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 2, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 2);
|
||||
|
@ -332,12 +332,12 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
//m_pcBinIf->encodeBinsEP(Suffix, 2);
|
||||
} else { //base_level=3
|
||||
if(codeNumber<=7 || codeNumber>11) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = (Suffix + ( state->tile->m_prev_pos^key ) ) & 3;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 2, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 2);
|
||||
} else {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = ((Suffix&2))+(( (Suffix&1) + ( state->tile->m_prev_pos^key)) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 2, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 2);
|
||||
|
@ -346,26 +346,26 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
} else
|
||||
if(r_param==3) {
|
||||
if( base_level ==1) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 3);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 3);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 7;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
}
|
||||
else if( base_level ==2) {
|
||||
if(codeNumber<=15 || codeNumber>23) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 3);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 3);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 7;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
} else
|
||||
if(codeNumber<=19){
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = ((Suffix&4))+(( (Suffix&3) + (state->tile->m_prev_pos^key )) & 3);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
} else
|
||||
if(codeNumber<=21){
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = 4+(( (Suffix&1) + ( state->tile->m_prev_pos^key )) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
|
@ -376,19 +376,19 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
CABAC_BINS_EP(cabac, Suffix, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(Suffix, 3);
|
||||
if(codeNumber<=15 || codeNumber>23) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 3);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 3);
|
||||
state->tile->m_prev_pos = (Suffix + ( state->tile->m_prev_pos^key ) ) & 7;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
} else
|
||||
if(codeNumber<=19) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = (( (Suffix&3) + ( state->tile->m_prev_pos^key )) &3);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
} else
|
||||
if(codeNumber<=23) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = (Suffix&6)+(( (Suffix&1) + (state->tile->m_prev_pos^key )) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 3, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 3);
|
||||
|
@ -397,32 +397,32 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
} else
|
||||
if(r_param==4) {
|
||||
if( base_level ==1) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 4);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 4);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 15;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
} else
|
||||
if( base_level ==2) {
|
||||
if(codeNumber<=31 || codeNumber>47) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 4);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 4);
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & 15;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, r_param, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, r_param);
|
||||
} else
|
||||
if(codeNumber<=39) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 3);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 3);
|
||||
state->tile->m_prev_pos = (( (Suffix&7) + ( state->tile->m_prev_pos^key )) & 7);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
} else
|
||||
if(codeNumber<=43) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = 8+(( (Suffix&3) + ( state->tile->m_prev_pos^key )) & 3);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
} else
|
||||
if(codeNumber<=45){
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = 12+(( (Suffix&1) + ( state->tile->m_prev_pos^key )) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
|
@ -431,25 +431,25 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
//m_pcBinIf->encodeBinsEP(Suffix, 4);
|
||||
} else {//base_level=3
|
||||
if(codeNumber<=31 || codeNumber>47) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 4);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 4);
|
||||
state->tile->m_prev_pos = (Suffix + ( state->tile->m_prev_pos^key ) ) & 15;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, r_param, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, r_param);
|
||||
} else
|
||||
if(codeNumber<=39) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 3);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 3);
|
||||
state->tile->m_prev_pos = (( (Suffix&7) + ( state->tile->m_prev_pos^key )) & 7);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
} else
|
||||
if(codeNumber<=43) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 2);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 2);
|
||||
state->tile->m_prev_pos = 8+(( (Suffix&3) + ( state->tile->m_prev_pos^key )) & 3);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
} else
|
||||
if(codeNumber<=47) {
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, 1);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
state->tile->m_prev_pos = (Suffix&14)+(( (Suffix&1) + (state->tile->m_prev_pos^key )) & 1);
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, 4, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP(m_prev_pos, 4);
|
||||
|
@ -466,7 +466,7 @@ void kvz_cabac_write_coeff_remain_encry(struct encoder_state_t * const state, ca
|
|||
CABAC_BINS_EP(cabac, (1 << (3 + length + 1 - r_param)) - 2, 3 + length + 1 - r_param, "coeff_abs_level_remaining");
|
||||
//m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-r_param))-2,COEF_REMAIN_BIN_REDUCTION+length+1-r_param);
|
||||
uint32_t Suffix = codeNumber;
|
||||
uint32_t key = ff_get_key(&state->tile->dbs_g, length);
|
||||
uint32_t key = kvz_crypto_get_key(state->tile->crypto_hdl, length);
|
||||
uint32_t mask = ( (1<<length ) -1 );
|
||||
state->tile->m_prev_pos = ( Suffix + ( state->tile->m_prev_pos^key ) ) & mask;
|
||||
CABAC_BINS_EP(cabac, state->tile->m_prev_pos, length, "coeff_abs_level_remaining");
|
||||
|
@ -551,7 +551,7 @@ void kvz_cabac_write_ep_ex_golomb(struct encoder_state_t * const state, cabac_da
|
|||
if (!state->cabac.only_count) {
|
||||
if (state->encoder_control->cfg.crypto_features & KVZ_CRYPTO_MVs) {
|
||||
uint32_t key, mask;
|
||||
key = ff_get_key(&state->tile->dbs_g, num_bins>>1);
|
||||
key = kvz_crypto_get_key(state->tile->crypto_hdl, num_bins>>1);
|
||||
mask = ( (1<<(num_bins >>1) ) -1 );
|
||||
state->tile->m_prev_pos = ( bins + ( state->tile->m_prev_pos^key ) ) & mask;
|
||||
bins = ( (bins >> (num_bins >>1) ) << (num_bins >>1) ) | state->tile->m_prev_pos;
|
||||
|
|
|
@ -302,13 +302,13 @@ void kvz_encode_coeff_nxn(encoder_state_t * const state,
|
|||
coeff_signs = coeff_signs >> 1;
|
||||
if(!state->cabac.only_count)
|
||||
if (state->encoder_control->cfg.crypto_features & KVZ_CRYPTO_TRANSF_COEFF_SIGNS) {
|
||||
coeff_signs = coeff_signs ^ ff_get_key(&state->tile->dbs_g, num_non_zero-1);
|
||||
coeff_signs = coeff_signs ^ kvz_crypto_get_key(state->tile->crypto_hdl, num_non_zero-1);
|
||||
}
|
||||
CABAC_BINS_EP(cabac, coeff_signs , (num_non_zero - 1), "coeff_sign_flag");
|
||||
} else {
|
||||
if(!state->cabac.only_count)
|
||||
if (state->encoder_control->cfg.crypto_features & KVZ_CRYPTO_TRANSF_COEFF_SIGNS)
|
||||
coeff_signs = coeff_signs ^ ff_get_key(&state->tile->dbs_g, num_non_zero);
|
||||
coeff_signs = coeff_signs ^ kvz_crypto_get_key(state->tile->crypto_hdl, num_non_zero);
|
||||
CABAC_BINS_EP(cabac, coeff_signs, num_non_zero, "coeff_sign_flag");
|
||||
}
|
||||
|
||||
|
@ -646,7 +646,7 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,
|
|||
uint32_t mvd_hor_sign = (mvd_hor>0)?0:1;
|
||||
if(!state->cabac.only_count)
|
||||
if (state->encoder_control->cfg.crypto_features & KVZ_CRYPTO_MV_SIGNS)
|
||||
mvd_hor_sign = mvd_hor_sign^ff_get_key(&state->tile->dbs_g, 1);
|
||||
mvd_hor_sign = mvd_hor_sign^kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
CABAC_BIN_EP(cabac, mvd_hor_sign, "mvd_sign_flag_hor");
|
||||
}
|
||||
if (ver_abs_gr0) {
|
||||
|
@ -656,7 +656,7 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,
|
|||
uint32_t mvd_ver_sign = (mvd_ver>0)?0:1;
|
||||
if(!state->cabac.only_count)
|
||||
if (state->encoder_control->cfg.crypto_features & KVZ_CRYPTO_MV_SIGNS)
|
||||
mvd_ver_sign = mvd_ver_sign^ff_get_key(&state->tile->dbs_g, 1);
|
||||
mvd_ver_sign = mvd_ver_sign^kvz_crypto_get_key(state->tile->crypto_hdl, 1);
|
||||
CABAC_BIN_EP(cabac, mvd_ver_sign, "mvd_sign_flag_ver");
|
||||
}
|
||||
}
|
||||
|
@ -692,7 +692,7 @@ static INLINE uint8_t intra_mode_encryption(encoder_state_t * const state,
|
|||
} else {
|
||||
uint8_t keybits, scan_dir, elem_idx=0;
|
||||
|
||||
keybits = ff_get_key(&state->tile->dbs_g, 5);
|
||||
keybits = kvz_crypto_get_key(state->tile->crypto_hdl, 5);
|
||||
|
||||
scan_dir = SCAN_DIAG;
|
||||
if (intra_pred_mode > 5 && intra_pred_mode < 15) {
|
||||
|
|
|
@ -140,8 +140,8 @@ static void encoder_state_config_tile_finalize(encoder_state_t * const state) {
|
|||
|
||||
kvz_videoframe_free(state->tile->frame);
|
||||
state->tile->frame = NULL;
|
||||
if (state->encoder_control->cfg.crypto_features && state->tile->dbs_g) {
|
||||
DeleteCryptoC(state->tile->dbs_g);
|
||||
if (state->encoder_control->cfg.crypto_features) {
|
||||
kvz_crypto_delete(&state->tile->crypto_hdl);
|
||||
}
|
||||
FREE_POINTER(state->tile->wf_jobs);
|
||||
}
|
||||
|
@ -329,7 +329,7 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t
|
|||
return 0;
|
||||
}
|
||||
|
||||
child_state->tile->dbs_g = NULL; // Not used. The used state is in the sub-tile.
|
||||
child_state->tile->crypto_hdl = NULL; // Not used. The used state is in the sub-tile.
|
||||
child_state->slice = MALLOC(encoder_state_config_slice_t, 1);
|
||||
if (!child_state->slice || !encoder_state_config_slice_init(child_state, 0, encoder->in.width_in_lcu * encoder->in.height_in_lcu - 1)) {
|
||||
fprintf(stderr, "Could not initialize encoder_state->slice!\n");
|
||||
|
@ -465,7 +465,7 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t
|
|||
new_child->frame = child_state->frame;
|
||||
new_child->tile = MALLOC(encoder_state_config_tile_t, 1);
|
||||
if (child_state->encoder_control->cfg.crypto_features) {
|
||||
new_child->tile->dbs_g = CreateC();
|
||||
new_child->tile->crypto_hdl = kvz_crypto_create();
|
||||
}
|
||||
new_child->slice = child_state->slice;
|
||||
new_child->wfrow = child_state->wfrow;
|
||||
|
|
|
@ -413,7 +413,7 @@ static void encoder_state_encode_leaf(encoder_state_t * const state) {
|
|||
|
||||
const kvz_config *cfg = &state->encoder_control->cfg;
|
||||
if (cfg->crypto_features) {
|
||||
InitC(state->tile->dbs_g);
|
||||
kvz_crypto_init(state->tile->crypto_hdl);
|
||||
state->tile->m_prev_pos = 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -178,7 +178,7 @@ typedef struct encoder_state_config_tile_t {
|
|||
threadqueue_job_t **wf_jobs;
|
||||
|
||||
// Instance of encryption generator by tile
|
||||
Crypto_Handle dbs_g;
|
||||
crypto_handle_t *crypto_hdl;
|
||||
uint32_t m_prev_pos;
|
||||
|
||||
} encoder_state_config_tile_t;
|
||||
|
|
|
@ -1,132 +1,133 @@
|
|||
#include <extras/crypto.h>
|
||||
|
||||
#ifndef KVZ_SEL_ENCRYPTION
|
||||
extern int kvz_make_vs_ignore_crypto_not_having_symbols;
|
||||
int kvz_make_vs_ignore_crypto_not_having_symbols = 0;
|
||||
#else
|
||||
|
||||
#include <cryptopp/aes.h>
|
||||
#include <cryptopp/modes.h>
|
||||
#include <cryptopp/osrng.h>
|
||||
typedef struct AESDecoder {
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption *CFBdec;
|
||||
typedef CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption cipher_t;
|
||||
#else
|
||||
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption *CFBdec;
|
||||
typedef CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption cipher_t;
|
||||
#endif
|
||||
|
||||
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE], out_stream_counter[CryptoPP::AES::BLOCKSIZE], counter[CryptoPP::AES::BLOCKSIZE];
|
||||
int couter_avail, counter_index, counter_index_pos;
|
||||
} AESDecoder;
|
||||
struct crypto_handle_t {
|
||||
cipher_t *cipher;
|
||||
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
|
||||
byte iv[CryptoPP::AES::BLOCKSIZE];
|
||||
byte out_stream_counter[CryptoPP::AES::BLOCKSIZE];
|
||||
byte counter[CryptoPP::AES::BLOCKSIZE];
|
||||
int couter_avail;
|
||||
int counter_index;
|
||||
int counter_index_pos;
|
||||
};
|
||||
|
||||
|
||||
AESDecoder* Create() {
|
||||
AESDecoder * AESdecoder = (AESDecoder *)malloc(sizeof(AESDecoder));
|
||||
return AESdecoder;
|
||||
crypto_handle_t* kvz_crypto_create()
|
||||
{
|
||||
return (crypto_handle_t*)calloc(1, sizeof(crypto_handle_t));
|
||||
}
|
||||
void Init(AESDecoder* AESdecoder) {
|
||||
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};
|
||||
for(int i=0;i<16; i++) {
|
||||
AESdecoder->iv [i] = init_val[i];
|
||||
AESdecoder->counter[i] = init_val[5+i];
|
||||
AESdecoder->key[i] = init_val[i+16];
|
||||
|
||||
void kvz_crypto_init(crypto_handle_t* hdl)
|
||||
{
|
||||
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,
|
||||
};
|
||||
|
||||
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->cipher = new cipher_t(hdl->key, CryptoPP::AES::DEFAULT_KEYLENGTH, hdl->iv);
|
||||
|
||||
hdl->couter_avail = 0;
|
||||
hdl->counter_index = 0;
|
||||
hdl->counter_index_pos = 0;
|
||||
}
|
||||
|
||||
void kvz_crypto_delete(crypto_handle_t **hdl)
|
||||
{
|
||||
FREE_POINTER(*hdl);
|
||||
}
|
||||
|
||||
void kvz_crypto_decrypt(crypto_handle_t* hdl,
|
||||
const uint8_t *in_stream,
|
||||
int size_bits,
|
||||
uint8_t *out_stream)
|
||||
{
|
||||
int num_bytes = ceil((double)size_bits/8);
|
||||
hdl->cipher->ProcessData(out_stream, in_stream, num_bytes);
|
||||
if (size_bits & 7) {
|
||||
hdl->cipher->SetKeyWithIV(hdl->key, CryptoPP::AES::DEFAULT_KEYLENGTH, hdl->iv);
|
||||
}
|
||||
}
|
||||
#if AESEncryptionStreamMode
|
||||
static void increment_counter(unsigned char *counter)
|
||||
{
|
||||
counter[0]++;
|
||||
}
|
||||
|
||||
static void decrypt_counter(crypto_handle_t *hdl)
|
||||
{
|
||||
hdl->cipher->ProcessData(hdl->out_stream_counter, hdl->counter, 16);
|
||||
hdl->couter_avail = 128;
|
||||
hdl->counter_index = 15;
|
||||
hdl->counter_index_pos = 8;
|
||||
increment_counter(hdl->counter);
|
||||
}
|
||||
|
||||
unsigned kvz_crypto_get_key(crypto_handle_t *hdl, int nb_bits)
|
||||
{
|
||||
unsigned key = 0;
|
||||
if (nb_bits > 32) {
|
||||
fprintf(stderr, "The generator cannot generate %d bits (max 32 bits)\n", nb_bits);
|
||||
return 0;
|
||||
}
|
||||
if (nb_bits == 0) return 0;
|
||||
|
||||
if (!hdl->couter_avail) {
|
||||
decrypt_counter(hdl);
|
||||
}
|
||||
|
||||
if(hdl->couter_avail >= nb_bits) {
|
||||
hdl->couter_avail -= nb_bits;
|
||||
} else {
|
||||
hdl->couter_avail = 0;
|
||||
}
|
||||
|
||||
int nb = 0;
|
||||
while (nb_bits) {
|
||||
if (nb_bits >= hdl->counter_index_pos) {
|
||||
nb = hdl->counter_index_pos;
|
||||
} else {
|
||||
nb = nb_bits;
|
||||
}
|
||||
#if AESEncryptionStreamMode
|
||||
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Encryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
|
||||
#else
|
||||
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Decryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
|
||||
#endif
|
||||
AESdecoder->couter_avail = 0;
|
||||
AESdecoder->counter_index = 0;
|
||||
AESdecoder->counter_index_pos = 0;
|
||||
}
|
||||
|
||||
void DeleteCrypto(AESDecoder * AESdecoder) {
|
||||
if(AESdecoder)
|
||||
free(AESdecoder);
|
||||
}
|
||||
key <<= nb;
|
||||
key += hdl->out_stream_counter[hdl->counter_index] & ((1 << nb) - 1);
|
||||
hdl->out_stream_counter[hdl->counter_index] >>= nb;
|
||||
nb_bits -= nb;
|
||||
|
||||
void Decrypt(AESDecoder *AESdecoder, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
|
||||
int nb_bytes = ceil((double)size_bits/8);
|
||||
AESdecoder->CFBdec->ProcessData(out_stream, in_stream, nb_bytes);
|
||||
if(size_bits&7)
|
||||
AESdecoder->CFBdec->SetKeyWithIV(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
|
||||
|
||||
}
|
||||
void Incr_counter (unsigned char *counter) {
|
||||
counter[0]++;
|
||||
}
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
void Decrypt_counter(AESDecoder * AESdecoder) {
|
||||
AESdecoder->CFBdec->ProcessData(AESdecoder->out_stream_counter, AESdecoder->counter, 16);
|
||||
AESdecoder->couter_avail = 128;
|
||||
AESdecoder->counter_index = 15;
|
||||
AESdecoder->counter_index_pos = 8;
|
||||
Incr_counter(AESdecoder->counter);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
unsigned int get_key (AESDecoder * AESdecoder, int nb_bits) {
|
||||
unsigned int key_ = 0;
|
||||
if(nb_bits > 32) {
|
||||
printf("The Generator can not generate more than 32 bit %d \n", nb_bits);
|
||||
return 0;
|
||||
if (hdl->counter_index && nb == hdl->counter_index_pos) {
|
||||
hdl->counter_index--;
|
||||
hdl->counter_index_pos = 8;
|
||||
} else {
|
||||
hdl->counter_index_pos -= nb;
|
||||
if (nb_bits) {
|
||||
decrypt_counter(hdl);
|
||||
hdl->couter_avail -= nb_bits;
|
||||
}
|
||||
}
|
||||
if( !nb_bits )
|
||||
return 0;
|
||||
if(!AESdecoder->couter_avail)
|
||||
Decrypt_counter(AESdecoder);
|
||||
|
||||
if(AESdecoder->couter_avail >= nb_bits)
|
||||
AESdecoder->couter_avail -= nb_bits;
|
||||
else
|
||||
AESdecoder->couter_avail = 0;
|
||||
int nb = 0;
|
||||
while( nb_bits ) {
|
||||
if( nb_bits >= AESdecoder->counter_index_pos )
|
||||
nb = AESdecoder->counter_index_pos;
|
||||
else
|
||||
nb = nb_bits;
|
||||
key_ <<= nb;
|
||||
key_ += (AESdecoder->out_stream_counter[AESdecoder->counter_index] & ((1<<nb)-1));
|
||||
AESdecoder->out_stream_counter[AESdecoder->counter_index] >>= nb;
|
||||
nb_bits -= nb;
|
||||
|
||||
if(AESdecoder->counter_index && nb == AESdecoder->counter_index_pos ) {
|
||||
AESdecoder->counter_index--;
|
||||
AESdecoder->counter_index_pos = 8;
|
||||
} else {
|
||||
AESdecoder->counter_index_pos -= nb;
|
||||
if(nb_bits) {
|
||||
Decrypt_counter(AESdecoder);
|
||||
AESdecoder->couter_avail -= nb_bits;
|
||||
}
|
||||
}
|
||||
}
|
||||
return key_;
|
||||
}
|
||||
#endif
|
||||
Crypto_Handle CreateC() {
|
||||
AESDecoder* AESdecoder = Create();
|
||||
return AESdecoder;
|
||||
}
|
||||
|
||||
void InitC(Crypto_Handle hdl) {
|
||||
Init((AESDecoder*)hdl);
|
||||
}
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits) {
|
||||
return get_key ((AESDecoder*)*hdl, nb_bits);
|
||||
}
|
||||
#endif
|
||||
void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
|
||||
Decrypt((AESDecoder*)hdl, in_stream, size_bits, out_stream);
|
||||
}
|
||||
|
||||
void DeleteCryptoC(Crypto_Handle hdl) {
|
||||
DeleteCrypto((AESDecoder *)hdl);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
#endif // AESEncryptionStreamMode
|
||||
|
||||
#endif // KVZ_SEL_ENCRYPTION
|
||||
|
|
|
@ -3,79 +3,73 @@
|
|||
|
||||
#include "global.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef KVZ_SEL_ENCRYPTION
|
||||
#define STUBBED extern
|
||||
#else
|
||||
#define STUBBED static
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#define AESEncryptionStreamMode 1
|
||||
#define AESEncryptionStreamMode 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef void* Crypto_Handle;
|
||||
STUBBED Crypto_Handle CreateC();
|
||||
STUBBED void InitC(Crypto_Handle hdl);
|
||||
STUBBED void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream);
|
||||
|
||||
typedef struct crypto_handle_t crypto_handle_t;
|
||||
|
||||
STUBBED crypto_handle_t* kvz_crypto_create();
|
||||
STUBBED void kvz_crypto_init(crypto_handle_t* hdl);
|
||||
STUBBED void kvz_crypto_decrypt(crypto_handle_t* hdl,
|
||||
const uint8_t *in_stream,
|
||||
int size_bits,
|
||||
uint8_t *out_stream);
|
||||
STUBBED void kvz_crypto_delete(crypto_handle_t **hdl);
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
STUBBED unsigned int ff_get_key(Crypto_Handle *hdl, int nb_bits);
|
||||
STUBBED unsigned kvz_crypto_get_key(crypto_handle_t *hdl, int num_bits);
|
||||
#endif
|
||||
STUBBED void DeleteCryptoC(Crypto_Handle hdl);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#undef STUBBED
|
||||
|
||||
|
||||
#ifndef KVZ_SEL_ENCRYPTION
|
||||
// Provide static stubs to allow linking without libcryptopp and allows us to
|
||||
// avoid sprinkling ifdefs everywhere and having a bunch of code that's not
|
||||
// compiled during normal development.
|
||||
// Provide static stubs to allow linking without libcryptopp and allows us
|
||||
// to avoid sprinkling ifdefs everywhere and having a bunch of code that's
|
||||
// not compiled during normal development.
|
||||
// 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.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static uintptr_t handle_id = 1;
|
||||
|
||||
static INLINE Crypto_Handle CreateC() {
|
||||
printf("Crypto CreateC %" PRIuPTR "\n", handle_id);
|
||||
return (void*)(handle_id++);
|
||||
}
|
||||
static INLINE void InitC(Crypto_Handle hdl) {
|
||||
printf("Crypto InitC %" PRIuPTR "\n", (uintptr_t)hdl);
|
||||
}
|
||||
|
||||
static INLINE void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream,
|
||||
int size_bits, unsigned char *out_stream)
|
||||
static INLINE crypto_handle_t* kvz_crypto_create()
|
||||
{
|
||||
// Stub.
|
||||
printf("Crypto DecryptC %" PRIuPTR "\n", (uintptr_t)hdl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static INLINE void kvz_crypto_init(crypto_handle_t* hdl)
|
||||
{}
|
||||
|
||||
static INLINE void kvz_crypto_decrypt(crypto_handle_t* hdl,
|
||||
const uint8_t *in_stream,
|
||||
int size_bits,
|
||||
uint8_t *out_stream)
|
||||
{}
|
||||
|
||||
static INLINE void kvz_crypto_delete(crypto_handle_t **hdl)
|
||||
{}
|
||||
|
||||
#if AESEncryptionStreamMode
|
||||
static INLINE unsigned int ff_get_key(Crypto_Handle *hdl, int nb_bits)
|
||||
static INLINE unsigned kvz_crypto_get_key(crypto_handle_t *hdl, int num_bits)
|
||||
{
|
||||
// Stub.
|
||||
static Crypto_Handle ff_get_key_last_hdl = 0;
|
||||
if (*hdl != ff_get_key_last_hdl) {
|
||||
printf("Crypto ff_get_key %" PRIuPTR "\n", (uintptr_t)*hdl);
|
||||
}
|
||||
ff_get_key_last_hdl = *hdl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static INLINE void DeleteCryptoC(Crypto_Handle hdl)
|
||||
{
|
||||
// Stub.
|
||||
printf("Crypto DeleteCryptoC %" PRIuPTR "\n", (uintptr_t)hdl);
|
||||
}
|
||||
|
||||
#endif // KVZ_SEL_ENCRYPTION
|
||||
|
||||
#endif // CRYPTO_H_
|
||||
|
|
Loading…
Reference in a new issue