1717#include < inttypes.h>
1818#include < M5Utility.hpp>
1919#include < algorithm>
20- #include < mbedtls/aes.h>
2120#include < esp_random.h>
2221#include < cstring>
2322
@@ -30,6 +29,8 @@ using namespace m5::nfc::ndef;
3029
3130namespace {
3231
32+ using m5::nfc::crypto::aes_cbc_crypt;
33+
3334constexpr char dump_sector_header[] =
3435 " Sec[Blk]:00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F [Access]\n "
3536 " -----------------------------------------------------------------" ;
@@ -2249,21 +2250,11 @@ bool NFCLayerA::mifare_plus_authenticateAES(const uint16_t key_no, const mifare:
22492250 uint8_t rndB[16 ]{};
22502251 {
22512252 uint8_t iv[16 ]{};
2252- mbedtls_aes_context aes{};
2253- mbedtls_aes_init (&aes);
2254- if (mbedtls_aes_setkey_dec (&aes, key.data (), 128 ) != 0 ) {
2255- M5_LIB_LOGE (" AuthAES setkey_dec failed" );
2256- mbedtls_aes_free (&aes);
2257- m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
2258- return false ;
2259- }
2260- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_DECRYPT , sizeof (rndB), iv, step1_payload, rndB) != 0 ) {
2253+ if (!aes_cbc_crypt (rndB, key.data (), iv, step1_payload, sizeof (rndB), false )) {
22612254 M5_LIB_LOGE (" AuthAES crypt_cbc failed" );
2262- mbedtls_aes_free (&aes);
22632255 m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
22642256 return false ;
22652257 }
2266- mbedtls_aes_free (&aes);
22672258 }
22682259
22692260 uint8_t rndA[16 ]{};
@@ -2283,27 +2274,14 @@ bool NFCLayerA::mifare_plus_authenticateAES(const uint16_t key_no, const mifare:
22832274 cmd2[0 ] = 0x72 ;
22842275 {
22852276 uint8_t iv[16 ]{};
2286- mbedtls_aes_context aes{};
2287- mbedtls_aes_init (&aes);
2288- if (mbedtls_aes_setkey_enc (&aes, key.data (), 128 ) != 0 ) {
2289- M5_LIB_LOGE (" AuthAES setkey_enc failed" );
2290- mbedtls_aes_free (&aes);
2291- m5::nfc::crypto::secure_zero (rndA, sizeof (rndA));
2292- m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
2293- m5::nfc::crypto::secure_zero (rndB_rot, sizeof (rndB_rot));
2294- m5::nfc::crypto::secure_zero (ab_plain, sizeof (ab_plain));
2295- return false ;
2296- }
2297- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_ENCRYPT , sizeof (ab_plain), iv, ab_plain, cmd2 + 1 ) != 0 ) {
2277+ if (!aes_cbc_crypt (cmd2 + 1 , key.data (), iv, ab_plain, sizeof (ab_plain), true )) {
22982278 M5_LIB_LOGE (" AuthAES crypt_cbc failed" );
2299- mbedtls_aes_free (&aes);
23002279 m5::nfc::crypto::secure_zero (rndA, sizeof (rndA));
23012280 m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
23022281 m5::nfc::crypto::secure_zero (rndB_rot, sizeof (rndB_rot));
23032282 m5::nfc::crypto::secure_zero (ab_plain, sizeof (ab_plain));
23042283 return false ;
23052284 }
2306- mbedtls_aes_free (&aes);
23072285 }
23082286
23092287 rx_len = sizeof (rx);
@@ -2343,29 +2321,15 @@ bool NFCLayerA::mifare_plus_authenticateAES(const uint16_t key_no, const mifare:
23432321 uint8_t ab_resp[32 ]{};
23442322 {
23452323 uint8_t iv[16 ]{};
2346- mbedtls_aes_context aes{};
2347- mbedtls_aes_init (&aes);
2348- if (mbedtls_aes_setkey_dec (&aes, key.data (), 128 ) != 0 ) {
2349- M5_LIB_LOGE (" AuthAES setkey_dec failed" );
2350- mbedtls_aes_free (&aes);
2351- m5::nfc::crypto::secure_zero (rndA, sizeof (rndA));
2352- m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
2353- m5::nfc::crypto::secure_zero (rndB_rot, sizeof (rndB_rot));
2354- m5::nfc::crypto::secure_zero (ab_plain, sizeof (ab_plain));
2355- m5::nfc::crypto::secure_zero (ab_resp, sizeof (ab_resp));
2356- return false ;
2357- }
2358- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_DECRYPT , sizeof (ab_resp), iv, step2_payload, ab_resp) != 0 ) {
2324+ if (!aes_cbc_crypt (ab_resp, key.data (), iv, step2_payload, sizeof (ab_resp), false )) {
23592325 M5_LIB_LOGE (" AuthAES crypt_cbc failed" );
2360- mbedtls_aes_free (&aes);
23612326 m5::nfc::crypto::secure_zero (rndA, sizeof (rndA));
23622327 m5::nfc::crypto::secure_zero (rndB, sizeof (rndB));
23632328 m5::nfc::crypto::secure_zero (rndB_rot, sizeof (rndB_rot));
23642329 m5::nfc::crypto::secure_zero (ab_plain, sizeof (ab_plain));
23652330 m5::nfc::crypto::secure_zero (ab_resp, sizeof (ab_resp));
23662331 return false ;
23672332 }
2368- mbedtls_aes_free (&aes);
23692333 }
23702334
23712335 uint8_t rndA_rot[16 ]{};
@@ -2399,31 +2363,19 @@ bool NFCLayerA::mifare_plus_authenticateAES(const uint16_t key_no, const mifare:
23992363
24002364 {
24012365 uint8_t iv[16 ]{};
2402- mbedtls_aes_context aes{};
2403- mbedtls_aes_init (&aes);
2404- if (mbedtls_aes_setkey_enc (&aes, key.data (), 128 ) != 0 ) {
2405- M5_LIB_LOGE (" AuthAES setkey_enc failed" );
2406- mbedtls_aes_free (&aes);
2407- m5::nfc::crypto::secure_zero (kenc, sizeof (kenc));
2408- m5::nfc::crypto::secure_zero (kmac, sizeof (kmac));
2409- return false ;
2410- }
2411- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_ENCRYPT , sizeof (kenc), iv, kenc, kenc) != 0 ) {
2366+ if (!aes_cbc_crypt (kenc, key.data (), iv, kenc, sizeof (kenc), true )) {
24122367 M5_LIB_LOGE (" AuthAES crypt_cbc failed" );
2413- mbedtls_aes_free (&aes);
24142368 m5::nfc::crypto::secure_zero (kenc, sizeof (kenc));
24152369 m5::nfc::crypto::secure_zero (kmac, sizeof (kmac));
24162370 return false ;
24172371 }
24182372 memset (iv, 0 , sizeof (iv));
2419- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_ENCRYPT , sizeof (kmac ), iv, kmac, kmac) != 0 ) {
2373+ if (! aes_cbc_crypt (kmac, key. data ( ), iv, kmac, sizeof ( kmac), true ) ) {
24202374 M5_LIB_LOGE (" AuthAES crypt_cbc failed" );
2421- mbedtls_aes_free (&aes);
24222375 m5::nfc::crypto::secure_zero (kenc, sizeof (kenc));
24232376 m5::nfc::crypto::secure_zero (kmac, sizeof (kmac));
24242377 return false ;
24252378 }
2426- mbedtls_aes_free (&aes);
24272379 }
24282380
24292381 _mfp_session.authenticated = true ;
@@ -2564,13 +2516,6 @@ bool NFCLayerA::mifare_plus_read_plain_mac(const uint16_t block, const uint8_t c
25642516
25652517 std::vector<uint8_t > payload (rx + 1 , rx + 1 + data_len);
25662518 if (!plain) {
2567- mbedtls_aes_context aes{};
2568- mbedtls_aes_init (&aes);
2569- if (mbedtls_aes_setkey_dec (&aes, _mfp_session.kenc .data (), 128 ) != 0 ) {
2570- M5_LIB_LOGE (" AES setkey_dec failed" );
2571- mbedtls_aes_free (&aes);
2572- return false ;
2573- }
25742519 for (uint8_t i = 0 ; i < count; ++i) {
25752520 uint8_t iv[16 ]{};
25762521 const uint8_t ctr = (uint8_t )(r_ctr & 0xFF );
@@ -2579,13 +2524,11 @@ bool NFCLayerA::mifare_plus_read_plain_mac(const uint16_t block, const uint8_t c
25792524 iv[8 ] = ctr;
25802525 memcpy (&iv[12 ], _mfp_session.ti .data (), 4 );
25812526 uint8_t * blk = payload.data () + i * 16 ;
2582- if (mbedtls_aes_crypt_cbc (&aes, MBEDTLS_AES_DECRYPT , 16 , iv, blk, blk) != 0 ) {
2527+ if (! aes_cbc_crypt (blk, _mfp_session. kenc . data (), iv, blk, 16 , false ) ) {
25832528 M5_LIB_LOGE (" AES crypt_cbc failed" );
2584- mbedtls_aes_free (&aes);
25852529 return false ;
25862530 }
25872531 }
2588- mbedtls_aes_free (&aes);
25892532 }
25902533
25912534 out.insert (out.end (), payload.begin (), payload.end ());
0 commit comments