From 055ea477a65a6f805731f0c7ee93c764703e9024 Mon Sep 17 00:00:00 2001 From: Heiko Reese Date: Sat, 7 Sep 2019 10:57:51 +0200 Subject: [PATCH] Fixed calls to be compatible with newer openssl versions --- include/nfc3d/drbg.h | 2 +- src/amiibo.c | 40 ++++++++++++++++++++-------------------- src/drbg.c | 12 ++++++------ src/util.c | 12 ++++++------ 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/include/nfc3d/drbg.h b/include/nfc3d/drbg.h index da1c082..4d81510 100644 --- a/include/nfc3d/drbg.h +++ b/include/nfc3d/drbg.h @@ -31,7 +31,7 @@ #define NFC3D_DRBG_OUTPUT_SIZE 32 /* Every iteration generates 32 bytes */ typedef struct { - HMAC_CTX hmacCtx; + HMAC_CTX *hmacCtx; bool used; uint16_t iteration; diff --git a/src/amiibo.c b/src/amiibo.c index 48c82f7..7c29691 100644 --- a/src/amiibo.c +++ b/src/amiibo.c @@ -89,21 +89,21 @@ bool nfc3d_amiibo_unpack(const nfc3d_amiibo_keys * amiiboKeys, const uint8_t * t nfc3d_amiibo_cipher(&dataKeys, internal, plain); // Init OpenSSL HMAC context - HMAC_CTX hmacCtx; - HMAC_CTX_init(&hmacCtx); + HMAC_CTX* hmacCtx = HMAC_CTX_new(); + HMAC_CTX_reset(hmacCtx); // Regenerate tag HMAC. Note: order matters, data HMAC depends on tag HMAC! - HMAC_Init_ex(&hmacCtx, tagKeys.hmacKey, sizeof(tagKeys.hmacKey), EVP_sha256(), NULL); - HMAC_Update(&hmacCtx, plain + 0x1D4, 0x34); - HMAC_Final(&hmacCtx, plain + HMAC_POS_TAG, NULL); + HMAC_Init_ex(hmacCtx, tagKeys.hmacKey, sizeof(tagKeys.hmacKey), EVP_sha256(), NULL); + HMAC_Update(hmacCtx, plain + 0x1D4, 0x34); + HMAC_Final(hmacCtx, plain + HMAC_POS_TAG, NULL); // Regenerate data HMAC - HMAC_Init_ex(&hmacCtx, dataKeys.hmacKey, sizeof(dataKeys.hmacKey), EVP_sha256(), NULL); - HMAC_Update(&hmacCtx, plain + 0x029, 0x1DF); - HMAC_Final(&hmacCtx, plain + HMAC_POS_DATA, NULL); + HMAC_Init_ex(hmacCtx, dataKeys.hmacKey, sizeof(dataKeys.hmacKey), EVP_sha256(), NULL); + HMAC_Update(hmacCtx, plain + 0x029, 0x1DF); + HMAC_Final(hmacCtx, plain + HMAC_POS_DATA, NULL); // HMAC cleanup - HMAC_CTX_cleanup(&hmacCtx); + HMAC_CTX_free(hmacCtx); return memcmp(plain + HMAC_POS_DATA, internal + HMAC_POS_DATA, 32) == 0 && @@ -120,23 +120,23 @@ void nfc3d_amiibo_pack(const nfc3d_amiibo_keys * amiiboKeys, const uint8_t * pla nfc3d_amiibo_keygen(&amiiboKeys->data, plain, &dataKeys); // Init OpenSSL HMAC context - HMAC_CTX hmacCtx; - HMAC_CTX_init(&hmacCtx); + HMAC_CTX* hmacCtx = HMAC_CTX_new();; + HMAC_CTX_reset(hmacCtx); // Generate tag HMAC - HMAC_Init_ex(&hmacCtx, tagKeys.hmacKey, sizeof(tagKeys.hmacKey), EVP_sha256(), NULL); - HMAC_Update(&hmacCtx, plain + 0x1D4, 0x34); - HMAC_Final(&hmacCtx, cipher + HMAC_POS_TAG, NULL); + HMAC_Init_ex(hmacCtx, tagKeys.hmacKey, sizeof(tagKeys.hmacKey), EVP_sha256(), NULL); + HMAC_Update(hmacCtx, plain + 0x1D4, 0x34); + HMAC_Final(hmacCtx, cipher + HMAC_POS_TAG, NULL); // Generate data HMAC - HMAC_Init_ex(&hmacCtx, dataKeys.hmacKey, sizeof(dataKeys.hmacKey), EVP_sha256(), NULL); - HMAC_Update(&hmacCtx, plain + 0x029, 0x18B); // Data - HMAC_Update(&hmacCtx, cipher + HMAC_POS_TAG, 0x20); // Tag HMAC - HMAC_Update(&hmacCtx, plain + 0x1D4, 0x34); // Here be dragons - HMAC_Final(&hmacCtx, cipher + HMAC_POS_DATA, NULL); + HMAC_Init_ex(hmacCtx, dataKeys.hmacKey, sizeof(dataKeys.hmacKey), EVP_sha256(), NULL); + HMAC_Update(hmacCtx, plain + 0x029, 0x18B); // Data + HMAC_Update(hmacCtx, cipher + HMAC_POS_TAG, 0x20); // Tag HMAC + HMAC_Update(hmacCtx, plain + 0x1D4, 0x34); // Here be dragons + HMAC_Final(hmacCtx, cipher + HMAC_POS_DATA, NULL); // HMAC cleanup - HMAC_CTX_cleanup(&hmacCtx); + HMAC_CTX_free(hmacCtx); // Encrypt nfc3d_amiibo_cipher(&dataKeys, plain, cipher); diff --git a/src/drbg.c b/src/drbg.c index 568e2c9..8c03630 100644 --- a/src/drbg.c +++ b/src/drbg.c @@ -40,8 +40,8 @@ void nfc3d_drbg_init(nfc3d_drbg_ctx * ctx, const uint8_t * hmacKey, size_t hmacK memcpy(ctx->buffer + sizeof(uint16_t), seed, seedSize); // Initialize underlying HMAC context - HMAC_CTX_init(&ctx->hmacCtx); - HMAC_Init_ex(&ctx->hmacCtx, hmacKey, hmacKeySize, EVP_sha256(), NULL); + ctx->hmacCtx = HMAC_CTX_new(); + HMAC_Init_ex(ctx->hmacCtx, hmacKey, hmacKeySize, EVP_sha256(), NULL); } void nfc3d_drbg_step(nfc3d_drbg_ctx * ctx, uint8_t * output) { @@ -50,7 +50,7 @@ void nfc3d_drbg_step(nfc3d_drbg_ctx * ctx, uint8_t * output) { if (ctx->used) { // If used at least once, reinitialize the HMAC - HMAC_Init_ex(&ctx->hmacCtx, NULL, 0, NULL, NULL); + HMAC_Init_ex(ctx->hmacCtx, NULL, 0, NULL, NULL); } else { ctx->used = true; } @@ -61,13 +61,13 @@ void nfc3d_drbg_step(nfc3d_drbg_ctx * ctx, uint8_t * output) { ctx->iteration++; // Do HMAC magic - HMAC_Update(&ctx->hmacCtx, ctx->buffer, ctx->bufferSize); - HMAC_Final(&ctx->hmacCtx, output, NULL); + HMAC_Update(ctx->hmacCtx, ctx->buffer, ctx->bufferSize); + HMAC_Final(ctx->hmacCtx, output, NULL); } void nfc3d_drbg_cleanup(nfc3d_drbg_ctx * ctx) { assert(ctx != NULL); - HMAC_CTX_cleanup(&ctx->hmacCtx); + HMAC_CTX_free(ctx->hmacCtx); } void nfc3d_drbg_generate_bytes(const uint8_t * hmacKey, size_t hmacKeySize, const uint8_t * seed, size_t seedSize, uint8_t * output, size_t outputSize) { diff --git a/src/util.c b/src/util.c index b9f1cb7..83ef011 100644 --- a/src/util.c +++ b/src/util.c @@ -26,14 +26,14 @@ #include void aes128ctr(const uint8_t * in, uint8_t * out, size_t size, const uint8_t * key, const uint8_t * iv) { - EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); int pos; - EVP_CIPHER_CTX_init(&ctx); - EVP_EncryptInit_ex(&ctx, EVP_aes_128_ctr(), NULL, key, iv); - EVP_EncryptUpdate(&ctx, out, &pos, in, size); - EVP_EncryptFinal_ex(&ctx, out + pos, &pos); - EVP_CIPHER_CTX_cleanup(&ctx); + EVP_CIPHER_CTX_init(ctx); + EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv); + EVP_EncryptUpdate(ctx, out, &pos, in, size); + EVP_EncryptFinal_ex(ctx, out + pos, &pos); + EVP_CIPHER_CTX_cleanup(ctx); } void sha256hmac(const uint8_t * key, size_t keySize, const uint8_t * in, size_t inSize, uint8_t * out) {