Path: blob/master/test/jdk/sun/security/ec/ed/EdDSAReuseTest.java
41152 views
/*1* Copyright (c) 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.security.KeyPair;24import java.security.KeyPairGenerator;25import java.security.PrivateKey;26import java.security.PublicKey;27import java.security.Signature;28import java.security.spec.NamedParameterSpec;29import java.util.ArrayList;30import java.util.List;3132/*33* @test34* @bug 820963235* @summary Test behaviour of Signature instance by re-using it multiple times36* in different way.37* @run main EdDSAReuseTest38*/39public class EdDSAReuseTest {4041private static final String EDDSA = "EdDSA";42private static final String ED25519 = "Ed25519";43private static final String ED448 = "Ed448";44private static final String PROVIDER = "SunEC";45private static final String MSG = "TEST";46private static final int REUSE = 20;47private static final int ONCE = 1;48private static final int TENTH = 10;49private static final int FIFTH = 5;5051public static void main(String[] args) throws Exception {5253for (boolean initKey : new boolean[]{true, false}) {54// Sign and Verify with data update once55test(PROVIDER, EDDSA, null, initKey, ONCE, ONCE);56test(PROVIDER, ED25519, ED25519, initKey, ONCE, ONCE);57test(PROVIDER, ED448, ED448, initKey, ONCE, ONCE);5859// Sign and Verify with data update 10 times60test(PROVIDER, EDDSA, null, initKey, TENTH, TENTH);61test(PROVIDER, ED25519, ED25519, initKey, TENTH, TENTH);62test(PROVIDER, ED448, ED448, initKey, TENTH, TENTH);6364// Sign and Verify with data update unmatched number of times65test(PROVIDER, EDDSA, null, initKey, TENTH, FIFTH);66test(PROVIDER, ED25519, ED25519, initKey, TENTH, FIFTH);67test(PROVIDER, ED448, ED448, initKey, TENTH, FIFTH);68}69}7071private static void test(String provider, String name, Object param,72boolean initKey, int signUpdate, int verifyUpdate)73throws Exception {7475System.out.printf("Case for signature name: %s, param: %s,"76+ " initialize signature instance before each operation: %s%n",77name, param, initKey);78KeyPairGenerator kpg = KeyPairGenerator.getInstance(name, provider);79if (param != null) {80kpg.initialize(new NamedParameterSpec((String) param));81}82KeyPair kp = kpg.generateKeyPair();83Signature sig = Signature.getInstance(name, provider);84testAPI(sig, kp, initKey, signUpdate, verifyUpdate);85System.out.println("Passed.");86}8788private static void testAPI(Signature sig, KeyPair kp, boolean initKey,89int signUpdate, int verifyUpdate) throws Exception {9091sig.initSign(kp.getPrivate());92List<byte[]> signatures = new ArrayList<>();93// Re-use the signature instance 20 times94for (int i = 0; i < REUSE; i++) {95signatures.add(sign(sig, kp.getPrivate(), MSG, initKey, signUpdate));96}97System.out.printf("Generated signatures %s times%n", signatures.size());98sig.initVerify(kp.getPublic());99for (byte[] sign : signatures) {100// Verification will pass when message update matches with101// the same used for sign102if (verify(sig, kp.getPublic(), MSG, sign, initKey, verifyUpdate)103!= (signUpdate == verifyUpdate)) {104throw new RuntimeException(105"Verification succed with unmatched message");106}107}108System.out.printf("Verified signatures %s times%n", signatures.size());109}110111private static byte[] sign(Signature sig, PrivateKey priKey, String msg,112boolean initKey, int signUpdate) throws Exception {113if (initKey) {114sig.initSign(priKey);115}116for (int update = 0; update < signUpdate; update++) {117sig.update(msg.getBytes());118}119return sig.sign();120}121122private static boolean verify(Signature sig, PublicKey pubKey, String msg,123byte[] sign, boolean initKey, int verifyUpdate) throws Exception {124if (initKey) {125sig.initVerify(pubKey);126}127for (int update = 0; update < verifyUpdate; update++) {128sig.update(msg.getBytes());129}130return sig.verify(sign);131}132}133134135