diff --git a/client/cmdsmartcard.c b/client/cmdsmartcard.c index 9bbf1412b..f00fc0907 100644 --- a/client/cmdsmartcard.c +++ b/client/cmdsmartcard.c @@ -196,8 +196,8 @@ static int PrintATR(uint8_t *atr, size_t atrlen) { uint8_t T0 = atr[1]; uint8_t K = T0 & 0x0F; uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0; - bool protocol_T0_present = true; - bool protocol_T15_present = false; + bool protocol_T0_present = true; + bool protocol_T15_present = false; if (T0 & 0x10) { PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]); @@ -217,14 +217,14 @@ static int PrintATR(uint8_t *atr, size_t atrlen) { if (T0 & 0x80) { TD1 = atr[2 + T1len]; PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f); - protocol_T0_present = false; - if ((TD1 & 0x0f) == 0) { - protocol_T0_present = true; - } - if ((TD1 & 0x0f) == 15) { - protocol_T15_present = true; - } - + protocol_T0_present = false; + if ((TD1 & 0x0f) == 0) { + protocol_T0_present = true; + } + if ((TD1 & 0x0f) == 15) { + protocol_T15_present = true; + } + T1len++; if (TD1 & 0x10) { @@ -242,12 +242,12 @@ static int PrintATR(uint8_t *atr, size_t atrlen) { if (TD1 & 0x80) { uint8_t TDi = atr[2 + T1len + TD1len]; PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f); - if ((TDi & 0x0f) == 0) { - protocol_T0_present = true; - } - if ((TDi & 0x0f) == 15) { - protocol_T15_present = true; - } + if ((TDi & 0x0f) == 0) { + protocol_T0_present = true; + } + if ((TDi & 0x0f) == 15) { + protocol_T15_present = true; + } TD1len++; bool nextCycle = true; @@ -278,16 +278,16 @@ static int PrintATR(uint8_t *atr, size_t atrlen) { } } - if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK - uint8_t vxor = 0; - for (int i = 1; i < atrlen; i++) - vxor ^= atr[i]; + if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK + uint8_t vxor = 0; + for (int i = 1; i < atrlen; i++) + vxor ^= atr[i]; - if (vxor) - PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor); - else - PrintAndLogEx(INFO, "Check sum OK."); - } + if (vxor) + PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor); + else + PrintAndLogEx(INFO, "Check sum OK."); + } if (atr[0] != 0x3b) PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]); @@ -580,7 +580,7 @@ int CmdSmartUpgrade(const char *Cmd) { PrintAndLogEx(WARNING, "WARNING - Sim module firmware upgrade."); PrintAndLogEx(WARNING, "A dangerous command, do wrong and you could brick the sim module"); - PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(NORMAL, ""); FILE *f; char filename[FILE_PATH_SIZE] = {0}; @@ -611,30 +611,30 @@ int CmdSmartUpgrade(const char *Cmd) { if (errors || cmdp == 0) return usage_sm_upgrade(); - char sha512filename[FILE_PATH_SIZE] = {'\0'}; - char *bin_extension = filename; - char *dot_position = NULL; - while ((dot_position = strchr(bin_extension, '.')) != NULL) { - bin_extension = dot_position + 1; - } - - if (!strcmp(bin_extension, "BIN") + char sha512filename[FILE_PATH_SIZE] = {'\0'}; + char *bin_extension = filename; + char *dot_position = NULL; + while ((dot_position = strchr(bin_extension, '.')) != NULL) { + bin_extension = dot_position + 1; + } + + if (!strcmp(bin_extension, "BIN") #ifdef _WIN32 - || !strcmp(bin_extension, "bin") + || !strcmp(bin_extension, "bin") #endif - ) { - memcpy(sha512filename, filename, strlen(filename) - strlen("bin")); - strcat(sha512filename, "sha512.txt"); - } else { - PrintAndLogEx(FAILED, "Filename extension of firmware upgrade file must be .BIN"); - return 1; - } - + ) { + memcpy(sha512filename, filename, strlen(filename) - strlen("bin")); + strcat(sha512filename, "sha512.txt"); + } else { + PrintAndLogEx(FAILED, "Filename extension of firmware upgrade file must be .BIN"); + return 1; + } + PrintAndLogEx(INFO, "firmware file : " _YELLOW_("%s"), filename); - PrintAndLogEx(INFO, "Checking integrity : " _YELLOW_("%s"), sha512filename); - - // load firmware file - f = fopen(filename, "rb"); + PrintAndLogEx(INFO, "Checking integrity : " _YELLOW_("%s"), sha512filename); + + // load firmware file + f = fopen(filename, "rb"); if (!f) { PrintAndLogEx(FAILED, "Firmware file " _YELLOW_("%s") " not found or locked.", filename); return 1; @@ -658,64 +658,64 @@ int CmdSmartUpgrade(const char *Cmd) { return 1; } - size_t firmware_size = fread(dump, 1, fsize, f); + size_t firmware_size = fread(dump, 1, fsize, f); if (f) fclose(f); - // load sha512 file - f = fopen(sha512filename, "rb"); - if ( !f ){ - PrintAndLogEx(FAILED, "SHA-512 file not found or locked."); - return 1; - } + // load sha512 file + f = fopen(sha512filename, "rb"); + if (!f) { + PrintAndLogEx(FAILED, "SHA-512 file not found or locked."); + return 1; + } - // get filesize in order to malloc memory - fseek(f, 0, SEEK_END); - fsize = ftell(f); - fseek(f, 0, SEEK_SET); + // get filesize in order to malloc memory + fseek(f, 0, SEEK_END); + fsize = ftell(f); + fseek(f, 0, SEEK_SET); - if (fsize < 0) { - PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file"); - fclose(f); - return 1; - } - - if (fsize < 128) { - PrintAndLogEx(FAILED, "SHA-512 file too short"); - fclose(f); - return 1; - } + if (fsize < 0) { + PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file"); + fclose(f); + return 1; + } - char hashstring[129]; - size_t bytes_read = fread(hashstring, 1, 128, f); - hashstring[128] = '\0'; + if (fsize < 128) { + PrintAndLogEx(FAILED, "SHA-512 file too short"); + fclose(f); + return 1; + } - if (f) - fclose(f); + char hashstring[129]; + size_t bytes_read = fread(hashstring, 1, 128, f); + hashstring[128] = '\0'; - uint8_t hash1[64]; - if (bytes_read != 128 || param_gethex(hashstring, 0, hash1, 128)) { - PrintAndLogEx(FAILED, "Couldn't read SHA-512 file"); - return 1; - } - - uint8_t hash2[64]; - if (sha512hash(dump, firmware_size, hash2)) { - PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware"); - return 1; - } + if (f) + fclose(f); - if (memcmp(hash1, hash2, 64)) { - PrintAndLogEx(FAILED, "Couldn't verify integrity of firmware file " _RED_("(wrong SHA-512 hash)") ); - return 1; - } - - PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3"); + uint8_t hash1[64]; + if (bytes_read != 128 || param_gethex(hashstring, 0, hash1, 128)) { + PrintAndLogEx(FAILED, "Couldn't read SHA-512 file"); + return 1; + } + + uint8_t hash2[64]; + if (sha512hash(dump, firmware_size, hash2)) { + PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware"); + return 1; + } + + if (memcmp(hash1, hash2, 64)) { + PrintAndLogEx(FAILED, "Couldn't verify integrity of firmware file " _RED_("(wrong SHA-512 hash)")); + return 1; + } + + PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3"); //Send to device uint32_t index = 0; uint32_t bytes_sent = 0; - uint32_t bytes_remaining = firmware_size; + uint32_t bytes_remaining = firmware_size; while (bytes_remaining > 0) { uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining); @@ -742,7 +742,7 @@ int CmdSmartUpgrade(const char *Cmd) { PrintAndLogEx(SUCCESS, "Sim module firmware updating, don\'t turn off your PM3!"); // trigger the firmware upgrade - UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}}; + UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}}; clearCommandBuffer(); SendCommand(&c); UsbCommand resp; @@ -751,10 +751,10 @@ int CmdSmartUpgrade(const char *Cmd) { return 1; } if ((resp.arg[0] & 0xFF)) { - PrintAndLogEx(SUCCESS, "Sim module firmware upgrade " _GREEN_("successful") ); - PrintAndLogEx(SUCCESS, "\n run " _YELLOW_("`hw status`") " to validate the fw version " ); + PrintAndLogEx(SUCCESS, "Sim module firmware upgrade " _GREEN_("successful")); + PrintAndLogEx(SUCCESS, "\n run " _YELLOW_("`hw status`") " to validate the fw version "); } else { - PrintAndLogEx(FAILED, "Sim module firmware upgrade " _RED_("failed") ); + PrintAndLogEx(FAILED, "Sim module firmware upgrade " _RED_("failed")); } return 0; } diff --git a/client/crypto/libpcrypto.c b/client/crypto/libpcrypto.c index d3335487e..095d16af3 100644 --- a/client/crypto/libpcrypto.c +++ b/client/crypto/libpcrypto.c @@ -103,20 +103,20 @@ int sha256hash(uint8_t *input, int length, uint8_t *hash) { mbedtls_sha256_update(&sctx, input, length); mbedtls_sha256_finish(&sctx, hash); mbedtls_sha256_free(&sctx); - - return 0; + + return 0; } int sha512hash(uint8_t *input, int length, uint8_t *hash) { - if (!hash || !input) - return 1; - - mbedtls_sha512_context sctx; - mbedtls_sha512_init(&sctx); - mbedtls_sha512_starts(&sctx, 0); //SHA-512, not 384 - mbedtls_sha512_update(&sctx, input, length); - mbedtls_sha512_finish(&sctx, hash); - mbedtls_sha512_free(&sctx); + if (!hash || !input) + return 1; + + mbedtls_sha512_context sctx; + mbedtls_sha512_init(&sctx); + mbedtls_sha512_starts(&sctx, 0); //SHA-512, not 384 + mbedtls_sha512_update(&sctx, input, length); + mbedtls_sha512_finish(&sctx, hash); + mbedtls_sha512_free(&sctx); return 0; }