diff --git a/lib/include/chiaki/ecdh.h b/lib/include/chiaki/ecdh.h index fed92c4..0185c09 100644 --- a/lib/include/chiaki/ecdh.h +++ b/lib/include/chiaki/ecdh.h @@ -27,7 +27,7 @@ extern "C" { #endif -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS #include "mbedtls/ecdh.h" #include "mbedtls/ctr_drbg.h" #endif @@ -38,9 +38,9 @@ extern "C" { typedef struct chiaki_ecdh_t { // the following lines may lead to memory corruption -// __SWITCH__ or CHIAKI_LIB_ENABLE_MBEDTLS must be defined +// CHIAKI_LIB_ENABLE_MBEDTLS must be defined // globally (whole project) -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS // mbedtls ecdh context mbedtls_ecdh_context ctx; // deterministic random bit generator diff --git a/lib/src/ecdh.c b/lib/src/ecdh.c index 056d69d..8549a97 100644 --- a/lib/src/ecdh.c +++ b/lib/src/ecdh.c @@ -19,7 +19,7 @@ #include #include -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS #include "mbedtls/entropy.h" #include "mbedtls/md.h" #else @@ -38,7 +38,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_init(ChiakiECDH *ecdh) { memset(ecdh, 0, sizeof(ChiakiECDH)); -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS #define CHECK(err) if((err) != 0) { \ chiaki_ecdh_fini(ecdh); \ return CHIAKI_ERR_UNKNOWN; } @@ -85,7 +85,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_init(ChiakiECDH *ecdh) CHIAKI_EXPORT void chiaki_ecdh_fini(ChiakiECDH *ecdh) { -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS mbedtls_ecdh_free(&ecdh->ctx); mbedtls_ctr_drbg_free(&ecdh->drbg); #else @@ -97,7 +97,7 @@ CHIAKI_EXPORT void chiaki_ecdh_fini(ChiakiECDH *ecdh) CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_set_local_key(ChiakiECDH *ecdh, const uint8_t *private_key, size_t private_key_size, const uint8_t *public_key, size_t public_key_size) { -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS //https://tls.mbed.org/discussions/generic/publickey-binary-data-in-der // Load keys from buffers (i.e: config file) // TODO test @@ -166,7 +166,7 @@ error_priv: CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_get_local_pub_key(ChiakiECDH *ecdh, uint8_t *key_out, size_t *key_out_size, const uint8_t *handshake_key, uint8_t *sig_out, size_t *sig_out_size) { -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS mbedtls_md_context_t ctx; mbedtls_md_init(&ctx); @@ -213,7 +213,7 @@ error: CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *secret_out, const uint8_t *remote_key, size_t remote_key_size, const uint8_t *handshake_key, const uint8_t *remote_sig, size_t remote_sig_size) { //compute DH shared key -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS // https://github.com/ARMmbed/mbedtls/blob/development/programs/pkey/ecdh_curve25519.c#L151 #define GOTO_ERROR(err) do { \ if((err) !=0){ \ diff --git a/lib/src/gkcrypt.c b/lib/src/gkcrypt.c index 50678ea..0a2bc15 100644 --- a/lib/src/gkcrypt.c +++ b/lib/src/gkcrypt.c @@ -21,7 +21,7 @@ #include #include -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS #include "mbedtls/aes.h" #include "mbedtls/md.h" #include "mbedtls/gcm.h" @@ -138,7 +138,7 @@ static ChiakiErrorCode gkcrypt_gen_key_iv(ChiakiGKCrypt *gkcrypt, uint8_t index, uint8_t hmac[CHIAKI_GKCRYPT_BLOCK_SIZE*2]; size_t hmac_size = sizeof(hmac); - #if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) + #ifdef CHIAKI_LIB_ENABLE_MBEDTLS mbedtls_md_context_t ctx; mbedtls_md_init(&ctx); @@ -198,7 +198,7 @@ CHIAKI_EXPORT void chiaki_gkcrypt_gen_gmac_key(uint64_t index, const uint8_t *ke memcpy(data, key_base, 0x10); counter_add(data + 0x10, iv, index * CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_IV_OFFSET); uint8_t md[0x20]; -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS // last param // is224 Determines which function to use. // This must be either 0 for SHA-256, or 1 for SHA-224. @@ -230,7 +230,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcry assert(key_pos % CHIAKI_GKCRYPT_BLOCK_SIZE == 0); assert(buf_size % CHIAKI_GKCRYPT_BLOCK_SIZE == 0); -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS // build mbedtls aes context mbedtls_aes_context ctx; mbedtls_aes_init(&ctx); @@ -262,7 +262,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcry for(uint8_t *cur = buf, *end = buf + buf_size; cur < end; cur += CHIAKI_GKCRYPT_BLOCK_SIZE) counter_add(cur, gkcrypt->iv, counter_offset++); -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS for(int i=0; i -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS //#include #include #include @@ -27,7 +27,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_random_bytes_crypt(uint8_t *buf, size_t buf_size) { -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS // mbedtls_havege_state hs; // mbedtls_havege_init(&hs); // int r = mbedtls_havege_random( &hs, buf, sizeof( buf ) ); diff --git a/lib/src/rpcrypt.c b/lib/src/rpcrypt.c index f2982b6..5659105 100644 --- a/lib/src/rpcrypt.c +++ b/lib/src/rpcrypt.c @@ -17,7 +17,7 @@ #include -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS #include "mbedtls/aes.h" #include "mbedtls/md.h" #else @@ -83,7 +83,7 @@ CHIAKI_EXPORT void chiaki_rpcrypt_init_regist(ChiakiRPCrypt *rpcrypt, const uint rpcrypt->bright[3] ^= (uint8_t)((pin >> 0x00) & 0xff); } -#if defined(__SWITCH__) || defined(CHIAKI_LIB_ENABLE_MBEDTLS) +#ifdef CHIAKI_LIB_ENABLE_MBEDTLS CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, uint8_t *iv, uint64_t counter) { uint8_t hmac_key[] = { 0xac, 0x07, 0x88, 0x83, 0xc8, 0x3a, 0x1f, 0xe8, 0x11, 0x46, 0x3a, 0xf3, 0x9e, 0xe3, 0xe3, 0x77 };