diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java | 162 |
1 files changed, 84 insertions, 78 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java b/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java index 004e2873..5d8e6b18 100644 --- a/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java +++ b/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java @@ -18,13 +18,6 @@ import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; // import org.bouncycastle.crypto.agreement.ECDHCBasicAgreement; // import org.bouncycastle.crypto.agreement.ECMQVBasicAgreement; // import org.bouncycastle.crypto.agreement.kdf.ConcatenationKDFGenerator; -// END android-removed -import org.bouncycastle.crypto.digests.SHA1Digest; -import org.bouncycastle.crypto.digests.SHA224Digest; -import org.bouncycastle.crypto.digests.SHA256Digest; -import org.bouncycastle.crypto.digests.SHA384Digest; -import org.bouncycastle.crypto.digests.SHA512Digest; -// BEGIN android-removed // import org.bouncycastle.crypto.generators.KDF2BytesGenerator; // END android-removed import org.bouncycastle.crypto.params.ECDomainParameters; @@ -33,6 +26,7 @@ import org.bouncycastle.crypto.params.ECPublicKeyParameters; // BEGIN android-removed // import org.bouncycastle.crypto.params.MQVPrivateParameters; // import org.bouncycastle.crypto.params.MQVPublicParameters; +// import org.bouncycastle.crypto.util.DigestFactory; // END android-removed import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi; import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil; @@ -66,6 +60,7 @@ public class KeyAgreementSpi // BEGIN android-removed // private MQVParameterSpec mqvParameters; // END android-removed + private BigInteger result; protected KeyAgreementSpi( String kaAlgorithm, @@ -106,9 +101,9 @@ public class KeyAgreementSpi // if (!(key instanceof MQVPublicKey)) // { // ECPublicKeyParameters staticKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter((PublicKey)key); + // ECUtils.generatePublicKeyParameter((PublicKey)key); // ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter(mqvParameters.getOtherPartyEphemeralKey()); + // ECUtils.generatePublicKeyParameter(mqvParameters.getOtherPartyEphemeralKey()); // // pubKey = new MQVPublicParameters(staticKey, ephemKey); // } @@ -116,13 +111,11 @@ public class KeyAgreementSpi // { // MQVPublicKey mqvPubKey = (MQVPublicKey)key; // ECPublicKeyParameters staticKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter(mqvPubKey.getStaticKey()); + // ECUtils.generatePublicKeyParameter(mqvPubKey.getStaticKey()); // ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter(mqvPubKey.getEphemeralKey()); + // ECUtils.generatePublicKeyParameter(mqvPubKey.getEphemeralKey()); // // pubKey = new MQVPublicParameters(staticKey, ephemKey); - // - // // TODO Validate that all the keys are using the same parameters? // } // } // else @@ -134,21 +127,27 @@ public class KeyAgreementSpi + getSimpleName(ECPublicKey.class) + " for doPhase"); } - pubKey = ECUtil.generatePublicKeyParameter((PublicKey)key); - - // TODO Validate that all the keys are using the same parameters? + pubKey = ECUtils.generatePublicKeyParameter((PublicKey)key); } - // BEGIN android-added - try { - // END android-added - result = agreement.calculateAgreement(pubKey); - // BEGIN android-added + try + { + result = agreement.calculateAgreement(pubKey); + // BEGIN android-changed + // Was: + // } catch (final Exception e) { + // throw new InvalidKeyException("calculation failed: " + e.getMessage()) + // { + // public Throwable getCause() + // { + // return e; + // } + // }; + // } + // END android-changed } catch (IllegalStateException e) { - throw new InvalidKeyException("Invalid public key"); + throw new InvalidKeyException("Invalid public key"); } - // END android-added - return null; } @@ -170,7 +169,7 @@ public class KeyAgreementSpi protected void engineInit( Key key, - SecureRandom random) + SecureRandom random) throws InvalidKeyException { initFromKey(key, null); @@ -204,7 +203,7 @@ public class KeyAgreementSpi // if (mqvPrivKey.getEphemeralPublicKey() != null) // { // ephemPubKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey()); + // ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey()); // } // } // else @@ -220,7 +219,7 @@ public class KeyAgreementSpi // if (mqvParameterSpec.getEphemeralPublicKey() != null) // { // ephemPubKey = (ECPublicKeyParameters) - // ECUtil.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey()); + // ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey()); // } // mqvParameters = mqvParameterSpec; // ukmParameters = mqvParameterSpec.getUserKeyingMaterial(); @@ -256,6 +255,12 @@ public class KeyAgreementSpi return fullName.substring(fullName.lastIndexOf('.') + 1); } + + protected byte[] calcSecret() + { + return bigIntToBytes(result); + } + public static class DH extends KeyAgreementSpi { @@ -274,7 +279,7 @@ public class KeyAgreementSpi // super("ECDHC", new ECDHCBasicAgreement(), null); // } // } - // + // public static class MQV // extends KeyAgreementSpi // { @@ -283,229 +288,230 @@ public class KeyAgreementSpi // super("ECMQV", new ECMQVBasicAgreement(), null); // } // } - // + // public static class DHwithSHA1KDF // extends KeyAgreementSpi // { // public DHwithSHA1KDF() // { - // super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest())); + // super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class DHwithSHA1KDFAndSharedInfo // extends KeyAgreementSpi // { // public DHwithSHA1KDFAndSharedInfo() // { - // super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest())); + // super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class CDHwithSHA1KDFAndSharedInfo // extends KeyAgreementSpi // { // public CDHwithSHA1KDFAndSharedInfo() // { - // super("ECCDHwithSHA1KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest())); + // super("ECCDHwithSHA1KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class DHwithSHA224KDFAndSharedInfo // extends KeyAgreementSpi // { // public DHwithSHA224KDFAndSharedInfo() // { - // super("ECDHwithSHA224KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest())); + // super("ECDHwithSHA224KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224())); // } // } - // + // public static class CDHwithSHA224KDFAndSharedInfo // extends KeyAgreementSpi // { // public CDHwithSHA224KDFAndSharedInfo() // { - // super("ECCDHwithSHA224KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest())); + // super("ECCDHwithSHA224KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224())); // } // } - // + // public static class DHwithSHA256KDFAndSharedInfo // extends KeyAgreementSpi // { // public DHwithSHA256KDFAndSharedInfo() // { - // super("ECDHwithSHA256KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest())); + // super("ECDHwithSHA256KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256())); // } // } - // + // public static class CDHwithSHA256KDFAndSharedInfo // extends KeyAgreementSpi // { // public CDHwithSHA256KDFAndSharedInfo() // { - // super("ECCDHwithSHA256KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest())); + // super("ECCDHwithSHA256KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256())); // } // } - // + // public static class DHwithSHA384KDFAndSharedInfo // extends KeyAgreementSpi // { // public DHwithSHA384KDFAndSharedInfo() // { - // super("ECDHwithSHA384KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest())); + // super("ECDHwithSHA384KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384())); // } // } - // + // public static class CDHwithSHA384KDFAndSharedInfo // extends KeyAgreementSpi // { // public CDHwithSHA384KDFAndSharedInfo() // { - // super("ECCDHwithSHA384KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest())); + // super("ECCDHwithSHA384KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384())); // } // } - // + // public static class DHwithSHA512KDFAndSharedInfo // extends KeyAgreementSpi // { // public DHwithSHA512KDFAndSharedInfo() // { - // super("ECDHwithSHA512KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest())); + // super("ECDHwithSHA512KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512())); // } // } - // + // public static class CDHwithSHA512KDFAndSharedInfo // extends KeyAgreementSpi // { // public CDHwithSHA512KDFAndSharedInfo() // { - // super("ECCDHwithSHA512KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest())); + // super("ECCDHwithSHA512KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512())); // } // } - // + // public static class MQVwithSHA1KDFAndSharedInfo // extends KeyAgreementSpi // { // public MQVwithSHA1KDFAndSharedInfo() // { - // super("ECMQVwithSHA1KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest())); + // super("ECMQVwithSHA1KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class MQVwithSHA224KDFAndSharedInfo // extends KeyAgreementSpi // { // public MQVwithSHA224KDFAndSharedInfo() // { - // super("ECMQVwithSHA224KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest())); + // super("ECMQVwithSHA224KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224())); // } // } - // + // public static class MQVwithSHA256KDFAndSharedInfo // extends KeyAgreementSpi // { // public MQVwithSHA256KDFAndSharedInfo() // { - // super("ECMQVwithSHA256KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest())); + // super("ECMQVwithSHA256KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256())); // } // } - // + // public static class MQVwithSHA384KDFAndSharedInfo // extends KeyAgreementSpi // { // public MQVwithSHA384KDFAndSharedInfo() // { - // super("ECMQVwithSHA384KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest())); + // super("ECMQVwithSHA384KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384())); // } // } - // + // public static class MQVwithSHA512KDFAndSharedInfo // extends KeyAgreementSpi // { // public MQVwithSHA512KDFAndSharedInfo() // { - // super("ECMQVwithSHA512KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest())); + // super("ECMQVwithSHA512KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512())); // } // } - // + // public static class DHwithSHA1CKDF // extends KeyAgreementSpi // { // public DHwithSHA1CKDF() // { - // super("ECDHwithSHA1CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA1Digest())); + // super("ECDHwithSHA1CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class DHwithSHA256CKDF // extends KeyAgreementSpi // { // public DHwithSHA256CKDF() // { - // super("ECDHwithSHA256CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA256Digest())); + // super("ECDHwithSHA256CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA256())); // } // } - // + // public static class DHwithSHA384CKDF // extends KeyAgreementSpi // { // public DHwithSHA384CKDF() // { - // super("ECDHwithSHA384CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA384Digest())); + // super("ECDHwithSHA384CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA384())); // } // } - // + // public static class DHwithSHA512CKDF // extends KeyAgreementSpi // { // public DHwithSHA512CKDF() // { - // super("ECDHwithSHA512CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA512Digest())); + // super("ECDHwithSHA512CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA512())); // } // } - // + // public static class MQVwithSHA1CKDF // extends KeyAgreementSpi // { // public MQVwithSHA1CKDF() // { - // super("ECMQVwithSHA1CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA1Digest())); + // super("ECMQVwithSHA1CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA1())); // } // } - // + // public static class MQVwithSHA224CKDF // extends KeyAgreementSpi // { // public MQVwithSHA224CKDF() // { - // super("ECMQVwithSHA224CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA224Digest())); + // super("ECMQVwithSHA224CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA224())); // } // } - // + // public static class MQVwithSHA256CKDF // extends KeyAgreementSpi // { // public MQVwithSHA256CKDF() // { - // super("ECMQVwithSHA256CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA256Digest())); + // super("ECMQVwithSHA256CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA256())); // } // } - // + // public static class MQVwithSHA384CKDF // extends KeyAgreementSpi // { // public MQVwithSHA384CKDF() // { - // super("ECMQVwithSHA384CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA384Digest())); + // super("ECMQVwithSHA384CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA384())); // } // } - // + // public static class MQVwithSHA512CKDF // extends KeyAgreementSpi // { // public MQVwithSHA512CKDF() // { - // super("ECMQVwithSHA512CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA512Digest())); + // super("ECMQVwithSHA512CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA512())); // } // } + // END android-removed } |