public static ECParameterSpec encodeECParameterSpec(EllipticCurveParameters params) { // Field final BigInteger pInt = new BigInteger(1, params.getP()); final ECField field = new ECFieldFp(pInt); final BigInteger aInt = new BigInteger(1, params.getA()); final BigInteger bInt = new BigInteger(1, params.getB()); final EllipticCurve curve = new EllipticCurve(field, aInt, bInt); // Fixed Point G final BigInteger xInt = new BigInteger(1, params.getX()); final BigInteger yInt = new BigInteger(1, params.getY()); final ECPoint g = new ECPoint(xInt, yInt); // Order N final BigInteger nInt = new BigInteger(1, params.getN()); return new ECParameterSpec(curve, g, nInt, params.getH()); }
private static BigInteger[] addPointsA(BigInteger[] P1, BigInteger[] P2, ECParameterSpec params) { final BigInteger p = ((ECFieldFp) params.getCurve().getField()).getP(); if (P2[0] == null || P2[1] == null) return P1; if (P1[0] == null || P1[1] == null) return P2; BigInteger d = (P2[1].subtract(P1[1])).multiply((P2[0].subtract(P1[0])) .modInverse(p)); BigInteger[] R = new BigInteger[2]; R[0] = d.pow(2).subtract(P1[0]).subtract(P2[0]).mod(p); R[1] = d.multiply(P1[0].subtract(R[0])).subtract(P1[1]).mod(p); return R; }
private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) { // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H final ECPoint generator = params.getGenerator(); final EllipticCurve curve = params.getCurve(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final ECFieldFp field = (ECFieldFp)curve.getField(); final BigInteger p = field.getP(); final BigInteger order = params.getOrder(); final int cofactor = params.getCofactor(); final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params); if (!toBouncyCastleECPoint(ephemeralGenerator, params).isValid()) { LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$ } return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor); }
public JCEECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec) { this.algorithm = algorithm; this.q = spec.getQ(); if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { if (q.getCurve() == null) { org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa(); q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false); } this.ecSpec = null; } }
public JCEECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public BCDSTU4145PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec) { this.q = spec.getQ(); if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { if (q.getCurve() == null) { org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa(); q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false); } this.ecSpec = null; } }
public BCDSTU4145PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public BCECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec, ProviderConfiguration configuration) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } this.configuration = configuration; }
public static ECParameterSpec convertSpec( EllipticCurve ellipticCurve, org.bouncycastle.jce.spec.ECParameterSpec spec) { if (spec instanceof ECNamedCurveParameterSpec) { return new ECNamedCurveSpec( ((ECNamedCurveParameterSpec)spec).getName(), ellipticCurve, new ECPoint( spec.getG().getX().toBigInteger(), spec.getG().getY().toBigInteger()), spec.getN(), spec.getH()); } else { return new ECParameterSpec( ellipticCurve, new ECPoint( spec.getG().getX().toBigInteger(), spec.getG().getY().toBigInteger()), spec.getN(), spec.getH().intValue()); } }
public static ECParameterSpec CfcaCurve() { // 素数P BigInteger p = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16); // 基于素数P的有限域 ECFieldFp gfp = new ECFieldFp(p); // 在有限域上的椭圆曲线y2 = x3 + ax + b EllipticCurve ellipticCurve = new EllipticCurve(gfp, new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16), new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)); // 基点G ECPoint G = new ECPoint(new BigInteger("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16), new BigInteger("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)); // G的阶 BigInteger n = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16); // 设置基点 ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, G, n, 1); return ecParameterSpec; }
public BCECGOST3410PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
/** * Returns the ECPublicKey instance from its encoded raw bytes. * The first byte has the fixed value 0x04 indicating the uncompressed form. * Therefore, the byte array must be of form: [0x04, x coord of point (32 bytes), y coord of point (32 bytes)] * * @param publicKeyBytes The byte array representing the encoded raw bytes of the public key * @return The ECPublicKey instance */ public static ECPublicKey getPublicKey(byte[] publicKeyBytes) { // First we separate x and y of coordinates into separate variables byte[] x = new byte[32]; byte[] y = new byte[32]; System.arraycopy(publicKeyBytes, 1, x, 0, 32); System.arraycopy(publicKeyBytes, 33, y, 0, 32); try { KeyFactory kf = KeyFactory.getInstance("EC"); AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC"); parameters.init(new ECGenParameterSpec("secp256r1")); ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class); ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), ecParameterSpec); ECPublicKey ecPublicKey = (ECPublicKey) kf.generatePublic(ecPublicKeySpec); return ecPublicKey; } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) { getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get public key from raw bytes", e); return null; } }
/** * Returns the ECPrivateKey instance from its raw bytes. Note that you must provide the "s" value of the * private key, not e.g. the byte array from reading a PKCS#8 key file. * * @param privateKeyBytes The byte array (the "s" value) of the private key * @return The ECPrivateKey instance */ public static ECPrivateKey getPrivateKey(byte[] privateKeyBytes) { try { AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC"); parameters.init(new ECGenParameterSpec("secp256r1")); ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class); ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(new BigInteger(privateKeyBytes), ecParameterSpec); ECPrivateKey privateKey = (ECPrivateKey) KeyFactory.getInstance("EC").generatePrivate(ecPrivateKeySpec); return privateKey; } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) { getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e); return null; } }
private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) { // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H final ECPoint generator = params.getGenerator(); final EllipticCurve curve = params.getCurve(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final ECFieldFp field = (ECFieldFp)curve.getField(); final BigInteger p = field.getP(); final BigInteger order = params.getOrder(); final int cofactor = params.getCofactor(); final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params); if (!toSpongyCastleECPoint(ephemeralGenerator, params).isValid()) { LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$ } return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor); }
@Override public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (params instanceof ECParameterSpec) { this.params = ECUtil.getECParameterSpec(null, (ECParameterSpec)params); if (this.params == null) { throw new InvalidAlgorithmParameterException( "Unsupported curve: " + params); } } else if (params instanceof ECGenParameterSpec) { String name = ((ECGenParameterSpec)params).getName(); this.params = ECUtil.getECParameterSpec(null, name); if (this.params == null) { throw new InvalidAlgorithmParameterException( "Unknown curve name: " + name); } } else { throw new InvalidAlgorithmParameterException( "ECParameterSpec or ECGenParameterSpec required for EC"); } this.keySize = ((ECParameterSpec)this.params).getCurve().getField().getFieldSize(); this.random = random; }
public static String getCurveName(ECParameterSpec spec) throws InvalidAlgorithmParameterException { int curve_id; /* Ecc object doesn't need to be initialied before call */ if (!(spec.getCurve().getField() instanceof ECFieldFp)) { throw new InvalidAlgorithmParameterException( "Currently only ECFieldFp fields supported"); } ECFieldFp field = (ECFieldFp)spec.getCurve().getField(); EllipticCurve curve = spec.getCurve(); curve_id = wc_ecc_get_curve_id_from_params( field.getFieldSize(), field.getP().toByteArray(), curve.getA().toByteArray(), curve.getB().toByteArray(), spec.getOrder().toByteArray(), spec.getGenerator().getAffineX().toByteArray(), spec.getGenerator().getAffineY().toByteArray(), spec.getCofactor()); return wc_ecc_get_curve_name_from_id(curve_id); }
@Test public void getEccCurveNameFromSpec() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException { /* generate key pair */ KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC"); ECGenParameterSpec genSpec = new ECGenParameterSpec("secp256r1"); kpg.initialize(genSpec); KeyPair pair = kpg.genKeyPair(); ECPrivateKey priv = (ECPrivateKey)pair.getPrivate(); ECParameterSpec spec = priv.getParams(); String curveName = Ecc.getCurveName(spec); assertEquals(curveName, "SECP256R1"); }
private static String getCurveOid(ECParameterSpec params) { // Check that the params represent one of the supported // curves. If there is a match, return the object identifier // of the curve. Curve match; if (matchCurve(params, SECP256R1)) { match = SECP256R1; } else if (matchCurve(params, SECP384R1)) { match = SECP384R1; } else if (matchCurve(params, SECP521R1)) { match = SECP521R1; } else { return null; } return match.getObjectId(); }
private static PrivateKey getECPrivateKey(String curve) throws Exception { String s; ECParameterSpec params; switch (curve) { case "P256": s = EC_P256_S; params = EC_P256_PARAMS; break; case "P384": s = EC_P384_S; params = EC_P384_PARAMS; break; case "P521": s = EC_P521_S; params = EC_P521_PARAMS; break; default: throw new Exception("Unsupported curve: " + curve); } KeyFactory kf = KeyFactory.getInstance("EC"); KeySpec kspec = new ECPrivateKeySpec(new BigInteger(s), params); return kf.generatePrivate(kspec); }
/** * Generates a random key pair based on a curve * @param curve the curve to use * @return a randomly generated KeyPair */ public static KeyPair generateEphemeralKeys(EllipticCurveParameters curve) { try { final ECParameterSpec curveParams = EllipticCurveParameters.encodeECParameterSpec(curve); final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC"); keyPairGenerator.initialize(curveParams); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; }
private static BigInteger getPrime(final ECParameterSpec params) { if (params == null) { throw new IllegalArgumentException( "Los parametros no pueden ser nulos" //$NON-NLS-1$ ); } final EllipticCurve curve = params.getCurve(); final ECField field = curve.getField(); if (!(field instanceof ECFieldFp)) { throw new IllegalStateException( "Solo se soporta 'ECFieldFp' y se proporciono " + field.getClass().getCanonicalName() //$NON-NLS-1$ ); } return ((ECFieldFp)field).getP(); }
public static BigInteger[] multiplyPointA(BigInteger[] P, BigInteger k, ECParameterSpec params) { BigInteger[] Q = new BigInteger[] {null, null}; for (int i = k.bitLength() - 1; i >= 0; i--) { Q = doublePointA(Q, params); if (k.testBit(i)) Q = addPointsA(Q, P, params); } return Q; }
public JCEECPrivateKey( String algorithm, ECPrivateKeyParameters params, JCEECPublicKey pubKey, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.d = params.getD(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); } else { this.ecSpec = spec; } publicKey = getPublicKeyDetails(pubKey); }
public org.bouncycastle.jce.spec.ECParameterSpec getParameters() { if (ecSpec == null) { return null; } return EC5Util.convertSpec(ecSpec, withCompression); }
org.bouncycastle.jce.spec.ECParameterSpec engineGetSpec() { if (ecSpec != null) { return EC5Util.convertSpec(ecSpec, withCompression); } return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa(); }
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 org.bouncycastle.jce.spec.ECParameterSpec getParameters() { if (ecSpec == null) // implictlyCA { return null; } return EC5Util.convertSpec(ecSpec, withCompression); }
public BCDSTU4145PrivateKey( String algorithm, ECPrivateKeyParameters params, BCDSTU4145PublicKey pubKey, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.d = params.getD(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); } else { this.ecSpec = spec; } publicKey = getPublicKeyDetails(pubKey); }
private static BigInteger computeAffineY(final BigInteger affineX, final ECParameterSpec params) { final ECCurve bcCurve = toBouncyCastleECCurve(params); final ECFieldElement a = bcCurve.getA(); final ECFieldElement b = bcCurve.getB(); final ECFieldElement x = bcCurve.fromBigInteger(affineX); final ECFieldElement y = x.multiply(x).add(a).multiply(x).add(b).sqrt(); return y.toBigInteger(); }
public BCECPrivateKey( String algorithm, ECPrivateKeyParameters params, BCECPublicKey pubKey, ECParameterSpec spec, ProviderConfiguration configuration) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.d = params.getD(); this.configuration = configuration; if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); } else { this.ecSpec = spec; } publicKey = getPublicKeyDetails(pubKey); }
protected static boolean Verify(byte[] msg, byte[] pkey, byte[] strsig, byte[] id, ECParameterSpec spec) { ECFieldFp fp = (ECFieldFp) spec.getCurve().getField(); byte[] pb = fp.getP().toByteArray(); int len = pb[0] == 0 ? pb.length - 1 : pb.length; byte[] r = new byte[len + 1]; byte[] s = new byte[len + 1]; System.arraycopy(strsig, 0, r, 1, len); System.arraycopy(strsig, len, s, 1, len); Sm2Signature sig = new Sm2Signature(new BigInteger(r), new BigInteger(s)); ECPoint pt = PkeyByte2Point(spec, pkey); return Verify(spec, id, msg, pt, sig); }
public BCECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { this.algorithm = algorithm; this.q = spec.getQ(); if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { if (q.getCurve() == null) { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false); } this.ecSpec = null; } this.configuration = configuration; }