Path: blob/master/test/jdk/sun/security/rsa/pss/TestSigGenPSS.java
41153 views
/*1* Copyright (c) 2018, 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*/2223import java.io.IOException;24import java.security.*;25import java.security.spec.*;26import java.util.HexFormat;27import java.util.List;2829/*30* @test31* @bug 814629332* @summary Known Answer Tests based on NIST 186-3 at:33* @compile SigRecord.java34* @run main/othervm TestSigGenPSS35*/36public class TestSigGenPSS {3738private static final String[] testFiles = {39"SigGenPSS_186-3.txt", "SigGenPSS_186-3_TruncatedSHAs.txt"40};4142static final class MyKnownRandomSrc extends SecureRandom {43final byte[] srcBytes;44int numBytes;4546MyKnownRandomSrc(String srcString) {47this.srcBytes = HexFormat.of().parseHex(srcString);48this.numBytes = this.srcBytes.length;49}50@Override51public void nextBytes(byte[] bytes) {52if (bytes.length > numBytes) {53throw new RuntimeException("Not enough bytes, need "54+ bytes.length + ", got " + numBytes);55}56System.arraycopy(this.srcBytes, this.srcBytes.length - numBytes, bytes, 0, bytes.length);57numBytes -= bytes.length;58}5960}6162public static void main(String[] args) throws Exception {63//for (Provider provider : Security.getProviders()) {64Provider p = Security.getProvider("SunRsaSign");65Signature sig;66try {67sig = Signature.getInstance("RSASSA-PSS", p);68} catch (NoSuchAlgorithmException e) {69System.out.println("Skip testing RSASSA-PSS" +70" due to no support");71return;72}7374boolean success = true;75for (String f : testFiles) {76System.out.println("[INPUT FILE " + f + "]");77try {78success &= runTest(SigRecord.read(f), sig);79} catch (IOException e) {80System.out.println("Unexpected exception: " + e);81e.printStackTrace(System.out);82success = false;83}84}8586if (!success) {87throw new RuntimeException("One or more test failed");88}89System.out.println("Test passed");90}9192/*93* Run all the tests in the data list with specified algorithm94*/95static boolean runTest(List<SigRecord> records, Signature sig) throws Exception {96boolean success = true;97KeyFactory kf = KeyFactory.getInstance("RSA", sig.getProvider());98for (SigRecord sr : records) {99System.out.println("==Testing Record : " + sr + "==");100PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);101PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);102success &= check(sig, privKey, pubKey, sr.testVectors);103System.out.println("==Done==");104}105return success;106}107108/*109* Generate the signature, check against known values and verify.110*/111static boolean check(Signature sig, PrivateKey privKey, PublicKey pubKey,112List<SigRecord.SigVector> vectors) throws Exception {113114boolean success = true;115for (SigRecord.SigVector v : vectors) {116System.out.println("\tAgainst " + v.mdAlg);117byte[] msgBytes = HexFormat.of().parseHex(v.msg);118byte[] expSigBytes = HexFormat.of().parseHex(v.sig);119120MyKnownRandomSrc saltSrc = new MyKnownRandomSrc(v.salt);121sig.initSign(privKey, saltSrc);122PSSParameterSpec params = new PSSParameterSpec(v.mdAlg, "MGF1",123new MGF1ParameterSpec(v.mdAlg), saltSrc.numBytes, 1);124sig.setParameter(params);125sig.update(msgBytes);126byte[] actualSigBytes = sig.sign();127128// Check if the supplied salt bytes are used up129if (saltSrc.numBytes != 0) {130throw new RuntimeException("Error: salt length mismatch! "131+ saltSrc.numBytes + " bytes leftover");132}133134success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);135136if (!success) {137System.out.println("\tFailed:");138System.out.println("\tSHAALG = " + v.mdAlg);139System.out.println("\tMsg = " + v.msg);140System.out.println("\tSalt = " + v.salt);141System.out.println("\tExpected Sig = " + v.sig);142System.out.println("\tActual Sig = " + HexFormat.of().formatHex(actualSigBytes));143} else {144System.out.println("\t" + v.mdAlg + " Test Vector Passed");145}146}147return success;148}149}150151152