Path: blob/master/test/jdk/sun/security/ec/SignatureDigestTruncate.java
41152 views
/*1* Copyright (c) 2019, 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.security.*;24import java.security.spec.*;25import java.math.*;26import java.util.*;2728/*29* @test30* @bug 814750231* @summary Test that digests are properly truncated before the signature32* is applied. The digest should be truncated to the bit length of the33* group order.34* @library /test/lib35* @build jdk.test.lib.Convert36* @run main/othervm SignatureDigestTruncate37*/38public class SignatureDigestTruncate {3940/*41* A SecureRandom that produces nextBytes in a way that causes the nonce42* to be set to the value supplied to the constructor. This class43* is specific to the way that the native ECDSA implementation in44* SunEC produces nonces from random input. It may not work for all45* test cases, and it will need to be updated when the behavior of46* SunEC changes.47*/48private static class FixedRandom extends SecureRandom {4950private final byte[] val;5152public FixedRandom(byte[] val) {53// SunEC adds one to the value returned, so subtract one here in54// order to get back to the correct value.55BigInteger biVal = new BigInteger(1, val);56biVal = biVal.subtract(BigInteger.ONE);57byte[] temp = biVal.toByteArray();58this.val = new byte[val.length];59int inStartPos = Math.max(0, temp.length - val.length);60int outStartPos = Math.max(0, val.length - temp.length);61System.arraycopy(temp, inStartPos, this.val, outStartPos,62temp.length - inStartPos);63}6465@Override66public void nextBytes(byte[] bytes) {67// SunEC samples (n + 1) * 2 bytes, but only n*2 bytes are used by68// the native implementation. So the value must be offset slightly.69Arrays.fill(bytes, (byte) 0);70int copyLength = Math.min(val.length, bytes.length - 2);71System.arraycopy(val, 0, bytes, bytes.length - copyLength - 2,72copyLength);73}74}7576private static void assertEquals(byte[] expected, byte[] actual,77String name) {78if (!Arrays.equals(actual, expected)) {79System.out.println("expect: " + HexFormat.of().withUpperCase().formatHex(expected));80System.out.println("actual: " + HexFormat.of().withUpperCase().formatHex(actual));81throw new RuntimeException("Incorrect " + name + " value");82}83}8485private static void runTest(String alg, String curveName,86String privateKeyStr, String msgStr, String kStr, String sigStr)87throws Exception {8889System.out.println("Testing " + alg + " with " + curveName);9091HexFormat hex = HexFormat.of();92byte[] privateKey = hex.parseHex(privateKeyStr);93byte[] msg = hex.parseHex(msgStr);94byte[] k = hex.parseHex(kStr);95byte[] expectedSig = hex.parseHex(sigStr);9697AlgorithmParameters params =98AlgorithmParameters.getInstance("EC", "SunEC");99params.init(new ECGenParameterSpec(curveName));100ECParameterSpec ecParams =101params.getParameterSpec(ECParameterSpec.class);102103KeyFactory kf = KeyFactory.getInstance("EC", "SunEC");104BigInteger s = new BigInteger(1, privateKey);105ECPrivateKeySpec privKeySpec = new ECPrivateKeySpec(s, ecParams);106PrivateKey privKey = kf.generatePrivate(privKeySpec);107108Signature sig = Signature.getInstance(alg, "SunEC");109sig.initSign(privKey, new FixedRandom(k));110sig.update(msg);111byte[] computedSig = sig.sign();112assertEquals(expectedSig, computedSig, "signature");113}114115public static void main(String[] args) throws Exception {116runTest("SHA384withECDSAinP1363Format", "secp256r1",117"abcdef10234567", "010203040506070809",118"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d" +119"1e1f20212223",120"d83534beccde787f9a4c6b0408337d9b9ca2e0a0259228526c15cc17a1d6" +121"4da6b34bf21b3bc4488c591d8ac9c33d93c7c6137e2ab4c503a42da7" +122"2fe0b6dda4c4");123}124}125126127