Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BCRainbowPublicKey.java « rainbow « provider « jcajce « pqc « bouncycastle « org « java « main « src « prov - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 453cb6155c2d398b64ef33819a58511e1d1189ba (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.bouncycastle.pqc.jcajce.provider.rainbow;

import java.security.PublicKey;

import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.pqc.asn1.PQCObjectIdentifiers;
import org.bouncycastle.pqc.asn1.RainbowPublicKey;
import org.bouncycastle.pqc.crypto.rainbow.RainbowParameters;
import org.bouncycastle.pqc.crypto.rainbow.RainbowPublicKeyParameters;
import org.bouncycastle.pqc.crypto.rainbow.util.RainbowUtil;
import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil;
import org.bouncycastle.pqc.jcajce.spec.RainbowPublicKeySpec;
import org.bouncycastle.util.Arrays;

/**
 * This class implements CipherParameters and PublicKey.
 * <p/>
 * The public key in Rainbow consists of n - v1 polynomial components of the
 * private key's F and the field structure of the finite field k.
 * <p/>
 * The quadratic (or mixed) coefficients of the polynomials from the public key
 * are stored in the 2-dimensional array in lexicographical order, requiring n *
 * (n + 1) / 2 entries for each polynomial. The singular terms are stored in a
 * 2-dimensional array requiring n entries per polynomial, the scalar term of
 * each polynomial is stored in a 1-dimensional array.
 * <p/>
 * More detailed information on the public key is to be found in the paper of
 * Jintai Ding, Dieter Schmidt: Rainbow, a New Multivariable Polynomial
 * Signature Scheme. ACNS 2005: 164-175 (http://dx.doi.org/10.1007/11496137_12)
 */
public class BCRainbowPublicKey
    implements PublicKey
{
    private static final long serialVersionUID = 1L;

    private short[][] coeffquadratic;
    private short[][] coeffsingular;
    private short[] coeffscalar;
    private int docLength; // length of possible document to sign

    private RainbowParameters rainbowParams;

    /**
     * Constructor
     *
     * @param docLength
     * @param coeffQuadratic
     * @param coeffSingular
     * @param coeffScalar
     */
    public BCRainbowPublicKey(int docLength,
                              short[][] coeffQuadratic, short[][] coeffSingular,
                              short[] coeffScalar)
    {
        this.docLength = docLength;
        this.coeffquadratic = coeffQuadratic;
        this.coeffsingular = coeffSingular;
        this.coeffscalar = coeffScalar;
    }

    /**
     * Constructor (used by the {@link RainbowKeyFactorySpi}).
     *
     * @param keySpec a {@link RainbowPublicKeySpec}
     */
    public BCRainbowPublicKey(RainbowPublicKeySpec keySpec)
    {
        this(keySpec.getDocLength(), keySpec.getCoeffQuadratic(), keySpec
            .getCoeffSingular(), keySpec.getCoeffScalar());
    }

    public BCRainbowPublicKey(
        RainbowPublicKeyParameters params)
    {
        this(params.getDocLength(), params.getCoeffQuadratic(), params.getCoeffSingular(), params.getCoeffScalar());
    }

    /**
     * @return the docLength
     */
    public int getDocLength()
    {
        return this.docLength;
    }

    /**
     * @return the coeffQuadratic
     */
    public short[][] getCoeffQuadratic()
    {
        return coeffquadratic;
    }

    /**
     * @return the coeffSingular
     */
    public short[][] getCoeffSingular()
    {
        short[][] copy = new short[coeffsingular.length][];

        for (int i = 0; i != coeffsingular.length; i++)
        {
            copy[i] = Arrays.clone(coeffsingular[i]);
        }

        return copy;
    }


    /**
     * @return the coeffScalar
     */
    public short[] getCoeffScalar()
    {
        return Arrays.clone(coeffscalar);
    }

    /**
     * Compare this Rainbow public key with another object.
     *
     * @param other the other object
     * @return the result of the comparison
     */
    public boolean equals(Object other)
    {
        if (other == null || !(other instanceof BCRainbowPublicKey))
        {
            return false;
        }
        BCRainbowPublicKey otherKey = (BCRainbowPublicKey)other;

        return docLength == otherKey.getDocLength()
            && RainbowUtil.equals(coeffquadratic, otherKey.getCoeffQuadratic())
            && RainbowUtil.equals(coeffsingular, otherKey.getCoeffSingular())
            && RainbowUtil.equals(coeffscalar, otherKey.getCoeffScalar());
    }

    public int hashCode()
    {
        int hash = docLength;

        hash = hash * 37 + Arrays.hashCode(coeffquadratic);
        hash = hash * 37 + Arrays.hashCode(coeffsingular);
        hash = hash * 37 + Arrays.hashCode(coeffscalar);

        return hash;
    }

    /**
     * @return name of the algorithm - "Rainbow"
     */
    public final String getAlgorithm()
    {
        return "Rainbow";
    }

    public String getFormat()
    {
        return "X.509";
    }

    public byte[] getEncoded()
    {
        RainbowPublicKey key = new RainbowPublicKey(docLength, coeffquadratic, coeffsingular, coeffscalar);
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.rainbow, DERNull.INSTANCE);

        return KeyUtil.getEncodedSubjectPublicKeyInfo(algorithmIdentifier, key);
    }
}