summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile4
-rw-r--r--src/cipher.c85
-rw-r--r--src/cipher.h39
-rw-r--r--src/key_derivation.c68
-rw-r--r--src/key_derivation.h45
5 files changed, 45 insertions, 196 deletions
diff --git a/src/Makefile b/src/Makefile
index 21a3918..1090fc0 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -34,9 +34,9 @@
TARGET=$(shell uname -s)
CC = gcc
-CCFLAGS = -g -O2 -DNO_LIBGMP #-DANYTUN_02_COMPAT #-DNO_CRYPT
+CCFLAGS = -g -O2 -DANYTUN_02_COMPAT #-DNO_CRYPT
LD = gcc
-LDFLAGS = -g -Wall -O2 -lgcrypt -lgpg-error #-lgmp
+LDFLAGS = -g -Wall -O2 -lgcrypt -lgpg-error
ifeq ($(TARGET),Linux)
LDFLAGS += -ldl
diff --git a/src/cipher.c b/src/cipher.c
index 1a2a4fb..e35e4db 100644
--- a/src/cipher.c
+++ b/src/cipher.c
@@ -44,10 +44,6 @@
#include <stdlib.h>
#include <string.h>
-#ifndef NO_LIBGMP
-#include <gmp.h>
-#endif
-
int cipher_init(cipher_t* c, const char* type)
{
if(!c)
@@ -62,7 +58,7 @@ int cipher_init(cipher_t* c, const char* type)
else if(!strncmp(type, "aes-ctr", 7)) {
c->type_ = c_aes_ctr;
if(type[7] == 0) {
- c->key_length_ = C_AES_DEFAULT_KEY_LENGTH;
+ c->key_length_ = C_AESCTR_DEFAULT_KEY_LENGTH;
}
else if(type[7] != '-')
return -1;
@@ -202,7 +198,7 @@ int cipher_aesctr_init(cipher_t* c)
if(c->salt_.buf_)
free(c->salt_.buf_);
- c->salt_.length_ = C_AES_CTR_LENGTH - C_AES_CTR_ZERO_LENGTH;
+ c->salt_.length_ = C_AESCTR_SALT_LENGTH;
c->salt_.buf_ = malloc(c->salt_.length_);
if(!c->salt_.buf_)
return -2;
@@ -215,24 +211,6 @@ int cipher_aesctr_init(cipher_t* c)
cipher_aesctr_param_t* params = c->params_;
-#ifndef NO_LIBGMP
- mpz_init2(params->mp_ctr, C_AES_CTR_LENGTH * 8);
- mpz_init2(params->mp_sid_mux, C_AES_CTR_LENGTH * 8);
- mpz_init2(params->mp_seq, C_AES_CTR_LENGTH * 8);
-
- params->ctr_.length_ = C_AES_CTR_LENGTH;
- params->ctr_.buf_ = malloc(params->ctr_.length_);
- if(!params->ctr_.buf_) {
- free(c->params_);
- c->params_ = NULL;
- return -2;
- }
-#else
- params->ctr_.length_ = C_AES_CTR_LENGTH;
- params->ctr_.buf_ = params->ctr_.ctr_.buf_;
-#endif
-
-
int algo;
switch(c->key_length_) {
case 128: algo = GCRY_CIPHER_AES128; break;
@@ -260,14 +238,6 @@ void cipher_aesctr_close(cipher_t* c)
if(c->params_) {
cipher_aesctr_param_t* params = c->params_;
-#ifndef NO_LIBGMP
- mpz_clear(params->mp_ctr);
- mpz_clear(params->mp_sid_mux);
- mpz_clear(params->mp_seq);
-
- if(params->ctr_.buf_)
- free(params->ctr_.buf_);
-#endif
if(params->handle_)
gcry_cipher_close(params->handle_);
@@ -282,10 +252,8 @@ int cipher_aesctr_calc_ctr(cipher_t* c, key_derivation_t* kd, seq_nr_t seq_nr, s
return -1;
cipher_aesctr_param_t* params = c->params_;
- if(!params->ctr_.buf_)
- return -1;
- int ret = key_derivation_generate(kd, LABEL_SATP_SALT, seq_nr, c->salt_.buf_, c->salt_.length_);
+ int ret = key_derivation_generate(kd, LABEL_SATP_SALT, seq_nr, c->salt_.buf_, C_AESCTR_SALT_LENGTH);
if(ret < 0)
return ret;
@@ -295,38 +263,11 @@ int cipher_aesctr_calc_ctr(cipher_t* c, key_derivation_t* kd, seq_nr_t seq_nr, s
faked_msb = 1;
}
-#ifndef NO_LIBGMP
- mpz_import(params->mp_ctr, c->salt_.length_, 1, 1, 0, 0, c->salt_.buf_);
-
- mpz_set_ui(params->mp_sid_mux, mux);
- mpz_mul_2exp(params->mp_sid_mux, params->mp_sid_mux, (sizeof(sender_id) * 8));
- mpz_add_ui(params->mp_sid_mux, params->mp_sid_mux, sender_id);
- mpz_mul_2exp(params->mp_sid_mux, params->mp_sid_mux, 48);
-
- mpz_set_ui(params->mp_seq, seq_nr);
-
- mpz_xor(params->mp_ctr, params->mp_ctr, params->mp_sid_mux);
- mpz_xor(params->mp_ctr, params->mp_ctr, params->mp_seq);
-
- mpz_mul_2exp(params->mp_ctr, params->mp_ctr, 16);
-
- int out_size = (mpz_sizeinbase(params->mp_ctr, 2) + 7) / 8;
- if(out_size > params->ctr_.length_) {
- log_printf(ERR, "computed cipher ctr is too big ?!?");
- return -1;
- }
- mpz_export(params->ctr_.buf_, NULL, 1, 1, 0, 0, params->mp_ctr);
-#else
- if(c->salt_.length_ != sizeof(params->ctr_.ctr_.salt_.buf_)) {
- log_printf(ERR, "cipher salt has the wrong length");
- return -1;
- }
- memcpy(params->ctr_.ctr_.salt_.buf_, c->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_.mux_ ^= MUX_T_HTON(mux);
- params->ctr_.ctr_.params_.sender_id_ ^= SENDER_ID_T_HTON(sender_id);
- params->ctr_.ctr_.params_.seq_nr_ ^= SEQ_NR_T_HTON(seq_nr);
-#endif
+ memcpy(params->ctr_.salt_.buf_, c->salt_.buf_, C_AESCTR_SALT_LENGTH);
+ params->ctr_.salt_.zero_ = 0;
+ params->ctr_.params_.mux_ ^= MUX_T_HTON(mux);
+ params->ctr_.params_.sender_id_ ^= SENDER_ID_T_HTON(sender_id);
+ params->ctr_.params_.seq_nr_ ^= SEQ_NR_T_HTON(seq_nr);
#ifndef ANYTUN_02_COMPAT
if(faked_msb) {
@@ -345,17 +286,13 @@ int32_t cipher_aesctr_crypt(cipher_t* c, key_derivation_t* kd, u_int8_t* in, u_i
return -1;
}
- cipher_aesctr_param_t* params = c->params_;
- if(!params->ctr_.buf_) {
- log_printf(ERR, "cipher not initialized");
- return -1;
- }
-
if(!kd) {
log_printf(ERR, "no key derivation supplied");
return -1;
}
+ cipher_aesctr_param_t* params = c->params_;
+
int ret = key_derivation_generate(kd, LABEL_SATP_ENCRYPTION, seq_nr, c->key_.buf_, c->key_.length_);
if(ret < 0)
return ret;
@@ -381,7 +318,7 @@ int32_t cipher_aesctr_crypt(cipher_t* c, key_derivation_t* kd, u_int8_t* in, u_i
log_printf(ERR, "failed to calculate cipher CTR");
return ret;
}
- err = gcry_cipher_setctr(params->handle_, params->ctr_.buf_, params->ctr_.length_);
+ err = gcry_cipher_setctr(params->handle_, params->ctr_.buf_, C_AESCTR_CTR_LENGTH);
if(err) {
log_printf(ERR, "failed to set cipher CTR: %s/%s", gcry_strerror(err), gcry_strsource(err));
diff --git a/src/cipher.h b/src/cipher.h
index 3e172a2..2cf45d6 100644
--- a/src/cipher.h
+++ b/src/cipher.h
@@ -65,45 +65,30 @@ int32_t cipher_null_crypt(u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t
#ifndef NO_CRYPT
-#define C_AES_DEFAULT_KEY_LENGTH 128
-#define C_AES_CTR_LENGTH 16
-#define C_AES_CTR_ZERO_LENGTH 2
-#ifdef NO_LIBGMP
-union __attribute__ ((__packed__)) cipher_aesctr_ctr_buf_union {
- u_int8_t buf_[C_AES_CTR_LENGTH];
+#define C_AESCTR_DEFAULT_KEY_LENGTH 128
+#define C_AESCTR_CTR_LENGTH 16
+#define C_AESCTR_SALT_LENGTH 14
+
+union __attribute__((__packed__)) cipher_aesctr_ctr_union {
+ u_int8_t buf_[C_AESCTR_CTR_LENGTH];
struct __attribute__ ((__packed__)) {
- u_int8_t buf_[C_AES_CTR_LENGTH - C_AES_CTR_ZERO_LENGTH];
- u_int8_t zero_[C_AES_CTR_ZERO_LENGTH];
+ u_int8_t buf_[C_AESCTR_SALT_LENGTH];
+ u_int16_t zero_;
} salt_;
- struct __attribute__ ((__packed__)) {
- u_int8_t fill_[C_AES_CTR_LENGTH - sizeof(mux_t) - sizeof(sender_id_t) - 2 - sizeof(seq_nr_t) - C_AES_CTR_ZERO_LENGTH];
+ struct __attribute__((__packed__)) {
+ u_int8_t fill_[C_AESCTR_SALT_LENGTH - sizeof(mux_t) - sizeof(sender_id_t) - 2 - sizeof(seq_nr_t)];
mux_t mux_;
sender_id_t sender_id_;
u_int8_t empty_[2];
seq_nr_t seq_nr_;
- u_int8_t zero_[C_AES_CTR_ZERO_LENGTH];
+ u_int16_t zero_;
} params_;
};
-typedef union cipher_aesctr_ctr_buf_union cipher_aesctr_ctr_buf_t;
-
-struct cipher_aesctr_ctr_struct {
- u_int32_t length_;
- u_int8_t* buf_;
- cipher_aesctr_ctr_buf_t ctr_;
-};
-typedef struct cipher_aesctr_ctr_struct cipher_aesctr_ctr_t;
-#endif
+typedef union cipher_aesctr_ctr_union cipher_aesctr_ctr_t;
struct cipher_aesctr_param_struct {
gcry_cipher_hd_t handle_;
-#ifndef NO_LIBGMP
- buffer_t ctr_;
- mpz_t mp_ctr;
- mpz_t mp_sid_mux;
- mpz_t mp_seq;
-#else
cipher_aesctr_ctr_t ctr_;
-#endif
};
typedef struct cipher_aesctr_param_struct cipher_aesctr_param_t;
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));
diff --git a/src/key_derivation.h b/src/key_derivation.h
index 1b8cd40..e95c276 100644
--- a/src/key_derivation.h
+++ b/src/key_derivation.h
@@ -36,9 +36,6 @@
#define _KEY_DERIVATION_H_
#include <gcrypt.h>
-#ifndef NO_LIBGMP
-#include <gmp.h>
-#endif
#define KD_LABEL_COUNT 3
enum satp_prf_label_enum {
@@ -75,51 +72,37 @@ 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];
+#define KD_AESCTR_CTR_LENGTH 16
+#define KD_AESCTR_SALT_LENGTH 14
+
+union __attribute__((__packed__)) key_derivation_aesctr_ctr_union {
+ u_int8_t buf_[KD_AESCTR_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];
+ u_int8_t buf_[KD_AESCTR_SALT_LENGTH];
+ u_int16_t zero_;
} 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];
+ struct __attribute__((__packed__)) {
+ u_int8_t fill_[KD_AESCTR_SALT_LENGTH - sizeof(u_int8_t) - sizeof(seq_nr_t)];
u_int8_t label_;
seq_nr_t r_;
- u_int8_t zero_[KD_AES_CTR_ZERO_LENGTH];
+ u_int16_t zero_;
} 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];
+ struct __attribute__((__packed__)) {
+ u_int8_t fill_[KD_AESCTR_SALT_LENGTH - sizeof(u_int8_t) - 2 - sizeof(seq_nr_t)];
u_int8_t label_;
u_int8_t r_fill_[2];
seq_nr_t r_;
- u_int8_t zero_[KD_AES_CTR_ZERO_LENGTH];
+ u_int16_t zero_;
} 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
+typedef union key_derivation_aesctr_ctr_union key_derivation_aesctr_ctr_t;
struct key_derivation_aesctr_param_struct {
gcry_cipher_hd_t handle_;
-#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;