summaryrefslogtreecommitdiff
path: root/src/key_derivation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/key_derivation.c')
-rw-r--r--src/key_derivation.c68
1 files changed, 6 insertions, 62 deletions
diff --git a/src/key_derivation.c b/src/key_derivation.c
index db139ad..19cd1b1 100644
--- a/src/key_derivation.c
+++ b/src/key_derivation.c
@@ -41,10 +41,6 @@
#include <stdlib.h>
#include <string.h>
-#ifndef NO_LIBGMP
-#include <gmp.h>
-#endif
-
int key_derivation_init(key_derivation_t* kd, const char* type, int8_t ld_kdr, u_int8_t* key, u_int32_t key_len, u_int8_t* salt, u_int32_t salt_len)
{
if(!kd)
@@ -187,22 +183,6 @@ 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, KD_AES_CTR_LENGTH * 8);
- mpz_init2(params->mp_key_id, KD_AES_CTR_LENGTH * 8);
-
- 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(&params->handle_, algo, GCRY_CIPHER_MODE_CTR, 0);
if(err) {
log_printf(ERR, "failed to open key derivation cipher: %s/%s", gcry_strerror(err), gcry_strsource(err));
@@ -225,13 +205,6 @@ void key_derivation_aesctr_close(key_derivation_t* kd)
if(kd->params_) {
key_derivation_aesctr_param_t* params = kd->params_;
-#ifndef NO_LIBGMP
- mpz_clear(params->mp_ctr);
- mpz_clear(params->mp_key_id);
-
- if(params->ctr_.buf_)
- free(params->ctr_.buf_);
-#endif
if(params->handle_)
gcry_cipher_close(params->handle_);
@@ -246,9 +219,6 @@ int key_derivation_aesctr_calc_ctr(key_derivation_t* kd, seq_nr_t* r, satp_prf_l
return -1;
key_derivation_aesctr_param_t* params = kd->params_;
- if(!params->ctr_.buf_)
- return -1;
-
*r = 0;
if(kd->ld_kdr_ >= 0)
@@ -265,36 +235,14 @@ int key_derivation_aesctr_calc_ctr(key_derivation_t* kd, seq_nr_t* r, satp_prf_l
faked_msb = 1;
}
-#ifndef NO_LIBGMP
- mpz_import(params->mp_ctr, kd->master_salt_.length_, 1, 1, 0, 0, kd->master_salt_.buf_);
-
- mpz_set_ui(params->mp_key_id, label);
-#ifndef ANYTUN_02_COMPAT
- mpz_mul_2exp(params->mp_key_id, params->mp_key_id, (sizeof(*r) * 8));
-#else
- mpz_mul_2exp(params->mp_key_id, params->mp_key_id, 48);
-#endif
- 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, KD_AES_CTR_ZERO_LENGTH * 8);
-
- int out_size = (mpz_sizeinbase(params->mp_ctr, 2) + 7) / 8;
- if(out_size > params->ctr_.length_) {
- log_printf(ERR, "computed key derivation ctr is too big ?!?");
- 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_)) {
+ if(kd->master_salt_.length_ != KD_AESCTR_SALT_LENGTH) {
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
+ memcpy(params->ctr_.salt_.buf_, kd->master_salt_.buf_, KD_AESCTR_SALT_LENGTH);
+ params->ctr_.salt_.zero_ = 0;
+ params->ctr_.params_.label_ ^= label;
+ params->ctr_.params_.r_ ^= SEQ_NR_T_HTON(*r);
#ifndef ANYTUN_02_COMPAT
if(faked_msb) {
@@ -314,10 +262,6 @@ int key_derivation_aesctr_generate(key_derivation_t* kd, satp_prf_label_t label,
}
key_derivation_aesctr_param_t* params = kd->params_;
- if(!params->ctr_.buf_) {
- log_printf(ERR, "key derivation not initialized or no key or salt set");
- return -1;
- }
seq_nr_t r;
int ret = key_derivation_aesctr_calc_ctr(kd, &r, label, seq_nr);
@@ -341,7 +285,7 @@ int key_derivation_aesctr_generate(key_derivation_t* kd, satp_prf_label_t label,
return -1;
}
- err = gcry_cipher_setctr(params->handle_, params->ctr_.buf_, params->ctr_.length_);
+ err = gcry_cipher_setctr(params->handle_, params->ctr_.buf_, KD_AESCTR_CTR_LENGTH);
if(err) {
log_printf(ERR, "failed to set key derivation CTR: %s/%s", gcry_strerror(err), gcry_strsource(err));