Search in sources :

Example 16 with EllipticCurve

use of java.security.spec.EllipticCurve in project protools by SeanDragon.

the class ToolECDSA method initKey.

/**
 * 初始化密钥
 *
 * @return Map 密钥Map
 *
 * @throws Exception
 */
public static Map<String, Object> initKey() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    // 加入BouncyCastleProvider支持
    Security.addProvider(new BouncyCastleProvider());
    BigInteger p = new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839");
    ECFieldFp ecFieldFp = new ECFieldFp(p);
    BigInteger a = new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16);
    BigInteger b = new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16);
    EllipticCurve ellipticCurve = new EllipticCurve(ecFieldFp, a, b);
    BigInteger x = new BigInteger("110282003749548856476348533541186204577905061504881242240149511594420911");
    BigInteger y = new BigInteger("869078407435509378747351873793058868500210384946040694651368759217025454");
    ECPoint g = new ECPoint(x, y);
    BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307");
    ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, g, n, 1);
    // 实例化密钥对儿生成器
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    // 初始化密钥对儿生成器
    kpg.initialize(ecParameterSpec, new SecureRandom());
    // 生成密钥对儿
    KeyPair keypair = kpg.generateKeyPair();
    ECPublicKey publicKey = (ECPublicKey) keypair.getPublic();
    ECPrivateKey privateKey = (ECPrivateKey) keypair.getPrivate();
    // 封装密钥
    Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
    map.put(PUBLIC_KEY, publicKey);
    map.put(PRIVATE_KEY, privateKey);
    return map;
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECFieldFp(java.security.spec.ECFieldFp) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint) ECPublicKey(java.security.interfaces.ECPublicKey) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 17 with EllipticCurve

use of java.security.spec.EllipticCurve in project tink by google.

the class EllipticCurves method validateSharedSecret.

/**
 * Checks that the shared secret is on the curve of the private key, to prevent arithmetic errors
 * or fault attacks.
 */
private static void validateSharedSecret(byte[] secret, ECPrivateKey privateKey) throws GeneralSecurityException {
    EllipticCurve privateKeyCurve = privateKey.getParams().getCurve();
    BigInteger x = new BigInteger(1, secret);
    if (x.signum() == -1 || x.compareTo(getModulus(privateKeyCurve)) != -1) {
        throw new GeneralSecurityException("shared secret is out of range");
    }
    // This will throw if x is not a valid coordinate.
    getY(x, true, /* lsb, doesn't matter here */
    privateKeyCurve);
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger)

Example 18 with EllipticCurve

use of java.security.spec.EllipticCurve in project tink by google.

the class EllipticCurvesTest method testPointEncode.

@Test
public void testPointEncode() throws Exception {
    for (TestVector2 test : testVectors2) {
        EllipticCurve curve = EllipticCurves.getCurveSpec(test.curve).getCurve();
        ECPoint p = new ECPoint(test.x, test.y);
        byte[] encoded = EllipticCurves.pointEncode(curve, test.format, p);
        assertEquals(TestUtil.hexEncode(encoded), TestUtil.hexEncode(test.encoded));
    }
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) ECPoint(java.security.spec.ECPoint) Test(org.junit.Test)

Example 19 with EllipticCurve

use of java.security.spec.EllipticCurve in project jmulticard by ctt-gob-es.

the class JseCryptoHelper method getPrime.

private static BigInteger getPrime(final ECParameterSpec params) {
    if (params == null) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "Los parametros no pueden ser nulos");
    }
    final EllipticCurve curve = params.getCurve();
    final ECField field = curve.getField();
    if (!(field instanceof ECFieldFp)) {
        throw new IllegalStateException(// $NON-NLS-1$
        "Solo se soporta 'ECFieldFp' y se proporciono  " + field.getClass().getCanonicalName());
    }
    return ((ECFieldFp) field).getP();
}
Also used : ECField(java.security.spec.ECField) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve)

Example 20 with EllipticCurve

use of java.security.spec.EllipticCurve in project wycheproof by google.

the class EcdhTest method testDistinctCurves.

@SuppressWarnings("InsecureCryptoUsage")
public void testDistinctCurves(String algorithm, ECPrivateKey priv, ECPublicKey pub) throws Exception {
    KeyAgreement kaA;
    try {
        kaA = KeyAgreement.getInstance(algorithm);
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Algorithm not supported: " + algorithm);
        return;
    }
    byte[] shared;
    try {
        kaA.init(priv);
        kaA.doPhase(pub, true);
        shared = kaA.generateSecret();
    } catch (InvalidKeyException ex) {
        // This is expected.
        return;
    }
    // Printing some information to determine what might have gone wrong:
    // E.g., if the generated secret is the same as the x-coordinate of the public key
    // then it is likely that the ECDH computation was using a fake group with small order.
    // Such a situation is probably exploitable.
    // This probably is exploitable. If the curve of the private key was used for the ECDH
    // then the generated secret and the x-coordinate of the public key are likely
    // distinct.
    EllipticCurve pubCurve = pub.getParams().getCurve();
    EllipticCurve privCurve = priv.getParams().getCurve();
    ECPoint pubW = pub.getW();
    System.out.println("testDistinctCurves: algorithm=" + algorithm);
    System.out.println("Private key: a=" + privCurve.getA() + " b=" + privCurve.getB() + " p" + EcUtil.getModulus(privCurve));
    System.out.println("        s =" + priv.getS());
    System.out.println("Public key: a=" + pubCurve.getA() + " b=" + pubCurve.getB() + " p" + EcUtil.getModulus(pubCurve));
    System.out.println("        w = (" + pubW.getAffineX() + ", " + pubW.getAffineY() + ")");
    System.out.println("          = (" + pubW.getAffineX().toString(16) + ", " + pubW.getAffineY().toString(16) + ")");
    System.out.println("generated shared secret:" + TestUtil.bytesToHex(shared));
    fail("Generated secret with distinct Curves using " + algorithm);
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) ECPoint(java.security.spec.ECPoint)

Aggregations

EllipticCurve (java.security.spec.EllipticCurve)78 BigInteger (java.math.BigInteger)48 ECFieldFp (java.security.spec.ECFieldFp)43 ECPoint (java.security.spec.ECPoint)30 ECFieldF2m (java.security.spec.ECFieldF2m)28 ECParameterSpec (java.security.spec.ECParameterSpec)23 GeneralSecurityException (java.security.GeneralSecurityException)7 ECField (java.security.spec.ECField)7 ECCurve (org.bouncycastle.math.ec.ECCurve)7 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)6 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)6 ECNamedCurveSpec (org.bouncycastle.jce.spec.ECNamedCurveSpec)6 KeyPair (java.security.KeyPair)5 KeyPairGenerator (java.security.KeyPairGenerator)5 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)5 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 ECPublicKey (java.security.interfaces.ECPublicKey)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ECPrivateKey (java.security.interfaces.ECPrivateKey)3