|
1 | 1 | package org.bouncycastle.crypto.signers; |
2 | 2 |
|
3 | | -import java.math.BigInteger; |
4 | | -import java.security.SecureRandom; |
5 | | - |
6 | | -import org.bouncycastle.crypto.CipherParameters; |
7 | | -import org.bouncycastle.crypto.CryptoServicesRegistrar; |
8 | | -import org.bouncycastle.crypto.DSAExt; |
9 | | -import org.bouncycastle.crypto.params.ECDomainParameters; |
10 | | -import org.bouncycastle.crypto.params.ECKeyParameters; |
11 | | -import org.bouncycastle.crypto.params.ECPrivateKeyParameters; |
12 | | -import org.bouncycastle.crypto.params.ECPublicKeyParameters; |
13 | | -import org.bouncycastle.crypto.params.ParametersWithRandom; |
14 | | -import org.bouncycastle.math.ec.ECAlgorithms; |
15 | | -import org.bouncycastle.math.ec.ECConstants; |
16 | | -import org.bouncycastle.math.ec.ECMultiplier; |
17 | | -import org.bouncycastle.math.ec.ECPoint; |
18 | | -import org.bouncycastle.math.ec.FixedPointCombMultiplier; |
19 | | -import org.bouncycastle.util.Arrays; |
20 | | -import org.bouncycastle.util.BigIntegers; |
21 | | - |
22 | 3 | /** |
23 | 4 | * GOST R 34.10-2012 Signature Algorithm |
| 5 | + * |
| 6 | + * @deprecated Use {@link ECGOST3410Signer} instead. |
24 | 7 | */ |
25 | 8 | public class ECGOST3410_2012Signer |
26 | | - implements DSAExt |
| 9 | + extends ECGOST3410Signer |
27 | 10 | { |
28 | | - ECKeyParameters key; |
29 | | - |
30 | | - SecureRandom random; |
31 | | - |
32 | | - public void init( |
33 | | - boolean forSigning, |
34 | | - CipherParameters param) |
35 | | - { |
36 | | - if (forSigning) |
37 | | - { |
38 | | - if (param instanceof ParametersWithRandom) |
39 | | - { |
40 | | - ParametersWithRandom rParam = (ParametersWithRandom)param; |
41 | | - |
42 | | - this.random = rParam.getRandom(); |
43 | | - this.key = (ECPrivateKeyParameters)rParam.getParameters(); |
44 | | - } |
45 | | - else |
46 | | - { |
47 | | - this.random = CryptoServicesRegistrar.getSecureRandom(); |
48 | | - this.key = (ECPrivateKeyParameters)param; |
49 | | - } |
50 | | - } |
51 | | - else |
52 | | - { |
53 | | - this.key = (ECPublicKeyParameters)param; |
54 | | - } |
55 | | - |
56 | | - CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("ECGOST3410_2012", key, forSigning)); |
57 | | - } |
58 | | - |
59 | | - public BigInteger getOrder() |
60 | | - { |
61 | | - return key.getParameters().getN(); |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * generate a signature for the given message using the key we were |
66 | | - * initialised with. For conventional GOST3410 2012 the message should be a GOST3411 2012 |
67 | | - * hash of the message of interest. |
68 | | - * |
69 | | - * @param message the message that will be verified later. |
70 | | - */ |
71 | | - public BigInteger[] generateSignature( |
72 | | - byte[] message) |
73 | | - { |
74 | | - byte[] mRev = Arrays.reverse(message); // conversion is little-endian |
75 | | - BigInteger e = new BigInteger(1, mRev); |
76 | | - |
77 | | - ECDomainParameters ec = key.getParameters(); |
78 | | - BigInteger n = ec.getN(); |
79 | | - BigInteger d = ((ECPrivateKeyParameters)key).getD(); |
80 | | - |
81 | | - BigInteger r, s; |
82 | | - |
83 | | - ECMultiplier basePointMultiplier = createBasePointMultiplier(); |
84 | | - |
85 | | - do // generate s |
86 | | - { |
87 | | - BigInteger k; |
88 | | - do // generate r |
89 | | - { |
90 | | - do |
91 | | - { |
92 | | - k = BigIntegers.createRandomBigInteger(n.bitLength(), random); |
93 | | - } |
94 | | - while (k.equals(ECConstants.ZERO)); |
95 | | - |
96 | | - ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize(); |
97 | | - |
98 | | - r = p.getAffineXCoord().toBigInteger().mod(n); |
99 | | - } |
100 | | - while (r.equals(ECConstants.ZERO)); |
101 | | - |
102 | | - s = (k.multiply(e)).add(d.multiply(r)).mod(n); |
103 | | - } |
104 | | - while (s.equals(ECConstants.ZERO)); |
105 | | - |
106 | | - return new BigInteger[]{ r, s }; |
107 | | - } |
108 | | - |
109 | | - /** |
110 | | - * return true if the value r and s represent a GOST3410 2012 signature for |
111 | | - * the passed in message (for standard GOST3410 2012 the message should be |
112 | | - * a GOST3411 2012 hash of the real message to be verified). |
113 | | - */ |
114 | | - public boolean verifySignature( |
115 | | - byte[] message, |
116 | | - BigInteger r, |
117 | | - BigInteger s) |
118 | | - { |
119 | | - byte[] mRev = Arrays.reverse(message); // conversion is little-endian |
120 | | - BigInteger e = new BigInteger(1, mRev); |
121 | | - BigInteger n = key.getParameters().getN(); |
122 | | - |
123 | | - // r in the range [1,n-1] |
124 | | - if (r.compareTo(ECConstants.ONE) < 0 || r.compareTo(n) >= 0) |
125 | | - { |
126 | | - return false; |
127 | | - } |
128 | | - |
129 | | - // s in the range [1,n-1] |
130 | | - if (s.compareTo(ECConstants.ONE) < 0 || s.compareTo(n) >= 0) |
131 | | - { |
132 | | - return false; |
133 | | - } |
134 | | - |
135 | | - BigInteger v = BigIntegers.modOddInverseVar(n, e); |
136 | | - |
137 | | - BigInteger z1 = s.multiply(v).mod(n); |
138 | | - BigInteger z2 = (n.subtract(r)).multiply(v).mod(n); |
139 | | - |
140 | | - ECPoint G = key.getParameters().getG(); // P |
141 | | - ECPoint Q = ((ECPublicKeyParameters)key).getQ(); |
142 | | - |
143 | | - ECPoint point = ECAlgorithms.sumOfTwoMultiplies(G, z1, Q, z2).normalize(); |
144 | | - |
145 | | - // components must be bogus. |
146 | | - if (point.isInfinity()) |
147 | | - { |
148 | | - return false; |
149 | | - } |
150 | | - |
151 | | - BigInteger R = point.getAffineXCoord().toBigInteger().mod(n); |
152 | | - |
153 | | - return R.equals(r); |
154 | | - } |
155 | | - |
156 | | - protected ECMultiplier createBasePointMultiplier() |
157 | | - { |
158 | | - return new FixedPointCombMultiplier(); |
159 | | - } |
160 | 11 | } |
0 commit comments