diff options
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/key_derivation.c | 24 | ||||
-rw-r--r-- | src/key_derivation.h | 41 |
3 files changed, 59 insertions, 8 deletions
diff --git a/src/Makefile b/src/Makefile index 45ae277..30aebf9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -34,7 +34,7 @@ TARGET=$(shell uname -s) CC = gcc -CCFLAGS = -g -O2 -DANYTUN_02_COMPAT #-DNO_LIBGMP -DNO_CRYPT +CCFLAGS = -g -O2 # -DNO_LIBGMP # -DANYTUN_02_COMPAT #-DNO_CRYPT LD = gcc LDFLAGS = -g -Wall -O2 -lgcrypt -lgpg-error -lgmp diff --git a/src/key_derivation.c b/src/key_derivation.c index aec90a1..eaf80e5 100644 --- a/src/key_derivation.c +++ b/src/key_derivation.c @@ -188,17 +188,20 @@ int key_derivation_aesctr_init(key_derivation_t* kd) key_derivation_aesctr_param_t* params = kd->params_; #ifndef NO_LIBGMP - mpz_init2(params->mp_ctr, 128); - mpz_init2(params->mp_key_id, 128); -#endif + mpz_init2(params->mp_ctr, KD_AES_CTR_LENGTH * 8); + mpz_init2(params->mp_key_id, KD_AES_CTR_LENGTH * 8); - params->ctr_.length_ = 16; + params->ctr_.length_ = KD_AES_CTR_LENGTH; params->ctr_.buf_ = malloc(params->ctr_.length_); if(!params->ctr_.buf_) { free(kd->params_); kd->params_ = NULL; return -2; } +#else + params->ctr_.length_ = KD_AES_CTR_LENGTH; + params->ctr_.buf_ = params->ctr_.ctr_.buf_; +#endif gcry_error_t err = gcry_cipher_open(¶ms->handle_, algo, GCRY_CIPHER_MODE_CTR, 0); if(err) { @@ -225,9 +228,9 @@ void key_derivation_aesctr_close(key_derivation_t* kd) #ifndef NO_LIBGMP mpz_clear(params->mp_ctr); mpz_clear(params->mp_key_id); -#endif if(params->ctr_.buf_) free(params->ctr_.buf_); +#endif if(params->handle_) gcry_cipher_close(params->handle_); @@ -273,7 +276,7 @@ int key_derivation_aesctr_calc_ctr(key_derivation_t* kd, seq_nr_t* r, satp_prf_l mpz_add_ui(params->mp_key_id, params->mp_key_id, *r); mpz_xor(params->mp_ctr, params->mp_ctr, params->mp_key_id); - mpz_mul_2exp(params->mp_ctr, params->mp_ctr, 16); + mpz_mul_2exp(params->mp_ctr, params->mp_ctr, KD_AES_CTR_ZERO_LENGTH * 8); int out_size = (mpz_sizeinbase(params->mp_ctr, 2) + 7) / 8; if(out_size > params->ctr_.length_) { @@ -281,6 +284,15 @@ int key_derivation_aesctr_calc_ctr(key_derivation_t* kd, seq_nr_t* r, satp_prf_l return -1; } mpz_export(params->ctr_.buf_, NULL, 1, 1, 0, 0, params->mp_ctr); +#else + if(kd->master_salt_.length_ != sizeof(params->ctr_.ctr_.salt_.buf_)) { + log_printf(ERR, "master salt has the wrong length"); + return -1; + } + memcpy(params->ctr_.ctr_.salt_.buf_, kd->master_salt_.buf_, sizeof(params->ctr_.ctr_.salt_.buf_)); + memset(params->ctr_.ctr_.salt_.zero_, 0, sizeof(params->ctr_.ctr_.salt_.zero_)); + params->ctr_.ctr_.params_.label_ ^= label; + params->ctr_.ctr_.params_.r_ ^= SEQ_NR_T_HTON(*r); #endif #ifndef ANYTUN_02_COMPAT diff --git a/src/key_derivation.h b/src/key_derivation.h index 72037bf..1b8cd40 100644 --- a/src/key_derivation.h +++ b/src/key_derivation.h @@ -74,12 +74,51 @@ int key_derivation_generate(key_derivation_t* kd, satp_prf_label_t label, seq_nr int key_derivation_null_generate(u_int8_t* key, u_int32_t len); + +#define KD_AES_CTR_LENGTH 16 +#define KD_AES_CTR_ZERO_LENGTH 2 +#ifdef NO_LIBGMP +union __attribute__ ((__packed__)) key_derivation_aesctr_ctr_buf_union { + u_int8_t buf_[KD_AES_CTR_LENGTH]; + struct __attribute__ ((__packed__)) { + u_int8_t buf_[KD_AES_CTR_LENGTH - KD_AES_CTR_ZERO_LENGTH]; + u_int8_t zero_[KD_AES_CTR_ZERO_LENGTH]; + } salt_; +#ifndef ANYTUN_02_COMPAT + struct __attribute__ ((__packed__)) { + u_int8_t fill_[KD_AES_CTR_LENGTH - sizeof(u_int8_t) - sizeof(seq_nr_t) - KD_AES_CTR_ZERO_LENGTH]; + u_int8_t label_; + seq_nr_t r_; + u_int8_t zero_[KD_AES_CTR_ZERO_LENGTH]; + } params_; +#else + struct __attribute__ ((__packed__)) { + u_int8_t fill_[KD_AES_CTR_LENGTH - sizeof(u_int8_t) - 2 - sizeof(seq_nr_t) - KD_AES_CTR_ZERO_LENGTH]; + u_int8_t label_; + u_int8_t r_fill_[2]; + seq_nr_t r_; + u_int8_t zero_[KD_AES_CTR_ZERO_LENGTH]; + } params_; +#endif +}; +typedef union key_derivation_aesctr_ctr_buf_union key_derivation_aesctr_ctr_buf_t; + +struct key_derivation_aesctr_ctr_struct { + u_int32_t length_; + u_int8_t* buf_; + key_derivation_aesctr_ctr_buf_t ctr_; +}; +typedef struct key_derivation_aesctr_ctr_struct key_derivation_aesctr_ctr_t; +#endif + struct key_derivation_aesctr_param_struct { gcry_cipher_hd_t handle_; - buffer_t ctr_; #ifndef NO_LIBGMP + buffer_t ctr_; mpz_t mp_ctr; mpz_t mp_key_id; +#else + key_derivation_aesctr_ctr_t ctr_; #endif }; typedef struct key_derivation_aesctr_param_struct key_derivation_aesctr_param_t; |