Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBMacBuffer.java
41161 views
/*1* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.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*/2223import java.nio.ByteBuffer;24import java.security.InvalidKeyException;25import java.security.NoSuchAlgorithmException;26import java.security.SecureRandom;27import java.security.spec.InvalidKeySpecException;28import java.util.Random;29import javax.crypto.Mac;30import javax.crypto.SecretKey;31import javax.crypto.SecretKeyFactory;32import javax.crypto.spec.PBEKeySpec;3334/**35* @test36* @bug 804178737* @summary verify that Mac.update works with different size ByteBuffer38* @author Alexander Fomin39* @run main PBMacBuffer40*/41public class PBMacBuffer {4243private final int LARGE_SIZE = 500000;4445public static void main(String[] args) {46String[] PBMAC1Algorithms = {47"HmacPBESHA1",48"PBEWithHmacSHA1",49"PBEWithHmacSHA224",50"PBEWithHmacSHA256",51"PBEWithHmacSHA384",52"PBEWithHmacSHA512"53};5455String[] PBKDF2Algorithms = {56"PBKDF2WithHmacSHA1",57"PBKDF2WithHmacSHA224",58"PBKDF2WithHmacSHA256",59"PBKDF2WithHmacSHA384",60"PBKDF2WithHmacSHA512"61};6263PBMacBuffer testRunner = new PBMacBuffer();64boolean failed = false;6566for (String thePBMacAlgo : PBMAC1Algorithms) {6768for (String thePBKDF2Algo : PBKDF2Algorithms) {6970System.out.println("Running test with " + thePBMacAlgo71+ " and " + thePBKDF2Algo + ":");72try {73if (!testRunner.doTest(thePBMacAlgo, thePBKDF2Algo)) {74failed = true;75}76} catch (NoSuchAlgorithmException | InvalidKeyException |77InvalidKeySpecException e) {78failed = true;79e.printStackTrace(System.out);80System.out.println("Test FAILED.");81}82}83}8485if (failed) {86throw new RuntimeException("One or more tests failed....");87}88}8990/**91* Tests Mac.update(ByteBuffer input) method. Three test cases are92* performed: - large ByteBuffer test case to test if the update() method93* process a large ByteBuffer correctly; - empty ByteBuffer test case to94* test if the update() method process an empty ByteBuffer correctly; - NULL95* ByteBuffer test case to test if the update() method throws expected96* IllegalArgumentException exception.97*98* @param theMacAlgo PBMAC algorithm to test99* @param thePBKDF2Algo PBKDF2 algorithm to test100* @return true - test passed; false - otherwise.101* @throws NoSuchAlgorithmException102* @throws InvalidKeyException103* @throws InvalidKeySpecException104* @see javax.crypto.Mac105*/106protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)107throws NoSuchAlgorithmException, InvalidKeyException,108InvalidKeySpecException {109// obtain a SecretKey using PBKDF2110SecretKey key = getSecretKey(thePBKDF2Algo);111112// Instantiate Mac object and init it with a SecretKey113Mac theMac = Mac.getInstance(theMacAlgo);114theMac.init(key);115116// Do large ByteBuffer test case117if (!largeByteBufferTest(theMac)) {118System.out.println("Large ByteBuffer test case failed.");119return false;120}121122// Do empty ByteBuffer test case123if (!emptyByteBufferTest(theMac)) {124System.out.println("Empty ByteBuffer test case failed.");125return false;126}127128// Do null ByteBuffer test case129if (!nullByteBufferTest(theMac)) {130System.out.println("NULL ByteBuffer test case failed.");131return false;132}133134return true;135}136137/**138* Large ByteBuffer test case. Generate random ByteBuffer of LARGE_SIZE139* size. Performs MAC operation with the given Mac object (theMac140* parameter).Verifies the assertion "Upon return, the buffer's position141* will be equal to its limit; its limit will not have changed".142*143* @param theMac MAC object to test.144* @return true - test case passed; false - otherwise;145*/146protected boolean largeByteBufferTest(Mac theMac) {147ByteBuffer buf = generateRandomByteBuffer(LARGE_SIZE);148int limitBefore = buf.limit();149150theMac.update(buf);151theMac.doFinal();152153int limitAfter = buf.limit();154int positonAfter = buf.position();155156if (limitAfter != limitBefore) {157System.out.println("FAIL: Buffer's limit has been chenged.");158return false;159}160161if (positonAfter != limitAfter) {162System.out.println("FAIL: "163+ "Buffer's position isn't equal to its limit");164return false;165}166167return true;168}169170/**171* Empty ByteBuffer test case. Generates an empty ByteBuffer. Perform MAC172* operation. No exceptions are expected.173*174* @param theMac175* @return true - test case pass; exception otherwise176*/177protected boolean emptyByteBufferTest(Mac theMac) {178ByteBuffer buf = generateRandomByteBuffer(0);179theMac.update(buf);180theMac.doFinal();181return true;182}183184/**185* NULL ByteBuffer test case. Pass NULL ByteBuffer to Mac.update(ByteBuffer186* buffer) method. An IllegalArgumentException expected.187*188* @param theMac Mac object to test.189* @return true - test case pass; false - otherwise.190*/191protected boolean nullByteBufferTest(Mac theMac) {192try {193ByteBuffer buf = null;194theMac.update(buf);195theMac.doFinal();196} catch (IllegalArgumentException e) {197// expected exception has been thrown198return true;199}200201System.out.println("FAIL: "202+ "IllegalArgumentException hasn't been thrown as expected");203204return false;205}206207/**208* Get SecretKey for the given PBKDF2 algorithm.209*210* @param thePBKDF2Algorithm - PBKDF2 algorithm211* @return SecretKey according to thePBKDF2Algorithm212* @throws NoSuchAlgorithmException213* @throws InvalidKeySpecException214*/215protected SecretKey getSecretKey(String thePBKDF2Algorithm)216throws NoSuchAlgorithmException, InvalidKeySpecException {217// Prepare salt218byte[] salt = new byte[64]; // PKCS #5 v2.1 recommendation219new SecureRandom().nextBytes(salt);220221// Generate secret key222PBEKeySpec pbeKeySpec = new PBEKeySpec(223"A #pwd# implied to be hidden!".toCharArray(),224salt, 1000, 128);225SecretKeyFactory keyFactory226= SecretKeyFactory.getInstance(thePBKDF2Algorithm);227return keyFactory.generateSecret(pbeKeySpec);228}229230/**231* An utility method to generate a random ByteBuffer of the requested size.232*233* @param size size of the ByteBuffer.234* @return ByteBuffer populated random data;235*/236private ByteBuffer generateRandomByteBuffer(int size) {237// generate randome byte array238byte[] data = new byte[size];239new Random().nextBytes(data);240241// create ByteBuffer242ByteBuffer bb = ByteBuffer.wrap(data);243244return bb;245}246247}248249250