Skip to content

Commit 00d40a5

Browse files
committed
added builder to KEMExtractSpec, minor JavaDoc in KEMGenerateSpec.
1 parent 6442ad8 commit 00d40a5

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

prov/src/main/java/org/bouncycastle/jcajce/spec/KEMExtractSpec.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.bouncycastle.jcajce.spec;
22

33
import java.security.PrivateKey;
4-
import java.security.PublicKey;
54
import java.security.spec.AlgorithmParameterSpec;
65

76
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
@@ -16,6 +15,87 @@ public class KEMExtractSpec
1615
private static final byte[] EMPTY_OTHER_INFO = new byte[0];
1716
private static AlgorithmIdentifier DefKdf = new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256));
1817

18+
/**
19+
* Builder class for creating a KEMExtractSpec.
20+
*/
21+
public static final class Builder
22+
{
23+
private final PrivateKey privateKey;
24+
private final byte[] encapsulation;
25+
private final String algorithmName;
26+
private final int keySizeInBits;
27+
28+
private AlgorithmIdentifier kdfAlgorithm;
29+
private byte[] otherInfo;
30+
31+
/**
32+
* Basic builder.
33+
*
34+
* @param privateKey the private key to use for the secret extraction.
35+
* @param encapsulation the encapsulation to process.
36+
* @param keyAlgorithmName the algorithm name for the secret key we want to generate.
37+
* @param keySizeInBits the size of the wrapping key we want to produce in bits.
38+
*/
39+
public Builder(PrivateKey privateKey, byte[] encapsulation, String keyAlgorithmName, int keySizeInBits)
40+
{
41+
this.privateKey = privateKey;
42+
this.encapsulation = Arrays.clone(encapsulation);
43+
this.algorithmName = keyAlgorithmName;
44+
this.keySizeInBits = keySizeInBits;
45+
this.kdfAlgorithm = new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256));
46+
this.otherInfo = EMPTY_OTHER_INFO;
47+
}
48+
49+
/**
50+
* Use the shared secret directly for key wrap generation.
51+
*
52+
* @return the current Builder instance.
53+
*/
54+
public Builder withNoKdf()
55+
{
56+
this.kdfAlgorithm = null;
57+
58+
return this;
59+
}
60+
61+
/**
62+
* Set the KDF algorithm and digest algorithm for wrap key generation. The default KDF is X9.44 KDF-3, also
63+
* known as the NIST concatenation KDF.
64+
*
65+
* @param kdfAlgorithm the KDF algorithm to apply.
66+
* @return the current Builder instance.
67+
*/
68+
public Builder withKdfAlgorithm(AlgorithmIdentifier kdfAlgorithm)
69+
{
70+
this.kdfAlgorithm = kdfAlgorithm;
71+
72+
return this;
73+
}
74+
75+
/**
76+
* Set the OtherInfo to use with the KDF. The default OtherInfo is a zero length byte[].
77+
*
78+
* @param otherInfo the other info to use.
79+
* @return the current Builder instance.
80+
*/
81+
public Builder withOtherInfo(byte[] otherInfo)
82+
{
83+
this.otherInfo = (otherInfo == null) ? EMPTY_OTHER_INFO : Arrays.clone(otherInfo);
84+
85+
return this;
86+
}
87+
88+
/**
89+
* Build the new parameter spec.
90+
*
91+
* @return a new parameter spec configured according to the builder state.
92+
*/
93+
public KEMExtractSpec build()
94+
{
95+
return new KEMExtractSpec(privateKey, encapsulation, algorithmName, keySizeInBits, kdfAlgorithm, otherInfo);
96+
}
97+
}
98+
1999
private final PrivateKey privateKey;
20100
private final byte[] encapsulation;
21101

prov/src/main/java/org/bouncycastle/jcajce/spec/KEMGenerateSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class KEMGenerateSpec
1616
private static AlgorithmIdentifier DefKdf = new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256));
1717

1818
/**
19-
* Builder class for creating a KTSParameterSpec.
19+
* Builder class for creating a KEMGenerateSpec.
2020
*/
2121
public static final class Builder
2222
{

0 commit comments

Comments
 (0)