Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nfc3d/drbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
40 changes: 20 additions & 20 deletions src/amiibo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#include <stdint.h>

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) {
Expand Down