Updated on 2026-08-14

This commit is contained in:
Tangem 2018-09-07 11:28:10 +03:00
parent 5c6ea5ca04
commit ff6679eaad
11 changed files with 15 additions and 391 deletions

View file

@ -63,73 +63,6 @@ public class CardCrypto {
}
// public static boolean isCanonical(BigInteger s) {
//
// ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
//
// BigInteger HALF_CURVE_ORDER = spec.getN().shiftRight(1);
// return s.compareTo(HALF_CURVE_ORDER) <= 0;
// }
//
// public static BigInteger toCanonicalised(BigInteger s) {
//
// // The order of the curve is the number of valid points that exist on that curve. If S is in the upper
// // half of the number of valid points, then bring it back to the lower half. Otherwise, imagine that
// // N = 10
// // s = 8, so (-8 % 10 == 2) thus both (r, 8) and (r, 2) are valid solutions.
// // 10 - 8 == 2, giving us always the latter solution, which is canonical.
// ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
// if(!isCanonical(s)) {
// BigInteger canon = spec.getN().subtract(s);
// //Log.e("TX_SIGN", "non Canonical S");
// return canon;
// }
//
// return s;
//
// }
//
// public static BigInteger[] calcSign2(byte[] priv, byte[] hash)
// {
// ECDSASigner signer = new ECDSASigner();
// BigInteger d = new BigInteger(priv);
//
// ECNamedCurveParameterSpec CURVE_PARAMS = ECNamedCurveTable.getParameterSpec("secp256k1");
// ECDomainParameters CURVE = new ECDomainParameters(CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(),
// CURVE_PARAMS.getH());
// ECPrivateKeyParameters params = new ECPrivateKeyParameters(d, CURVE);
// //ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
// signer.init(true, params);
// BigInteger[] rs = signer.generateSignature(hash);
// return rs;
//
// }
//
// public static byte[] Signature3(byte[] privateKeyArray, byte[] data) throws Exception
// {
// byte[] hash=Util.calculateSHA256(data);
// BigInteger[] signBI=calcSign2(privateKeyArray, hash);
// //signBI[0]=toCanonicalised(signBI[0]);
// signBI[1]=toCanonicalised(signBI[1]);
// byte[] r = signBI[0].toByteArray();
// byte[] s = signBI[1].toByteArray();
//
// byte[] res = new byte[64];
// if( r.length==32 ) {
// System.arraycopy(r, 0, res, 0, r.length);
// }else if( r.length==33 && r[0]==0 ){
// System.arraycopy(r, 1, res, 0, r.length-1);
// }else {
// throw new Exception("unsupported r-length");
// }
// if( s.length==32 ) {
// System.arraycopy(s, 0, res, 32, 32);
// }else{
// throw new Exception("unsupported s-length");
// }
// return res;
// }
public static byte[] Signature(byte[] privateKeyArray, byte[] data) throws Exception {
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyFactory factory = KeyFactory.getInstance("EC", "SC");

View file

@ -283,64 +283,6 @@ public class CardProtocol {
}
}
// send command APDU, get response APDU, and display HEX data to user
// private ResponseApdu SendAndReceive(CommandApdu cmdApdu, boolean breakOnNeedPause) throws Exception {
// if (mCard.encryptionMode != TangemCard.EncryptionMode.None) {
// if (sessionKey == null) {
// run_OpenSession(mCard.encryptionMode);
// }
// cmdApdu.Crypt(sessionKey);
// }
// cmdApdu.setP1(mCard.encryptionMode.getP());
// byte[] cmdBytes = cmdApdu.toBytes();
// String cmdStr = CommandApdu.toString(cmdBytes, cmdApdu.getLc());
// Log.v("NFC", String.format("<< [%s]: %s", cmdApdu.getCommandName(), cmdStr));
// byte[] rsp;
// ResponseApdu rspApdu;
// try {
// do {
// try {
// mNotifications.onReadBeforeRequest(mIsoDep.getTimeout());
// try {
// rsp = mIsoDep.transceive(cmdBytes);
// } finally {
// mNotifications.onReadAfterRequest();
// }
// } catch (IOException e) {
// if (e.getMessage().contains("length exceeds supported maximum") && mIsoDep.isExtendedLengthApduSupported()) {
// throw new TangemException_ExtendedLengthNotSupported(e.getMessage());
// }
// throw e;
// }
// if (mCard.encryptionMode != TangemCard.EncryptionMode.None) {
// rspApdu = ResponseApdu.Decrypt(rsp, sessionKey);
// } else {
// rspApdu = new ResponseApdu(rsp);
// }
//
// if (rspApdu.isParsedWithError()) {
// Log.v("NFC", String.format(">> [%s]: %s", cmdApdu.getCommandName(), Util.bytesToHex(rsp)));
// throw new TangemException(rspApdu.getParseErroMessage());
// } else if (rspApdu.getSW1SW2() == SW.NEED_PAUSE && mNotifications != null) {
// int remainingPause = rspApdu.getTLVs().getTagAsInt(TLV.Tag.TAG_Pause) * 10;
// Log.v("NFC", String.format(">> Security delay, remaining %f s", remainingPause / 1000.0));
// if (breakOnNeedPause) {
// break;
// } else {
// mNotifications.onReadWait(remainingPause);
// }
// } else {
// Log.v("NFC", String.format(">> [%s]: %s", cmdApdu.getCommandName(), Util.bytesToHex(rsp)));
// }
// } while (rspApdu.getSW1SW2() == SW.NEED_PAUSE);
// } finally {
// mNotifications.onReadWait(0);
// }
//
// return rspApdu;
// }
private ResponseApdu SendAndReceive(CommandApdu cmdApdu, boolean breakOnNeedPause) throws Exception {
if (mCard.encryptionMode != TangemCard.EncryptionMode.None) {
if (sessionKey == null) {

View file

@ -1,88 +0,0 @@
package com.tangem.domain.cardReader;
import android.content.Context;
import android.nfc.TagLostException;
import android.nfc.tech.IsoDep;
import com.tangem.util.Util;
import java.io.IOException;
public class CustomCardReader implements Runnable {
protected NfcManager mNfcManager;
public interface UiCallbacks {
// display console messages
void onMessageSend(String raw, String name);
void onMessageRcv(String raw, String name);
void onOkay(String message);
void onError(String message);
void onStart(String message);
void onAction(String message, String name);
void onSeparator();
// clear console messages
void clearMessages();
// ui listeners
void setUserSelectListener(UiListener callback);
// cleanup, if needed
void onFinish(boolean err);
}
public interface UiListener {
void onUserSelect(String aid);
}
public static final int SW_NO_ERROR = 0x9000;
public static final int SW_GET_RESPONSE = 0x6700;
protected IsoDep mIsoDep;
protected UiCallbacks mUiCallbacks;
protected String mAid;
protected byte[] mAidBytes;
protected Context mContext;
public CustomCardReader(Context context, NfcManager manager, IsoDep isoDep, String aid, UiCallbacks uiCallbacks) {
this.mIsoDep = isoDep;
this.mAid = aid;
this.mAidBytes = Util.hexToBytes(aid);
this.mUiCallbacks = uiCallbacks;
this.mNfcManager = manager;
this.mContext = context;
}
// send command APDU, get response APDU, and display HEX data to user
protected ResponseApdu sendAndRcv(CommandApdu cmdApdu)
throws TagLostException, IOException {
byte[] cmdBytes = cmdApdu.toBytes();
String cmdStr = CommandApdu.toString(cmdBytes, cmdApdu.getLc());
//mUiCallbacks.onMessageSend(cmdStr, cmdApdu.getCommandName());
byte[] rsp = mIsoDep.transceive(cmdBytes);
ResponseApdu rspApdu = new ResponseApdu(rsp);
byte[] data = rspApdu.getData();
//mUiCallbacks.onMessageRcv(Util.bytesToHex(rsp), cmdApdu.getCommandName());
if (rspApdu.isParsedWithError()) {
mUiCallbacks.onError(rspApdu.getParseErroMessage());
}
/*
Log.d(TAG, "response APDU: " + Util.bytesToHex(rsp));
if (data.length > 0) {
Log.d(TAG, TLVUtil.prettyPrintAPDUResponse(data));
}
*/
return rspApdu;
}
@Override
public void run() {
}
}

View file

@ -5,9 +5,6 @@ package com.tangem.domain.cardReader;
*/
public enum INS {
Unknown(0x00),
BootROM_SOS(0x40),
BootROM_Tangem(0xF0),
Personalize(0xF1),
Read(0xF2),
VerifyCard(0xF3),
ValidateCard(0xF4),

View file

@ -27,7 +27,6 @@ public class TLV {
TAG_SettingsMask(0x0A),
TAG_CardData(0x0C),
TAG_NDEFData(0x0D),
TAG_CreateWalletAtPersonalize(0x0E),
TAG_Health(0x0F),
TAG_PIN(0x10),
@ -72,11 +71,6 @@ public class TLV {
TAG_RemainingSignatures(0x62),
TAG_SignedHashes(0x63),
TAG_Wallet_PrivateKey(0x70),
TAG_Card_PrivateKey(0x71),
TAG_Block_Reason(0x72),
TAG_Firmware(0x80),
TAG_Batch(0x81),
TAG_ManufactureDateTime(0x82),