private void testKeyFactory() throws Exception { KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "SC"); ECCurve curve = new ECCurve.Fp( new BigInteger( "883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger( "6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECParameterSpec ecSpec = new ECParameterSpec( curve, curve.decodePoint( Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger( "883423532389192164791648750360308884807550341691627752275345424702807307")); // n ConfigurableProvider config = (ConfigurableProvider) Security.getProvider("SC"); config.setParameter(ConfigurableProvider.EC_IMPLICITLY_CA, ecSpec); g.initialize(null, new SecureRandom()); KeyPair p = g.generateKeyPair(); ECPrivateKey sKey = (ECPrivateKey) p.getPrivate(); ECPublicKey vKey = (ECPublicKey) p.getPublic(); KeyFactory fact = KeyFactory.getInstance("ECDSA", "SC"); vKey = (ECPublicKey) fact.generatePublic(new ECPublicKeySpec(vKey.getQ(), null)); sKey = (ECPrivateKey) fact.generatePrivate(new ECPrivateKeySpec(sKey.getD(), null)); testECDSA(sKey, vKey); testBCParamsAndQ(sKey, vKey); testEC5Params(sKey, vKey); testEncoding(sKey, vKey); ECPublicKey vKey2 = (ECPublicKey) fact.generatePublic(new ECPublicKeySpec(vKey.getQ(), ecSpec)); ECPrivateKey sKey2 = (ECPrivateKey) fact.generatePrivate(new ECPrivateKeySpec(sKey.getD(), ecSpec)); if (!vKey.equals(vKey2) || vKey.hashCode() != vKey2.hashCode()) { fail("private equals/hashCode failed"); } if (!sKey.equals(sKey2) || sKey.hashCode() != sKey2.hashCode()) { fail("private equals/hashCode failed"); } // check we can get specs. fact.getKeySpec(vKey, java.security.spec.ECPublicKeySpec.class); fact.getKeySpec(sKey, java.security.spec.ECPrivateKeySpec.class); }
protected int engineGetKeySize(Key key) { if (!(key instanceof IESKey)) { throw new IllegalArgumentException("must be passed IE key"); } IESKey ieKey = (IESKey) key; if (ieKey.getPrivate() instanceof DHPrivateKey) { DHPrivateKey k = (DHPrivateKey) ieKey.getPrivate(); return k.getX().bitLength(); } else if (ieKey.getPrivate() instanceof ECPrivateKey) { ECPrivateKey k = (ECPrivateKey) ieKey.getPrivate(); return k.getD().bitLength(); } throw new IllegalArgumentException("not an IE key!"); }
private void testEncoding(ECPrivateKey privKey, ECPublicKey pubKey) throws Exception { KeyFactory kFact = KeyFactory.getInstance("ECDSA", "SC"); byte[] bytes = privKey.getEncoded(); PrivateKeyInfo sInfo = PrivateKeyInfo.getInstance(new ASN1InputStream(bytes).readObject()); if (!sInfo.getPrivateKeyAlgorithm().getParameters().equals(DERNull.INSTANCE)) { fail("private key parameters wrong"); } ECPrivateKey sKey = (ECPrivateKey) kFact.generatePrivate(new PKCS8EncodedKeySpec(bytes)); if (!sKey.equals(privKey)) { fail("private equals failed"); } if (sKey.hashCode() != privKey.hashCode()) { fail("private hashCode failed"); } bytes = pubKey.getEncoded(); SubjectPublicKeyInfo vInfo = SubjectPublicKeyInfo.getInstance(new ASN1InputStream(bytes).readObject()); if (!vInfo.getAlgorithm().getParameters().equals(DERNull.INSTANCE)) { fail("public key parameters wrong"); } ECPublicKey vKey = (ECPublicKey) kFact.generatePublic(new X509EncodedKeySpec(bytes)); if (!vKey.equals(pubKey) || vKey.hashCode() != pubKey.hashCode()) { fail("public equals/hashCode failed"); } testBCParamsAndQ(sKey, vKey); testEC5Params(sKey, vKey); testECDSA(sKey, vKey); }
private void testBCParamsAndQ(ECPrivateKey sKey, ECPublicKey vKey) { if (sKey.getParameters() != null) { fail("parameters exposed in private key"); } if (vKey.getParameters() != null) { fail("parameters exposed in public key"); } if (vKey.getQ().getCurve() != null) { fail("curve exposed in public point"); } }