diff --git a/client/mifarehost.c b/client/mifarehost.c index 9f4ddbd9f..4ba29abc6 100644 --- a/client/mifarehost.c +++ b/client/mifarehost.c @@ -852,7 +852,7 @@ bool detect_classic_prng(void){ if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { PrintAndLog("PRNG UID: Reply timeout."); - return -1; + return false; } // if select tag failed. @@ -862,13 +862,13 @@ bool detect_classic_prng(void){ } if (!WaitForResponseTimeout(CMD_ACK, &respA, 2500)) { PrintAndLog("PRNG data: Reply timeout."); - return -1; + return false; } // check respA if (respA.arg[0] != 4) { PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]); - return -1; + return false; } uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]); diff --git a/client/scripting.c b/client/scripting.c index 301b9e46e..27614784c 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -104,35 +104,32 @@ static int l_WaitForResponseTimeout(lua_State *L){ //Check number of arguments int n = lua_gettop(L); - if(n == 0) - { + if (n == 0) { //signal error by returning Nil, errorstring lua_pushnil(L); - lua_pushstring(L,"You need to supply at least command to wait for"); - return 2; // two return values + lua_pushstring(L, "You need to supply at least command to wait for"); + return 2; } - if(n >= 1) - { - //pop cmd - cmd = luaL_checkunsigned(L,1); + + // extract first param. cmd byte to look for + if (n >= 1) { + cmd = luaL_checkunsigned(L, 1); } - if(n >= 2) - { - //Did the user send a timeout ? - //Check if the current top of stack is an integer - ms_timeout = luaL_checkunsigned(L,2); - //printf("Timeout set to %dms\n" , (int) ms_timeout); + // extract second param. timeout value + if(n >= 2){ + ms_timeout = luaL_checkunsigned(L, 2); } UsbCommand response; - if(WaitForResponseTimeout(cmd, &response, ms_timeout)) { + if (WaitForResponseTimeout(cmd, &response, ms_timeout)) { //Push it as a string - lua_pushlstring(L,(const char *)&response, sizeof(UsbCommand)); - return 1;// return 1 to signal one return value - }else{ - //Push a Nil instead + lua_pushlstring(L,(const char *)&response, sizeof(UsbCommand)); + return 1; + } else { + //signal error by returning Nil, errorstring lua_pushnil(L); - return 1;// one return value + lua_pushstring(L, "No response from the device"); + return 2; } } @@ -160,12 +157,12 @@ static int l_mfDarkside(lua_State *L){ switch (n) { case 2:{ const char *p_keytype = luaL_checklstring(L, 2, &size); - if(size != 2) return returnToLuaWithError(L,"Wrong size of keytype, got %d bytes, expected 1", (int) size); + if (size != 2) return returnToLuaWithError(L,"Wrong size of keytype, got %d bytes, expected 1", (int) size); sscanf(p_keytype, "%x", &keytype); } case 1: { const char *p_blockno = luaL_checklstring(L, 1, &size); - if(size != 2) return returnToLuaWithError(L,"Wrong size of blockno, got %d bytes, expected 2", (int) size); + if (size != 2) return returnToLuaWithError(L,"Wrong size of blockno, got %d bytes, expected 2", (int) size); sscanf(p_blockno, "%02x", &blockno); break; } @@ -174,19 +171,15 @@ static int l_mfDarkside(lua_State *L){ int retval = mfDarkside(blockno & 0xFF, keytype & 0xFF, &key); - //Push the retval on the stack - lua_pushinteger(L,retval); - - //Push the key onto the stack uint8_t dest_key[8]; - num_to_bytes(key,sizeof(dest_key),dest_key); - - //printf("Pushing to lua stack: %012" PRIx64 "\n",key); - lua_pushlstring(L,(const char *) dest_key,sizeof(dest_key)); - - return 2; //Two return values + num_to_bytes(key, sizeof(dest_key), dest_key); + + //Push the retval on the stack + lua_pushinteger(L, retval); + lua_pushlstring(L, (const char *) dest_key, sizeof(dest_key)); + return 2; } -//static int l_PrintAndLog(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));} + static int l_clearCommandBuffer(lua_State *L){ clearCommandBuffer(); return 0; @@ -222,7 +215,7 @@ static int l_foobar(lua_State *L) { * @return boolean, true if kbhit, false otherwise. */ static int l_ukbhit(lua_State *L) { - lua_pushboolean(L,ukbhit() ? true : false); + lua_pushboolean(L, ukbhit() ? true : false); return 1; } /** @@ -278,7 +271,7 @@ static int l_aes128decrypt_cbc(lua_State *L) { uint32_t tmp; size_t size; const char *p_key = luaL_checklstring(L, 1, &size); - if(size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); + if (size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); const char *p_encTxt = luaL_checklstring(L, 2, &size); @@ -303,14 +296,13 @@ static int l_aes128decrypt_cbc(lua_State *L) { lua_pushlstring(L,(const char *)&outdata, sizeof(outdata)); return 1;// return 1 to signal one return value } -static int l_aes128decrypt_ecb(lua_State *L) -{ +static int l_aes128decrypt_ecb(lua_State *L) { //Check number of arguments int i; uint32_t tmp; size_t size; const char *p_key = luaL_checklstring(L, 1, &size); - if(size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); + if (size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); const char *p_encTxt = luaL_checklstring(L, 2, &size); @@ -335,14 +327,13 @@ static int l_aes128decrypt_ecb(lua_State *L) return 1;// return 1 to signal one return value } -static int l_aes128encrypt_cbc(lua_State *L) -{ +static int l_aes128encrypt_cbc(lua_State *L) { //Check number of arguments int i; uint32_t tmp; size_t size; const char *p_key = luaL_checklstring(L, 1, &size); - if(size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); + if (size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); const char *p_txt = luaL_checklstring(L, 2, &size); @@ -367,14 +358,13 @@ static int l_aes128encrypt_cbc(lua_State *L) return 1;// return 1 to signal one return value } -static int l_aes128encrypt_ecb(lua_State *L) -{ +static int l_aes128encrypt_ecb(lua_State *L) { //Check number of arguments int i; uint32_t tmp; size_t size; const char *p_key = luaL_checklstring(L, 1, &size); - if(size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); + if (size != 32) return returnToLuaWithError(L,"Wrong size of key, got %d bytes, expected 32", (int) size); const char *p_txt = luaL_checklstring(L, 2, &size); @@ -397,8 +387,7 @@ static int l_aes128encrypt_ecb(lua_State *L) return 1;// return 1 to signal one return value } -static int l_crc8legic(lua_State *L) -{ +static int l_crc8legic(lua_State *L) { size_t size; const char *p_str = luaL_checklstring(L, 1, &size); @@ -407,8 +396,7 @@ static int l_crc8legic(lua_State *L) return 1; } -static int l_crc16(lua_State *L) -{ +static int l_crc16(lua_State *L) { size_t size; const char *p_str = luaL_checklstring(L, 1, &size); @@ -417,8 +405,7 @@ static int l_crc16(lua_State *L) return 1; } -static int l_crc64(lua_State *L) -{ +static int l_crc64(lua_State *L) { size_t size; uint64_t crc = 0; unsigned char outdata[8] = {0x00}; @@ -439,8 +426,7 @@ static int l_crc64(lua_State *L) return 1; } -static int l_crc64_ecma182(lua_State *L) -{ +static int l_crc64_ecma182(lua_State *L) { //size_t size; uint64_t crc = 0; unsigned char outdata[8] = {0x00}; @@ -461,17 +447,16 @@ static int l_crc64_ecma182(lua_State *L) outdata[5] = (uint8_t)(crc >> 16) & 0xff; outdata[6] = (uint8_t)(crc >> 8) & 0xff; outdata[7] = crc & 0xff; - lua_pushlstring(L,(const char *)&outdata, sizeof(outdata)); + lua_pushlstring(L, (const char *)&outdata, sizeof(outdata)); return 1; } -static int l_sha1(lua_State *L) -{ +static int l_sha1(lua_State *L) { size_t size; const char *p_str = luaL_checklstring(L, 1, &size); unsigned char outdata[20] = {0x00}; sha1( (uint8_t*) p_str, size, outdata); - lua_pushlstring(L,(const char *)&outdata, sizeof(outdata)); + lua_pushlstring(L, (const char *)&outdata, sizeof(outdata)); return 1; } @@ -482,7 +467,7 @@ static int l_reveng_models(lua_State *L){ int count = 0; int in_width = luaL_checkinteger(L, 1); - if( in_width > 89 ) return returnToLuaWithError(L,"Width cannot exceed 89, got %d", in_width); + if ( in_width > 89 ) return returnToLuaWithError(L,"Width cannot exceed 89, got %d", in_width); // This array needs to be adjusted if RevEng adds more crc-models. uint8_t width[100]; @@ -497,7 +482,6 @@ static int l_reveng_models(lua_State *L){ lua_rawseti(L,-2,i+1); free(models[i]); } - return 1; } @@ -598,15 +582,13 @@ static int l_hardnested(lua_State *L){ uint64_t foundkey = 0; int retval = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, haveTarget ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey); DropField(); - - //Push the retval on the stack - lua_pushinteger(L,retval); //Push the key onto the stack uint8_t dest_key[6]; num_to_bytes(foundkey, sizeof(dest_key), dest_key); - - //printf("Pushing to lua stack: %012" PRIx64 "\n",key); + + //Push the retval on the stack + lua_pushinteger(L,retval); lua_pushlstring(L, (const char *) dest_key, sizeof(dest_key)); return 2; //Two return values } @@ -617,9 +599,7 @@ static int l_hardnested(lua_State *L){ * @return */ static int l_detect_prng(lua_State *L) { - bool valid = detect_classic_prng(); - //Push the retval on the stack - lua_pushinteger(L, valid); + lua_pushboolean(L, detect_classic_prng()); return 1; } /* @@ -728,4 +708,4 @@ int set_pm3_libraries(lua_State *L) { setLuaPath(L, libraries_path); return 1; -} +} \ No newline at end of file