public void initialize( int strength, SecureRandom random) { this.strength = strength; this.random = random; this.ecParams = (ECParameterSpec)ecParameters.get(new Integer(strength)); if (ecParams != null) { param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; } }
public void initialize( AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof ECParameterSpec)) { throw new InvalidAlgorithmParameterException("parameter object not a ECParameterSpec"); } this.ecParams = (ECParameterSpec)params; param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; }
/************* * This method will create the ECDSA public and private key which is returned in a Byte array of Byte arrays, index 0 is the private key and * index 1 of the array will return the ECDSA public key * * -Wisdom: This guy didn't check whether the returned Private Key would be more than 32. * @return */ private byte[][] ECDSAgeneratePublicAndPrivateKey(){ int length = 0; byte[][] keys; do{ ECKeyPairGenerator gen = new ECKeyPairGenerator(); SecureRandom secureRandom = new SecureRandom(); X9ECParameters secnamecurves = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(), secnamecurves.getN(), secnamecurves.getH()); ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom); gen.init(keyGenParam); AsymmetricCipherKeyPair kp = gen.generateKeyPair(); ECPrivateKeyParameters privatekey = (ECPrivateKeyParameters)kp.getPrivate(); ECPoint dd = secnamecurves.getG().multiply(privatekey.getD()); byte[] publickey=new byte[65]; System.arraycopy(dd.getY().toBigInteger().toByteArray(), 0, publickey, 64-dd.getY().toBigInteger().toByteArray().length+1, dd.getY().toBigInteger().toByteArray().length); System.arraycopy(dd.getX().toBigInteger().toByteArray(), 0, publickey, 32-dd.getX().toBigInteger().toByteArray().length+1, dd.getX().toBigInteger().toByteArray().length); publickey[0]=4; length = privatekey.getD().toByteArray().length; keys = new byte[][]{privatekey.getD().toByteArray(),publickey}; }while(length != 32); return keys; }
@Test public void curveTest() { final X9ECParameters curve = NISTNamedCurves.getByName("P-384"); final ECDomainParameters domainParameters = new ECDomainParameters( curve.getCurve(), curve.getG(), curve.getN()); final SecureRandom random = new SecureRandom(); final ECKeyPairGenerator gen = new ECKeyPairGenerator(); gen.init(new ECKeyGenerationParameters(domainParameters, random)); final AsymmetricCipherKeyPair senderPair = gen.generateKeyPair(); final AsymmetricCipherKeyPair receiverPair = gen.generateKeyPair(); final ECDHBasicAgreement senderAgreement = new ECDHBasicAgreement(); senderAgreement.init(senderPair.getPrivate()); final BigInteger senderResult = senderAgreement.calculateAgreement( receiverPair.getPublic()); final ECDHBasicAgreement receiverAgreement = new ECDHBasicAgreement(); receiverAgreement.init(receiverPair.getPrivate()); final BigInteger receiverResult = receiverAgreement.calculateAgreement( senderPair.getPublic()); assertEquals(senderResult, receiverResult); //System.out.println(receiverResult); }
@Test public void curveTest() { final X9ECParameters curve = NISTNamedCurves.getByName("P-384"); final ECDomainParameters domainParameters = new ECDomainParameters( curve.getCurve(), curve.getG(), curve.getN()); final SecureRandom random = new SecureRandom(); final ECKeyPairGenerator gen = new ECKeyPairGenerator(); gen.init(new ECKeyGenerationParameters(domainParameters, random)); final AsymmetricCipherKeyPair senderPair = gen.generateKeyPair(); final ECDSASigner signer = new ECDSASigner(); signer.init(true, senderPair.getPrivate()); final byte[] message = "The magic words are squeamish ossifrage".getBytes( Constants.ASCII); final SHA384Digest digester = new SHA384Digest(); digester.update(message, 0, message.length); final byte[] digest = new byte[digester.getDigestSize()]; digester.doFinal(digest, 0); final BigInteger[] signature = signer.generateSignature(digest); final ECDSASigner verifier = new ECDSASigner(); verifier.init(false, senderPair.getPublic()); assertTrue(verifier.verifySignature(digest, signature[0], signature[1])); }
private void doTestOnPeers(int numberOfPeers) { X9ECParameters params = SECNamedCurves.getByName("secp256r1"); ECKeyPairGenerator kpGen = new ECKeyPairGenerator(); ECDomainParameters domainParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed()); kpGen.init(new ECKeyGenerationParameters(domainParams, new SecureRandom())); AsymmetricCipherKeyPair[] kps = new AsymmetricCipherKeyPair[numberOfPeers]; // Generate Private Keys - normally this would be done by each // individual server. For this example we will just create them in an // array. for (int i = 0; i < kps.length; i++) { kps[i] = kpGen.generateKeyPair(); } doTest(domainParams, kps, numberOfPeers - 1, true, 1); doTest(domainParams, kps, numberOfPeers - 2, true, 1); doTest(domainParams, kps, numberOfPeers - 2, true, 1, 3); doTest(domainParams, kps, numberOfPeers - 1, false, 1, 3); }
public void initialize( int strength, SecureRandom random) { this.strength = strength; this.random = random; this.ecParams = (ECParameterSpec)ecParameters.get(Integers.valueOf(strength)); if (ecParams != null) { param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; } else { throw new InvalidParameterException("unknown key size."); } }
public void initialize( int strength, SecureRandom random) { this.strength = strength; this.random = random; if (ecParams != null) { param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; } else { throw new InvalidParameterException("unknown key size."); } }
public static AsymmetricCipherKeyPair generateECKeyPair(SecureRandom random, ECDomainParameters ecParams) { ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator(); ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(ecParams, random); keyPairGenerator.init(keyGenerationParameters); return keyPairGenerator.generateKeyPair(); }
public void init( KeyGenerationParameters param) { ECKeyGenerationParameters ecP = (ECKeyGenerationParameters)param; this.random = ecP.getRandom(); this.params = ecP.getDomainParameters(); }
/** * Create a new PrivateKey using the platform provided secure random source. * * @param compressed set to false if you relly want legacy format * @return new PrivateKey */ public static PrivateKey createNew(boolean compressed) { ECKeyPairGenerator generator = new ECKeyPairGenerator(); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom); generator.init(keygenParams); AsymmetricCipherKeyPair keypair = generator.generateKeyPair(); ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate(); ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic(); return new PrivateKey(privParams.getD(), compressed, pubParams.getQ().getEncoded(compressed)); }
public static AsymmetricCipherKeyPair generateECPublicKey() { // Should we also generate random curves? X9ECParameters ecp = SECNamedCurves.getByName(getRandomCurveName()); ECKeyPairGenerator keygen = new ECKeyPairGenerator(); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); keygen.init(new ECKeyGenerationParameters(domainParams, RandomHelper.getBadSecureRandom())); return keygen.generateKeyPair(); }
public void init( KeyGenerationParameters param) { ECKeyGenerationParameters ecP = (ECKeyGenerationParameters)param; this.random = ecP.getRandom(); this.params = ecP.getDomainParameters(); if (this.random == null) { this.random = new SecureRandom(); } }
private void testECMQVRandom() { SecureRandom random = new SecureRandom(); BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b n, ECConstants.ONE); ECDomainParameters parameters = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G n); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); pGen.init(new ECKeyGenerationParameters(parameters, random)); // Pre-established key pairs AsymmetricCipherKeyPair U1 = pGen.generateKeyPair(); AsymmetricCipherKeyPair V1 = pGen.generateKeyPair(); // Ephemeral key pairs AsymmetricCipherKeyPair U2 = pGen.generateKeyPair(); AsymmetricCipherKeyPair V2 = pGen.generateKeyPair(); BigInteger x = calculateAgreement(U1, U2, V1, V2); if (x == null) { fail("MQV Test Vector (random) agreement failed"); } }
public void performTest() throws Exception { doStaticTest(null); doStaticTest(TWOFISH_IV); doShortTest(null); BigInteger n = new BigInteger("6277101735386680763835789423176059013767194773182842284081"); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16), // b n, ECConstants.ONE); ECDomainParameters params = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G n); ECKeyPairGenerator eGen = new ECKeyPairGenerator(); KeyGenerationParameters gParam = new ECKeyGenerationParameters(params, new SecureRandom()); eGen.init(gParam); AsymmetricCipherKeyPair p1 = eGen.generateKeyPair(); AsymmetricCipherKeyPair p2 = eGen.generateKeyPair(); doTest(p1, p2); doEphemeralTest(null, false); doEphemeralTest(null, true); doEphemeralTest(TWOFISH_IV, false); doEphemeralTest(TWOFISH_IV, true); }
@Override public byte[] createNewPrivateKey() { ECKeyPairGenerator generator = new ECKeyPairGenerator(); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom); generator.init(keygenParams); AsymmetricCipherKeyPair keypair = generator.generateKeyPair(); ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate(); return privParams.getD().toByteArray(); }
protected ECKeyGenerationParameters createKeyGenParamsJCE(java.security.spec.ECParameterSpec p, SecureRandom r) { ECCurve curve = EC5Util.convertCurve(p.getCurve()); ECPoint g = EC5Util.convertPoint(curve, p.getGenerator(), false); BigInteger n = p.getOrder(); BigInteger h = BigInteger.valueOf(p.getCofactor()); ECDomainParameters dp = new ECDomainParameters(curve, g, n, h); return new ECKeyGenerationParameters(dp, r); }
private void ECDH_GenerateEphemeral() throws CoseException { X9ECParameters p = privateKey.GetCurve(); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam); AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); CBORObject epk = CBORObject.NewMap(); epk.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); epk.Add(KeyKeys.EC2_Curve.AsCBOR(), privateKey.get(KeyKeys.EC2_Curve.AsCBOR())); ECPublicKeyParameters priv = (ECPublicKeyParameters) p1.getPublic(); byte[] rgbEncoded = priv.getQ().normalize().getEncoded(true); byte[] X = new byte[rgbEncoded.length - 1]; System.arraycopy(rgbEncoded, 1, X, 0, X.length); epk.Add(KeyKeys.EC2_X.AsCBOR(), CBORObject.FromObject(X)); epk.Add(KeyKeys.EC2_Y.AsCBOR(), CBORObject.FromObject((rgbEncoded[0] & 1) == 1)); addAttribute(HeaderKeys.ECDH_EPK, epk, Attribute.UNPROTECTED); OneKey secretKey = new OneKey(); secretKey.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2); secretKey.add(KeyKeys.EC2_Curve, privateKey.get(KeyKeys.EC2_Curve.AsCBOR())); secretKey.add(KeyKeys.EC2_X, CBORObject.FromObject(X)); secretKey.add(KeyKeys.EC2_Y, CBORObject.FromObject((rgbEncoded[0] & 1) == 1)); ECPrivateKeyParameters priv1 = (ECPrivateKeyParameters) p1.getPrivate(); secretKey.add(KeyKeys.EC2_D, CBORObject.FromObject(BigIntegers.asUnsignedByteArray( priv1.getD()))); senderKey = secretKey; }
static private OneKey generateECDSAKey(String curveName, CBORObject curve) { X9ECParameters p = NISTNamedCurves.getByName(curveName); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam); AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); ECPublicKeyParameters keyPublic = (ECPublicKeyParameters) p1.getPublic(); ECPrivateKeyParameters keyPrivate = (ECPrivateKeyParameters) p1.getPrivate(); byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded(); byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded(); boolean signY = true; byte[] rgbD = keyPrivate.getD().toByteArray(); OneKey key = new OneKey(); key.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2); key.add(KeyKeys.EC2_Curve, curve); key.add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX)); key.add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY)); key.add(KeyKeys.EC2_D, CBORObject.FromObject(rgbD)); return key; }
@BeforeClass public static void setUpClass() throws CoseException { X9ECParameters p = NISTNamedCurves.getByName("P-256"); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam); AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); keyPublic = (ECPublicKeyParameters) p1.getPublic(); keyPrivate = (ECPrivateKeyParameters) p1.getPrivate(); byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded(); byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded(); boolean signY = true; byte[] rgbD = keyPrivate.getD().toByteArray(); CBORObject key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublic = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublicCompressed = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_D.AsCBOR(), rgbD); cnKeyPrivate = new OneKey(key); }
/** * Creates an ECKey with a new public/private key pair. Point compression is used * so the resulting public key will be 33 bytes (32 bytes for the x-coordinate and * 1 byte to represent the y-coordinate sign) */ public ECKey() { ECKeyPairGenerator keyGenerator = new ECKeyPairGenerator(); ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(ecParams, secureRandom); keyGenerator.init(keyGenParams); AsymmetricCipherKeyPair keyPair = keyGenerator.generateKeyPair(); ECPrivateKeyParameters privKeyParams = (ECPrivateKeyParameters)keyPair.getPrivate(); ECPublicKeyParameters pubKeyParams = (ECPublicKeyParameters)keyPair.getPublic(); privKey = privKeyParams.getD(); pubKey = pubKeyParams.getQ().getEncoded(true); creationTime = System.currentTimeMillis()/1000; isCompressed = true; }
public static ECKeyPair createNew (boolean compressed) { ECKeyPairGenerator generator = new ECKeyPairGenerator (); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters (domain, secureRandom); generator.init (keygenParams); AsymmetricCipherKeyPair keypair = generator.generateKeyPair (); ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate (); ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic (); ECKeyPair k = new ECKeyPair (); k.priv = privParams.getD (); k.compressed = compressed; k.pub = pubParams.getQ ().getEncoded (compressed); return k; }
public synchronized AsymmetricCipherKeyPair generateKeyPair(String keyID, Algorithm algorithm, int numberOfPeers, ECDomainParameters domainParameters, ECPoint h) { if (!paramsMap.containsKey(keyID)) { ECKeyPairGenerator kpGen = new ECKeyPairGenerator(); kpGen.init(new ECKeyGenerationParameters(domainParameters, new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.generateKeyPair(); sharedPrivateKeyMap.init(keyID, numberOfPeers); sharedPublicKeyMap.init(keyID, numberOfPeers); if (algorithm.equals(Algorithm.ECDSA)) { signingKeys.add(keyID); } hMap.put(keyID, h); paramsMap.put(keyID, ((ECPublicKeyParameters)kp.getPublic()).getParameters()); return kp; } else { throw new IllegalStateException("Key " + keyID + " already exists."); } }
/** * key generation test */ private void testECDSAKeyGenTest() { SecureRandom random = new SecureRandom(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters params = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( params, random); pGen.init(genParam); AsymmetricCipherKeyPair pair = pGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); ECDSASigner ecdsa = new ECDSASigner(); ecdsa.init(true, param); byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").toByteArray(); BigInteger[] sig = ecdsa.generateSignature(message); ecdsa.init(false, pair.getPublic()); if (!ecdsa.verifySignature(message, sig[0], sig[1])) { fail("signature fails"); } }
private void testECMQVRandom() { SecureRandom random = new SecureRandom(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters parameters = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n ECKeyPairGenerator pGen = new ECKeyPairGenerator(); pGen.init(new ECKeyGenerationParameters(parameters, random)); // Pre-established key pairs AsymmetricCipherKeyPair U1 = pGen.generateKeyPair(); AsymmetricCipherKeyPair V1 = pGen.generateKeyPair(); // Ephemeral key pairs AsymmetricCipherKeyPair U2 = pGen.generateKeyPair(); AsymmetricCipherKeyPair V2 = pGen.generateKeyPair(); BigInteger x = calculateAgreement(U1, U2, V1, V2); if (x == null) { fail("MQV Test Vector (random) agreement failed"); } }
public void performTest() throws Exception { staticTest(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b ECDomainParameters params = new ECDomainParameters( curve, curve.decodePoint(Hex.decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G new BigInteger("6277101735386680763835789423176059013767194773182842284081")); // n ECKeyPairGenerator eGen = new ECKeyPairGenerator(); KeyGenerationParameters gParam = new ECKeyGenerationParameters(params, new SecureRandom()); eGen.init(gParam); AsymmetricCipherKeyPair p1 = eGen.generateKeyPair(); AsymmetricCipherKeyPair p2 = eGen.generateKeyPair(); doTest(p1, p2); doEphemeralTest(); }