11package org .bouncycastle .jcajce .spec ;
22
33import java .security .PrivateKey ;
4- import java .security .PublicKey ;
54import java .security .spec .AlgorithmParameterSpec ;
65
76import 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
0 commit comments