1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/**
25
25
* @test
26
- * @bug 8153029
26
+ * @bug 8153029 8305091
27
27
* @library /test/lib
28
28
* @run main ChaCha20NoReuse
29
29
* @summary ChaCha20 Cipher Implementation (key/nonce reuse protection)
@@ -376,26 +376,20 @@ public boolean run(String algorithm) {
376
376
}
377
377
SecretKey key = new SecretKeySpec (testData .key , ALG_CC20 );
378
378
379
- // Initialize and encrypt
379
+ // Initialize and decrypt
380
380
cipher .init (testData .direction , key , spec );
381
381
if (algorithm .equals (ALG_CC20_P1305 )) {
382
382
cipher .updateAAD (testData .aad );
383
383
}
384
384
cipher .doFinal (testData .input );
385
385
System .out .println ("First decryption complete" );
386
386
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 );
398
391
}
392
+ cipher .doFinal (testData .input );
399
393
} catch (Exception exc ) {
400
394
System .out .println ("Unexpected exception: " + exc );
401
395
exc .printStackTrace ();
@@ -408,7 +402,8 @@ public boolean run(String algorithm) {
408
402
409
403
/**
410
404
* 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.
412
407
*/
413
408
public static final TestMethod decFailNoInit = new TestMethod () {
414
409
@ Override
@@ -441,16 +436,16 @@ public boolean run(String algorithm) {
441
436
System .out .println ("Expected decryption failure occurred" );
442
437
}
443
438
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" );
454
449
}
455
450
} catch (Exception exc ) {
456
451
System .out .println ("Unexpected exception: " + exc );
@@ -562,18 +557,17 @@ public boolean run(String algorithm) {
562
557
if (algorithm .equals (ALG_CC20_P1305 )) {
563
558
cipher .updateAAD (testData .aad );
564
559
}
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
+ }
566
565
System .out .println ("First decryption complete" );
567
566
568
567
// 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" );
577
571
} catch (Exception exc ) {
578
572
System .out .println ("Unexpected exception: " + exc );
579
573
exc .printStackTrace ();
0 commit comments