Skip to content

Commit

Permalink
before final cleansing
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed May 1, 2023
1 parent 52036a0 commit cc0d717
Show file tree
Hide file tree
Showing 29 changed files with 817 additions and 507 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# JavaCard Template project with Gradle
# Policy-Based Validation of Bitcoin Transactions on Cryptographic Smartcards using JavaCard Template project with Gradle

## Showcases
```aidl
./gradlew run
```
Runs a showcase. I recommend to look into code of it for understanding.

[![Build Status](https://travis-ci.org/ph4r05/javacard-gradle-template.svg?branch=master)](https://travis-ci.org/ph4r05/javacard-gradle-template)

Expand Down Expand Up @@ -74,16 +80,6 @@ Typical output:
[ant:verify] XII 10, 2017 10:45:05 ODP.
[ant:verify] INFO: Verification completed with 0 warnings and 0 errors.
```
## Parsing PSBTv0
```aidl
./gradlew run --args="70736274ff0100750200000001268171371edff285e937adeea4b37b78000c0566cbb3ad64641713ca42171bf60000000000feffffff02d3dff505000000001976a914d0c59903c5bac2868760e90fd521a4665aa7652088ac00e1f5050000000017a9143545e6e33b832c47050f24d3eeb93c9c03948bc787b32e1300000100fda5010100000000010289a3c71eab4d20e0371bbba4cc698fa295c9463afa2e397f8533ccb62f9567e50100000017160014be18d152a9b012039daf3da7de4f53349eecb985ffffffff86f8aa43a71dff1448893a530a7237ef6b4608bbb2dd2d0171e63aec6a4890b40100000017160014fe3e9ef1a745e974d902c4355943abcb34bd5353ffffffff0200c2eb0b000000001976a91485cff1097fd9e008bb34af709c62197b38978a4888ac72fef84e2c00000017a914339725ba21efd62ac753a9bcd067d6c7a6a39d05870247304402202712be22e0270f394f568311dc7ca9a68970b8025fdd3b240229f07f8a5f3a240220018b38d7dcd314e734c9276bd6fb40f673325bc4baa144c800d2f2f02db2765c012103d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f210502483045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01210223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab300000000000000"
```

## Parsing PSBTv2

```aidl
./gradlew run --args="70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300"
```

## Installation on a (physical) card

Expand Down
17 changes: 10 additions & 7 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ public AppletInstructions() {
* when I plan to create different upload class for PSBT transaction and different class for
* applet initialization
*/
public static final short APPLET_INSTRUCTION_SUCC_MORE = (short) 0x9001;

public static final short INSTRUCTION_VERSION = 0;


public static final short CLASS_PSBT_UPLOAD = 0;

public static final short CLASS_POLICY_UPLOAD = 1;
Expand All @@ -52,6 +49,7 @@ public AppletInstructions() {
public static final short INS_DOWNLOAD_NUM_OUTPUT_V0 = 4;
public static final short INS_DOWNLOAD_VERSION = 6;
public static final short INS_DOWNLOAD_SIZE = 7;

public static final short CLASS_DOWNLOAD_INPUT_MAP = 6; // p1 is position of map, map must be present, first is 0
public static final short CLASS_DOWNLOAD_OUTPUT_MAP = 7; // --above--

Expand All @@ -66,15 +64,20 @@ public AppletInstructions() {
public static final short CLASS_VALIDATE_POLICY = 16;


public static final short CLASS_PSBT_UPLOAD_AND_BACK = 4;
// scenario where applet parses the PSBT and then sends it back
// to implement this one will take greater changes in how upload works. Different upload might need to be implemented

public static final short INS_REQUEST = 0;
public static final short INS_UPLOAD = 1;
public static final short INS_FINISH = 2;
public static final short INS_DOWNLOAD_ARRAY = 3;

//applet error throws below

/*
public static final short SOME_CRYPTO_ERROR = (short) 0x8444;
public static final short ALREADY_UPLOADED_POLICY_ERROR = (short) 0x8555;
public static final short STORAGE_UNUSED_ERROR = (short) 0x8666;
cant do cos (short) 0x6666 != (int) 0x6666...
*/

//global keytype bytes below
public static final byte PSBT_GLOBAL_UNSIGNED_TX = 0x00; // p2
public static final byte PSBT_GLOBAL_XPUB = 0x01;
Expand Down
11 changes: 11 additions & 0 deletions applet/src/main/java/applet/ArrayInDisguise.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package applet;

public class ArrayInDisguise {
public byte[] array;
public short offset;

ArrayInDisguise(short size) {
array = new byte[size];
offset = 0;
}
}
17 changes: 5 additions & 12 deletions applet/src/main/java/applet/FromApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void send_data(APDU apdu, GeneralMap map) {
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, KeyPair keyPair) {
static void send_data(APDU apdu, PSBTKeyPair keyPair) {
apdu.getBuffer()[0] = (byte) (keyPair.key.start << 8);
apdu.getBuffer()[1] = (byte) (keyPair.key.start);
apdu.getBuffer()[2] = (byte) ((keyPair.key.start + keyPair.getSize()) << 8);
Expand All @@ -48,17 +48,10 @@ public static void send_data(APDU apdu, GlobalUnsignedTXInput input) {
}

public static void send_data(APDU apdu, GlobalUnsignedTXOutput output) {
apdu.getBuffer()[0] = (byte) (output.value_start << 8);
apdu.getBuffer()[1] = (byte) (output.value_start);
apdu.getBuffer()[2] = (byte) ((output.size + output.value_start) << 8);
apdu.getBuffer()[3] = (byte) (output.size + output.value_start);
apdu.getBuffer()[0] = (byte) (output.valueStart << 8);
apdu.getBuffer()[1] = (byte) (output.valueStart);
apdu.getBuffer()[2] = (byte) ((output.size + output.valueStart) << 8);
apdu.getBuffer()[3] = (byte) (output.size + output.valueStart);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}
}

/**
static void send_data(APDU apdu, KeyPair key_pair) {
//TODO rewrite this to take a byte of KeyPair type instead of itself
send_data(apdu, PSBTdata, key_pair.start, key_pair.getSize());
}
**/
18 changes: 9 additions & 9 deletions applet/src/main/java/applet/GeneralMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ public class GeneralMap {

public short map_start = -1;
public short NUM_OF_KEYPAIR = 8;
public short current_key_pair = -1;
public KeyPair[] key_pairs = new KeyPair[NUM_OF_KEYPAIR];
public short currentKeyPair = -1;
public PSBTKeyPair[] keyPairs = new PSBTKeyPair[NUM_OF_KEYPAIR];
public short map_size = 0;

public GeneralMap() {
short i = 0;
while (i < NUM_OF_KEYPAIR) {
key_pairs[i] = new KeyPair();
keyPairs[i] = new PSBTKeyPair();
i++;
}
}

public void fill(short arrayIndex) {
map_start = (short) (arrayIndex + 1);
while ((PSBTdata[(short) (arrayIndex + map_size)] & 0xff) != (short) 0x00 && current_key_pair < (short) (NUM_OF_KEYPAIR - 1)) {
current_key_pair++;
key_pairs[current_key_pair].fill((short) (arrayIndex + map_size));
map_size += key_pairs[current_key_pair].getSize();
while ((PSBTdata[(short) (arrayIndex + map_size)] & 0xff) != (short) 0x00 && currentKeyPair < (short) (NUM_OF_KEYPAIR - 1)) {
currentKeyPair++;
keyPairs[currentKeyPair].fill((short) (arrayIndex + map_size));
map_size += keyPairs[currentKeyPair].getSize();
}
}

public void reset() {
short i = 0;
while (i < NUM_OF_KEYPAIR) {
key_pairs[i].reset();
keyPairs[i].reset();
i++;
}
map_start = -1;
current_key_pair = -1;
currentKeyPair = -1;
map_size = 0;
}
}
32 changes: 16 additions & 16 deletions applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@

public class GlobalMap extends GeneralMap {
short input_maps_total = -1;
short output_maps_total = -1;
short outputMapsTotal = -1;
static short PSBTversion = 0;
GlobalUnsignedTX globalUnsignedTX = new GlobalUnsignedTX();

public void fill(short arrayIndex) {
map_start = (short) (arrayIndex + 1);
while ((PSBTdata[(short) (arrayIndex + map_size)] & 0xff) != 0x00 && current_key_pair < (short) (NUM_OF_KEYPAIR - 1)) {
current_key_pair++;
key_pairs[current_key_pair].fill((short) (arrayIndex + map_size));
while ((PSBTdata[(short) (arrayIndex + map_size)] & 0xff) != 0x00 && currentKeyPair < (short) (NUM_OF_KEYPAIR - 1)) {
currentKeyPair++;
keyPairs[currentKeyPair].fill((short) (arrayIndex + map_size));

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_UNSIGNED_TX) {
if (keyPairs[currentKeyPair].key.keyKype == PSBT_GLOBAL_UNSIGNED_TX) {
globalUnsignedTX.fill((short) (arrayIndex + map_size + 2 +
key_pairs[current_key_pair].value.value_len_bytes));
keyPairs[currentKeyPair].value.value_len_bytes));
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_INPUT_COUNT) {
input_maps_total = key_pairs[current_key_pair].value.getByte((short) 0);
if (keyPairs[currentKeyPair].key.keyKype == PSBT_GLOBAL_INPUT_COUNT) {
input_maps_total = keyPairs[currentKeyPair].value.getByte((short) 0);
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_OUTPUT_COUNT) {
output_maps_total = key_pairs[current_key_pair].value.getByte((short) 0);
if (keyPairs[currentKeyPair].key.keyKype == PSBT_GLOBAL_OUTPUT_COUNT) {
outputMapsTotal = keyPairs[currentKeyPair].value.getByte((short) 0);
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_TX_VERSION) {
PSBTversion = key_pairs[current_key_pair].value.getByte((short) 0);
if (keyPairs[currentKeyPair].key.keyKype == PSBT_GLOBAL_TX_VERSION) {
PSBTversion = keyPairs[currentKeyPair].value.getByte((short) 0);
}

// TODO maybe add more special key types here later on

map_size += key_pairs[current_key_pair].getSize();
map_size += keyPairs[currentKeyPair].getSize();
}
}

public void reset() {
short i = 0;
while (i < NUM_OF_KEYPAIR) {
key_pairs[i].reset();
keyPairs[i].reset();
i++;
}
input_maps_total = -1;
output_maps_total = -1;
outputMapsTotal = -1;
PSBTversion = 0;
globalUnsignedTX.reset();
map_start = -1;
current_key_pair = -1;
currentKeyPair = -1;
map_size = 0;
}
}
3 changes: 0 additions & 3 deletions applet/src/main/java/applet/GlobalUnsignedTX.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package applet;

import static applet.MainApplet.PSBTdata;
import static applet.MainApplet.controlArray;
import static applet.Tools.byteSizeOfCWI;
import static applet.Tools.compactWeirdoInt;

Expand Down Expand Up @@ -36,7 +35,6 @@ public void fill(short arrayIndex){
version = (short) PSBTdata[start];
size += 4;
input_count = getCount();
controlArray[0] = (byte) getCount();
size += byteSizeOfCWI(input_count);


Expand All @@ -46,7 +44,6 @@ public void fill(short arrayIndex){
}

output_count = getCount();
controlArray[1] = (byte) getCount();
size += byteSizeOfCWI(output_count);

for (short i = 0; i < output_count; i++) {
Expand Down
6 changes: 3 additions & 3 deletions applet/src/main/java/applet/GlobalUnsignedTXOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import static applet.Tools.compactWeirdoInt;

public class GlobalUnsignedTXOutput {
short value_start = -1;
short valueStart = -1;
short script_size_start = -1;
short script_size = -1;
short script_pub_key_start = -1;
short size = 0;

void fill(short start){
value_start = start; // value has static size of 8 bytes
valueStart = start; // value has static size of 8 bytes
script_size_start = (short) (start + 8);
script_size = compactWeirdoInt(script_size_start);
script_pub_key_start = (short) (script_size_start + byteSizeOfCWI(script_size));
size = (short) (8 + byteSizeOfCWI(script_size) + script_size); // easier to read and understand this way
}

void reset() {
value_start = -1;
valueStart = -1;
script_size_start = -1;
script_size = -1;
script_pub_key_start = -1;
Expand Down
Loading

0 comments on commit cc0d717

Please sign in to comment.