Skip to content

Instantly share code, notes, and snippets.

@im-infamou5
Created March 29, 2016 17:15
Show Gist options
  • Save im-infamou5/88e80c31418a2f0528eb31ce12fb650e to your computer and use it in GitHub Desktop.
Save im-infamou5/88e80c31418a2f0528eb31ce12fb650e to your computer and use it in GitHub Desktop.
jcardsim API extension for ECC support
package com.licel.jcardsim.crypto;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import java.math.BigInteger;
public class ECOperations {
private BigInteger prime, A, B;
public byte[] Qx = new byte[32], Qy = new byte[32];
public ECOperations(byte[] prime, byte[] A, byte[] B) {
this.A = new BigInteger(A);
this.B = new BigInteger(B);
this.prime = new BigInteger(prime);
}
public void addPoints(byte[] x1, byte[] y1, byte[] x2, byte[] y2, byte[] xresult, byte[] yresult)
{
BigInteger Px = new BigInteger(x1);
BigInteger Py = new BigInteger(y1);
BigInteger Qx = new BigInteger(x2);
BigInteger Qy = new BigInteger(y2);
ECCurve curve = new ECCurve.Fp(this.prime, this.A, this.B);
/*
// Explicit affine addition
ECFieldElement xp = curve.fromBigInteger(Px), yp = curve.fromBigInteger(Py);
ECFieldElement xq = curve.fromBigInteger(Qx), yq = curve.fromBigInteger(Qy);
ECFieldElement alpha = yq.subtract(yp).divide(xq.subtract(xp));
ECFieldElement xr = alpha.square().subtract(xp).subtract(xq);
ECFieldElement yr = xp.subtract(xr).multiply(alpha).subtract(yp);
*/
// Point addition using built-in formulae
ECPoint P = curve.createPoint(Px, Py, false);
ECPoint Q = curve.createPoint(Qx, Qy, false);
ECPoint R = P.add(Q);
xresult = R.getX().toBigInteger().toByteArray();
yresult = R.getY().toBigInteger().toByteArray();
}
public void multiplyPoint(byte[] x, byte[] y, byte[] multiplier)
{
BigInteger Px = new BigInteger(x);
BigInteger Py = new BigInteger(y);
BigInteger d = new BigInteger(multiplier);
ECCurve curve = new ECCurve.Fp(this.prime, this.A, this.B);
ECPoint P = curve.createPoint(Px, Py, false);
ECPoint R = P.multiply(d);
Qx = R.getX().toBigInteger().toByteArray();
Qy = R.getY().toBigInteger().toByteArray();
}
}
package com.licel.jcardsim.SESPAKE;
import com.licel.jcardsim.crypto.ECOperations;
import javacard.framework.JCSystem;
import javacard.security.RandomData;
class JCECC {
private final static byte[] ECCp_tc26_256_paramSetA = {
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xfd, (byte) 0x97
};
private final static byte[] ECCa_tc26_256_paramSetA = {
(byte) 0xC2, (byte) 0x17, (byte) 0x3F, (byte) 0x15,
(byte) 0x13, (byte) 0x98, (byte) 0x16, (byte) 0x73,
(byte) 0xAF, (byte) 0x48, (byte) 0x92, (byte) 0xC2,
(byte) 0x30, (byte) 0x35, (byte) 0xA2, (byte) 0x7C,
(byte) 0xE2, (byte) 0x5E, (byte) 0x20, (byte) 0x13,
(byte) 0xBF, (byte) 0x95, (byte) 0xAA, (byte) 0x33,
(byte) 0xB2, (byte) 0x2C, (byte) 0x65, (byte) 0x6F,
(byte) 0x27, (byte) 0x7E, (byte) 0x73, (byte) 0x35
};
private final static byte[] ECCb_tc26_256_paramSetA = {
(byte) 0x29, (byte) 0x5F, (byte) 0x9B, (byte) 0xAE,
(byte) 0x74, (byte) 0x28, (byte) 0xED, (byte) 0x9C,
(byte) 0xCC, (byte) 0x20, (byte) 0xE7, (byte) 0xC3,
(byte) 0x59, (byte) 0xA9, (byte) 0xD4, (byte) 0x1A,
(byte) 0x22, (byte) 0xFC, (byte) 0xCD, (byte) 0x91,
(byte) 0x08, (byte) 0xE1, (byte) 0x7B, (byte) 0xF7,
(byte) 0xBA, (byte) 0x93, (byte) 0x37, (byte) 0xA6,
(byte) 0xF8, (byte) 0xAE, (byte) 0x95, (byte) 0x13
};
private final static byte[] ECCq_tc26_256_paramSetA = {
(byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x0F, (byte) 0xD8, (byte) 0xCD, (byte) 0xDF,
(byte) 0xC8, (byte) 0x7B, (byte) 0x66, (byte) 0x35,
(byte) 0xC1, (byte) 0x15, (byte) 0xAF, (byte) 0x55,
(byte) 0x6C, (byte) 0x36, (byte) 0x0C, (byte) 0x67
};
//P.x
private final static byte[] ECCx_tc26_256_paramSetA = {
(byte) 0x91, (byte) 0xE3, (byte) 0x84, (byte) 0x43,
(byte) 0xA5, (byte) 0xE8, (byte) 0x2C, (byte) 0x0D,
(byte) 0x88, (byte) 0x09, (byte) 0x23, (byte) 0x42,
(byte) 0x57, (byte) 0x12, (byte) 0xB2, (byte) 0xBB,
(byte) 0x65, (byte) 0x8B, (byte) 0x91, (byte) 0x96,
(byte) 0x93, (byte) 0x2E, (byte) 0x02, (byte) 0xC7,
(byte) 0x8B, (byte) 0x25, (byte) 0x82, (byte) 0xFE,
(byte) 0x74, (byte) 0x2D, (byte) 0xAA, (byte) 0x28
};
//P.y
private final static byte[] ECCy_tc26_256_paramSetA = {
(byte) 0x32, (byte) 0x87, (byte) 0x94, (byte) 0x23,
(byte) 0xAB, (byte) 0x1A, (byte) 0x03, (byte) 0x75,
(byte) 0x89, (byte) 0x57, (byte) 0x86, (byte) 0xC4,
(byte) 0xBB, (byte) 0x46, (byte) 0xE9, (byte) 0x56,
(byte) 0x5F, (byte) 0xDE, (byte) 0x0B, (byte) 0x53,
(byte) 0x44, (byte) 0x76, (byte) 0x67, (byte) 0x40,
(byte) 0xAF, (byte) 0x26, (byte) 0x8A, (byte) 0xDB,
(byte) 0x32, (byte) 0x32, (byte) 0x2E, (byte) 0x5C
};
JCECC(short pointsize) {
this.pointsize = pointsize;
}
private short pointsize;
private ECOperations eco = new ECOperations(ECCp_tc26_256_paramSetA, ECCa_tc26_256_paramSetA, ECCb_tc26_256_paramSetA);
private byte[] pointMultiplier = JCSystem.makeTransientByteArray(pointsize, JCSystem.CLEAR_ON_DESELECT);
public byte[] Qx = JCSystem.makeTransientByteArray(pointsize, JCSystem.CLEAR_ON_DESELECT);
public byte[] Qy = JCSystem.makeTransientByteArray(pointsize, JCSystem.CLEAR_ON_DESELECT);
private byte[] Rx = JCSystem.makeTransientByteArray(pointsize, JCSystem.CLEAR_ON_DESELECT);
private byte[] Ry = JCSystem.makeTransientByteArray(pointsize, JCSystem.CLEAR_ON_DESELECT);
public byte[] getRx() {
return Rx;
}
public byte[] getRy() {
return Ry;
}
public void setRx(byte[] rx) {
Rx = rx;
}
public void setRy(byte[] ry) {
Ry = ry;
}
final void generateTokenPoint() {
RandomData rd;
rd = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
rd.generateData(pointMultiplier, (short) 0, pointsize);
}
public final void multiplyBasepoint() {
eco.multiplyPoint(ECCx_tc26_256_paramSetA, ECCy_tc26_256_paramSetA, pointMultiplier);
Qx = eco.Qx;
Qy = eco.Qy;
}
public final void addPoints(byte[] Qx, byte[] Qy, byte[] Rx, byte[] Ry) {
eco.addPoints(Qx, Qy, Rx, Ry, Rx, Ry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment