From bb6d78b5eb4fdf7d46599e7010a93c726c193aaa Mon Sep 17 00:00:00 2001 From: ascarpino Date: Tue, 7 May 2013 14:13:53 -0700 Subject: [PATCH] 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 --- .../sun/security/pkcs11/P11Cipher.java | 4 +-- .../pkcs11/Cipher/TestSymmCiphersNoPad.java | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/share/classes/sun/security/pkcs11/P11Cipher.java b/src/share/classes/sun/security/pkcs11/P11Cipher.java index 44121fb2c..6fe59f8da 100644 --- a/src/share/classes/sun/security/pkcs11/P11Cipher.java +++ b/src/share/classes/sun/security/pkcs11/P11Cipher.java @@ -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 @@ final class P11Cipher extends CipherSpi { } 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)); } diff --git a/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java b/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java index 5f94ea48f..a8b71a30a 100644 --- a/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java +++ b/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java @@ -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 @@ public class TestSymmCiphersNoPad extends PKCS11Test { 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 @@ public class TestSymmCiphersNoPad extends PKCS11Test { 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; } -- GitLab