|
1 | 1 | package org.tron.common.crypto.sm2; |
2 | 2 |
|
3 | 3 | import java.math.BigInteger; |
4 | | -import javax.annotation.Nullable; |
5 | 4 | import org.bouncycastle.crypto.CipherParameters; |
6 | | -import org.bouncycastle.crypto.Digest; |
7 | 5 | import org.bouncycastle.crypto.digests.SM3Digest; |
8 | 6 | import org.bouncycastle.crypto.params.ECDomainParameters; |
9 | 7 | import org.bouncycastle.crypto.params.ECKeyParameters; |
10 | 8 | import org.bouncycastle.crypto.params.ECPrivateKeyParameters; |
11 | 9 | import org.bouncycastle.crypto.params.ECPublicKeyParameters; |
12 | | -import org.bouncycastle.crypto.params.ParametersWithID; |
13 | 10 | import org.bouncycastle.crypto.signers.DSAKCalculator; |
14 | 11 | import org.bouncycastle.crypto.signers.HMacDSAKCalculator; |
15 | 12 | import org.bouncycastle.math.ec.ECConstants; |
16 | | -import org.bouncycastle.math.ec.ECFieldElement; |
17 | 13 | import org.bouncycastle.math.ec.ECMultiplier; |
18 | 14 | import org.bouncycastle.math.ec.ECPoint; |
19 | 15 | import org.bouncycastle.math.ec.FixedPointCombMultiplier; |
20 | | -import org.bouncycastle.util.BigIntegers; |
21 | 16 | import org.tron.common.utils.ByteArray; |
22 | 17 |
|
| 18 | +/** |
| 19 | + * Low-level SM2 signer used by {@link org.tron.common.crypto.sm2.SM2}. |
| 20 | + * |
| 21 | + * <p>Exposes two operations: {@link #generateHashSignature} (sign a pre-computed 32-byte hash) |
| 22 | + * and {@link #verifyHashSignature} (verify against a pre-computed hash). The standard SM2 |
| 23 | + * {@code Z_A} pre-hash step is intentionally absent; see {@link org.tron.common.crypto.sm2.SM2} |
| 24 | + * for the rationale. |
| 25 | + */ |
23 | 26 | public class SM2Signer |
24 | 27 | implements ECConstants { |
25 | 28 |
|
26 | 29 | private final DSAKCalculator kCalculator = new HMacDSAKCalculator(new SM3Digest()); |
27 | 30 |
|
28 | | - private byte[] userID; |
29 | | - |
30 | | - private int curveLength; |
31 | 31 | private ECDomainParameters ecParams; |
32 | | - private ECPoint pubPoint; |
33 | 32 | private ECKeyParameters ecKey; |
34 | 33 |
|
35 | | - public void init(boolean forSigning, CipherParameters param) { |
| 34 | + public void init(CipherParameters param) { |
36 | 35 | if (param == null) { |
37 | 36 | throw new IllegalArgumentException("CipherParameters cannot be null"); |
38 | 37 | } |
39 | | - CipherParameters baseParam; |
40 | | - |
41 | | - if (param instanceof ParametersWithID) { |
42 | | - baseParam = ((ParametersWithID) param).getParameters(); |
43 | | - userID = ((ParametersWithID) param).getID(); |
44 | | - } else { |
45 | | - baseParam = param; |
46 | | - userID = new byte[0]; |
47 | | - } |
48 | | - |
49 | | - ecKey = (ECKeyParameters) baseParam; |
| 38 | + ecKey = (ECKeyParameters) param; |
50 | 39 | ecParams = ecKey.getParameters(); |
51 | | - |
52 | | - if (forSigning) { |
53 | | - pubPoint = ecParams.getG().multiply(((ECPrivateKeyParameters) ecKey).getD()).normalize(); |
54 | | - } else { |
55 | | - pubPoint = ((ECPublicKeyParameters) ecKey).getQ(); |
56 | | - } |
57 | | - |
58 | | - curveLength = (ecParams.getCurve().getFieldSize() + 7) / 8; |
59 | | - } |
60 | | - |
61 | | - |
62 | | - /** |
63 | | - * generate the signature for the message |
64 | | - * |
65 | | - * @param message plaintext |
66 | | - */ |
67 | | - public BigInteger[] generateSignature(byte[] message) { |
68 | | - byte[] eHash = generateSM3Hash(message); |
69 | | - return generateHashSignature(eHash); |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * generate the signature for the message |
74 | | - */ |
75 | | - |
76 | | - public byte[] generateSM3Hash(byte[] message) { |
77 | | - if (message == null) { |
78 | | - throw new IllegalArgumentException("Message cannot be null"); |
79 | | - } |
80 | | - SM3Digest digest = new SM3Digest(); |
81 | | - byte[] z = getZ(digest); |
82 | | - |
83 | | - digest.update(z, 0, z.length); |
84 | | - digest.update(message, 0, message.length); |
85 | | - |
86 | | - byte[] eHash = new byte[digest.getDigestSize()]; |
87 | | - |
88 | | - digest.doFinal(eHash, 0); |
89 | | - return eHash; |
90 | 40 | } |
91 | 41 |
|
92 | 42 | /** |
@@ -136,51 +86,6 @@ public BigInteger[] generateHashSignature(byte[] hash) { |
136 | 86 | return new BigInteger[]{r, s}; |
137 | 87 | } |
138 | 88 |
|
139 | | - /** |
140 | | - * verify the message signature |
141 | | - */ |
142 | | - public boolean verifySignature(byte[] message, BigInteger r, BigInteger s, |
143 | | - @Nullable String userID) { |
144 | | - if (message == null || r == null || s == null) { |
145 | | - throw new IllegalArgumentException("Message, R, or S cannot be null"); |
146 | | - } |
147 | | - BigInteger n = ecParams.getN(); |
148 | | - |
149 | | - // 5.3.1 Draft RFC: SM2 Public Key Algorithms |
150 | | - // B1 |
151 | | - if (r.compareTo(ONE) < 0 || r.compareTo(n) >= 0) { |
152 | | - return false; |
153 | | - } |
154 | | - |
155 | | - // B2 |
156 | | - if (s.compareTo(ONE) < 0 || s.compareTo(n) >= 0) { |
157 | | - return false; |
158 | | - } |
159 | | - |
160 | | - ECPoint q = ((ECPublicKeyParameters) ecKey).getQ(); |
161 | | - |
162 | | - if (userID != null) { |
163 | | - this.userID = userID.getBytes(); |
164 | | - } |
165 | | - byte[] eHash = generateSM3Hash(message); |
166 | | - |
167 | | - // B4 |
168 | | - BigInteger e = calculateE(eHash); |
169 | | - |
170 | | - // B5 |
171 | | - BigInteger t = r.add(s).mod(n); |
172 | | - if (t.equals(ZERO)) { |
173 | | - return false; |
174 | | - } else { |
175 | | - // B6 |
176 | | - ECPoint x1y1 = ecParams.getG().multiply(s); |
177 | | - x1y1 = x1y1.add(q.multiply(t)).normalize(); |
178 | | - |
179 | | - // B7 |
180 | | - return r.equals(e.add(x1y1.getAffineXCoord().toBigInteger()).mod(n)); |
181 | | - } |
182 | | - } |
183 | | - |
184 | 89 | /** |
185 | 90 | * verify the hash signature |
186 | 91 | */ |
@@ -224,36 +129,6 @@ public boolean verifyHashSignature(byte[] hash, BigInteger r, BigInteger s) { |
224 | 129 | } |
225 | 130 | } |
226 | 131 |
|
227 | | - private byte[] getZ(Digest digest) { |
228 | | - |
229 | | - //addUserID(digest, userID); |
230 | | - |
231 | | - addFieldElement(digest, ecParams.getCurve().getA()); |
232 | | - addFieldElement(digest, ecParams.getCurve().getB()); |
233 | | - addFieldElement(digest, ecParams.getG().getAffineXCoord()); |
234 | | - addFieldElement(digest, ecParams.getG().getAffineYCoord()); |
235 | | - addFieldElement(digest, pubPoint.getAffineXCoord()); |
236 | | - addFieldElement(digest, pubPoint.getAffineYCoord()); |
237 | | - |
238 | | - byte[] rv = new byte[digest.getDigestSize()]; |
239 | | - |
240 | | - digest.doFinal(rv, 0); |
241 | | - |
242 | | - return rv; |
243 | | - } |
244 | | - |
245 | | - private void addUserID(Digest digest, byte[] userID) { |
246 | | - int len = userID.length * 8; |
247 | | - digest.update((byte) (len >> 8 & 0xFF)); |
248 | | - digest.update((byte) (len & 0xFF)); |
249 | | - digest.update(userID, 0, userID.length); |
250 | | - } |
251 | | - |
252 | | - private void addFieldElement(Digest digest, ECFieldElement v) { |
253 | | - byte[] p = BigIntegers.asUnsignedByteArray(curveLength, v.toBigInteger()); |
254 | | - digest.update(p, 0, p.length); |
255 | | - } |
256 | | - |
257 | 132 | protected ECMultiplier createBasePointMultiplier() { |
258 | 133 | return new FixedPointCombMultiplier(); |
259 | 134 | } |
|
0 commit comments