Path: blob/master/test/jdk/sun/security/ec/SignatureKAT.java
41152 views
/*1* Copyright (c) 2020, 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 jdk.test.lib.Convert;2425import java.security.*;26import java.security.spec.*;27import java.math.*;28import java.util.*;2930/*31* @test32* @bug 817236633* @summary Known Answer Test for ECDSA signature34* @library /test/lib35* @build jdk.test.lib.Convert36* @run main/othervm SignatureKAT37*/38public class SignatureKAT {3940private static String checkHex(String hex) {41// if hex length is odd, need to prepend 042if (hex.length() % 2 != 0) {43hex = "0" + hex;44}45return hex;46}4748private static class CurveData {49private String name;50private byte[] msgBytes;51private BigInteger priv;52private BigInteger pubX;53private BigInteger pubY;5455private static BigInteger toBigInteger(String hex) {56byte[] bytes = HexFormat.of().parseHex(checkHex(hex));57return new BigInteger(1, bytes);58}59CurveData(String name, String msg, String priv, String pubX,60String pubY) {61this.name = name;62this.msgBytes = msg.getBytes();63this.priv = toBigInteger(priv);64this.pubX = toBigInteger(pubX);65this.pubY = toBigInteger(pubY);66}67}6869private static class TestData {70private String sigName;71private CurveData cd;72private byte[] expSig;7374TestData(String sigName, CurveData cd, String r, String s) {75this.sigName = sigName;76this.cd = cd;77if (r.length() != s.length() || r != checkHex(r) ||78s != checkHex(s)) {79throw new RuntimeException("Error: invalid r, s");80}81this.expSig = HexFormat.of().parseHex(r + s);82}83}8485// These test values are from the examples shown in the page below:86// https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values87private static final CurveData P_256 = new CurveData(88"secp256r1", "Example of ECDSA with P-256",89"C477F9F65C22CCE20657FAA5B2D1D8122336F851A508A1ED04E479C34985BF96",90"B7E08AFDFE94BAD3F1DC8C734798BA1C62B3A0AD1E9EA2A38201CD0889BC7A19",91"3603F747959DBF7A4BB226E41928729063ADC7AE43529E61B563BBC606CC5E09"92);9394private static final CurveData P_384 = new CurveData(95"secp384r1", "Example of ECDSA with P-384",96"F92C02ED629E4B48C0584B1C6CE3A3E3B4FAAE4AFC6ACB0455E73DFC392E6A0AE393A8565E6B9714D1224B57D83F8A08",97"3BF701BC9E9D36B4D5F1455343F09126F2564390F2B487365071243C61E6471FB9D2AB74657B82F9086489D9EF0F5CB5",98"D1A358EAFBF952E68D533855CCBDAA6FF75B137A5101443199325583552A6295FFE5382D00CFCDA30344A9B5B68DB855"99);100101private static final CurveData P_521 = new CurveData(102"secp521r1", "Example of ECDSA with P-521",103"100085F47B8E1B8B11B7EB33028C0B2888E304BFC98501955B45BBA1478DC184EEEDF09B86A5F7C21994406072787205E69A63709FE35AA93BA333514B24F961722",104"98E91EEF9A68452822309C52FAB453F5F117C1DA8ED796B255E9AB8F6410CCA16E59DF403A6BDC6CA467A37056B1E54B3005D8AC030DECFEB68DF18B171885D5C4",105"164350C321AECFC1CCA1BA4364C9B15656150B4B78D6A48D7D28E7F31985EF17BE8554376B72900712C4B83AD668327231526E313F5F092999A4632FD50D946BC2E"106);107108private static TestData[] TEST_DATUM = {109// secp256r1, secp384r1, and secp521r1 remain enabled110new TestData("SHA256withECDSAinP1363Format", P_256,111"2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F",112"DC42C2122D6392CD3E3A993A89502A8198C1886FE69D262C4B329BDB6B63FAF1"),113new TestData("SHA3-256withECDSAinP1363Format", P_256,114"2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F",115"0A861C2526900245C73BACB9ADAEC1A5ACB3BA1F7114A3C334FDCD5B7690DADD"),116new TestData("SHA384withECDSAinP1363Format", P_384,117"30EA514FC0D38D8208756F068113C7CADA9F66A3B40EA3B313D040D9B57DD41A332795D02CC7D507FCEF9FAF01A27088",118"CC808E504BE414F46C9027BCBF78ADF067A43922D6FCAA66C4476875FBB7B94EFD1F7D5DBE620BFB821C46D549683AD8"),119new TestData("SHA3-384withECDSAinP1363Format", P_384,120"30EA514FC0D38D8208756F068113C7CADA9F66A3B40EA3B313D040D9B57DD41A332795D02CC7D507FCEF9FAF01A27088",121"691B9D4969451A98036D53AA725458602125DE74881BBC333012CA4FA55BDE39D1BF16A6AAE3FE4992C567C6E7892337"),122new TestData("SHA512withECDSAinP1363Format", P_521,123"0140C8EDCA57108CE3F7E7A240DDD3AD74D81E2DE62451FC1D558FDC79269ADACD1C2526EEEEF32F8C0432A9D56E2B4A8A732891C37C9B96641A9254CCFE5DC3E2BA",124"00D72F15229D0096376DA6651D9985BFD7C07F8D49583B545DB3EAB20E0A2C1E8615BD9E298455BDEB6B61378E77AF1C54EEE2CE37B2C61F5C9A8232951CB988B5B1"),125new TestData("SHA3-512withECDSAinP1363Format", P_521,126"0140C8EDCA57108CE3F7E7A240DDD3AD74D81E2DE62451FC1D558FDC79269ADACD1C2526EEEEF32F8C0432A9D56E2B4A8A732891C37C9B96641A9254CCFE5DC3E2BA",127"00B25188492D58E808EDEBD7BF440ED20DB771CA7C618595D5398E1B1C0098E300D8C803EC69EC5F46C84FC61967A302D366C627FCFA56F87F241EF921B6E627ADBF"),128};129130private static void runTest(TestData td) throws Exception {131System.out.println("Testing " + td.sigName + " with " + td.cd.name);132133AlgorithmParameters params =134AlgorithmParameters.getInstance("EC", "SunEC");135params.init(new ECGenParameterSpec(td.cd.name));136ECParameterSpec ecParams =137params.getParameterSpec(ECParameterSpec.class);138139KeyFactory kf = KeyFactory.getInstance("EC", "SunEC");140PrivateKey privKey = kf.generatePrivate141(new ECPrivateKeySpec(td.cd.priv, ecParams));142143Signature sig = Signature.getInstance(td.sigName, "SunEC");144sig.initSign(privKey);145sig.update(td.cd.msgBytes);146// NOTE: there is no way to set the nonce value into current SunEC147// ECDSA signature, thus the output signature bytes likely won't148// match the expected signature bytes149byte[] ov = sig.sign();150151ECPublicKeySpec pubKeySpec = new ECPublicKeySpec152(new ECPoint(td.cd.pubX, td.cd.pubY), ecParams);153PublicKey pubKey = kf.generatePublic(pubKeySpec);154155sig.initVerify(pubKey);156sig.update(td.cd.msgBytes);157if (!sig.verify(ov)) {158throw new RuntimeException("Error verifying actual sig bytes");159}160161sig.update(td.cd.msgBytes);162if (!sig.verify(td.expSig)) {163throw new RuntimeException("Error verifying expected sig bytes");164}165}166167public static void main(String[] args) throws Exception {168for (TestData td : TEST_DATUM) {169runTest(td);170}171}172}173174175