ECDHBasicAgreement.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.crypto.agreement;
2
3import java.math.BigInteger;
4
5import org.bouncycastle.crypto.BasicAgreement;
6import org.bouncycastle.crypto.CipherParameters;
7import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
8import org.bouncycastle.crypto.params.ECPublicKeyParameters;
9import org.bouncycastle.math.ec.ECPoint;
10
11/**
12 * P1363 7.2.1 ECSVDP-DH
13 *
14 * ECSVDP-DH is Elliptic Curve Secret Value Derivation Primitive,
15 * Diffie-Hellman version. It is based on the work of [DH76], [Mil86],
16 * and [Kob87]. This primitive derives a shared secret value from one
17 * party's private key and another party's public key, where both have
18 * the same set of EC domain parameters. If two parties correctly
19 * execute this primitive, they will produce the same output. This
20 * primitive can be invoked by a scheme to derive a shared secret key;
21 * specifically, it may be used with the schemes ECKAS-DH1 and
22 * DL/ECKAS-DH2. It assumes that the input keys are valid (see also
23 * Section 7.2.2).
24 */
25public class ECDHBasicAgreement
26    implements BasicAgreement
27{
28    private ECPrivateKeyParameters key;
29
30    public void init(
31        CipherParameters key)
32    {
33        this.key = (ECPrivateKeyParameters)key;
34    }
35
36    public int getFieldSize()
37    {
38        return (key.getParameters().getCurve().getFieldSize() + 7) / 8;
39    }
40
41    public BigInteger calculateAgreement(
42        CipherParameters pubKey)
43    {
44        ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey;
45        ECPoint P = pub.getQ().multiply(key.getD());
46
47        // if (p.isInfinity()) throw new RuntimeException("d*Q == infinity");
48
49        return P.getX().toBigInteger();
50    }
51}
52