8001284: Buffer problems with SunPKCS11-Solaris and CKM_AES_CTR
authorascarpino
Tue, 07 May 2013 14:13:53 -0700
changeset 17479 39579306a62f
parent 17478 2cd7d026396c
child 17480 e2177bd019a2
8001284: Buffer problems with SunPKCS11-Solaris and CKM_AES_CTR Summary: Changed output length calculation to include incomplete blocks for CTR mode. Reviewed-by: valeriep
jdk/src/share/classes/sun/security/pkcs11/P11Cipher.java
jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java
--- a/jdk/src/share/classes/sun/security/pkcs11/P11Cipher.java	Tue May 07 14:06:19 2013 -0700
+++ b/jdk/src/share/classes/sun/security/pkcs11/P11Cipher.java	Tue May 07 14:13:53 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -460,7 +460,7 @@
         }
 
         int result = inLen + bytesBuffered;
-        if (blockSize != 0) {
+        if (blockSize != 0 && blockMode != MODE_CTR) {
             // minus the number of bytes in the last incomplete block.
             result -= (result & (blockSize - 1));
         }
--- a/jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java	Tue May 07 14:06:19 2013 -0700
+++ b/jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java	Tue May 07 14:13:53 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 4898484 6604496
+ * @bug 4898484 6604496 8001284
  * @summary basic test for symmetric ciphers with no padding
  * @author Valerie Peng
  * @library ..
@@ -60,7 +60,8 @@
         new CI("DESede/CBC/NoPadding", "DESede", 160),
         new CI("AES/CBC/NoPadding", "AES", 4800),
         new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
-        new CI("AES/CTR/NoPadding", "AES", 1600)
+        new CI("AES/CTR/NoPadding", "AES", 1600),
+        new CI("AES/CTR/NoPadding", "AES", 65)
     };
 
     private static StringBuffer debugBuf;
@@ -182,6 +183,29 @@
         debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");
         match(outDirectBuf, answer);
 
+        // test#6: Streams
+        debugBuf.append("Test#6: Streaming\n");
+        outBuf.position(0);
+        InputStream stream =
+            new CipherInputStream(new ByteArrayInputStream(in), cipher);
+        byte[] data = new byte[1024];
+        int bytesRead = 0;
+        try {
+            while (bytesRead >= 0) {
+                bytesRead = stream.read(data);
+                if (bytesRead == -1)
+                    break;
+                debugBuf.append("bytesRead: " + bytesRead);
+                debugBuf.append("\toutBuf.position(): " + outBuf.position() +
+                    "\n");
+                outBuf.put(data, 0 , bytesRead);
+            }
+        } catch (Exception ex) {
+            debugBuf.append("Caught Exception during stream reading\n");
+            throw ex;
+        }
+        match(outBuf, answer);
+
         debugBuf = null;
     }