Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/SigRecord.java
38839 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.math.BigInteger;29import java.security.*;30import java.security.spec.*;31import java.util.ArrayList;32import java.util.List;3334public final class SigRecord {3536static final String TEST_SRC = System.getProperty("test.src", ".");3738// utility method for converting byte array to hex string39static String toHexString(byte[] array) {40StringBuilder sb = new StringBuilder(array.length * 2);41for (byte b : array) {42// The single digits 0123456789abcdef get a leading 043if ((b >= 0x00) && (b < 0x10)) {44sb.append('0');45}46sb.append(Integer.toHexString(b & 0xff));47}48return sb.toString();49}5051// utility method for converting hex string to byte array52static byte[] toByteArray(String s) {53byte[] bytes = new byte[s.length() / 2];54for (int i = 0; i < bytes.length; i++) {55int index = i * 2;56int v = Integer.parseInt(s.substring(index, index + 2), 16);57bytes[i] = (byte) v;58}59return bytes;60}6162public static final class SigVector {63// digest algorithm to use64final String mdAlg;6566// message to test67final String msg;6869// expected signature70final String sig;7172public SigVector(String mdAlg, String msg, String sig) {73if (mdAlg == null || mdAlg.isEmpty()) {74throw new IllegalArgumentException("Digest algo must be specified");75}76if (msg == null || mdAlg.isEmpty()) {77throw new IllegalArgumentException("Message must be specified");78}79if (sig == null || mdAlg.isEmpty()) {80throw new IllegalArgumentException("Signature must be specified");81}82this.mdAlg = mdAlg;83this.msg = msg;84this.sig = sig;85}8687@Override88public String toString() {89return (mdAlg + ": msg=" + msg + ": sig=" + sig);90}91}9293final String id;94// RSA private key value associated with the corresponding test vectors95final RSAPrivateKeySpec privKeySpec;9697// RSA public key value associated with the corresponding test vectors98final RSAPublicKeySpec pubKeySpec;99100// set of test vectors101final List<SigVector> testVectors;102103SigRecord(String mod, String pubExp, String privExp, List<SigVector> testVectors) {104if (mod == null || mod.isEmpty()) {105throw new IllegalArgumentException("Modulus n must be specified");106}107if (pubExp == null || pubExp.isEmpty()) {108throw new IllegalArgumentException("Public Exponent e must be specified");109}110if (privExp == null || privExp.isEmpty()) {111throw new IllegalArgumentException("Private Exponent d must be specified");112}113if (testVectors == null || (testVectors.size() == 0)) {114throw new IllegalArgumentException("One or more test vectors must be specified");115}116117BigInteger n = new BigInteger(1, toByteArray(mod));118BigInteger e = new BigInteger(1, toByteArray(pubExp));119BigInteger d = new BigInteger(1, toByteArray(privExp));120this.id = ("n=" + mod + ", e=" + pubExp);121this.pubKeySpec = new RSAPublicKeySpec(n, e);122this.privKeySpec = new RSAPrivateKeySpec(n, d);123this.testVectors = testVectors;124}125126/*127* Read a data file into an ArrayList.128* This function will exit the program if reading the file fails129* or if the file is not in the expected format.130*/131public static List<SigRecord> read(String filename)132throws IOException {133134List<SigRecord> data = new ArrayList<>();135try (BufferedReader br = new BufferedReader(136new InputStreamReader(new FileInputStream(137TEST_SRC + File.separator + filename)))) {138String line;139String mod = null;140String pubExp = null;141String privExp = null;142List<SigVector> testVectors = new ArrayList<>();143while ((line = br.readLine()) != null) {144if (line.startsWith("n =")) {145mod = line.split("=")[1].trim();146} else if (line.startsWith("e =")) {147pubExp = line.split("=")[1].trim();148} else if (line.startsWith("d =")) {149privExp = line.split("=")[1].trim();150151// now should start parsing for test vectors152String mdAlg = null;153String msg = null;154String sig = null;155boolean sigVectorDone = false;156while ((line = br.readLine()) != null) {157// we only care for lines starting with158// SHAALG, Msg, S159if (line.startsWith("SHAAlg =")) {160mdAlg = line.split(" = ")[1].trim();161} else if (line.startsWith("Msg =")) {162msg = line.split(" = ")[1].trim();163} else if (line.startsWith("S =")) {164sig = line.split(" = ")[1].trim();165} else if (line.startsWith("[mod")) {166sigVectorDone = true;167}168169if ((mdAlg != null) && (msg != null) && (sig != null)) {170// finish off current SigVector171testVectors.add(new SigVector(mdAlg, msg, sig));172mdAlg = msg = sig = null;173}174if (sigVectorDone) {175break;176}177}178// finish off current SigRecord and clear data for next SigRecord179data.add(new SigRecord(mod, pubExp, privExp, testVectors));180mod = pubExp = privExp = null;181testVectors = new ArrayList<>();182}183}184185if (data.isEmpty()) {186throw new RuntimeException("Nothing read from file "187+ filename);188}189}190return data;191}192193@Override194public String toString() {195return (id + ", " + testVectors.size() + " test vectors");196}197}198199200