Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port cve-2021-34600 poc, fix device desfire aes crypto #1594

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix device desfire AES en-/decryption
This commit fixes `mifare_cypher_single_block()` when used with `T_AES`.
`mifare_cypher_single_block()` essentially re-implements CBC mode for
all used ciphers by XOR-ing the IV with the data either before
encryption or after decryption and using AES in ECB mode. However, for
AES encryption `mbedtls_aes_crypt_cbc()` was then called to perform the
en-/decryption operation, which then also XOR-ed the IV with the data,
all of which resulted in the wrong en-/decryption of the data. This is
fixed by replacing the call to `mbedtls_aes_crypt_cbc()` with a call to
`mbedtls_aes_crypt_ecb()`.
  • Loading branch information
y-x41 committed Feb 9, 2022
commit 611dbbc89231974a51c0404cf862344fd440b107
8 changes: 5 additions & 3 deletions armsrc/desfire_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ void *mifare_cryto_postprocess_data(desfiretag_t tag, void *data, size_t *nbytes
break;
}

free(edata);
// TODO it doesn't build with this uncommented
// desfire_crypto.c:(.text.mifare_cryto_postprocess_data+0x4c): undefined reference to `free'
//free(edata);

break;
case MDCM_ENCIPHERED:
Expand Down Expand Up @@ -811,13 +813,13 @@ void mifare_cypher_single_block(desfirekey_t key, uint8_t *data, uint8_t *ivect,
case MCO_ENCYPHER: {
mbedtls_aes_init(&actx);
mbedtls_aes_setkey_enc(&actx, key->data, 128);
mbedtls_aes_crypt_cbc(&actx, MBEDTLS_AES_ENCRYPT, sizeof(edata), ivect, data, edata);
mbedtls_aes_crypt_ecb(&actx, MBEDTLS_AES_ENCRYPT, data, edata);
break;
}
case MCO_DECYPHER: {
mbedtls_aes_init(&actx);
mbedtls_aes_setkey_dec(&actx, key->data, 128);
mbedtls_aes_crypt_cbc(&actx, MBEDTLS_AES_DECRYPT, sizeof(edata), ivect, edata, data);
mbedtls_aes_crypt_ecb(&actx, MBEDTLS_AES_DECRYPT, data, edata);
break;
}
}
Expand Down