Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/TestSigGen15.java
38838 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 TestSigGen1540*/41public class TestSigGen15 {4243private static final String[] testFiles = {44"SigGen15_186-3.txt", "SigGen15_186-3_TruncatedSHAs.txt"45};4647public static void main(String[] args) throws Exception {48boolean success = true;49for (String f : testFiles) {50System.out.println("[INPUT FILE " + f + "]");51try {52success &= runTest(SigRecord.read(f));53} catch (IOException e) {54System.out.println("Unexpected exception: " + e);55e.printStackTrace(System.out);56success = false;57}58}5960if (!success) {61throw new RuntimeException("One or more test failed");62}63System.out.println("Test passed");64}6566/*67* Run all the tests in the data list with specified algorithm68*/69static boolean runTest(List<SigRecord> records) throws Exception {70boolean success = true;71//for (Provider provider : Security.getProviders()) {72Provider p = Security.getProvider("SunRsaSign");73KeyFactory kf = KeyFactory.getInstance("RSA", p);74for (SigRecord sr : records) {75System.out.println("==Testing Record : " + sr + "==");76PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);77PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);78success &= check(privKey, pubKey, sr.testVectors, p);79System.out.println("==Done==");80}81return success;82}8384/*85* Generate the signature, check against known values and verify.86*/87static boolean check(PrivateKey privKey, PublicKey pubKey,88List<SigRecord.SigVector> vectors, Provider p) throws Exception {8990boolean success = true;91for (SigRecord.SigVector v : vectors) {92System.out.println("\tAgainst " + v.mdAlg);93String sigAlgo = v.mdAlg + "withRSA";94Signature sig;95try {96sig = Signature.getInstance(sigAlgo, p);97} catch (NoSuchAlgorithmException e) {98System.out.println("\tSkip " + sigAlgo +99" due to no support");100continue;101}102byte[] msgBytes = SigRecord.toByteArray(v.msg);103byte[] expSigBytes = SigRecord.toByteArray(v.sig);104105sig.initSign(privKey);106sig.update(msgBytes);107byte[] actualSigBytes = sig.sign();108109success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);110111if (!success) {112System.out.println("\tFailed:");113System.out.println("\tSHAALG = " + v.mdAlg);114System.out.println("\tMsg = " + v.msg);115System.out.println("\tExpected Sig = " + v.sig);116System.out.println("\tActual Sig = " + SigRecord.toHexString(actualSigBytes));117} else {118System.out.println("\t" + v.mdAlg + " Test Vector Passed");119}120}121122return success;123}124}125126127