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;
}
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);
}
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));
}
}
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();
}
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);
}
Aggregations