Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyGenerator/DESParity.java
38855 views
/*1* Copyright (c) 2003, 2016, 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 ..29* @key randomness30* @run main/othervm DESParity31* @run main/othervm DESParity sm32*/3334import java.security.Provider;35import java.util.Random;36import javax.crypto.SecretKey;37import javax.crypto.SecretKeyFactory;38import javax.crypto.spec.DESKeySpec;39import javax.crypto.spec.DESedeKeySpec;40import javax.crypto.spec.SecretKeySpec;4142public class DESParity extends PKCS11Test {4344@Override45public void main(Provider p) throws Exception {46if (p.getService("SecretKeyFactory", "DES") == null) {47System.out.println("Not supported by provider, skipping");48return;49}50Random random = new Random();51SecretKeyFactory kf;52// DES53kf = SecretKeyFactory.getInstance("DES", p);54for (int i = 0; i < 10; i++ ) {55byte[] b = new byte[8];56random.nextBytes(b);57SecretKeySpec spec = new SecretKeySpec(b, "DES");58SecretKey key = kf.generateSecret(spec);59if (DESKeySpec.isParityAdjusted(key.getEncoded(), 0) == false) {60throw new Exception("DES key not parity adjusted");61}62}63// DESede64kf = SecretKeyFactory.getInstance("DESede", p);65for (int i = 0; i < 10; i++ ) {66byte[] b = new byte[24];67random.nextBytes(b);68SecretKeySpec spec = new SecretKeySpec(b, "DESede");69SecretKey key = kf.generateSecret(spec);70if (DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0) == false) {71throw new Exception("DESede key not parity adjusted");72}73}74}7576public static void main(String[] args) throws Exception {77main(new DESParity(), args);78}7980}818283