commit to push, too long so forget

This commit is contained in:
nganhkhoa 2024-01-25 15:15:50 +07:00
parent ce7a132ae2
commit 68cbe56e9d
11 changed files with 112 additions and 40 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" /> <bytecodeTargetLevel target="17" />
</component> </component>
</project> </project>

View File

@ -7,6 +7,7 @@
<option name="testRunner" value="GRADLE" /> <option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="Embedded JDK" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

6
.idea/kotlinc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.8.0" />
</component>
</project>

View File

@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <project version="4">
<project version="4"> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="Android Studio default JDK" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" />
<output url="file://$PROJECT_DIR$/build/classes" /> </component>
</component> <component name="ProjectType">
<component name="ProjectType"> <option name="id" value="Android" />
<option name="id" value="Android" /> </component>
</component>
</project> </project>

View File

@ -19,6 +19,9 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/tech_list" />
</activity> </activity>
</application> </application>
<uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.NFC" />

View File

@ -794,6 +794,8 @@ void Connector::activeAuthentication(bytes m2) {
// 256 is hardcoded // 256 is hardcoded
// MRTD returns signature of size [sigLength] or of arbitrarily size if [sigLength] is 256. // MRTD returns signature of size [sigLength] or of arbitrarily size if [sigLength] is 256.
logBytes("Internal auth with %s", m2);
auto response = internalAuthenticate(m2, 256); auto response = internalAuthenticate(m2, 256);
auto message = response.data; auto message = response.data;
@ -833,6 +835,7 @@ void Connector::activeAuthentication(bytes m2) {
break; break;
default: default:
hashlen = 0; hashlen = 0;
LOGI("cannot detect hash type, wrong !!!!");
break; break;
} }
} }
@ -863,8 +866,34 @@ void Connector::activeAuthentication(bytes m2) {
bytes check_hash(160 / 8); bytes check_hash(160 / 8);
SHA1(m1_m2.data(), m1_m2.size(), check_hash.data()); SHA1(m1_m2.data(), m1_m2.size(), check_hash.data());
bool verified = std::equal(hash.begin(), hash.end(), check_hash.begin()); bool verified = std::equal(hash.begin(), hash.end(), check_hash.begin());
verified &= hash.size() == check_hash.size();
logBytes("hash %s", hash); logBytes("hash %s", hash);
logBytes("check hash %s", check_hash); logBytes("check hash %s", check_hash);
LOGI("active authentication verified %d", verified); LOGI("active authentication verified %d", verified);
}
void Connector::selectSAM() {
bytes command;
bytes response;
// 00A404000C4D4B4D4F43436C69656E7431
// command = {0, 164, 4, 0, 12, 77, 75, 77, 79, 67, 67, 108, 105, 101, 110, 116, 49};
// response = transceive(command);
// command = {0x80, 0x50, 0, 0, 0x20};
// response = transceive(command);
//
// bytes random = response;
// logBytes("random %s", random);
// 00A404000CA000000063504B43532D3135
command = {0, 164, 4, 0, 12, 160, 0, 0, 0, 99, 80, 75, 67, 83, 45, 49, 53};
response = transceive(command);
logBytes("SAM selected %s", response);
// get pubkey
// 00F1000100
command = {00, 0xf1, 00, 01, 00};
response = transceive(command);
logBytes("pubkey %s", response);
} }

View File

@ -298,6 +298,8 @@ public:
void readEFSOD(); void readEFSOD();
void activeAuthentication(bytes challenge); void activeAuthentication(bytes challenge);
void selectSAM();
}; };
#endif //CCCC_API_H #endif //CCCC_API_H

View File

@ -232,12 +232,16 @@ uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3, bo
bytes msg = data; bytes msg = data;
if (pad) iso9797_pad(msg); if (pad) iso9797_pad(msg);
logBytes("eifd padded %s", msg);
bytes mac = des_encrypt(msg, key1); bytes mac = des_encrypt(msg, key1);
mac.erase(mac.begin(), mac.end() - 8); mac.erase(mac.begin(), mac.end() - 8);
uint64_t mac_value = bytes2num(mac); uint64_t mac_value = bytes2num(mac);
LOGI("mac1 %llx", mac_value);
mac_value = des(mac_value, key2, 'd'); mac_value = des(mac_value, key2, 'd');
LOGI("mac2 %llx", mac_value);
mac_value = des(mac_value, key3, 'e'); mac_value = des(mac_value, key3, 'e');
LOGI("mac3 %llx", mac_value);
return mac_value; return mac_value;
} }

View File

@ -22,22 +22,24 @@ Java_com_bshield_cccc_MainActivity_initNFCScan(
// TODO: move to connector class // TODO: move to connector class
connector->selectDf1(); connector->selectSAM();
connector->initBAC();
connector->readEFCOM(); // connector->selectDf1();
connector->readEFDG1(); // connector->initBAC();
connector->readEFDG2(); //
connector->readEFDG13(); // connector->readEFCOM();
connector->readEFDG14(); // connector->readEFDG1();
connector->readEFDG15(); // connector->readEFDG2();
// connector->readEFDG13();
connector->readEFSOD(); // connector->readEFDG14();
// connector->readEFDG15();
bytes challenge = randomBytes(8); //
connector->activeAuthentication(challenge); // connector->readEFSOD();
//
connector->ready = true; // bytes challenge = randomBytes(8);
// connector->activeAuthentication(challenge);
//
// connector->ready = true;
return 1; return 1;
} }

View File

@ -6,9 +6,11 @@ import android.nfc.NfcAdapter
import android.nfc.NfcAdapter.ACTION_ADAPTER_STATE_CHANGED import android.nfc.NfcAdapter.ACTION_ADAPTER_STATE_CHANGED
import android.nfc.NfcAdapter.getDefaultAdapter import android.nfc.NfcAdapter.getDefaultAdapter
import android.nfc.tech.IsoDep import android.nfc.tech.IsoDep
import android.nfc.tech.NfcA
import android.nfc.tech.TagTechnology import android.nfc.tech.TagTechnology
import android.os.Bundle import android.os.Bundle
import android.text.InputType import android.text.InputType
import android.util.Log
import android.view.View import android.view.View
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -107,14 +109,16 @@ class MainActivity : AppCompatActivity() {
super.onResume(); super.onResume();
nfcAdapter.enableReaderMode(this, {tag -> nfcAdapter.enableReaderMode(this, {tag ->
Log.d("BRUH", tag.techList.joinToString())
if (tag.techList.contains(IsoDep::class.java.name)) { if (tag.techList.contains(IsoDep::class.java.name)) {
// type = "iso7816" // type = "iso7816"
// standard = "ISO 14443-4 (Type B)" // standard = "ISO 14443-4 (Type B)"
val isoDep = IsoDep.get(tag) val isoDep = IsoDep.get(tag)
(isoDep as IsoDep).timeout = 5000;
tagTechnology = isoDep tagTechnology = isoDep
PerfomNFCScanning(); PerfomNFCScanning();
} }
}, (0x1 or 0x2), null); }, (0x1 or 0x2 or 0x4 or 0x8 or 16 or 256), null);
// Example of a call to a native method // Example of a call to a native method
// binding.sampleText.text = "nfc enabled? ${nfcAdapter.isEnabled()}" // binding.sampleText.text = "nfc enabled? ${nfcAdapter.isEnabled()}"
@ -145,19 +149,19 @@ class MainActivity : AppCompatActivity() {
return; return;
} }
if (binding.idnumber.text.length != 12) { // if (binding.idnumber.text.length != 12) {
setError("ID number must be 12 digits"); // setError("ID number must be 12 digits");
return; // return;
} // }
//
if (!::birthday.isInitialized) { // if (!::birthday.isInitialized) {
setError("Birthday is not entered"); // setError("Birthday is not entered");
} // }
if (!::expirydate.isInitialized) { // if (!::expirydate.isInitialized) {
setError("Birthday is not entered"); // setError("Birthday is not entered");
} // }
//
val id = binding.idnumber.text.substring(3); // val id = binding.idnumber.text.substring(3);
fun checkdigit(digits: String): String { fun checkdigit(digits: String): String {
fun mapValue(x: Char): Int { fun mapValue(x: Char): Int {
@ -173,7 +177,8 @@ class MainActivity : AppCompatActivity() {
return (hash % 10).toString(); return (hash % 10).toString();
} }
val passcode = id + checkdigit(id) + birthday + checkdigit(birthday) + expirydate + checkdigit(expirydate); // val passcode = id + checkdigit(id) + birthday + checkdigit(birthday) + expirydate + checkdigit(expirydate);
val passcode = "098002079798112232311229";
try { try {
setError("Scanning the identity card"); setError("Scanning the identity card");

View File

@ -0,0 +1,21 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- capture anything using NfcF -->
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<!-- OR -->
<!-- capture all MIFARE Classics with NDEF payloads -->
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>