Skip to content

Commit 08623da

Browse files
committed
Fix point length check.
1 parent 79a611a commit 08623da

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

common/src/main/java/cz/crcs/ectester/common/ec/EC_Data.java

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public boolean readBytes(byte[] bytes) {
152152
return false;
153153
}
154154
short paramLength = ByteUtil.getShort(bytes, offset);
155+
System.out.println("paramLength: " + paramLength);
155156
offset += 2;
156157
if (bytes.length < offset + paramLength) {
157158
return false;

common/src/main/java/cz/crcs/ectester/common/ec/EC_Params.java

+63
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cz.crcs.ectester.common.ec;
22

33
import cz.crcs.ectester.common.util.ByteUtil;
4+
import cz.crcs.ectester.common.util.CardUtil;
45

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

206+
public boolean inflate(byte[] flattened) {
207+
short paramMask = EC_Consts.PARAMETER_FP;
208+
int i = 0;
209+
int offset = 0;
210+
while (paramMask <= EC_Consts.PARAMETER_S) {
211+
short masked = (short) (this.params & paramMask);
212+
if (masked != 0) {
213+
short length = ByteUtil.getShort(flattened, offset);
214+
offset += 2;
215+
byte[] param = new byte[length];
216+
System.arraycopy(flattened, offset, param, 0, length);
217+
offset += length;
218+
//System.out.println(CardUtil.getParams(masked) + " Length: " + length + " Param: " + ByteUtil.bytesToHex(param, false));
219+
//System.out.println();
220+
if (masked == EC_Consts.PARAMETER_F2M) {
221+
data[i] = new byte[2];
222+
data[i + 1] = new byte[2];
223+
data[i + 2] = new byte[2];
224+
data[i + 3] = new byte[2];
225+
if (length == 4) {
226+
// only m and e_1, other e are zero
227+
System.arraycopy(param, 0, data[i], 0, 2);
228+
System.arraycopy(param, 2, data[i + 1], 0, 2);
229+
} else if (length == 8) {
230+
// all m, e_1, e_2, e_3 are specified
231+
System.arraycopy(param, 0, data[i], 0, 2);
232+
System.arraycopy(param, 2, data[i + 1], 0, 2);
233+
System.arraycopy(param, 4, data[i + 2], 0, 2);
234+
System.arraycopy(param, 6, data[i + 3], 0, 2);
235+
}
236+
} else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) {
237+
if ((length - 1) % 2 != 0) {
238+
return false;
239+
}
240+
int half = (length - 1) / 2;
241+
data[i] = new byte[half];
242+
data[i + 1] = new byte[half];
243+
System.arraycopy(param, 1, data[i], 0, half);
244+
System.arraycopy(param, 1 + half, data[i + 1], 0, half);
245+
} else {
246+
data[i] = param;
247+
}
248+
}
249+
250+
if (masked == EC_Consts.PARAMETER_F2M) {
251+
i += 4;
252+
} else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) {
253+
i += 2;
254+
} else if (masked != 0) {
255+
i++;
256+
}
257+
paramMask = (short) (paramMask << 1);
258+
}
259+
return true;
260+
}
261+
262+
public static EC_Params inflate(short params, byte[] flattened) {
263+
EC_Params p = new EC_Params(params);
264+
p.inflate(flattened);
265+
return p;
266+
}
267+
205268
@Override
206269
public String[] expand() {
207270
List<String> out = new ArrayList<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cz.crcs.ectester.common;
2+
3+
import cz.crcs.ectester.common.ec.EC_Category;
4+
import cz.crcs.ectester.common.ec.EC_Data;
5+
import cz.crcs.ectester.common.ec.EC_Params;
6+
import cz.crcs.ectester.data.EC_Store;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Map;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class ParamSerializationTests {
14+
15+
@Test
16+
public void test() {
17+
EC_Store store = EC_Store.getInstance();
18+
Map<String, EC_Category> categories = store.getCategories();
19+
20+
for (EC_Category category : categories.values()) {
21+
Map<String, EC_Data> objects = category.getObjects();
22+
for (EC_Data object : objects.values()) {
23+
if (object instanceof EC_Params) {
24+
EC_Params params = (EC_Params) object;
25+
byte[] serialized = params.flatten();
26+
EC_Params deserialized = new EC_Params(params.getId(), params.getParams());
27+
deserialized.inflate(serialized);
28+
assertEquals(params, deserialized, "Category: " + category.getName() + ", Params: " + params.getId());
29+
}
30+
}
31+
}
32+
}
33+
}

reader/src/main/java/cz/crcs/ectester/reader/command/Command.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,18 @@ public Set(CardMngr cardManager, byte keyPair, byte curve, short params, byte[]
373373
System.arraycopy(external, 0, data, 2, external.length);
374374
if ((params & EC_Consts.PARAMETER_FP) != 0) {
375375
EC_Params par = new EC_Params(params);
376-
par.readBytes(external);
376+
boolean read = par.inflate(external);
377+
if (!read) {
378+
throw new IllegalArgumentException("External curve data does not match parameters.");
379+
}
377380
byte[][] prime = par.getParam(EC_Consts.PARAMETER_FP);
378381
byte[] p = prime[0];
379382
int bytes = p.length;
380383
if ((params & EC_Consts.PARAMETER_G) != 0) {
381384
byte[][] generator = par.getParam(EC_Consts.PARAMETER_G);
382385
if (generator[0].length != bytes || generator[1].length != bytes) {
386+
System.err.println("Generator x length: " + generator[0].length + " vs " + bytes);
387+
System.err.println("Generator y length: " + generator[1].length + " vs " + bytes);
383388
throw new IllegalArgumentException("Generator point does not match prime field size.");
384389
}
385390
}

0 commit comments

Comments
 (0)