diff options
| author | J08nY | 2017-11-26 23:50:00 +0100 |
|---|---|---|
| committer | J08nY | 2017-11-26 23:50:00 +0100 |
| commit | 7737039d8c1ad743ed1f5dc5e40224e297acd08d (patch) | |
| tree | 273d9e4895797c419a4a1fc26e489b2b0808fdf1 | |
| parent | 83943c809c56c1856038b21fd91f50cc709310aa (diff) | |
| download | ECTester-7737039d8c1ad743ed1f5dc5e40224e297acd08d.tar.gz ECTester-7737039d8c1ad743ed1f5dc5e40224e297acd08d.tar.zst ECTester-7737039d8c1ad743ed1f5dc5e40224e297acd08d.zip | |
| -rw-r--r-- | src/cz/crcs/ectester/common/util/CardUtil.java | 4 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/util/ECUtil.java | 37 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/ECTesterStandalone.java | 5 |
3 files changed, 44 insertions, 2 deletions
diff --git a/src/cz/crcs/ectester/common/util/CardUtil.java b/src/cz/crcs/ectester/common/util/CardUtil.java index a94c773..edcb510 100644 --- a/src/cz/crcs/ectester/common/util/CardUtil.java +++ b/src/cz/crcs/ectester/common/util/CardUtil.java @@ -7,6 +7,10 @@ import javacard.security.CryptoException; import static cz.crcs.ectester.applet.ECTesterApplet.*; +/** + * @author Petr Svenda petr@svenda.com + * @author Jan Jancar johny@neuromancer.sk + */ public class CardUtil { public static String getSWSource(short sw) { switch (sw) { diff --git a/src/cz/crcs/ectester/common/util/ECUtil.java b/src/cz/crcs/ectester/common/util/ECUtil.java new file mode 100644 index 0000000..713effe --- /dev/null +++ b/src/cz/crcs/ectester/common/util/ECUtil.java @@ -0,0 +1,37 @@ +package cz.crcs.ectester.common.util; + +import java.math.BigInteger; +import java.security.spec.ECPoint; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class ECUtil { + public static byte[] toX962Compressed(ECPoint point) { + if (point.equals(ECPoint.POINT_INFINITY)) { + return new byte[]{0}; + } + byte[] x = point.getAffineX().toByteArray(); + byte marker = (byte) (0x02 | point.getAffineY().mod(BigInteger.valueOf(2)).byteValue()); + return ByteUtil.concatenate(new byte[]{marker}, x); + } + + public static byte[] toX962Uncompressed(ECPoint point) { + if (point.equals(ECPoint.POINT_INFINITY)) { + return new byte[]{0}; + } + byte[] x = point.getAffineX().toByteArray(); + byte[] y = point.getAffineY().toByteArray(); + return ByteUtil.concatenate(new byte[]{0x04}, x, y); + } + + public static byte[] toX962Hybrid(ECPoint point) { + if (point.equals(ECPoint.POINT_INFINITY)) { + return new byte[]{0}; + } + byte[] x = point.getAffineX().toByteArray(); + byte[] y = point.getAffineY().toByteArray(); + byte marker = (byte) (0x06 | point.getAffineY().mod(BigInteger.valueOf(2)).byteValue()); + return ByteUtil.concatenate(new byte[]{marker}, x, y); + } +} diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index 9f100d0..af01a46 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -3,6 +3,7 @@ package cz.crcs.ectester.standalone; import cz.crcs.ectester.common.cli.*; import cz.crcs.ectester.common.ec.EC_Curve; import cz.crcs.ectester.common.util.ByteUtil; +import cz.crcs.ectester.common.util.ECUtil; import cz.crcs.ectester.data.EC_Store; import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; @@ -221,8 +222,8 @@ public class ECTesterStandalone { ECPublicKey publicKey = (ECPublicKey) kp.getPublic(); ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate(); - String pub = ByteUtil.bytesToHex(publicKey.getEncoded(), false); - String priv = ByteUtil.bytesToHex(privateKey.getEncoded(), false); + String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(publicKey.getW()), false); + String priv = ByteUtil.bytesToHex(privateKey.getS().toByteArray(), false); System.out.println(String.format("%d;%d;%s;%s", i, elapsed / 1000000, pub, priv)); } } |
