less hardcoded sizes and more ARRAYLEN

This commit is contained in:
Philippe Teuwen 2019-07-31 23:44:53 +02:00
parent 74288ad128
commit f276dca3f1
16 changed files with 43 additions and 53 deletions

View file

@ -242,9 +242,7 @@ void RunMod() {
// French VIGIK system @2017
//----------------------------
#define STKEYS 37
const uint64_t mfKeys[STKEYS] = {
const uint64_t mfKeys[] = {
0xffffffffffff, // TRANSPORTS
0x000000000000, // Blankkey
0x484558414354, // INFINEONON A / 0F SEC B / INTRATONE / HEXACT...
@ -285,8 +283,8 @@ void RunMod() {
};
// Can remember something like that in case of Bigbuf
keyBlock = BigBuf_malloc(STKEYS * 6);
int mfKeysCnt = sizeof(mfKeys) / sizeof(uint64_t);
keyBlock = BigBuf_malloc(ARRAYLEN(mfKeys) * 6);
int mfKeysCnt = ARRAYLEN(mfKeys);
for (int mfKeyCounter = 0; mfKeyCounter < mfKeysCnt; mfKeyCounter++) {
num_to_bytes(mfKeys[mfKeyCounter], 6, (uint8_t *)(keyBlock + mfKeyCounter * 6));

View file

@ -262,7 +262,7 @@ void RunMod() {
keys in keyBlock's memory space .
*/
keyBlock = BigBuf_malloc(stKeyBlock * 6);
int mfKeysCnt = sizeof(mfKeys) / sizeof(uint64_t);
int mfKeysCnt = ARRAYLEN(mfKeys);
for (int mfKeyCounter = 0; mfKeyCounter < mfKeysCnt; mfKeyCounter++) {
num_to_bytes(mfKeys[mfKeyCounter], 6, (uint8_t *)(keyBlock + mfKeyCounter * 6));

View file

@ -139,14 +139,13 @@ static const manufactureName manufactureMapping[] = {
const char *getTagInfo(uint8_t uid) {
int i;
int len = sizeof(manufactureMapping) / sizeof(manufactureName);
for (i = 0; i < len; ++i)
for (i = 0; i < ARRAYLEN(manufactureMapping); ++i)
if (uid == manufactureMapping[i].uid)
return manufactureMapping[i].desc;
//No match, return default
return manufactureMapping[len - 1].desc;
return manufactureMapping[ARRAYLEN(manufactureMapping) - 1].desc;
}
// iso14a apdu input frame length

View file

@ -1126,7 +1126,7 @@ bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isRes
// check default keys
if (!traceCrypto1) {
for (int i = 0; i < MIFARE_DEFAULTKEYS_SIZE; i++) {
for (int i = 0; i < ARRAYLEN(g_mifare_default_keys); i++) {
if (NestedCheckKey(g_mifare_default_keys[i], &AuthData, cmd, cmdsize, parity)) {
PrintAndLogEx(NORMAL, " | | * |%61s %012"PRIx64"| |", "key", g_mifare_default_keys[i]);

View file

@ -1069,7 +1069,7 @@ static int CmdHF14AMfNested(const char *Cmd) {
uint8_t trgKeyType = 0;
uint8_t SectorsCnt = 0;
uint8_t key[6] = {0, 0, 0, 0, 0, 0};
uint8_t keyBlock[(MIFARE_DEFAULTKEYS_SIZE + 1) * 6];
uint8_t keyBlock[(ARRAYLEN(g_mifare_default_keys) + 1) * 6];
uint64_t key64 = 0;
bool transferToEml = false;
bool createDumpFile = false;
@ -1173,17 +1173,17 @@ static int CmdHF14AMfNested(const char *Cmd) {
//test current key and additional standard keys first
// add parameter key
memcpy(keyBlock + (MIFARE_DEFAULTKEYS_SIZE * 6), key, 6);
memcpy(keyBlock + (ARRAYLEN(g_mifare_default_keys) * 6), key, 6);
for (int cnt = 0; cnt < MIFARE_DEFAULTKEYS_SIZE; cnt++) {
for (int cnt = 0; cnt < ARRAYLEN(g_mifare_default_keys); cnt++) {
num_to_bytes(g_mifare_default_keys[cnt], 6, (uint8_t *)(keyBlock + cnt * 6));
}
PrintAndLogEx(SUCCESS, "Testing known keys. Sector count=%d", SectorsCnt);
mfCheckKeys_fast(SectorsCnt, true, true, 1, MIFARE_DEFAULTKEYS_SIZE + 1, keyBlock, e_sector, false);
mfCheckKeys_fast(SectorsCnt, true, true, 1, ARRAYLEN(g_mifare_default_keys) + 1, keyBlock, e_sector, false);
uint64_t t2 = msclock() - t1;
PrintAndLogEx(SUCCESS, "Time to check %d known keys: %.0f seconds\n", MIFARE_DEFAULTKEYS_SIZE, (float)t2 / 1000.0);
PrintAndLogEx(SUCCESS, "Time to check %d known keys: %.0f seconds\n", ARRAYLEN(g_mifare_default_keys), (float)t2 / 1000.0);
PrintAndLogEx(SUCCESS, "enter nested attack");
// nested sectors
@ -1554,15 +1554,15 @@ static int CmdHF14AMfChk_fast(const char *Cmd) {
int i, keycnt = 0;
int clen = 0;
int transferToEml = 0, createDumpFile = 0;
uint32_t keyitems = MIFARE_DEFAULTKEYS_SIZE;
uint32_t keyitems = ARRAYLEN(g_mifare_default_keys);
bool use_flashmemory = false;
sector_t *e_sector = NULL;
keyBlock = calloc(MIFARE_DEFAULTKEYS_SIZE, 6);
keyBlock = calloc(ARRAYLEN(g_mifare_default_keys), 6);
if (keyBlock == NULL) return 1;
for (int cnt = 0; cnt < MIFARE_DEFAULTKEYS_SIZE; cnt++)
for (int cnt = 0; cnt < ARRAYLEN(g_mifare_default_keys); cnt++)
num_to_bytes(g_mifare_default_keys[cnt], 6, keyBlock + cnt * 6);
// sectors
@ -1661,7 +1661,7 @@ static int CmdHF14AMfChk_fast(const char *Cmd) {
if (keycnt == 0 && !use_flashmemory) {
PrintAndLogEx(SUCCESS, "No key specified, trying default keys");
for (; keycnt < MIFARE_DEFAULTKEYS_SIZE; keycnt++)
for (; keycnt < ARRAYLEN(g_mifare_default_keys); keycnt++)
PrintAndLogEx(NORMAL, "[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
(keyBlock + 6 * keycnt)[0], (keyBlock + 6 * keycnt)[1], (keyBlock + 6 * keycnt)[2],
(keyBlock + 6 * keycnt)[3], (keyBlock + 6 * keycnt)[4], (keyBlock + 6 * keycnt)[5]);
@ -1806,7 +1806,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
uint8_t blockNo = 0;
uint8_t SectorsCnt = 1;
uint8_t keyType = 0;
uint32_t keyitems = MIFARE_DEFAULTKEYS_SIZE;
uint32_t keyitems = ARRAYLEN(g_mifare_default_keys);
uint64_t key64 = 0;
uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
char *fptr;
@ -1815,10 +1815,10 @@ static int CmdHF14AMfChk(const char *Cmd) {
int createDumpFile = 0;
int i, keycnt = 0;
keyBlock = calloc(MIFARE_DEFAULTKEYS_SIZE, 6);
keyBlock = calloc(ARRAYLEN(g_mifare_default_keys), 6);
if (keyBlock == NULL) return PM3_EMALLOC;
for (int cnt = 0; cnt < MIFARE_DEFAULTKEYS_SIZE; cnt++)
for (int cnt = 0; cnt < ARRAYLEN(g_mifare_default_keys); cnt++)
num_to_bytes(g_mifare_default_keys[cnt], 6, (uint8_t *)(keyBlock + cnt * 6));
if (param_getchar(Cmd, 0) == '*') {
@ -1928,7 +1928,7 @@ static int CmdHF14AMfChk(const char *Cmd) {
if (keycnt == 0) {
PrintAndLogEx(INFO, "No key specified, trying default keys");
for (; keycnt < MIFARE_DEFAULTKEYS_SIZE; keycnt++)
for (; keycnt < ARRAYLEN(g_mifare_default_keys); keycnt++)
PrintAndLogEx(NORMAL, "[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
(keyBlock + 6 * keycnt)[0], (keyBlock + 6 * keycnt)[1], (keyBlock + 6 * keycnt)[2],
(keyBlock + 6 * keycnt)[3], (keyBlock + 6 * keycnt)[4], (keyBlock + 6 * keycnt)[5], 6);

View file

@ -210,8 +210,7 @@ static int usage_hf_mfu_pwdgen(void) {
}
#define KEYS_3DES_COUNT 7
uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = {
uint8_t default_3des_keys[][16] = {
{ 0x42, 0x52, 0x45, 0x41, 0x4b, 0x4d, 0x45, 0x49, 0x46, 0x59, 0x4f, 0x55, 0x43, 0x41, 0x4e, 0x21 }, // 3des std key
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // all zeroes
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, // 0x00-0x0F
@ -221,8 +220,7 @@ uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = {
{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF } // 11 22 33
};
#define KEYS_PWD_COUNT 1
uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = {
uint8_t default_pwd_pack[][4] = {
{0xFF, 0xFF, 0xFF, 0xFF}, // PACK 0x00,0x00 -- factory default
};
@ -1298,7 +1296,7 @@ static int CmdHF14AMfUInfo(const char *Cmd) {
// also try to diversify default keys.. look into CmdHF14AMfGenDiverseKeys
PrintAndLogEx(INFO, "Trying some default 3des keys");
for (uint8_t i = 0; i < KEYS_3DES_COUNT; ++i) {
for (uint8_t i = 0; i < ARRAYLEN(default_3des_keys); ++i) {
key = default_3des_keys[i];
if (ulc_authentication(key, true)) {
PrintAndLogEx(SUCCESS, "Found default 3des key: ");
@ -1431,7 +1429,7 @@ static int CmdHF14AMfUInfo(const char *Cmd) {
if (ul_auth_select(&card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack)) == PM3_ESOFT) return PM3_ESOFT;
for (uint8_t i = 0; i < KEYS_PWD_COUNT; ++i) {
for (uint8_t i = 0; i < ARRAYLEN(default_pwd_pack); ++i) {
key = default_pwd_pack[i];
len = ulev1_requestAuthentication(key, pack, sizeof(pack));
if (len > -1) {
@ -2349,7 +2347,7 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
//Change key to user defined one
if (cmdp == 'k') {
keyNo = param_get8(Cmd, 1);
if (keyNo >= KEYS_3DES_COUNT)
if (keyNo >= ARRAYLEN(default_3des_keys))
errors = true;
}

View file

@ -80,8 +80,8 @@ static int CmdTIDemod(const char *Cmd) {
save_restoreGB(GRAPH_SAVE);
int lowLen = sizeof(LowTone) / sizeof(int);
int highLen = sizeof(HighTone) / sizeof(int);
int lowLen = ARRAYLEN(LowTone);
int highLen = ARRAYLEN(HighTone);
int convLen = (highLen > lowLen) ? highLen : lowLen;
uint16_t crc;
int i, j, TagType;

View file

@ -257,7 +257,6 @@ const APDUCode APDUCodeTable[] = {
{"9FXX", APDUCODE_TYPE_NONE, "Command successfully executed; 'xx' bytes of data are available and can be requested using GET RESPONSE."},
{"9XXX", APDUCODE_TYPE_NONE, "Application related status, (ISO 7816-3)"}
};
const size_t APDUCodeTableLen = sizeof(APDUCodeTable) / sizeof(APDUCode);
static int CodeCmp(const char *code1, const char *code2) {
int xsymb = 0;
@ -279,12 +278,12 @@ static int CodeCmp(const char *code1, const char *code2) {
const APDUCode *GetAPDUCode(uint8_t sw1, uint8_t sw2) {
char buf[6] = {0};
int mineq = APDUCodeTableLen;
int mineq = ARRAYLEN(APDUCodeTable);
int mineqindx = 0;
sprintf(buf, "%02X%02X", sw1, sw2);
for (int i = 0; i < APDUCodeTableLen; i++) {
for (int i = 0; i < ARRAYLEN(APDUCodeTable); i++) {
int res = CodeCmp(APDUCodeTable[i].ID, buf);
// equal
@ -300,7 +299,7 @@ const APDUCode *GetAPDUCode(uint8_t sw1, uint8_t sw2) {
}
// if we have not equal, but with some 'X'
if (mineqindx < APDUCodeTableLen) {
if (mineqindx < ARRAYLEN(APDUCodeTable)) {
return &APDUCodeTable[mineqindx];
}

View file

@ -17,6 +17,7 @@
#include <config.h>
#endif
#include "commonutil.h"
#include "tlv.h"
#include "emv_tags.h"
@ -437,7 +438,7 @@ static int emv_tlv_compare(const void *a, const void *b) {
}
static const struct emv_tag *emv_get_tag(const struct tlv *tlv) {
struct emv_tag *tag = bsearch(tlv, emv_tags, sizeof(emv_tags) / sizeof(emv_tags[0]),
struct emv_tag *tag = bsearch(tlv, emv_tags, ARRAYLEN(emv_tags),
sizeof(emv_tags[0]), emv_tlv_compare);
return tag ? tag : &emv_tags[0];

View file

@ -18,7 +18,6 @@ static const char *PSElist [] = {
"325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
"315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
};
//static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
const char *TransactionTypeStr[] = {
"MSD",
@ -119,7 +118,6 @@ static const TAIDList AIDlist [] = {
{ CV_OTHER, "D5780000021010" }, // Bankaxept Norway Bankaxept Norwegian domestic debit card
{ CV_OTHER, "F0000000030001" }, // BRADESCO - Brazilian Bank Banco Bradesco
};
static const size_t AIDlistLen = sizeof(AIDlist) / sizeof(TAIDList);
static bool APDULogging = false;
void SetAPDULogging(bool logging) {
@ -133,7 +131,7 @@ enum CardPSVendor GetCardPSVendor(uint8_t *AID, size_t AIDlen) {
hex_to_buffer((uint8_t *)buf, AID, AIDlen, sizeof(buf) - 1, 0, 0, true);
for (int i = 0; i < AIDlistLen; i ++) {
for (int i = 0; i < ARRAYLEN(AIDlist); i ++) {
if (strncmp(AIDlist[i].aid, buf, strlen(AIDlist[i].aid)) == 0) {
return AIDlist[i].vendor;
}
@ -530,7 +528,7 @@ int EMVSearch(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON,
int res = 0;
int retrycnt = 0;
for (int i = 0; i < AIDlistLen; i ++) {
for (int i = 0; i < ARRAYLEN(AIDlist); i ++) {
param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen);
res = EMVSelect(channel, (i == 0) ? ActivateField : false, true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
// retry if error and not returned sw error

View file

@ -57,10 +57,9 @@ static const ApplicationDataElm ApplicationData[] = {
{0x00, "end..."}
};
int ApplicationDataLen = sizeof(ApplicationData) / sizeof(ApplicationDataElm);
const char *GetApplicationDataName(tlv_tag_t tag) {
for (int i = 0; i < ApplicationDataLen; i++)
for (int i = 0; i < ARRAYLEN(ApplicationData); i++)
if (ApplicationData[i].Tag == tag)
return ApplicationData[i].Name;

View file

@ -20,6 +20,7 @@
#include "../crypto.h"
#include "../dump.h"
#include "util_posix.h"
#include "commonutil.h"
#include <stdlib.h>
#include <string.h>
@ -312,7 +313,7 @@ int exec_crypto_test(bool verbose) {
}
fprintf(stdout, "Crypto raw test: passed\n\n");
for (i = 0; i < sizeof(keylengths) / sizeof(keylengths[0]); i++) {
for (i = 0; i < ARRAYLEN(keylengths); i++) {
unsigned int kl = keylengths[i];
ret = test_genkey(kl, message, kl / 8, verbose);
if (ret) {

View file

@ -150,7 +150,7 @@ fido2Desc_t fido2CmdGetInfoRespDesc[] = {
};
const char *fido2GetCmdErrorDescription(uint8_t errorCode) {
for (size_t i = 0; i < sizeof(fido2Errors) / sizeof(fido2Error_t); i++)
for (size_t i = 0; i < ARRAYLEN(fido2Errors); i++)
if (fido2Errors[i].ErrorCode == errorCode)
return fido2Errors[i].Description;
@ -158,7 +158,7 @@ const char *fido2GetCmdErrorDescription(uint8_t errorCode) {
}
const char *fido2GetCmdMemberDescription(uint8_t cmdCode, bool isResponse, int memberNum) {
for (size_t i = 0; i < sizeof(fido2CmdGetInfoRespDesc) / sizeof(fido2Desc_t); i++)
for (size_t i = 0; i < ARRAYLEN(fido2CmdGetInfoRespDesc); i++)
if (fido2CmdGetInfoRespDesc[i].Command == cmdCode &&
fido2CmdGetInfoRespDesc[i].PckType == (isResponse ? ptResponse : ptQuery) &&
fido2CmdGetInfoRespDesc[i].MemberNumber == memberNum)

View file

@ -40,10 +40,9 @@ static const PlusErrorsElm PlusErrors[] = {
{0x0f, "General Manipulation Error. Failure in the operation of the PICC (cannot write to the data block), etc."},
{0x90, "OK"},
};
int PlusErrorsLen = sizeof(PlusErrors) / sizeof(PlusErrorsElm);
const char *mfpGetErrorDescription(uint8_t errorCode) {
for (int i = 0; i < PlusErrorsLen; i++)
for (int i = 0; i < ARRAYLEN(PlusErrors); i++)
if (errorCode == PlusErrors[i].Code)
return PlusErrors[i].Description;

View file

@ -13,8 +13,6 @@
#include <inttypes.h>
#define MIFARE_DEFAULTKEYS_SIZE sizeof(g_mifare_default_keys) / sizeof(uint64_t)
static const uint64_t g_mifare_default_keys[] = {
0xffffffffffff, // Default key (first key used by program if no user defined key)
0x000000000000, // Blank key

View file

@ -69,11 +69,11 @@ static int WAI_PREFIX(getModulePath_)(HMODULE module, char *out, int capacity, i
DWORD size;
int length_, length__;
size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
size = GetModuleFileNameW(module, buffer1, ARRAYLEN(buffer1));
if (size == 0)
break;
else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0]))) {
else if (size == (DWORD)(ARRAYLEN(buffer1))) {
DWORD size_ = size;
do {
wchar_t *path_;
@ -521,7 +521,7 @@ int WAI_PREFIX(getExecutablePath)(char *out, int capacity, int *dirname_length)
#endif
size_t size = sizeof(buffer1);
if (sysctl(mib, (u_int)(sizeof(mib) / sizeof(mib[0])), path, &size, NULL, 0) != 0)
if (sysctl(mib, (u_int)(ARRAYLEN(mib)), path, &size, NULL, 0) != 0)
break;
resolved = realpath(path, buffer2);