Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ec/SignatureDigestTruncate.java
38838 views
/*1* Copyright (c) 2019, 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.testlibrary.Convert;2425import java.security.*;26import java.security.spec.*;27import java.math.*;28import java.util.*;2930/*31* @test32* @bug 814750233* @summary Test that digests are properly truncated before the signature34* is applied. The digest should be truncated to the bit length of the35* group order.36* @library /lib/testlibrary37* @build jdk.testlibrary.Convert38* @run main SignatureDigestTruncate39*/40public class SignatureDigestTruncate {4142/*43* A SecureRandom that produces nextBytes in a way that causes the nonce44* to be set to the value supplied to the constructor. This class45* is specific to the way that the native ECDSA implementation in46* SunEC produces nonces from random input. It may not work for all47* test cases, and it will need to be updated when the behavior of48* SunEC changes.49*/50private static class FixedRandom extends SecureRandom {5152private final byte[] val;5354public FixedRandom(byte[] val) {55// SunEC adds one to the value returned, so subtract one here in56// order to get back to the correct value.57BigInteger biVal = new BigInteger(1, val);58biVal = biVal.subtract(BigInteger.ONE);59byte[] temp = biVal.toByteArray();60this.val = new byte[val.length];61int inStartPos = Math.max(0, temp.length - val.length);62int outStartPos = Math.max(0, val.length - temp.length);63System.arraycopy(temp, inStartPos, this.val, outStartPos,64temp.length - inStartPos);65}6667@Override68public void nextBytes(byte[] bytes) {69// SunEC samples (n + 1) * 2 bytes, but only n*2 bytes are used by70// the native implementation. So the value must be offset slightly.71Arrays.fill(bytes, (byte) 0);72int copyLength = Math.min(val.length, bytes.length - 2);73System.arraycopy(val, 0, bytes, bytes.length - copyLength - 2,74copyLength);75}76}7778private static void assertEquals(byte[] expected, byte[] actual,79String name) {80if (!Arrays.equals(actual, expected)) {81System.out.println("expect: "82+ Convert.byteArrayToHexString(expected));83System.out.println("actual: "84+ Convert.byteArrayToHexString(actual));85throw new RuntimeException("Incorrect " + name + " value");86}87}8889private static void runTest(String alg, String curveName,90String privateKeyStr, String msgStr, String kStr, String sigStr)91throws Exception {9293System.out.println("Testing " + alg + " with " + curveName);9495byte[] privateKey = Convert.hexStringToByteArray(privateKeyStr);96byte[] msg = Convert.hexStringToByteArray(msgStr);97byte[] k = Convert.hexStringToByteArray(kStr);98byte[] expectedSig = Convert.hexStringToByteArray(sigStr);99100AlgorithmParameters params =101AlgorithmParameters.getInstance("EC", "SunEC");102params.init(new ECGenParameterSpec(curveName));103ECParameterSpec ecParams =104params.getParameterSpec(ECParameterSpec.class);105106KeyFactory kf = KeyFactory.getInstance("EC", "SunEC");107BigInteger s = new BigInteger(1, privateKey);108ECPrivateKeySpec privKeySpec = new ECPrivateKeySpec(s, ecParams);109PrivateKey privKey = kf.generatePrivate(privKeySpec);110111Signature sig = Signature.getInstance(alg, "SunEC");112sig.initSign(privKey, new FixedRandom(k));113sig.update(msg);114byte[] computedSig = sig.sign();115assertEquals(expectedSig, computedSig, "signature");116}117118public static void main(String[] args) throws Exception {119runTest("SHA384withECDSA", "sect283r1",120"abcdef10234567", "010203040506070809",121"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d" +122"1e1f20212223",123"304c022401d7544b5d3935216bd45e2f8042537e1e0296a11e0eb9666619" +124"9281b40942abccd5358a0224035de8a314d3e6c2a97614daebf5fb131354" +125"0eec3f9a3272068aa10922ccae87d255c84c");126}127}128129130