use of java.security.spec.ECFieldFp in project robovm by robovm.
the class ECFieldFpTest method testHashCode01.
/**
* Test #1 for <code>hashCode()</code> method.<br>
*
* Assertion: must return the same value if invoked
* repeatedly on the same object.
*/
public final void testHashCode01() {
ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
int hc = f.hashCode();
assertTrue(hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode() && hc == f.hashCode());
}
use of java.security.spec.ECFieldFp in project cxf by apache.
the class JweUtils method getECDHKey.
public static byte[] getECDHKey(ECPrivateKey privateKey, ECPublicKey peerPublicKey, byte[] partyUInfo, byte[] partyVInfo, String algoName, int algoKeyBitLen) {
// Step 1: Verify public key is not point at infinity.
if (ECPoint.POINT_INFINITY.equals(peerPublicKey.getW())) {
throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
}
EllipticCurve curve = peerPublicKey.getParams().getCurve();
final BigInteger x = peerPublicKey.getW().getAffineX();
final BigInteger y = peerPublicKey.getW().getAffineY();
final BigInteger p = ((ECFieldFp) curve.getField()).getP();
// Step 2: Verify x and y are in range [0,p-1]
if (x.compareTo(BigInteger.ZERO) < 0 || x.compareTo(p) >= 0 || y.compareTo(BigInteger.ZERO) < 0 || y.compareTo(p) >= 0) {
throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
}
final BigInteger a = curve.getA();
final BigInteger b = curve.getB();
// Step 3: Verify that y^2 == x^3 + ax + b (mod p)
final BigInteger ySquared = y.modPow(BigInteger.valueOf(2), p);
final BigInteger xCubedPlusAXPlusB = x.modPow(BigInteger.valueOf(3), p).add(a.multiply(x)).add(b).mod(p);
if (!ySquared.equals(xCubedPlusAXPlusB)) {
throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
}
// All the NIST curves used here define h = 1.
if (peerPublicKey.getParams().getCofactor() != 1) {
throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
}
// Finally calculate the derived key
byte[] keyZ = generateKeyZ(privateKey, peerPublicKey);
return calculateDerivedKey(keyZ, algoName, partyUInfo, partyVInfo, algoKeyBitLen);
}
use of java.security.spec.ECFieldFp in project wycheproof by google.
the class EcUtil method getBrainpoolP256r1Params.
public static ECParameterSpec getBrainpoolP256r1Params() {
BigInteger p = new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", 16);
BigInteger a = new BigInteger("7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", 16);
BigInteger b = new BigInteger("26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", 16);
BigInteger x = new BigInteger("8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", 16);
BigInteger y = new BigInteger("547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", 16);
BigInteger n = new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", 16);
final int h = 1;
ECFieldFp fp = new ECFieldFp(p);
EllipticCurve curve = new EllipticCurve(fp, a, b);
ECPoint g = new ECPoint(x, y);
return new ECParameterSpec(curve, g, n, h);
}
use of java.security.spec.ECFieldFp in project wycheproof by google.
the class EcUtil method decompressPoint.
/**
* Decompress a point on an elliptic curve.
*
* @param bytes The compressed point. Its representation is z || x where z is 2+lsb(y) and x is
* using a unsigned fixed length big-endian representation.
* @param ecParams the specification of the curve. Only Weierstrass curves over prime order fields
* are implemented.
*/
public static ECPoint decompressPoint(byte[] bytes, ECParameterSpec ecParams) throws GeneralSecurityException {
EllipticCurve ec = ecParams.getCurve();
ECField field = ec.getField();
if (!(field instanceof ECFieldFp)) {
throw new GeneralSecurityException("Only curves over prime order fields are supported");
}
BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
int expectedLength = 1 + (p.bitLength() + 7) / 8;
if (bytes.length != expectedLength) {
throw new GeneralSecurityException("compressed point has wrong length");
}
boolean lsb;
switch(bytes[0]) {
case 2:
lsb = false;
break;
case 3:
lsb = true;
break;
default:
throw new GeneralSecurityException("Invalid format");
}
BigInteger x = new BigInteger(1, Arrays.copyOfRange(bytes, 1, bytes.length));
if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
throw new GeneralSecurityException("x is out of range");
}
// Compute rhs == x^3 + a x + b (mod p)
BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
BigInteger y = modSqrt(rhs, p);
if (lsb != y.testBit(0)) {
y = p.subtract(y).mod(p);
}
return new ECPoint(x, y);
}
use of java.security.spec.ECFieldFp in project wycheproof by google.
the class EcUtil method getNistCurveSpec.
public static ECParameterSpec getNistCurveSpec(String decimalP, String decimalN, String hexB, String hexGX, String hexGY) {
final BigInteger p = new BigInteger(decimalP);
final BigInteger n = new BigInteger(decimalN);
final BigInteger three = new BigInteger("3");
final BigInteger a = p.subtract(three);
final BigInteger b = new BigInteger(hexB, 16);
final BigInteger gx = new BigInteger(hexGX, 16);
final BigInteger gy = new BigInteger(hexGY, 16);
final int h = 1;
ECFieldFp fp = new ECFieldFp(p);
java.security.spec.EllipticCurve curveSpec = new java.security.spec.EllipticCurve(fp, a, b);
ECPoint g = new ECPoint(gx, gy);
ECParameterSpec ecSpec = new ECParameterSpec(curveSpec, g, n, h);
return ecSpec;
}
Aggregations