Path: blob/master/test/jdk/sun/security/rsa/TestSigGen15.java
41152 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.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.HexFormat;33import java.util.List;3435/*36* @test37* @bug 814629338* @summary Known Answer Tests based on NIST 186-3 at:39* @compile SigRecord.java40* @run main/othervm TestSigGen1541*/42public class TestSigGen15 {4344private static final String[] testFiles = {45"SigGen15_186-3.txt", "SigGen15_186-3_TruncatedSHAs.txt"46};4748public static void main(String[] args) throws Exception {49boolean success = true;50for (String f : testFiles) {51System.out.println("[INPUT FILE " + f + "]");52try {53success &= runTest(SigRecord.read(f));54} catch (IOException e) {55System.out.println("Unexpected exception: " + e);56e.printStackTrace(System.out);57success = false;58}59}6061if (!success) {62throw new RuntimeException("One or more test failed");63}64System.out.println("Test passed");65}6667/*68* Run all the tests in the data list with specified algorithm69*/70static boolean runTest(List<SigRecord> records) throws Exception {71boolean success = true;72//for (Provider provider : Security.getProviders()) {73Provider p = Security.getProvider("SunRsaSign");74KeyFactory kf = KeyFactory.getInstance("RSA", p);75for (SigRecord sr : records) {76System.out.println("==Testing Record : " + sr + "==");77PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);78PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);79success &= check(privKey, pubKey, sr.testVectors, p);80System.out.println("==Done==");81}82return success;83}8485/*86* Generate the signature, check against known values and verify.87*/88static boolean check(PrivateKey privKey, PublicKey pubKey,89List<SigRecord.SigVector> vectors, Provider p) throws Exception {9091boolean success = true;92for (SigRecord.SigVector v : vectors) {93System.out.println("\tAgainst " + v.mdAlg);94String sigAlgo = v.mdAlg + "withRSA";95Signature sig;96try {97sig = Signature.getInstance(sigAlgo, p);98} catch (NoSuchAlgorithmException e) {99System.out.println("\tSkip " + sigAlgo +100" due to no support");101continue;102}103byte[] msgBytes = HexFormat.of().parseHex(v.msg);104byte[] expSigBytes = HexFormat.of().parseHex(v.sig);105106sig.initSign(privKey);107sig.update(msgBytes);108byte[] actualSigBytes = sig.sign();109110success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);111112if (!success) {113System.out.println("\tFailed:");114System.out.println("\tSHAALG = " + v.mdAlg);115System.out.println("\tMsg = " + v.msg);116System.out.println("\tExpected Sig = " + v.sig);117System.out.println("\tActual Sig = " + HexFormat.of().formatHex(actualSigBytes));118} else {119System.out.println("\t" + v.mdAlg + " Test Vector Passed");120}121}122123return success;124}125}126127128