Skip to content

Commit 3e09351

Browse files
gefeilidghgit
authored andcommitted
Add java doc and more tests with different digests
1 parent 3d3ca2d commit 3e09351

File tree

5 files changed

+224
-17
lines changed

5 files changed

+224
-17
lines changed

core/src/main/java/org/bouncycastle/crypto/params/ECCSIKeyGenerationParameters.java

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,68 @@
99
import org.bouncycastle.math.ec.ECPoint;
1010
import org.bouncycastle.util.Arrays;
1111

12+
/**
13+
* Parameters for ECCSI key generation.
14+
*
15+
* <p>
16+
* This class encapsulates the parameters required for ECCSI (Elliptic Curve
17+
* Certificateless Signatures for Identity-based encryption) key generation.
18+
* It holds the elliptic curve domain parameters and computes the key pair
19+
* components used in ECCSI.
20+
* </p>
21+
*
22+
* <p>
23+
* The secret component {@code ksak} is generated randomly and reduced modulo
24+
* {@code q}, while {@code kpak} is derived from {@code ksak} by multiplying the
25+
* generator point.
26+
* </p>
27+
*/
1228
public class ECCSIKeyGenerationParameters
1329
extends KeyGenerationParameters
1430
{
31+
/**
32+
* The order of the elliptic curve.
33+
*/
1534
private final BigInteger q;
35+
36+
/**
37+
* The generator (base point) of the elliptic curve.
38+
*/
1639
private final ECPoint G;
40+
41+
/**
42+
* The digest algorithm used in key generation.
43+
*/
1744
private final Digest digest;
45+
46+
/**
47+
* The identifier (e.g. user identity) used in key generation.
48+
*/
1849
private final byte[] id;
50+
51+
/**
52+
* The secret key component (ksak) used in ECCSI, generated randomly.
53+
*/
1954
private final BigInteger ksak;
55+
56+
/**
57+
* The public key component (kpak), computed as G * ksak.
58+
*/
2059
private final ECPoint kpak;
60+
61+
/**
62+
* The bit length used for key generation (typically the bit length of the curve's parameter A).
63+
*/
2164
private final int n;
2265

2366
/**
24-
* initialise the generator with a source of randomness
25-
* and a strength (in bits).
67+
* Constructs an instance of {@code ECCSIKeyGenerationParameters} with the specified
68+
* source of randomness, elliptic curve parameters, digest algorithm, and identifier.
2669
*
27-
* @param random the random byte source.
70+
* @param random the source of randomness.
71+
* @param params the elliptic curve parameters (in X9.62 format) providing the curve, order, and generator.
72+
* @param digest the digest algorithm to be used.
73+
* @param id the identifier associated with the key generation (e.g. a user or device ID).
2874
*/
2975
public ECCSIKeyGenerationParameters(SecureRandom random, X9ECParameters params, Digest digest, byte[] id)
3076
{
@@ -38,36 +84,73 @@ public ECCSIKeyGenerationParameters(SecureRandom random, X9ECParameters params,
3884
this.kpak = G.multiply(ksak).normalize();
3985
}
4086

87+
/**
88+
* Returns a copy of the identifier used in these parameters.
89+
*
90+
* @return a byte array containing the identifier.
91+
*/
4192
public byte[] getId()
4293
{
43-
return id;
94+
return Arrays.clone(id);
4495
}
4596

97+
/**
98+
* Returns the public key component (kpak) corresponding to the secret key.
99+
*
100+
* @return the public key point.
101+
*/
46102
public ECPoint getKPAK()
47103
{
48104
return kpak;
49105
}
50106

107+
/**
108+
* Computes the session secret key (SSK) by adding the provided value to the secret key component
109+
* and reducing modulo the curve order.
110+
*
111+
* @param hs_v a BigInteger value (typically derived from a hash) to be added to the secret.
112+
* @return the computed session secret key.
113+
*/
51114
public BigInteger computeSSK(BigInteger hs_v)
52115
{
53116
return ksak.add(hs_v).mod(q);
54117
}
55118

119+
/**
120+
* Returns the order of the elliptic curve.
121+
*
122+
* @return the curve order.
123+
*/
56124
public BigInteger getQ()
57125
{
58126
return q;
59127
}
60128

129+
/**
130+
* Returns the generator (base point) of the elliptic curve.
131+
*
132+
* @return the generator point.
133+
*/
61134
public ECPoint getG()
62135
{
63136
return G;
64137
}
65138

139+
/**
140+
* Returns the digest algorithm used for key generation.
141+
*
142+
* @return the digest.
143+
*/
66144
public Digest getDigest()
67145
{
68146
return digest;
69147
}
70148

149+
/**
150+
* Returns the bit length used in key generation.
151+
*
152+
* @return the bit length.
153+
*/
71154
public int getN()
72155
{
73156
return n;

core/src/main/java/org/bouncycastle/crypto/params/ECCSIPrivateKeyParameters.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,69 @@
22

33
import java.math.BigInteger;
44

5+
/**
6+
* Represents the private key parameters for the Elliptic Curve-based Certificateless
7+
* Signature Infrastructure (ECCSI) scheme as defined in RFC 6507.
8+
*
9+
* <p>
10+
* This class encapsulates the secret signing key (SSK) used in ECCSI. The SSK is generated by
11+
* the Key Management Service (KMS) and is a random integer modulo the order of the elliptic curve.
12+
* It is paired with the corresponding public key parameters, represented by an instance of
13+
* {@link ECCSIPublicKeyParameters}, to form the complete key material required for generating
14+
* and verifying ECCSI signatures without the use of traditional certificates.
15+
* </p>
16+
*
17+
* <p>
18+
* Per RFC 6507 Section 5.1:
19+
* <ul>
20+
* <li>The SSK is generated as a random value in the appropriate range.</li>
21+
* <li>It is used in conjunction with the public validation token (PVT) to perform signature
22+
* operations.</li>
23+
* <li>The combination of the SSK and the public key parameters enables certificateless
24+
* signature generation and verification.</li>
25+
* </ul>
26+
* </p>
27+
*
28+
* @see ECCSIPublicKeyParameters
29+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6507">
30+
* RFC 6507: Elliptic Curve-Based Certificateless Signatures for Identity-Based Encryption (ECCSI)
31+
* </a>
32+
*/
533
public class ECCSIPrivateKeyParameters
634
extends AsymmetricKeyParameter
735
{
836
private final BigInteger ssk;
937
private final ECCSIPublicKeyParameters pub;
1038

39+
/**
40+
* Constructs {@code ECCSIPrivateKeyParameters} with the specified secret signing key
41+
* and associated public key parameters.
42+
*
43+
* @param ssk the secret signing key (SSK) as a BigInteger.
44+
* @param pub the corresponding public key parameters, which encapsulate the public validation token.
45+
*/
1146
public ECCSIPrivateKeyParameters(BigInteger ssk, ECCSIPublicKeyParameters pub)
1247
{
1348
super(true);
1449
this.ssk = ssk;
1550
this.pub = pub;
1651
}
1752

53+
/**
54+
* Returns the public key parameters associated with this private key.
55+
*
56+
* @return the {@link ECCSIPublicKeyParameters} containing the public validation token (PVT).
57+
*/
1858
public ECCSIPublicKeyParameters getPublicKeyParameters()
1959
{
2060
return pub;
2161
}
2262

63+
/**
64+
* Returns the secret signing key (SSK) used in ECCSI.
65+
*
66+
* @return the SSK as a BigInteger.
67+
*/
2368
public BigInteger getSSK()
2469
{
2570
return ssk;

core/src/main/java/org/bouncycastle/crypto/params/ECCSIPublicKeyParameters.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,47 @@
22

33
import org.bouncycastle.math.ec.ECPoint;
44

5+
/**
6+
* Represents the public key parameters for the Elliptic Curve-based Certificateless
7+
* Signature Infrastructure (ECCSI) scheme as defined in RFC 6507.
8+
* <p>
9+
* This class encapsulates the Public Validation Token (PVT) required for verifying
10+
* ECCSI signatures. The PVT is cryptographically bound to a user's identity and
11+
* generated by the Key Management Service (KMS) as part of the key material.
12+
*
13+
* <p>Per RFC 6507 Section 5.1:
14+
* <ul>
15+
* <li>The PVT is derived from the user's identity and KMS secret material</li>
16+
* <li>Used during signature verification to validate the signer's identity</li>
17+
* <li>Does not require certificates for authentication</li>
18+
* </ul>
19+
*
20+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6507">RFC 6507: Elliptic Curve-Based Certificateless
21+
* * Signatures for Identity-Based Encryption (ECCSI)</a>
22+
*/
23+
524
public class ECCSIPublicKeyParameters
625
extends AsymmetricKeyParameter
726
{
827
private final ECPoint pvt;
928

29+
/**
30+
* Constructs {@code ECCSIPublicKeyParameters} with the provided Public Validation Token (PVT).
31+
*/
1032
public ECCSIPublicKeyParameters(ECPoint pvt)
1133
{
1234
super(false);
1335
this.pvt = pvt;
1436
}
1537

38+
/**
39+
* Returns the Public Validation Token (PVT) for signature verification.
40+
* <p>
41+
* The PVT is used in conjunction with the KMS Public Authentication Key (KPAK)
42+
* to verify signatures per RFC 6507 Section 5.2.2.
43+
*
44+
* @return The PVT as an elliptic curve point in uncompressed format
45+
*/
1646
public final ECPoint getPVT()
1747
{
1848
return pvt;

core/src/main/java/org/bouncycastle/crypto/signers/ECCSISigner.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
/**
2121
* Implementation of Elliptic Curve-based Certificateless Signatures for Identity-Based Encryption (ECCSI)
2222
* as defined in RFC 6507.
23-
* <p>
24-
* This class handles both signature generation and verification using the ECCSI scheme. It supports:
25-
* <ul>
26-
* <li>NIST P-256 (secp256r1) elliptic curve parameters</li>
27-
* <li>SHA-256 hash function</li>
28-
* <li>Certificateless signatures using KMS Public Authentication Key (KPAK)</li>
29-
* <li>Identity-based signatures with Secret Signing Key (SSK)</li>
30-
* </ul>
3123
*
3224
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6507">RFC 6507: Elliptic Curve-Based Certificateless
3325
* Signatures for Identity-Based Encryption (ECCSI)</a>
@@ -48,7 +40,12 @@ public class ECCSISigner
4840
private boolean forSigning;
4941
private final int N;
5042

51-
43+
/**
44+
* Constructs an ECCSI signer/verifier with KMS Public Authentication Key and user identity.
45+
*
46+
* @param kpak KMS Public Authentication Key (KPAK) from RFC 6507 Section 2
47+
* @param id User identity byte array formatted
48+
*/
5249
public ECCSISigner(ECPoint kpak, X9ECParameters params, Digest digest, byte[] id)
5350
{
5451
this.kpak = kpak;
@@ -60,6 +57,15 @@ public ECCSISigner(ECPoint kpak, X9ECParameters params, Digest digest, byte[] id
6057
this.N = (params.getCurve().getOrder().bitLength() + 7) >> 3;
6158
}
6259

60+
/**
61+
* Initializes the signer for either signature generation or verification.
62+
*
63+
* @param forSigning true for signing, false for verification
64+
* @param param Key parameters:
65+
* - For signing: {@code ParametersWithRandom} containing {@code ECCSIPrivateKeyParameters}
66+
* - For verification: {@code ECCSIPublicKeyParameters}
67+
* @throws IllegalArgumentException if invalid parameters are provided
68+
*/
6369
@Override
6470
public void init(boolean forSigning, CipherParameters param)
6571
{
@@ -94,6 +100,17 @@ public void update(byte[] in, int off, int len)
94100
}
95101
}
96102

103+
/**
104+
* Generates an ECCSI signature according to RFC 6507 Section 5.2.1.
105+
*
106+
* @return Signature structure containing:
107+
* - r (N bytes)
108+
* - s (N bytes)
109+
* - PVT (Public Validation Token)
110+
* @throws CryptoException if cryptographic operations fail
111+
* @throws DataLengthException if input data is invalid
112+
* @throws IllegalArgumentException if invalid SSK or j parameter is detected
113+
*/
97114
@Override
98115
public byte[] generateSignature()
99116
throws CryptoException, DataLengthException
@@ -116,6 +133,13 @@ public byte[] generateSignature()
116133
params.getPublicKeyParameters().getPVT().getEncoded(false));
117134
}
118135

136+
/**
137+
* Verifies an ECCSI signature according to RFC 6507 Section 5.2.2.
138+
*
139+
* @param signature Signature to verify (r || s || PVT)
140+
* @return true if signature is valid, false otherwise
141+
* @throws IllegalArgumentException if signature format is invalid
142+
*/
119143
@Override
120144
public boolean verifySignature(byte[] signature)
121145
{
@@ -141,6 +165,13 @@ public boolean verifySignature(byte[] signature)
141165
return rComputed.mod(q).equals(r.mod(q));
142166
}
143167

168+
/**
169+
* Resets the signer/verifier state and performs initial computations:
170+
* - For signing: Validates KPAK consistency (RFC 6507 Section 5.1.2)
171+
* - For verification: Computes Y = HS·PVT + KPAK
172+
*
173+
* Also computes HS = hash(G || KPAK || ID || PVT) as per RFC 6507 Section 5.1.1
174+
*/
144175
@Override
145176
public void reset()
146177
{

0 commit comments

Comments
 (0)