This repository was archived by the owner on Jul 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSECP256R1KeyPair.java
More file actions
106 lines (84 loc) · 4.18 KB
/
SECP256R1KeyPair.java
File metadata and controls
106 lines (84 loc) · 4.18 KB
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
package org.biscuitsec.biscuit.crypto;
import biscuit.format.schema.Schema.PublicKey.Algorithm;
import org.biscuitsec.biscuit.token.builder.Utils;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.util.BigIntegers;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Optional;
class SECP256R1KeyPair extends KeyPair {
private final BCECPrivateKey privateKey;
private final BCECPublicKey publicKey;
private final Signer signer;
private static final String ALGORITHM = "ECDSA";
private static final String CURVE = "secp256r1";
private static final ECNamedCurveParameterSpec CURVE_SPEC = ECNamedCurveTable.getParameterSpec(CURVE);
public SECP256R1KeyPair(byte[] bytes) {
var privateKeySpec = new ECPrivateKeySpec(BigIntegers.fromUnsignedByteArray(bytes), CURVE_SPEC);
var privateKey = new BCECPrivateKey(ALGORITHM, privateKeySpec, BouncyCastleProvider.CONFIGURATION);
var publicKeySpec = new ECPublicKeySpec(CURVE_SPEC.getG().multiply(privateKeySpec.getD()), CURVE_SPEC);
var publicKey = new BCECPublicKey(ALGORITHM, publicKeySpec, BouncyCastleProvider.CONFIGURATION);
this.privateKey = privateKey;
this.publicKey = publicKey;
this.signer = new PrivateKeySigner(Algorithm.SECP256R1, privateKey);
}
public SECP256R1KeyPair(SecureRandom rng) {
byte[] bytes = new byte[32];
rng.nextBytes(bytes);
var privateKeySpec = new ECPrivateKeySpec(BigIntegers.fromUnsignedByteArray(bytes), CURVE_SPEC);
var privateKey = new BCECPrivateKey(ALGORITHM, privateKeySpec, BouncyCastleProvider.CONFIGURATION);
var publicKeySpec = new ECPublicKeySpec(CURVE_SPEC.getG().multiply(privateKeySpec.getD()), CURVE_SPEC);
var publicKey = new BCECPublicKey(ALGORITHM, publicKeySpec, BouncyCastleProvider.CONFIGURATION);
this.privateKey = privateKey;
this.publicKey = publicKey;
this.signer = new PrivateKeySigner(Algorithm.SECP256R1, privateKey);
}
public SECP256R1KeyPair(String hex) {
this(Utils.hexStringToByteArray(hex));
}
public static java.security.PublicKey decode(byte[] data) {
var params = ECNamedCurveTable.getParameterSpec(CURVE);
var spec = new ECPublicKeySpec(params.getCurve().decodePoint(data), params);
return new BCECPublicKey(ALGORITHM, spec, BouncyCastleProvider.CONFIGURATION);
}
public static Signature getSignature() throws NoSuchAlgorithmException {
return Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider());
}
@Override
public byte[] toBytes() {
return BigIntegers.asUnsignedByteArray(privateKey.getD());
}
@Override
public String toHex() {
return Utils.byteArrayToHexString(toBytes());
}
@Override
public java.security.PublicKey publicKey() {
return publicKey;
}
@Override
public byte[] sign(byte[] block, byte[] publicKey) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return signer.signStandard(block, Algorithm.SECP256R1, publicKey);
}
@Override
public byte[] signExternal(byte[] block, byte[] publicKey, byte[] external) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return signer.signExternal(block, Algorithm.SECP256R1, publicKey, external);
}
@Override
public byte[] signSealed(byte[] block, byte[] publicKey, byte[] seal) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return signer.signSealed(block, Algorithm.SECP256R1, publicKey, seal);
}
@Override
public PublicKey public_key() {
return new PublicKey(Algorithm.SECP256R1, publicKey);
}
}