Skip to content

Commit

Permalink
feat: final touches and aid added
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed May 16, 2023
1 parent 1dbbf83 commit 5de1124
Show file tree
Hide file tree
Showing 26 changed files with 437 additions and 589 deletions.
8 changes: 5 additions & 3 deletions applet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ javacard {
cap {
packageName 'applet'
version '0.1'
aid '01:ff:ff:04:05:06:07:08:00'
output 'applet_main_applet.cap'
aid '50:53:42:54:50:6f:6c:69:63:79:56:61:6c:2e' // PSBTPolicyVal.
//aid '01:ff:ff:04:05:06:07:08:00'
output 'PSBTPolicyValidator.cap'

sources 'src/main/java/applet'

Expand All @@ -145,7 +146,8 @@ javacard {
//noinspection GroovyAssignabilityCheck
applet {
className 'applet.MainApplet'
aid '01:ff:ff:04:05:06:07:08:00:01:02'
aid '50:53:42:54:50:6f:6c:69:63:79:56:61:6c:2e:01:02'
//aid '01:ff:ff:04:05:06:07:08:00:01:02'
}

//noinspection GroovyAssignabilityCheck
Expand Down
11 changes: 4 additions & 7 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,17 @@ public AppletInstructions() {
*/
public static final short INSTRUCTION_VERSION = 0;

public static final short CLASS_PSBT_UPLOAD = 0;
public static final short CLASS_PSBT_UP = 0;

public static final short CLASS_POLICY_UPLOAD = 1;
public static final short CLASS_POLICY_UP = 1;
// simple scenario where applet returns "HAND SHAKE" in bytes
public static final short CLASS_HAND_SHAKE = 3;
public static final short CLASS_ADDITIONAL_DATA_UPLOAD = 2; // p1 will determine reference, where to store it
public static final short CLASS_DOWNLOAD_PSBT_ARRAY = 4;
public static final short CLASS_ADD_DATA_UP = 2; // p1 will determine reference, where to store it
public static final short CLASS_DOWN_PSBT_ARRAY = 4;

public static final short CLASS_SET_MODE_SECRET = 14;
public static final short CLASS_SET_MODE_TIME = 15;

/**
* Below are debug classes
*/
public static final short CLASS_DOWNLOAD_GLOBAL_MAP = 5; // p1 which global map, here always 0
public static final short INS_DOWNLOAD_NUM_INPUT_V0 = 5;
public static final short INS_DOWNLOAD_NUM_OUTPUT_V0 = 4;
Expand Down
29 changes: 14 additions & 15 deletions applet/src/main/java/applet/FromApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,42 @@ public class FromApplet {
* @param apdu apdu used for communication
* @param array array of data to send
*/

static void send_data(APDU apdu, byte[] array, short from, short to) {
static void sendData(APDU apdu, byte[] array, short from, short to) {
Util.arrayCopyNonAtomic(array, from, apdu.getBuffer(), (byte) 0, (short) (to - from));
apdu.setOutgoingAndSend((short) 0, (short) (to - from));
}

static void send_data(APDU apdu, short data) {
static void sendData(APDU apdu, short data) {
apdu.getBuffer()[0] = (byte) (data << 8);
apdu.getBuffer()[1] = (byte) (data);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, GeneralMap map) {
apdu.getBuffer()[0] = (byte) (map.map_start << 8);
apdu.getBuffer()[1] = (byte) (map.map_start);
apdu.getBuffer()[2] = (byte) ((map.map_start + map.map_size) << 8);
apdu.getBuffer()[3] = (byte) (map.map_start + map.map_size);
static void sendData(APDU apdu, GeneralMap map) {
apdu.getBuffer()[0] = (byte) (map.mapStart << 8);
apdu.getBuffer()[1] = (byte) (map.mapStart);
apdu.getBuffer()[2] = (byte) ((map.mapStart + map.mapSize) << 8);
apdu.getBuffer()[3] = (byte) (map.mapStart + map.mapSize);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, PSBTKeyPair keyPair) {
static void sendData(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);
apdu.getBuffer()[3] = (byte) (keyPair.key.start + keyPair.getSize());
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

public static void send_data(APDU apdu, GlobalUnsignedTXInput input) {
apdu.getBuffer()[0] = (byte) (input.previous_output_start << 8);
apdu.getBuffer()[1] = (byte) (input.previous_output_start);
apdu.getBuffer()[2] = (byte) ((input.size + input.previous_output_start) << 8);
apdu.getBuffer()[3] = (byte) (input.size + input.previous_output_start);
public static void sendData(APDU apdu, GlobalUnsignedTXInput input) {
apdu.getBuffer()[0] = (byte) (input.previousOutputStart << 8);
apdu.getBuffer()[1] = (byte) (input.previousOutputStart);
apdu.getBuffer()[2] = (byte) ((input.size + input.previousOutputStart) << 8);
apdu.getBuffer()[3] = (byte) (input.size + input.previousOutputStart);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

public static void send_data(APDU apdu, GlobalUnsignedTXOutput output) {
public static void sendData(APDU apdu, GlobalUnsignedTXOutput output) {
apdu.getBuffer()[0] = (byte) (output.valueStart << 8);
apdu.getBuffer()[1] = (byte) (output.valueStart);
apdu.getBuffer()[2] = (byte) ((output.size + output.valueStart) << 8);
Expand Down
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 @@ -4,11 +4,11 @@

public class GeneralMap {

public short map_start = -1;
public short NUM_OF_KEYPAIR = 8;
public short mapStart = -1;
public final static short NUM_OF_KEYPAIR = 8;
public short currentKeyPair = -1;
public PSBTKeyPair[] keyPairs = new PSBTKeyPair[NUM_OF_KEYPAIR];
public short map_size = 0;
public short mapSize = 0;

public GeneralMap() {
short i = 0;
Expand All @@ -19,11 +19,11 @@ public GeneralMap() {
}

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

Expand All @@ -33,8 +33,8 @@ public void reset() {
keyPairs[i].reset();
i++;
}
map_start = -1;
mapStart = -1;
currentKeyPair = -1;
map_size = 0;
mapSize = 0;
}
}
24 changes: 11 additions & 13 deletions applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
import static applet.MainApplet.PSBTdata;

public class GlobalMap extends GeneralMap {
short input_maps_total = -1;
short inputMapsTotal = -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 && currentKeyPair < (short) (NUM_OF_KEYPAIR - 1)) {
mapStart = (short) (arrayIndex + 1);
while ((PSBTdata[(short) (arrayIndex + mapSize)] & 0xff) != 0x00 && currentKeyPair < (short) (NUM_OF_KEYPAIR - 1)) {
currentKeyPair++;
keyPairs[currentKeyPair].fill((short) (arrayIndex + map_size));
keyPairs[currentKeyPair].fill((short) (arrayIndex + mapSize));

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

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

if (keyPairs[currentKeyPair].key.keyKype == PSBT_GLOBAL_OUTPUT_COUNT) {
Expand All @@ -32,9 +32,7 @@ public void fill(short arrayIndex) {
PSBTversion = keyPairs[currentKeyPair].value.getByte((short) 0);
}

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

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

Expand All @@ -44,12 +42,12 @@ public void reset() {
keyPairs[i].reset();
i++;
}
input_maps_total = -1;
inputMapsTotal = -1;
outputMapsTotal = -1;
PSBTversion = 0;
globalUnsignedTX.reset();
map_start = -1;
mapStart = -1;
currentKeyPair = -1;
map_size = 0;
mapSize = 0;
}
}
23 changes: 9 additions & 14 deletions applet/src/main/java/applet/GlobalUnsignedTX.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class GlobalUnsignedTX {
short version = -1;
short size = 0;

short input_count = -1;
short inputCount = -1;
GlobalUnsignedTXInput[] inputs = new GlobalUnsignedTXInput[MAX_COUNT_OF_IO];

short output_count = -1;
short outputCount = -1;
GlobalUnsignedTXOutput[] outputs = new GlobalUnsignedTXOutput[MAX_COUNT_OF_IO];

short lockTimeStart = -1;
Expand All @@ -32,21 +32,21 @@ public class GlobalUnsignedTX {

public void fill(short arrayIndex){
start = arrayIndex;
version = (short) PSBTdata[start];
version = PSBTdata[start];
size += 4;
input_count = getCount();
size += byteSizeOfCWI(input_count);
inputCount = getCount();
size += byteSizeOfCWI(inputCount);


for (short i = 0; i < input_count; i++) {
for (short i = 0; i < inputCount; i++) {
inputs[i].fill((short) (start + size));
size += inputs[i].size;
}

output_count = getCount();
size += byteSizeOfCWI(output_count);
outputCount = getCount();
size += byteSizeOfCWI(outputCount);

for (short i = 0; i < output_count; i++) {
for (short i = 0; i < outputCount; i++) {
outputs[i].fill((short)(start + size));
size += outputs[i].size;
}
Expand All @@ -57,11 +57,6 @@ short getCount() {
return compactWeirdoInt((short) (start + size));
}

short ignoreInput(short bytesIgnored) {
short signature_scrip_size = compactWeirdoInt((short) (bytesIgnored + 36));
return (short) (signature_scrip_size + 40 + byteSizeOfCWI(signature_scrip_size));
}

void reset() {
short i = 0;
while (i < MAX_COUNT_OF_IO) {
Expand Down
32 changes: 16 additions & 16 deletions applet/src/main/java/applet/GlobalUnsignedTXInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
import static applet.Tools.compactWeirdoInt;

public class GlobalUnsignedTXInput {
short previous_output_start = -1;
short script_size_start = -1;
short script_size = -1;
short script_sig_start = -1;
short sequence_start = -1;
short previousOutputStart = -1;
short scriptSizeStart = -1;
short scriptSize = -1;
short scriptSigStart = -1;
short sequenceStart = -1;
short size = 0;

void fill(short start){
previous_output_start = start;
script_size_start = (short) (start + 36);
script_size = compactWeirdoInt(script_size_start);
script_sig_start = (short) (script_size_start + byteSizeOfCWI(script_size));
sequence_start = (short) (script_sig_start + script_size);
size = (short) (36 + byteSizeOfCWI(script_size) + script_size + 4); // easier to read and understand this way
previousOutputStart = start;
scriptSizeStart = (short) (start + 36);
scriptSize = compactWeirdoInt(scriptSizeStart);
scriptSigStart = (short) (scriptSizeStart + byteSizeOfCWI(scriptSize));
sequenceStart = (short) (scriptSigStart + scriptSize);
size = (short) (36 + byteSizeOfCWI(scriptSize) + scriptSize + 4); // easier to read and understand this way
}

void reset(){
previous_output_start = -1;
script_size_start = -1;
script_size = -1;
script_sig_start = -1;
sequence_start = -1;
previousOutputStart = -1;
scriptSizeStart = -1;
scriptSize = -1;
scriptSigStart = -1;
sequenceStart = -1;
size = 0;
}
}
20 changes: 10 additions & 10 deletions applet/src/main/java/applet/GlobalUnsignedTXOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

public class GlobalUnsignedTXOutput {
short valueStart = -1;
short script_size_start = -1;
short script_size = -1;
short script_pub_key_start = -1;
short scriptSizeStart = -1;
short scriptSize = -1;
short scriptPubKeyStart = -1;
short size = 0;

void fill(short start){
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
scriptSizeStart = (short) (start + 8);
scriptSize = compactWeirdoInt(scriptSizeStart);
scriptPubKeyStart = (short) (scriptSizeStart + byteSizeOfCWI(scriptSize));
size = (short) (8 + byteSizeOfCWI(scriptSize) + scriptSize); // easier to read and understand this way
}

void reset() {
valueStart = -1;
script_size_start = -1;
script_size = -1;
script_pub_key_start = -1;
scriptSizeStart = -1;
scriptSize = -1;
scriptPubKeyStart = -1;
size = 0;
}
}
Loading

0 comments on commit 5de1124

Please sign in to comment.