11/*
2- * Copyright (c) 2018, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424/**
2525 * @test
26- * @bug 8153029
26+ * @bug 8153029 8305091
2727 * @library /test/lib
2828 * @run main ChaCha20NoReuse
2929 * @summary ChaCha20 Cipher Implementation (key/nonce reuse protection)
@@ -376,26 +376,20 @@ public boolean run(String algorithm) {
376376 }
377377 SecretKey key = new SecretKeySpec (testData .key , ALG_CC20 );
378378
379- // Initialize and encrypt
379+ // Initialize and decrypt
380380 cipher .init (testData .direction , key , spec );
381381 if (algorithm .equals (ALG_CC20_P1305 )) {
382382 cipher .updateAAD (testData .aad );
383383 }
384384 cipher .doFinal (testData .input );
385385 System .out .println ("First decryption complete" );
386386
387- // Now attempt to encrypt again without changing the key/IV
388- // This should fail.
389- try {
390- if (algorithm .equals (ALG_CC20_P1305 )) {
391- cipher .updateAAD (testData .aad );
392- }
393- cipher .doFinal (testData .input );
394- throw new RuntimeException (
395- "Expected IllegalStateException not thrown" );
396- } catch (IllegalStateException ise ) {
397- // Do nothing, this is what we expected to happen
387+ // Now attempt to decrypt again without changing the key/IV
388+ // We allow this scenario.
389+ if (algorithm .equals (ALG_CC20_P1305 )) {
390+ cipher .updateAAD (testData .aad );
398391 }
392+ cipher .doFinal (testData .input );
399393 } catch (Exception exc ) {
400394 System .out .println ("Unexpected exception: " + exc );
401395 exc .printStackTrace ();
@@ -408,7 +402,8 @@ public boolean run(String algorithm) {
408402
409403 /**
410404 * Perform an AEAD decryption with corrupted data so the tag does not
411- * match. Then attempt to reuse the cipher without initialization.
405+ * match. Then use the uncorrupted test vector input and attempt to
406+ * reuse the cipher without initialization.
412407 */
413408 public static final TestMethod decFailNoInit = new TestMethod () {
414409 @ Override
@@ -441,16 +436,16 @@ public boolean run(String algorithm) {
441436 System .out .println ("Expected decryption failure occurred" );
442437 }
443438
444- // Make sure that despite the exception, the Cipher object is
445- // not in a state that would leave it initialized and able
446- // to process future decryption operations without init.
447- try {
448- cipher . updateAAD ( testData . aad );
449- cipher .doFinal (testData .input );
450- throw new RuntimeException (
451- "Expected IllegalStateException not thrown" );
452- } catch ( IllegalStateException ise ) {
453- // Do nothing, this is what we expected to happen
439+ // Even though an exception occurred during decryption, the
440+ // Cipher object should be returned to its post-init state.
441+ // Since this is a decryption operation, we should allow
442+ // key/nonce reuse. It should properly decrypt the uncorrupted
443+ // input.
444+ cipher .updateAAD (testData .aad );
445+ byte [] pText = cipher . doFinal ( testData . input );
446+ if (! Arrays . equals ( pText , testData . expOutput )) {
447+ throw new RuntimeException ( "FAIL: Attempted decryption " +
448+ "did not match expected plaintext" );
454449 }
455450 } catch (Exception exc ) {
456451 System .out .println ("Unexpected exception: " + exc );
@@ -562,18 +557,17 @@ public boolean run(String algorithm) {
562557 if (algorithm .equals (ALG_CC20_P1305 )) {
563558 cipher .updateAAD (testData .aad );
564559 }
565- cipher .doFinal (testData .input );
560+ byte [] pText = cipher .doFinal (testData .input );
561+ if (!Arrays .equals (pText , testData .expOutput )) {
562+ throw new RuntimeException ("FAIL: Attempted decryption " +
563+ "did not match expected plaintext" );
564+ }
566565 System .out .println ("First decryption complete" );
567566
568567 // Initializing after the completed decryption with
569- // the same key and nonce should fail.
570- try {
571- cipher .init (testData .direction , key , spec );
572- throw new RuntimeException (
573- "Expected InvalidKeyException not thrown" );
574- } catch (InvalidKeyException ike ) {
575- // Do nothing, this is what we expected to happen
576- }
568+ // the same key and nonce is allowed.
569+ cipher .init (testData .direction , key , spec );
570+ System .out .println ("Successful reinit in DECRYPT_MODE" );
577571 } catch (Exception exc ) {
578572 System .out .println ("Unexpected exception: " + exc );
579573 exc .printStackTrace ();
0 commit comments