Skip to content

Commit 76bbbb3

Browse files
committed
Add exception to the set command that checks the generator and pubkey size.
1 parent 2d1f792 commit 76bbbb3

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

common/src/test/java/cz/crcs/ectester/common/CardIntegerPaddingTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public void testCurves() {
9494
} catch (AssertionFailedError e) {
9595
errors.add(e);
9696
}
97+
byte[][] field = curve.getParam(EC_Consts.PARAMETER_FP);
98+
byte[] p = field[0];
99+
try {
100+
assertEquals(bytes, p.length, "Curve: " + category.getName() + "/" + curve.getId() + " (p)");
101+
} catch (AssertionFailedError e) {
102+
errors.add(e);
103+
}
97104
}
98105
}
99106
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package cz.crcs.ectester.reader.command;
22

3+
import cz.crcs.ectester.common.ec.EC_Consts;
34
import cz.crcs.ectester.common.ec.EC_Curve;
45
import cz.crcs.ectester.common.ec.EC_Params;
5-
import cz.crcs.ectester.common.ec.EC_Consts;
66
import cz.crcs.ectester.common.util.ByteUtil;
7+
import cz.crcs.ectester.common.util.CardConsts;
78
import cz.crcs.ectester.common.util.CardUtil;
89
import cz.crcs.ectester.common.util.ECUtil;
9-
import cz.crcs.ectester.common.util.CardConsts;
1010
import cz.crcs.ectester.data.EC_Store;
1111
import cz.crcs.ectester.reader.CardMngr;
1212
import cz.crcs.ectester.reader.ECTesterReader;
@@ -19,7 +19,6 @@
1919
import java.io.FileInputStream;
2020
import java.io.IOException;
2121
import java.util.ArrayList;
22-
import java.util.Arrays;
2322
import java.util.List;
2423

2524
/**
@@ -372,6 +371,25 @@ public Set(CardMngr cardManager, byte keyPair, byte curve, short params, byte[]
372371
ByteUtil.setShort(data, 0, params);
373372
if (external != null) {
374373
System.arraycopy(external, 0, data, 2, external.length);
374+
if ((params & EC_Consts.PARAMETER_FP) != 0) {
375+
EC_Params par = new EC_Params(params);
376+
par.readBytes(external);
377+
byte[][] prime = par.getParam(EC_Consts.PARAMETER_FP);
378+
byte[] p = prime[0];
379+
int bytes = p.length;
380+
if ((params & EC_Consts.PARAMETER_G) != 0) {
381+
byte[][] generator = par.getParam(EC_Consts.PARAMETER_G);
382+
if (generator[0].length != bytes || generator[1].length != bytes) {
383+
throw new IllegalArgumentException("Generator point does not match prime field size.");
384+
}
385+
}
386+
if ((params & EC_Consts.PARAMETER_W) != 0) {
387+
byte[][] w = par.getParam(EC_Consts.PARAMETER_W);
388+
if (w[0].length != bytes || w[1].length != bytes) {
389+
throw new IllegalArgumentException("Public key point does not match prime field size.");
390+
}
391+
}
392+
}
375393
}
376394

377395
this.cmd = new CommandAPDU(CardConsts.CLA_ECTESTERAPPLET, CardConsts.INS_SET, keyPair, curve, data);

0 commit comments

Comments
 (0)