Skip to content

Commit

Permalink
Fix point length check.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Jan 30, 2025
1 parent 79a611a commit 08623da
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public boolean readBytes(byte[] bytes) {
return false;
}
short paramLength = ByteUtil.getShort(bytes, offset);
System.out.println("paramLength: " + paramLength);
offset += 2;
if (bytes.length < offset + paramLength) {
return false;
Expand Down
63 changes: 63 additions & 0 deletions common/src/main/java/cz/crcs/ectester/common/ec/EC_Params.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.crcs.ectester.common.ec;

import cz.crcs.ectester.common.util.ByteUtil;
import cz.crcs.ectester.common.util.CardUtil;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -202,6 +203,68 @@ public byte[] flatten(short params) {
return (out.size() == 0) ? null : out.toByteArray();
}

public boolean inflate(byte[] flattened) {
short paramMask = EC_Consts.PARAMETER_FP;
int i = 0;
int offset = 0;
while (paramMask <= EC_Consts.PARAMETER_S) {
short masked = (short) (this.params & paramMask);
if (masked != 0) {
short length = ByteUtil.getShort(flattened, offset);
offset += 2;
byte[] param = new byte[length];
System.arraycopy(flattened, offset, param, 0, length);
offset += length;
//System.out.println(CardUtil.getParams(masked) + " Length: " + length + " Param: " + ByteUtil.bytesToHex(param, false));
//System.out.println();
if (masked == EC_Consts.PARAMETER_F2M) {
data[i] = new byte[2];
data[i + 1] = new byte[2];
data[i + 2] = new byte[2];
data[i + 3] = new byte[2];
if (length == 4) {
// only m and e_1, other e are zero
System.arraycopy(param, 0, data[i], 0, 2);
System.arraycopy(param, 2, data[i + 1], 0, 2);
} else if (length == 8) {
// all m, e_1, e_2, e_3 are specified
System.arraycopy(param, 0, data[i], 0, 2);
System.arraycopy(param, 2, data[i + 1], 0, 2);
System.arraycopy(param, 4, data[i + 2], 0, 2);
System.arraycopy(param, 6, data[i + 3], 0, 2);
}
} else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) {
if ((length - 1) % 2 != 0) {
return false;
}
int half = (length - 1) / 2;
data[i] = new byte[half];
data[i + 1] = new byte[half];
System.arraycopy(param, 1, data[i], 0, half);
System.arraycopy(param, 1 + half, data[i + 1], 0, half);
} else {
data[i] = param;
}
}

if (masked == EC_Consts.PARAMETER_F2M) {
i += 4;
} else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) {
i += 2;
} else if (masked != 0) {
i++;
}
paramMask = (short) (paramMask << 1);
}
return true;
}

public static EC_Params inflate(short params, byte[] flattened) {
EC_Params p = new EC_Params(params);
p.inflate(flattened);
return p;
}

@Override
public String[] expand() {
List<String> out = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cz.crcs.ectester.common;

import cz.crcs.ectester.common.ec.EC_Category;
import cz.crcs.ectester.common.ec.EC_Data;
import cz.crcs.ectester.common.ec.EC_Params;
import cz.crcs.ectester.data.EC_Store;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParamSerializationTests {

@Test
public void test() {
EC_Store store = EC_Store.getInstance();
Map<String, EC_Category> categories = store.getCategories();

for (EC_Category category : categories.values()) {
Map<String, EC_Data> objects = category.getObjects();
for (EC_Data object : objects.values()) {
if (object instanceof EC_Params) {
EC_Params params = (EC_Params) object;
byte[] serialized = params.flatten();
EC_Params deserialized = new EC_Params(params.getId(), params.getParams());
deserialized.inflate(serialized);
assertEquals(params, deserialized, "Category: " + category.getName() + ", Params: " + params.getId());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,18 @@ public Set(CardMngr cardManager, byte keyPair, byte curve, short params, byte[]
System.arraycopy(external, 0, data, 2, external.length);
if ((params & EC_Consts.PARAMETER_FP) != 0) {
EC_Params par = new EC_Params(params);
par.readBytes(external);
boolean read = par.inflate(external);
if (!read) {
throw new IllegalArgumentException("External curve data does not match parameters.");
}
byte[][] prime = par.getParam(EC_Consts.PARAMETER_FP);
byte[] p = prime[0];
int bytes = p.length;
if ((params & EC_Consts.PARAMETER_G) != 0) {
byte[][] generator = par.getParam(EC_Consts.PARAMETER_G);
if (generator[0].length != bytes || generator[1].length != bytes) {
System.err.println("Generator x length: " + generator[0].length + " vs " + bytes);
System.err.println("Generator y length: " + generator[1].length + " vs " + bytes);
throw new IllegalArgumentException("Generator point does not match prime field size.");
}
}
Expand Down

0 comments on commit 08623da

Please sign in to comment.