Path: blob/master/test/jdk/sun/security/pkcs11/KeyGenerator/DESParity.java
41154 views
/*1* Copyright (c) 2003, 2018, 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*/2223/*24* @test25* @bug 489847926* @summary Verify that the parity bits are set correctly27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm DESParity32* @run main/othervm -Djava.security.manager=allow DESParity sm33*/3435import java.security.Provider;36import java.util.Random;37import javax.crypto.SecretKey;38import javax.crypto.SecretKeyFactory;39import javax.crypto.spec.DESKeySpec;40import javax.crypto.spec.DESedeKeySpec;41import javax.crypto.spec.SecretKeySpec;4243public class DESParity extends PKCS11Test {4445@Override46public void main(Provider p) throws Exception {47if (p.getService("SecretKeyFactory", "DES") == null) {48System.out.println("Not supported by provider, skipping");49return;50}51Random random = new Random();52SecretKeyFactory kf;53// DES54kf = SecretKeyFactory.getInstance("DES", p);55for (int i = 0; i < 10; i++ ) {56byte[] b = new byte[8];57random.nextBytes(b);58SecretKeySpec spec = new SecretKeySpec(b, "DES");59SecretKey key = kf.generateSecret(spec);60if (DESKeySpec.isParityAdjusted(key.getEncoded(), 0) == false) {61throw new Exception("DES key not parity adjusted");62}63}64// DESede65kf = SecretKeyFactory.getInstance("DESede", p);66for (int i = 0; i < 10; i++ ) {67byte[] b = new byte[24];68random.nextBytes(b);69SecretKeySpec spec = new SecretKeySpec(b, "DESede");70SecretKey key = kf.generateSecret(spec);71if (DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0) == false) {72throw new Exception("DESede key not parity adjusted");73}74}75}7677public static void main(String[] args) throws Exception {78main(new DESParity(), args);79}8081}828384