/** * SECG 2.3.4 Octet String to ECPoint */ public static ECPoint getDecoded(byte[] M, EllipticCurve curve) { int elementSize = getElementSize(curve); if (M.length != 2 * elementSize + 1 || M[0] != 0x04) { throw new SSHRuntimeException("Invalid 'f' for Elliptic Curve " + curve.toString()); } byte[] xBytes = new byte[elementSize]; byte[] yBytes = new byte[elementSize]; System.arraycopy(M, 1, xBytes, 0, elementSize); System.arraycopy(M, 1 + elementSize, yBytes, 0, elementSize); return new ECPoint(new BigInteger(1, xBytes), new BigInteger(1, yBytes)); }
X9ECParameters ecParams = NISTNamedCurves.getByName(name); ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECPoint p = new ECPoint(bigX, bigY); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(p, ecCurveSpec);
.orElse(null); ECPoint point = new ECPoint(x, y); this.publicKey = toPublicKey(kf, point, keySpec);
ECPoint g = new ECPoint(x, y);
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) { if (r.equals(ECPoint.POINT_INFINITY)) return r; BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE); slope = slope.add(curve.getA()); BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineY().multiply(TWO); tmp = new NativeBigInteger(tmp); slope = slope.multiply(tmp.modInverse(prime)); BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime); BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
private static ECPublicKey cvtToJavaECKey(SigningPublicKey pk) throws GeneralSecurityException { SigType type = pk.getType(); BigInteger[] xy = split(pk.getData()); ECPoint w = new ECPoint(xy[0], xy[1]); // see ECConstants re: casting ECPublicKeySpec ks = new ECPublicKeySpec(w, (ECParameterSpec) type.getParams()); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPublicKey) kf.generatePublic(ks); }
public ECParameterSpec genSpec() { BigInteger pb = new NativeBigInteger(ps); BigInteger nb = new NativeBigInteger(ns); BigInteger sb = new NativeBigInteger(ss.replace(" ", ""), 16); BigInteger bb = new NativeBigInteger(bs.replace(" ", ""), 16); BigInteger gxb = new NativeBigInteger(gxs.replace(" ", ""), 16); BigInteger gyb = new NativeBigInteger(gys.replace(" ", ""), 16); BigInteger ab = new NativeBigInteger(A.mod(pb)); ECField field = new ECFieldFp(pb); EllipticCurve curve = new EllipticCurve(field, ab, bb, sb.toByteArray()); ECPoint g = new ECPoint(gxb, gyb); return new ECParameterSpec(curve, g, nb, H); } }
private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) { if (r.equals(s)) return doublePoint(r, curve); else if (r.equals(ECPoint.POINT_INFINITY)) return s; else if (s.equals(ECPoint.POINT_INFINITY)) return r; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineX().subtract(s.getAffineX()); tmp = new NativeBigInteger(tmp); BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime); slope = new NativeBigInteger(slope); BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime); BigInteger yOut = s.getAffineY().negate().mod(prime); yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
@Override public ECPoint getW() { return new ECPoint(x, y); }
public static ECPoint decodePoint(byte[] encodedBytes, EllipticCurve elliptiCcurve) { if (encodedBytes[0] != 0x04) { throw new IllegalArgumentException("Only uncompressed format is supported"); } int size = (elliptiCcurve.getField().getFieldSize() + 7) / 8; byte affineXBytes[] = new byte[size]; byte affineYBytes[] = new byte[size]; System.arraycopy(encodedBytes, 1, affineXBytes, 0, size); System.arraycopy(encodedBytes, size + 1, affineYBytes, 0, size); return new ECPoint(new BigInteger(1, affineXBytes), new BigInteger(1, affineYBytes)); }
private static PublicKey generateEcPublicKey(GOSTCurve curve, CustomECPoint point, String keyFactoryAlg) { try { ECParameterSpec ecParameterSpec = getEcParameterSpec(curve); ECPoint ecPoint = new ECPoint(point.getX(), point.getY()); ECPublicKeySpec privateKeySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec); return KeyFactory.getInstance(keyFactoryAlg).generatePublic(privateKeySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("Could not generate GOST public key", e); return null; } }
public static ECPublicKey getECPublicKey(String curve, byte[] xPoint, byte[] yPoint) { try { ECParameterSpec params = getECParameterSpec(curve, false); ECPoint ecPoint = new ECPoint(toBigInteger(xPoint), toBigInteger(yPoint)); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, params); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPublicKey) kf.generatePublic(keySpec); } catch (Exception ex) { throw new SecurityException(ex); } } private static BigInteger toBigInteger(byte[] bytes) {
private static Curve initializeCurve(String name, String oid, String sfield, String a, String b, String x, String y, String n, int h) { BigInteger p = bigInt(sfield); ECField field = new ECFieldFp(p); EllipticCurve curve = new EllipticCurve(field, bigInt(a), bigInt(b)); ECPoint g = new ECPoint(bigInt(x), bigInt(y)); return new Curve(name, oid, curve, g, bigInt(n), h); }
private static PublicKey toECDSAPublicKey(KEYBase r, ECKeyInfo keyinfo) throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput in = new DNSInput(r.getKey()); // RFC 6605 Section 4 BigInteger x = readBigInteger(in, keyinfo.length); BigInteger y = readBigInteger(in, keyinfo.length); ECPoint q = new ECPoint(x, y); KeyFactory factory = KeyFactory.getInstance("EC"); return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); }
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp) { return new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getAffineXCoord().toBigInteger(), dp.getG().getAffineYCoord().toBigInteger()), dp.getN(), dp.getH().intValue()); }
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp) { return new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); }
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp) { return new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); }
public static ECPoint convertPoint(org.bouncycastle.math.ec.ECPoint point) { point = point.normalize(); return new ECPoint( point.getAffineXCoord().toBigInteger(), point.getAffineYCoord().toBigInteger()); } }
@Override public AlgorithmParameterSpec getEcPoint(final byte[] nonceS, final byte[] sharedSecretH, final EcCurve curveName) { final AlgorithmParameterSpec ecParams = ECNamedCurveTable.getParameterSpec(curveName.toString()); final BigInteger affineX = os2i(sharedSecretH); final BigInteger affineY = computeAffineY(affineX, (ECParameterSpec) ecParams); final ECPoint sharedSecretPointH = new ECPoint(affineX, affineY); return mapNonceGMWithECDH(os2i(nonceS), sharedSecretPointH, (ECParameterSpec) ecParams); }
private java.security.spec.ECPoint getPublicPoint(ECDSAPublicKey key) { if (!key.hasParameters()) { throw new IllegalArgumentException("Public key does not contains EC Params"); } BigInteger p = key.getPrimeModulusP(); ECCurve.Fp curve = new ECCurve.Fp(p, key.getFirstCoefA(), key.getSecondCoefB(), key.getOrderOfBasePointR(), key.getCofactorF()); ECPoint.Fp pubY = (ECPoint.Fp)curve.decodePoint(key.getPublicPointY()); return new java.security.spec.ECPoint(pubY.getAffineXCoord().toBigInteger(), pubY.getAffineYCoord().toBigInteger()); }