Path: blob/master/test/jdk/sun/security/pkcs11/KeyGenerator/TestChaCha20.java
41154 views
/*1* Copyright (c) 2021, 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 825541026* @modules jdk.crypto.cryptoki27* @summary Check ChaCha20 key generator.28* @library /test/lib ..29* @run main/othervm TestChaCha2030*/31import java.security.Provider;32import java.security.InvalidAlgorithmParameterException;33import java.security.InvalidParameterException;34import java.security.NoSuchAlgorithmException;35import java.util.HexFormat;3637import javax.crypto.KeyGenerator;38import javax.crypto.SecretKey;39import javax.crypto.spec.ChaCha20ParameterSpec;4041public class TestChaCha20 extends PKCS11Test {4243private static final String ALGO = "ChaCha20";4445public static void main(String[] args) throws Exception {46main(new TestChaCha20(), args);47}4849@Override50public void main(Provider p) throws Exception {51System.out.println("Testing " + p.getName());52KeyGenerator kg;53try {54kg = KeyGenerator.getInstance(ALGO, p);55} catch (NoSuchAlgorithmException nsae) {56System.out.println("Skip; no support for " + ALGO);57return;58}5960try {61kg.init(new ChaCha20ParameterSpec(new byte[12], 0));62throw new RuntimeException(63"ChaCha20 key generation should not need any paramSpec");64} catch (InvalidAlgorithmParameterException e) {65System.out.println("Expected IAPE: " + e.getMessage());66}6768for (int keySize : new int[] { 32, 64, 128, 256, 512, 1024 }) {69try {70kg.init(keySize);71if (keySize != 256) {72throw new RuntimeException(keySize + " is invalid keysize");73}74} catch (InvalidParameterException e) {75if (keySize == 256) {76throw new RuntimeException("IPE thrown for valid keySize");77} else {78System.out.println("Expected IPE thrown for " + keySize);79}80}81}8283//kg.init(256);84SecretKey key = kg.generateKey();85byte[] keyValue = key.getEncoded();86System.out.println("Key: " + HexFormat.of().formatHex(keyValue));87if (keyValue.length != 32) {88throw new RuntimeException("The size of generated key must be 256");89}90}91}929394