Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/EncryptionPadding.java
41152 views
/*1* Copyright (c) 2021, Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 826135526* @library /test/lib ..27* @run main/othervm EncryptionPadding28*/2930import java.nio.ByteBuffer;31import java.security.Key;32import java.security.Provider;33import java.util.Arrays;34import javax.crypto.Cipher;35import javax.crypto.spec.SecretKeySpec;3637public class EncryptionPadding extends PKCS11Test {3839private static String transformation = "AES/ECB/PKCS5Padding";40private static Key key = new SecretKeySpec(new byte[16], "AES");4142public static void main(String[] args) throws Exception {43main(new EncryptionPadding(), args);44}4546@Override47public void main(Provider p) throws Exception {48testWithInputSize(p, 1);49testWithInputSize(p, 15);50testWithInputSize(p, 16);51testWithInputSize(p, 17);52System.out.println("TEST PASS - OK");53}5455private static void testWithInputSize(Provider p, int inputSize)56throws Exception {57testWithInputSize(p, inputSize, false);58testWithInputSize(p, inputSize, true);59}6061private static void testWithInputSize(Provider p, int inputSize,62boolean isByteBuffer) throws Exception {63byte[] plainText = new byte[inputSize];64Arrays.fill(plainText, (byte)(inputSize & 0xFF));65ByteBuffer cipherText =66ByteBuffer.allocate(((inputSize / 16 ) + 1) * 16);67byte[] tmp;6869Cipher sunPKCS11cipher = Cipher.getInstance(transformation, p);70sunPKCS11cipher.init(Cipher.ENCRYPT_MODE, key);71for (int i = 0; i < ((inputSize - 1) / 16) + 1; i++) {72int updateLength = Math.min(inputSize - (16 * i), 16);73if (!isByteBuffer) {74tmp = sunPKCS11cipher.update(plainText, i * 16,75updateLength);76if (tmp != null) {77cipherText.put(tmp);78}79} else {80ByteBuffer bb = ByteBuffer.allocate(updateLength);81bb.put(plainText, i * 16, updateLength);82bb.flip();83sunPKCS11cipher.update(bb, cipherText);84}85}86if (!isByteBuffer) {87tmp = sunPKCS11cipher.doFinal();88if (tmp != null) {89cipherText.put(tmp);90}91} else {92sunPKCS11cipher.doFinal(ByteBuffer.allocate(0), cipherText);93}9495Cipher sunJCECipher = Cipher.getInstance(transformation, "SunJCE");96sunJCECipher.init(Cipher.DECRYPT_MODE, key);97byte[] sunJCEPlain = sunJCECipher.doFinal(cipherText.array());9899if (!Arrays.equals(plainText, sunJCEPlain)) {100throw new Exception("Cross-provider cipher test failed.");101}102}103}104105106