-
Notifications
You must be signed in to change notification settings - Fork 8
/
ECDSASignerRecoverable.kt
245 lines (218 loc) · 8.78 KB
/
ECDSASignerRecoverable.kt
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package io.ethers.crypto
import org.bouncycastle.crypto.CipherParameters
import org.bouncycastle.crypto.CryptoServicePurpose
import org.bouncycastle.crypto.CryptoServicesRegistrar
import org.bouncycastle.crypto.DSAExt
import org.bouncycastle.crypto.constraints.ConstraintUtils
import org.bouncycastle.crypto.constraints.DefaultServiceProperties
import org.bouncycastle.crypto.params.ECKeyParameters
import org.bouncycastle.crypto.params.ECPrivateKeyParameters
import org.bouncycastle.crypto.params.ECPublicKeyParameters
import org.bouncycastle.crypto.params.ParametersWithRandom
import org.bouncycastle.crypto.signers.DSAKCalculator
import org.bouncycastle.crypto.signers.RandomDSAKCalculator
import org.bouncycastle.math.ec.ECAlgorithms
import org.bouncycastle.math.ec.ECConstants
import org.bouncycastle.math.ec.ECCurve
import org.bouncycastle.math.ec.ECFieldElement
import org.bouncycastle.math.ec.ECMultiplier
import org.bouncycastle.math.ec.ECPoint
import org.bouncycastle.math.ec.FixedPointCombMultiplier
import org.bouncycastle.util.BigIntegers
import java.math.BigInteger
import java.security.SecureRandom
/**
* EC-DSA as described in X9.62
*
* IMPORTANT: This class has been modified from the original source code to allow returning the 'y' value. Code
* should be kept in sync with the original source code.
*
* Original: [org.bouncycastle.crypto.signers.ECDSASigner]
*/
internal class ECDSASignerRecoverable : ECConstants, DSAExt {
private val kCalculator: DSAKCalculator
private var key: ECKeyParameters? = null
private var random: SecureRandom? = null
/**
* Default configuration, random K values.
*/
constructor() {
kCalculator = RandomDSAKCalculator()
}
/**
* Configuration with an alternate, possibly deterministic calculator of K.
*
* @param kCalculator a K value calculator.
*/
constructor(kCalculator: DSAKCalculator) {
this.kCalculator = kCalculator
}
override fun init(forSigning: Boolean, param: CipherParameters) {
var providedRandom: SecureRandom? = null
if (forSigning) {
if (param is ParametersWithRandom) {
val rParam = param
key = rParam.parameters as ECPrivateKeyParameters
providedRandom = rParam.random
} else {
key = param as ECPrivateKeyParameters
}
} else {
key = param as ECPublicKeyParameters
}
// inlined the following method since it's in package-private class:
// Utils.getDefaultProperties("ECDSA", key, forSigning)
val props = DefaultServiceProperties(
"ECDSA",
ConstraintUtils.bitsOfSecurityFor(key!!.parameters.curve),
key,
if (forSigning) CryptoServicePurpose.SIGNING else CryptoServicePurpose.VERIFYING,
)
CryptoServicesRegistrar.checkConstraints(props)
random = initSecureRandom(forSigning && !kCalculator.isDeterministic, providedRandom)
}
override fun getOrder(): BigInteger {
return key!!.parameters.n
}
// 5.3 pg 28
/**
* generate a signature for the given message using the key we were
* initialised with. For conventional DSA the message should be a SHA-1
* hash of the message of interest.
*
* @param message the message that will be verified later.
*/
override fun generateSignature(message: ByteArray): Array<BigInteger> {
return generateSignatureInternal(message, false)
}
fun generateSignatureWithY(message: ByteArray): Array<BigInteger> {
return generateSignatureInternal(message, true)
}
private fun generateSignatureInternal(message: ByteArray, includeY: Boolean): Array<BigInteger> {
val ec = key!!.parameters
val n = ec.n
val e = calculateE(n, message)
val d = (key as ECPrivateKeyParameters?)!!.d
if (kCalculator.isDeterministic) {
kCalculator.init(n, d, message)
} else {
kCalculator.init(n, random)
}
var r: BigInteger
var s: BigInteger
val basePointMultiplier = createBasePointMultiplier()
// ################## CUSTOM CODE START ##################
var p: ECPoint?
// ################## CUSTOM CODE END ####################
// 5.3.2
do // generate s
{
var k: BigInteger?
do // generate r
{
k = kCalculator.nextK()
p = basePointMultiplier.multiply(ec.g, k).normalize()
// 5.3.3
r = p.affineXCoord.toBigInteger().mod(n)
} while (r == ECConstants.ZERO)
s = BigIntegers.modOddInverse(n, k).multiply(e.add(d.multiply(r))).mod(n)
} while (s == ECConstants.ZERO)
// ################## CUSTOM CODE START ##################
if (includeY) {
return arrayOf(r, s, p!!.affineYCoord.toBigInteger())
}
// ################## CUSTOM CODE END ####################
return arrayOf(r, s)
}
// 5.4 pg 29
/**
* return true if the value r and s represent a DSA signature for
* the passed in message (for standard DSA the message should be
* a SHA-1 hash of the real message to be verified).
*/
override fun verifySignature(
message: ByteArray,
r: BigInteger,
s: BigInteger,
): Boolean {
var r = r
val ec = key!!.parameters
val n = ec.n
val e = calculateE(n, message)
// r in the range [1,n-1]
if (r.compareTo(ECConstants.ONE) < 0 || r.compareTo(n) >= 0) {
return false
}
// s in the range [1,n-1]
if (s.compareTo(ECConstants.ONE) < 0 || s.compareTo(n) >= 0) {
return false
}
val c = BigIntegers.modOddInverseVar(n, s)
val u1 = e.multiply(c).mod(n)
val u2 = r.multiply(c).mod(n)
val G = ec.g
val Q = (key as ECPublicKeyParameters?)!!.q
val point = ECAlgorithms.sumOfTwoMultiplies(G, u1, Q, u2)
// components must be bogus.
if (point.isInfinity) {
return false
}
/*
* If possible, avoid normalizing the point (to save a modular inversion in the curve field).
*
* There are ~cofactor elements of the curve field that reduce (modulo the group order) to 'r'.
* If the cofactor is known and small, we generate those possible field values and project each
* of them to the same "denominator" (depending on the particular projective coordinates in use)
* as the calculated point.X. If any of the projected values matches point.X, then we have:
* (point.X / Denominator mod p) mod n == r
* as required, and verification succeeds.
*
* Based on an original idea by Gregory Maxwell (https://github.com/gmaxwell), as implemented in
* the libsecp256k1 project (https://github.com/bitcoin/secp256k1).
*/
val curve = point.curve
if (curve != null) {
val cofactor = curve.cofactor
if (cofactor != null && cofactor.compareTo(ECConstants.EIGHT) <= 0) {
val D = getDenominator(curve.coordinateSystem, point)
if (D != null && !D.isZero) {
val X = point.xCoord
while (curve.isValidFieldElement(r)) {
val R = curve.fromBigInteger(r).multiply(D)
if (R == X) {
return true
}
r = r.add(n)
}
return false
}
}
}
val v = point.normalize().affineXCoord.toBigInteger().mod(n)
return v == r
}
protected fun calculateE(n: BigInteger, message: ByteArray): BigInteger {
val log2n = n.bitLength()
val messageBitLength = message.size * 8
var e = BigInteger(1, message)
if (log2n < messageBitLength) {
e = e.shiftRight(messageBitLength - log2n)
}
return e
}
protected fun createBasePointMultiplier(): ECMultiplier {
return MULTIPLIER
}
protected fun getDenominator(coordinateSystem: Int, p: ECPoint): ECFieldElement? {
return when (coordinateSystem) {
ECCurve.COORD_HOMOGENEOUS, ECCurve.COORD_LAMBDA_PROJECTIVE, ECCurve.COORD_SKEWED -> p.getZCoord(0)
ECCurve.COORD_JACOBIAN, ECCurve.COORD_JACOBIAN_CHUDNOVSKY, ECCurve.COORD_JACOBIAN_MODIFIED -> p.getZCoord(0)
.square()
else -> null
}
}
protected fun initSecureRandom(needed: Boolean, provided: SecureRandom?): SecureRandom? {
return if (needed) CryptoServicesRegistrar.getSecureRandom(provided) else null
}
}
private val MULTIPLIER = FixedPointCombMultiplier()