Path: blob/master/test/jdk/sun/security/ec/ed/EdDSAParamSpec.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*/22import java.security.KeyPair;23import java.security.KeyPairGenerator;24import java.security.PrivateKey;25import java.security.PublicKey;26import java.security.SecureRandom;27import java.security.Signature;28import java.security.spec.EdDSAParameterSpec;29import java.util.Arrays;30import java.util.HexFormat;3132/*33* @test34* @bug 820963235* @summary Test EdDSAParameterSpec.36* @library /test/lib37* @build jdk.test.lib.Convert38* @run main EdDSAParamSpec39*/40public class EdDSAParamSpec {4142private static final String EDDSA = "EdDSA";43private static final String ED25519 = "Ed25519";44private static final String ED448 = "Ed448";45private static final String PROVIDER = "SunEC";46private static final byte[] MSG = "TEST".getBytes();47private static final SecureRandom RND = new SecureRandom(new byte[]{0x1});4849public static void main(String[] args) throws Exception {5051testParam(PROVIDER, EDDSA);52testParam(PROVIDER, ED25519);53testParam(PROVIDER, ED448);54}5556/**57* Test Signature.58*/59private static void testParam(String provider, String name)60throws Exception {6162KeyPair kp = genKeyPair(provider, name);63Signature sig = Signature.getInstance(name, provider);64EdDSAParameterSpec initParam65= new EdDSAParameterSpec(true, "testContext".getBytes());66sig.setParameter(initParam);67byte[] origSign = sign(sig, kp.getPrivate(), MSG);68for (boolean preHash : new boolean[]{true, false}) {69System.out.printf("Testing signature for name: %s,"70+ " algorithm spec: (prehash:%s)%n", name, preHash);71verifyPublic(sig, kp.getPublic(), MSG,72new EdDSAParameterSpec(preHash), initParam, origSign);73// Test Case with Context size combined.74// As per rfc8032, value of context is maximum of 255 octet75byte[] maxCtx = new byte[255];76RND.nextBytes(maxCtx);77for (byte[] context : new byte[][]{"others".getBytes(), maxCtx}) {78System.out.printf("Testing signature for name: %s,"79+ " algorithm spec: (prehash:%s, context:%s)%n",80name, preHash, HexFormat.of().withUpperCase().formatHex(context));81EdDSAParameterSpec params82= new EdDSAParameterSpec(preHash, context);83verifyPublic(sig, kp.getPublic(), MSG, params, initParam,84origSign);85}86}87System.out.println("Passed.");88}8990private static KeyPair genKeyPair(String provider, String name)91throws Exception {9293KeyPairGenerator kpg = KeyPairGenerator.getInstance(name, provider);94return kpg.generateKeyPair();95}9697private static byte[] sign(Signature sig, PrivateKey priKey, byte[] msg)98throws Exception {99100sig.initSign(priKey);101sig.update(msg);102return sig.sign();103}104105private static boolean verify(Signature sig, PublicKey pubKey, byte[] msg,106byte[] sign) throws Exception {107108sig.initVerify(pubKey);109sig.update(msg);110return sig.verify(sign);111}112113private static void verifyPublic(Signature sig, PublicKey pubKey,114byte[] msg, EdDSAParameterSpec params, EdDSAParameterSpec initParam,115byte[] origSign) throws Exception {116117sig.setParameter(params);118if (verify(sig, pubKey, msg, origSign)) {119byte[] context = params.getContext().isPresent()120? params.getContext().get() : null;121byte[] initContext = initParam.getContext().isPresent()122? initParam.getContext().get() : null;123boolean preHash = params.isPrehash();124boolean initPreHash = initParam.isPrehash();125// The signature should not get verified other than same parameter126// which is set through the signature instance.127if (!(equals(context, initContext) && equals(preHash, initPreHash))) {128throw new RuntimeException(String.format("Signature verification"129+ " success with different param context(actual:%s, "130+ "expected:%s), Prehash(actual:%s, expected:%s)",131HexFormat.of().withUpperCase().formatHex(context),132HexFormat.of().withUpperCase().formatHex(initContext),133preHash, initPreHash));134} else {135System.out.println("Atleast a case matched");136}137}138}139140private static boolean equals(Object actual, Object expected) {141142if (actual == expected) {143return true;144}145if (actual == null || expected == null) {146return false;147}148boolean equals = actual.equals(expected);149if (!equals) {150throw new RuntimeException(String.format("Actual: %s, Expected: %s",151actual, expected));152}153return equals;154}155156private static boolean equals(byte[] actual, byte[] expected) {157158if (actual == expected) {159return true;160}161if (actual == null || expected == null) {162return false;163}164boolean equals = Arrays.equals(actual, expected);165if (!equals) {166throw new RuntimeException(String.format("Actual array: %s, "167+ "Expected array:%s", HexFormat.of().withUpperCase().formatHex(actual),168HexFormat.of().withUpperCase().formatHex(expected)));169}170return equals;171}172173}174175176