2727import java .security .spec .InvalidParameterSpecException ;
2828import javax .crypto .spec .IvParameterSpec ;
2929
30+ import com .wolfssl .wolfcrypt .Aes ;
31+
3032/**
3133 * wolfCrypt JCE AlgorithmParametersSpi implementation for AES parameters
3234 */
@@ -46,6 +48,12 @@ public WolfCryptAesParameters() {
4648 protected void engineInit (AlgorithmParameterSpec paramSpec )
4749 throws InvalidParameterSpecException {
4850
51+ /* Prevent double initialization */
52+ if (this .ivSpec != null ) {
53+ throw new InvalidParameterSpecException (
54+ "AlgorithmParameters already initialized" );
55+ }
56+
4957 if (!(paramSpec instanceof IvParameterSpec )) {
5058 throw new InvalidParameterSpecException (
5159 "Only IvParameterSpec supported" );
@@ -60,7 +68,7 @@ protected void engineInit(AlgorithmParameterSpec paramSpec)
6068 }
6169
6270 /* AES block size is 16 bytes, IV should match */
63- if (spec .getIV ().length != 16 ) {
71+ if (spec .getIV ().length != Aes . BLOCK_SIZE ) {
6472 throw new InvalidParameterSpecException (
6573 "AES IV must be 16 bytes, got: " + spec .getIV ().length );
6674 }
@@ -73,14 +81,58 @@ protected void engineInit(AlgorithmParameterSpec paramSpec)
7381 protected void engineInit (byte [] params )
7482 throws IOException {
7583
76- throw new IOException ("Encoded AES parameters not supported" );
84+ /* Prevent double initialization */
85+ if (this .ivSpec != null ) {
86+ throw new IOException (
87+ "AlgorithmParameters already initialized" );
88+ }
89+
90+ if (params == null ) {
91+ throw new NullPointerException ("params must not be null" );
92+ }
93+
94+ if (params .length == 0 ) {
95+ throw new IOException ("AES parameters cannot be empty" );
96+ }
97+
98+ /* AES IV parameters are encoded as ASN.1 OCTET STRING:
99+ * tag (0x04) + length + IV bytes
100+ * Expected: 04 10 [16 IV bytes] = 18 bytes */
101+ if (params .length != Aes .BLOCK_SIZE + 2 ) {
102+ throw new IOException (
103+ "Invalid AES parameter encoding length: " + params .length );
104+ }
105+
106+ /* Verify OCTET STRING tag */
107+ if (params [0 ] != 0x04 ) {
108+ throw new IOException (
109+ "DER input not an octet string" );
110+ }
111+
112+ /* Verify length is 16 (0x10) */
113+ if (params [1 ] != 0x10 ) {
114+ throw new IOException (
115+ "Invalid AES IV length in encoding: " + params [1 ]);
116+ }
117+
118+ /* Extract IV bytes (skip tag and length) */
119+ byte [] iv = new byte [Aes .BLOCK_SIZE ];
120+ System .arraycopy (params , 2 , iv , 0 , Aes .BLOCK_SIZE );
121+
122+ this .ivSpec = new IvParameterSpec (iv );
77123 }
78124
79125 @ Override
80126 protected void engineInit (byte [] params , String format )
81127 throws IOException {
82128
83- throw new IOException ("Encoded AES parameters not supported" );
129+ if (format != null && !format .equalsIgnoreCase ("ASN.1" ) &&
130+ !format .equalsIgnoreCase ("DER" )) {
131+ throw new IOException ("Unsupported format: " + format +
132+ ", only ASN.1 and DER supported" );
133+ }
134+
135+ engineInit (params );
84136 }
85137
86138 @ Override
@@ -110,12 +162,38 @@ protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(
110162
111163 @ Override
112164 protected byte [] engineGetEncoded () throws IOException {
113- throw new IOException ("Encoded AES parameters not supported" );
165+
166+ byte [] iv ;
167+ byte [] encoded ;
168+
169+ if (this .ivSpec == null ) {
170+ throw new IOException ("AES parameters not initialized" );
171+ }
172+
173+ iv = this .ivSpec .getIV ();
174+ if (iv == null || iv .length != Aes .BLOCK_SIZE ) {
175+ throw new IOException ("Invalid AES IV for encoding" );
176+ }
177+
178+ /* Encode as OCTET STRING: tag (0x04) + len (0x10) + IV */
179+ encoded = new byte [18 ];
180+ encoded [0 ] = 0x04 ; /* OCTET STRING */
181+ encoded [1 ] = 0x10 ; /* length = 16 */
182+ System .arraycopy (iv , 0 , encoded , 2 , Aes .BLOCK_SIZE );
183+
184+ return encoded ;
114185 }
115186
116187 @ Override
117188 protected byte [] engineGetEncoded (String format ) throws IOException {
118- throw new IOException ("Encoded AES parameters not supported" );
189+
190+ if (format != null && !format .equalsIgnoreCase ("ASN.1" ) &&
191+ !format .equalsIgnoreCase ("DER" )) {
192+ throw new IOException ("Unsupported format: " + format +
193+ ", only ASN.1 and DER supported" );
194+ }
195+
196+ return engineGetEncoded ();
119197 }
120198
121199 @ Override
0 commit comments