Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/pss/TestSigGenPSS.java
38853 views
/*1* Copyright (c) 2018, 2020, 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.BufferedReader;24import java.io.File;25import java.io.FileInputStream;26import java.io.IOException;27import java.io.InputStreamReader;28import java.security.*;29import java.security.spec.*;30import java.security.interfaces.*;31import java.util.ArrayList;32import java.util.List;3334/*35* @test36* @bug 814629337* @summary Known Answer Tests based on NIST 186-3 at:38* @compile SigRecord.java39* @run main/othervm TestSigGenPSS40*/41public class TestSigGenPSS {4243private static final String[] testFiles = {44"SigGenPSS_186-3.txt", "SigGenPSS_186-3_TruncatedSHAs.txt"45};4647static final class MyKnownRandomSrc extends SecureRandom {48final byte[] srcBytes;49int numBytes;5051MyKnownRandomSrc(String srcString) {52this.srcBytes = SigRecord.toByteArray(srcString);53this.numBytes = this.srcBytes.length;54}55@Override56public void nextBytes(byte[] bytes) {57if (bytes.length > numBytes) {58throw new RuntimeException("Not enough bytes, need "59+ bytes.length + ", got " + numBytes);60}61System.arraycopy(this.srcBytes, this.srcBytes.length - numBytes, bytes, 0, bytes.length);62numBytes -= bytes.length;63}6465}6667public static void main(String[] args) throws Exception {68//for (Provider provider : Security.getProviders()) {69Provider p = Security.getProvider("SunRsaSign");70Signature sig;71try {72sig = Signature.getInstance("RSASSA-PSS", p);73} catch (NoSuchAlgorithmException e) {74System.out.println("Skip testing RSASSA-PSS" +75" due to no support");76return;77}7879boolean success = true;80for (String f : testFiles) {81System.out.println("[INPUT FILE " + f + "]");82try {83success &= runTest(SigRecord.read(f), sig);84} catch (IOException e) {85System.out.println("Unexpected exception: " + e);86e.printStackTrace(System.out);87success = false;88}89}9091if (!success) {92throw new RuntimeException("One or more test failed");93}94System.out.println("Test passed");95}9697/*98* Run all the tests in the data list with specified algorithm99*/100static boolean runTest(List<SigRecord> records, Signature sig) throws Exception {101boolean success = true;102KeyFactory kf = KeyFactory.getInstance("RSA", sig.getProvider());103for (SigRecord sr : records) {104System.out.println("==Testing Record : " + sr + "==");105PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);106PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);107success &= check(sig, privKey, pubKey, sr.testVectors);108System.out.println("==Done==");109}110return success;111}112113/*114* Generate the signature, check against known values and verify.115*/116static boolean check(Signature sig, PrivateKey privKey, PublicKey pubKey,117List<SigRecord.SigVector> vectors) throws Exception {118119boolean success = true;120for (SigRecord.SigVector v : vectors) {121System.out.println("\tAgainst " + v.mdAlg);122byte[] msgBytes = SigRecord.toByteArray(v.msg);123byte[] expSigBytes = SigRecord.toByteArray(v.sig);124125MyKnownRandomSrc saltSrc = new MyKnownRandomSrc(v.salt);126sig.initSign(privKey, saltSrc);127PSSParameterSpec params = new PSSParameterSpec(v.mdAlg, "MGF1",128new MGF1ParameterSpec(v.mdAlg), saltSrc.numBytes, 1);129sig.setParameter(params);130sig.update(msgBytes);131byte[] actualSigBytes = sig.sign();132133// Check if the supplied salt bytes are used up134if (saltSrc.numBytes != 0) {135throw new RuntimeException("Error: salt length mismatch! "136+ saltSrc.numBytes + " bytes leftover");137}138139success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);140141if (!success) {142System.out.println("\tFailed:");143System.out.println("\tSHAALG = " + v.mdAlg);144System.out.println("\tMsg = " + v.msg);145System.out.println("\tSalt = " + v.salt);146System.out.println("\tExpected Sig = " + v.sig);147System.out.println("\tActual Sig = " + SigRecord.toHexString(actualSigBytes));148} else {149System.out.println("\t" + v.mdAlg + " Test Vector Passed");150}151}152return success;153}154}155156157