summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/prng/drbg/DualECPoints.java
blob: 7dcfa94f5db96f165d05562dd6ce281813368b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.bouncycastle.crypto.prng.drbg;

import org.bouncycastle.math.ec.ECPoint;

/**
 * General class for providing point pairs for use with DualEC DRBG. See NIST SP 800-90A for further details.
 */
public class DualECPoints
{
    private final ECPoint p;
    private final ECPoint q;
    private final int securityStrength;
    private final int cofactor;

    /**
     * Base Constructor.
     * <p>
     * The cofactor is used to calculate the output block length (maxOutlen) according to
     * <pre>
     *     max_outlen = largest multiple of 8 less than ((field size in bits) - (13 + log2(cofactor))
     * </pre>
     *
     * @param securityStrength maximum security strength to be associated with these parameters
     * @param p the P point.
     * @param q the Q point.
     * @param cofactor cofactor associated with the domain parameters for the point generation.
     */
    public DualECPoints(int securityStrength, ECPoint p, ECPoint q, int cofactor)
    {
        if (!p.getCurve().equals(q.getCurve()))
        {
            throw new IllegalArgumentException("points need to be on the same curve");
        }

        this.securityStrength = securityStrength;
        this.p = p;
        this.q = q;
        this.cofactor = cofactor;
    }

    public int getSeedLen()
    {
        return p.getCurve().getFieldSize();
    }

    public int getMaxOutlen()
    {
        return ((p.getCurve().getFieldSize() - (13 + log2(cofactor))) / 8) * 8;
    }

    public ECPoint getP()
    {
        return p;
    }

    public ECPoint getQ()
    {
        return q;
    }

    public int getSecurityStrength()
    {
        return securityStrength;
    }

    public int getCofactor()
    {
        return cofactor;
    }

    private static int log2(int value)
    {
        int log = 0;

        while ((value >>= 1) != 0)
        {
            log++;
        }

        return log;
    }
}