Path: blob/master/test/jdk/javax/crypto/KeyGenerator/TestKGParity.java
41149 views
/*1* Copyright (c) 2015, 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.io.PrintStream;24import java.lang.String;25import java.lang.System;26import java.security.Provider;27import java.security.SecureRandom;28import java.security.Security;29import javax.crypto.KeyGenerator;30import static java.lang.System.out;3132/*33* @test34* @bug 804860735* @compile ../../../com/sun/crypto/provider/Cipher/DES/TestUtility.java36* @run main TestKGParity37* @summary Test key generation of DES and DESEDE38* @key randomness39*/40public class TestKGParity {4142private static final String[] ALGORITHM_ARR = {43"deS", "DesEDE"44};4546public static void main(String argv[]) throws Exception {4748TestKGParity test = new TestKGParity();49test.run();50}5152private void run() throws Exception {53Provider[] providers = Security.getProviders();54for (Provider p : providers) {55String prvName = p.getName();56if (prvName.startsWith("SunJCE")57|| prvName.startsWith("SunPKCS11-")) {58for (String algorithm : ALGORITHM_ARR) {59if (!runTest(p, algorithm)) {60throw new RuntimeException(61"Test failed with provider/algorithm:"62+ p.getName() + "/" + algorithm);63} else {64out.println("Test passed with provider/algorithm:"65+ p.getName() + "/" + algorithm);66}67}68}69}70}7172public boolean runTest(Provider p, String algo) throws Exception {73byte[] keyValue = null;74try {75// Initialization76SecureRandom sRdm = new SecureRandom();77KeyGenerator kg = KeyGenerator.getInstance(algo, p);78kg.init(sRdm);7980// Generate a SecretKey and retrieve its value81keyValue = kg.generateKey().getEncoded();8283// Verify its parity in the unit of byte84for (int i = 0; i < keyValue.length; i++) {85if (!checkParity(keyValue[i])) {86out.println("Testing: "87+ p.getName()88+ "/"89+ algo90+ " failed when verify its parity in the unit of byte:"91+ TestUtility.hexDump(keyValue, i));92return false;93}94}95return true;96} catch (Exception ex) {97out.println("Testing: " + p.getName() + "/" + algo98+ " failed with unexpected exception");99ex.printStackTrace();100throw ex;101}102}103104private boolean checkParity(byte keyByte) {105boolean even = false;106byte[] PARITY_BIT_MASK = {107(byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,108(byte) 0x04, (byte) 0x02, (byte) 0x01109};110111for (int i = 0; i < 7; i++) {112if ((keyByte & PARITY_BIT_MASK[i]) > 0) {113even = !even;114}115}116if (keyByte < 0) {117even = !even;118}119120return even;121}122}123124125