11package net.aholbrook.paseto.crypto
22
33import net.aholbrook.paseto.exception.ByteArrayLengthException
4- import net.aholbrook.paseto.exception.KeyV3Exception
4+ import net.aholbrook.paseto.exception.EcKeyException
55import org.bouncycastle.asn1.ASN1ObjectIdentifier
66import org.bouncycastle.asn1.ASN1Primitive
77import org.bouncycastle.asn1.DERBitString
@@ -11,16 +11,13 @@ import org.bouncycastle.asn1.sec.SECObjectIdentifiers
1111import org.bouncycastle.asn1.x509.AlgorithmIdentifier
1212import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo
1313import org.bouncycastle.asn1.x9.X9ObjectIdentifiers
14- import org.bouncycastle.crypto.CryptoException
1514import org.bouncycastle.crypto.digests.SHA384Digest
1615import org.bouncycastle.crypto.ec.CustomNamedCurves
1716import org.bouncycastle.crypto.generators.ECKeyPairGenerator
1817import org.bouncycastle.crypto.params.ECDomainParameters
1918import org.bouncycastle.crypto.params.ECKeyGenerationParameters
2019import org.bouncycastle.crypto.params.ECPrivateKeyParameters
2120import org.bouncycastle.crypto.params.ECPublicKeyParameters
22- import org.bouncycastle.crypto.params.RSAKeyParameters
23- import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters
2421import org.bouncycastle.crypto.signers.ECDSASigner
2522import org.bouncycastle.crypto.signers.HMacDSAKCalculator
2623import org.bouncycastle.crypto.util.PrivateKeyFactory
@@ -49,7 +46,7 @@ internal fun ecdsaP384Sign(sig: ByteArray, m: ByteArray, sk: ByteArray, enforceL
4946
5047 val d = BigInteger (1 , sk)
5148 if (d.signum() <= 0 || d >= params.n) {
52- throw KeyV3Exception (" Invalid P-384 private key" )
49+ throw EcKeyException (" Invalid P-384 private key" )
5350 }
5451 val secretKey = ECPrivateKeyParameters (d, params)
5552
@@ -134,7 +131,7 @@ internal fun p384SkToPk(sk: ByteArray): ByteArray {
134131 val params = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
135132 val d = BigInteger (1 , sk)
136133 if (d.signum() <= 0 || d >= params.n) {
137- throw KeyV3Exception (" Invalid P-384 private key" )
134+ throw EcKeyException (" Invalid P-384 private key" )
138135 }
139136 val q = params.g.multiply(d).normalize()
140137 return q.getEncoded(true )
@@ -145,21 +142,21 @@ internal fun p384VerifyPk(pk: ByteArray): ECPoint {
145142 throw ByteArrayLengthException (" pk" , pk.size, ECDSA_P384_PUBLICKEYBYTES )
146143 }
147144 if (pk[0 ] != 0x02 .toByte() && pk[0 ] != 0x03 .toByte()) {
148- throw KeyV3Exception (" must use point compression" )
145+ throw EcKeyException (" must use point compression" )
149146 }
150147
151148 val params = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
152149 val q = try {
153150 params.curve.decodePoint(pk)
154151 } catch (ex: IllegalArgumentException ) {
155- throw KeyV3Exception (" decode point" , ex)
152+ throw EcKeyException (" decode point" , ex)
156153 }
157154
158155 if (q.isInfinity) {
159- throw KeyV3Exception (" Point at infinity" )
156+ throw EcKeyException (" Point at infinity" )
160157 }
161158 if (! q.isValid) {
162- throw KeyV3Exception (" Point not on curve" )
159+ throw EcKeyException (" Point not on curve" )
163160 }
164161
165162 return q
@@ -173,7 +170,7 @@ internal fun p384EncodeSkPkcs8(sk: ByteArray): ByteArray {
173170 val d = BigInteger (1 , sk)
174171
175172 if (d.signum() <= 0 || d >= params.n) {
176- throw KeyV3Exception (" Invalid P-384 private key" )
173+ throw EcKeyException (" Invalid P-384 private key" )
177174 }
178175
179176 @Suppress(" MagicNumber" )
@@ -190,23 +187,23 @@ internal fun p384EncodeSkPkcs8(sk: ByteArray): ByteArray {
190187internal fun p384DecodeSkPkcs8 (der : ByteArray ): ByteArray {
191188 try {
192189 val params = PrivateKeyFactory .createKey(der) as ? ECPrivateKeyParameters
193- ? : throw KeyV3Exception (" Private key is not on secp384r1" )
190+ ? : throw EcKeyException (" Private key is not on secp384r1" )
194191 val domain = params.parameters
195192 val expected = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
196193
197194 if (domain.curve != expected.curve || domain.g != expected.g || domain.n != expected.n ||
198195 domain.h != expected.h
199196 ) {
200- throw KeyV3Exception (" Private key is not on secp384r1" )
197+ throw EcKeyException (" Private key is not on secp384r1" )
201198 }
202199
203200 if (params.d.signum() <= 0 || params.d >= domain.n) {
204- throw KeyV3Exception (" Invalid private key" )
201+ throw EcKeyException (" Invalid private key" )
205202 }
206203
207204 return BigIntegers .asUnsignedByteArray(ECDSA_P384_SECRETKEYBYTES , params.d)
208205 } catch (ex: Throwable ) {
209- throw KeyV3Exception (" invalid private key" , ex)
206+ throw EcKeyException (" invalid private key" , ex)
210207 }
211208}
212209
@@ -218,7 +215,7 @@ internal fun p384EncodeSkSec1(sk: ByteArray): ByteArray {
218215 val params = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
219216 val d = BigInteger (1 , sk)
220217 if (d.signum() <= 0 || d >= params.n) {
221- throw KeyV3Exception (" Invalid P-384 private key" )
218+ throw EcKeyException (" Invalid P-384 private key" )
222219 }
223220
224221 val publicKey = DERBitString (params.g.multiply(d).normalize().getEncoded(false ))
@@ -232,31 +229,31 @@ internal fun p384EncodeSkSec1(sk: ByteArray): ByteArray {
232229internal fun p384DecodeSkSec1 (der : ByteArray ): ByteArray {
233230 try {
234231 val key = ECPrivateKey .getInstance(ASN1Primitive .fromByteArray(der))
235- ? : throw KeyV3Exception (" Invalid private key" )
232+ ? : throw EcKeyException (" Invalid private key" )
236233 val curveParams = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
237234
238235 key.parametersObject?.let { params ->
239236 when (params) {
240237 is ASN1ObjectIdentifier -> {
241238 if (params != SECObjectIdentifiers .secp384r1) {
242- throw KeyV3Exception (" SEC1 key not on secp384r1" )
239+ throw EcKeyException (" SEC1 key not on secp384r1" )
243240 }
244241 }
245242
246243 else -> {
247- throw KeyV3Exception (" Unsupported curve parameters" )
244+ throw EcKeyException (" Unsupported curve parameters" )
248245 }
249246 }
250- } ? : throw KeyV3Exception (" SEC1 key must include curve parameters" )
247+ } ? : throw EcKeyException (" SEC1 key must include curve parameters" )
251248
252249 val d = key.key
253250 if (d.signum() <= 0 || d >= curveParams.n) {
254- throw KeyV3Exception (" Invalid P-384 private key" )
251+ throw EcKeyException (" Invalid P-384 private key" )
255252 }
256253
257254 return BigIntegers .asUnsignedByteArray(ECDSA_P384_SECRETKEYBYTES , d)
258255 } catch (ex: Throwable ) {
259- throw KeyV3Exception (" Invalid private key" , ex)
256+ throw EcKeyException (" Invalid private key" , ex)
260257 }
261258}
262259
@@ -272,21 +269,21 @@ internal fun p384EncodePkSpki(pk: ByteArray): ByteArray {
272269internal fun p384DecodePkSpki (der : ByteArray ): ByteArray {
273270 try {
274271 val params = PublicKeyFactory .createKey(der) as ? ECPublicKeyParameters
275- ? : throw KeyV3Exception (" Public key is not on secp384r1" )
272+ ? : throw EcKeyException (" Public key is not on secp384r1" )
276273 val domain = params.parameters
277274 val expected = CustomNamedCurves .getByOID(SECObjectIdentifiers .secp384r1)
278275
279276 if (domain.curve != expected.curve || domain.g != expected.g || domain.n != expected.n ||
280277 domain.h != expected.h
281278 ) {
282- throw KeyV3Exception (" Public key is not on secp384r1" )
279+ throw EcKeyException (" Public key is not on secp384r1" )
283280 }
284281
285282 return params.q.getEncoded(true ).also {
286283 p384VerifyPk(it)
287284 }
288285 } catch (ex: Throwable ) {
289- throw KeyV3Exception (" invalid public key" , ex)
286+ throw EcKeyException (" invalid public key" , ex)
290287 }
291288}
292289
0 commit comments