01: package org.bouncycastle.crypto.agreement;
02:
03: import java.math.BigInteger;
04:
05: import org.bouncycastle.math.ec.ECPoint;
06:
07: import org.bouncycastle.crypto.BasicAgreement;
08: import org.bouncycastle.crypto.CipherParameters;
09: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
10: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
11: import org.bouncycastle.crypto.params.ECDomainParameters;
12:
13: /**
14: * P1363 7.2.2 ECSVDP-DHC
15: *
16: * ECSVDP-DHC is Elliptic Curve Secret Value Derivation Primitive,
17: * Diffie-Hellman version with cofactor multiplication. It is based on
18: * the work of [DH76], [Mil86], [Kob87], [LMQ98] and [Kal98a]. This
19: * primitive derives a shared secret value from one party's private key
20: * and another party's public key, where both have the same set of EC
21: * domain parameters. If two parties correctly execute this primitive,
22: * they will produce the same output. This primitive can be invoked by a
23: * scheme to derive a shared secret key; specifically, it may be used
24: * with the schemes ECKAS-DH1 and DL/ECKAS-DH2. It does not assume the
25: * validity of the input public key (see also Section 7.2.1).
26: * <p>
27: * Note: As stated P1363 compatability mode with ECDH can be preset, and
28: * in this case the implementation doesn't have a ECDH compatability mode
29: * (if you want that just use ECDHBasicAgreement and note they both implement
30: * BasicAgreement!).
31: */
32: public class ECDHCBasicAgreement implements BasicAgreement {
33: ECPrivateKeyParameters key;
34:
35: public void init(CipherParameters key) {
36: this .key = (ECPrivateKeyParameters) key;
37: }
38:
39: public BigInteger calculateAgreement(CipherParameters pubKey) {
40: ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
41: ECDomainParameters params = pub.getParameters();
42: ECPoint P = pub.getQ().multiply(
43: params.getH().multiply(key.getD()));
44:
45: // if (p.isInfinity()) throw new RuntimeException("Invalid public key");
46:
47: return P.getX().toBigInteger();
48: }
49: }
|