11package org .bouncycastle .crypto .test ;
22
3+ import org .bouncycastle .crypto .Digest ;
4+ import org .bouncycastle .crypto .EncodableService ;
35import org .bouncycastle .crypto .macs .KMAC ;
46import org .bouncycastle .crypto .params .KeyParameter ;
57import org .bouncycastle .util .Arrays ;
8+ import org .bouncycastle .util .Memoable ;
69import org .bouncycastle .util .Strings ;
710import org .bouncycastle .util .encoders .Hex ;
811import org .bouncycastle .util .test .SimpleTest ;
@@ -137,7 +140,15 @@ public void performTest()
137140 checkKMAC (256 , new KMAC (256 , null ), Hex .decode ("eeaabeef" ));
138141 checkKMAC (128 , new KMAC (128 , new byte [0 ]), Hex .decode ("eeaabeef" ));
139142 checkKMAC (128 , new KMAC (128 , null ), Hex .decode ("eeaabeef" ));
140- checkKMAC (256 , new KMAC (256 , null ), Hex .decode ("eeaabeef" ));
143+ checkKMAC (256 , new KMAC (256 , null ), Hex .decode ("eeaabeef" ));
144+
145+ byte [] resBuf = new byte [32 ];
146+ byte [] message = Hex .decode ("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F" );
147+ byte [] expected = Hex .decode ("059a2eb4961b482ff5bb6a0278d3ad2117b20aafb2f0df33e7748176648c8192" );
148+
149+ testClone (resBuf , message , expected , new KMAC (128 , new byte [0 ]), Hex .decode ("eeaabeef" ));
150+ testMemo (resBuf , message , expected , new KMAC (128 , new byte [0 ]), Hex .decode ("eeaabeef" ));
151+ testEncodedState (resBuf , message , expected , new KMAC (128 , new byte [0 ]), Hex .decode ("eeaabeef" ));
141152 }
142153
143154 private void doFinalTest ()
@@ -146,7 +157,7 @@ private void doFinalTest()
146157
147158 kmac .init (new KeyParameter (Hex .decode (
148159 "404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F" )));
149-
160+
150161 kmac .update (Hex .decode ("00010203" ), 0 , 4 );
151162
152163 byte [] res = new byte [32 ];
@@ -236,7 +247,7 @@ private void checkKMAC(int bitSize, KMAC kmac, byte[] msg)
236247
237248 ref .init (new KeyParameter (new byte [0 ]));
238249 kmac .init (new KeyParameter (new byte [0 ]));
239-
250+
240251 ref .update (msg , 0 , msg .length );
241252 kmac .update (msg , 0 , msg .length );
242253
@@ -249,6 +260,110 @@ private void checkKMAC(int bitSize, KMAC kmac, byte[] msg)
249260 isTrue (Arrays .areEqual (res1 , res2 ));
250261 }
251262
263+ private void testEncodedState (byte [] resBuf , byte [] input , byte [] expected , KMAC kmac , byte [] key )
264+ {
265+ kmac .init (new KeyParameter (key ));
266+
267+ // test state encoding;
268+ kmac .update (input , 0 , input .length / 2 );
269+
270+ // copy the Digest
271+ Digest copy1 = new KMAC (((EncodableService )kmac ).getEncodedState ());
272+ Digest copy2 = new KMAC (((EncodableService )copy1 ).getEncodedState ());
273+
274+ kmac .update (input , input .length / 2 , input .length - input .length / 2 );
275+
276+ kmac .doFinal (resBuf , 0 );
277+
278+ if (!areEqual (expected , resBuf ))
279+ {
280+ fail ("failing state vector test" , expected , new String (Hex .encode (resBuf )));
281+ }
282+
283+ copy1 .update (input , input .length / 2 , input .length - input .length / 2 );
284+ copy1 .doFinal (resBuf , 0 );
285+
286+ if (!areEqual (expected , resBuf ))
287+ {
288+ fail ("failing state copy1 vector test" , expected , new String (Hex .encode (resBuf )));
289+ }
290+
291+ copy2 .update (input , input .length / 2 , input .length - input .length / 2 );
292+ copy2 .doFinal (resBuf , 0 );
293+
294+ if (!areEqual (expected , resBuf ))
295+ {
296+ fail ("failing state copy2 vector test" , expected , new String (Hex .encode (resBuf )));
297+ }
298+ }
299+
300+ private void testMemo (byte [] resBuf , byte [] input , byte [] expected , KMAC kmac , byte [] key )
301+ {
302+ kmac .init (new KeyParameter (key ));
303+
304+ Memoable m = (Memoable )kmac ;
305+
306+ kmac .update (input , 0 , input .length / 2 );
307+
308+ // copy the Digest
309+ Memoable copy1 = m .copy ();
310+ Memoable copy2 = copy1 .copy ();
311+
312+ kmac .update (input , input .length / 2 , input .length - input .length / 2 );
313+ kmac .doFinal (resBuf , 0 );
314+
315+ if (!areEqual (expected , resBuf ))
316+ {
317+ fail ("failing memo vector test" , Hex .toHexString (expected ), Hex .toHexString (resBuf ));
318+ }
319+
320+ m .reset (copy1 );
321+
322+ kmac .update (input , input .length / 2 , input .length - input .length / 2 );
323+ kmac .doFinal (resBuf , 0 );
324+
325+ if (!areEqual (expected , resBuf ))
326+ {
327+ fail ("failing memo reset vector test" , Hex .toHexString (expected ), Hex .toHexString (resBuf ));
328+ }
329+
330+ KMAC md = (KMAC )copy2 ;
331+
332+ md .update (input , input .length / 2 , input .length - input .length / 2 );
333+ md .doFinal (resBuf , 0 );
334+
335+ if (!areEqual (expected , resBuf ))
336+ {
337+ fail ("failing memo copy vector test" , Hex .toHexString (expected ), Hex .toHexString (resBuf ));
338+ }
339+ }
340+
341+ private void testClone (byte [] resBuf , byte [] input , byte [] expected , KMAC kmac , byte [] key )
342+ {
343+ kmac .init (new KeyParameter (key ));
344+
345+ kmac .update (input , 0 , input .length / 2 );
346+
347+ // clone the Digest
348+ KMAC d = new KMAC (kmac );
349+
350+ kmac .update (input , input .length / 2 , input .length - input .length / 2 );
351+ kmac .doFinal (resBuf , 0 );
352+
353+ if (!areEqual (expected , resBuf ))
354+ {
355+ fail ("failing clone vector test" , Hex .toHexString (expected ), Hex .toHexString (resBuf ));
356+ }
357+
358+ d .update (input , input .length / 2 , input .length - input .length / 2 );
359+ d .doFinal (resBuf , 0 );
360+
361+ if (!areEqual (expected , resBuf ))
362+ {
363+ fail ("failing second clone vector test" , Hex .toHexString (expected ), Hex .toHexString (resBuf ));
364+ }
365+ }
366+
252367 public static void main (
253368 String [] args )
254369 {
0 commit comments