Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/ECKeyCheck.java
41155 views
/*1* Copyright (c) 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*/2223/*24* @test25* @bug 826150226* @summary Check that ECPrivateKey's that are not ECPrivateKeyImpl can use27* ECDHKeyAgreement28*/2930import javax.crypto.KeyAgreement;31import java.math.BigInteger;32import java.security.KeyPairGenerator;33import java.security.interfaces.ECPrivateKey;34import java.security.interfaces.ECPublicKey;35import java.security.spec.ECGenParameterSpec;36import java.security.spec.ECParameterSpec;3738public class ECKeyCheck {3940public static final void main(String args[]) throws Exception {41ECGenParameterSpec spec = new ECGenParameterSpec("secp256r1");42KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");43kpg.initialize(spec);4445ECPrivateKey privKey = (ECPrivateKey) kpg.generateKeyPair().getPrivate();46ECPublicKey pubKey = (ECPublicKey) kpg.generateKeyPair().getPublic();47generateECDHSecret(privKey, pubKey);48generateECDHSecret(new newPrivateKeyImpl(privKey), pubKey);49}5051private static byte[] generateECDHSecret(ECPrivateKey privKey,52ECPublicKey pubKey) throws Exception {53KeyAgreement ka = KeyAgreement.getInstance("ECDH");54ka.init(privKey);55ka.doPhase(pubKey, true);56return ka.generateSecret();57}5859// Test ECPrivateKey class60private static class newPrivateKeyImpl implements ECPrivateKey {61private ECPrivateKey p;6263newPrivateKeyImpl(ECPrivateKey p) {64this.p = p;65}6667public BigInteger getS() {68return p.getS();69}7071public byte[] getEncoded() {72return p.getEncoded();73}7475public String getFormat() {76return p.getFormat();77}7879public String getAlgorithm() {80return p.getAlgorithm();81}8283public ECParameterSpec getParams() {84return p.getParams();85}86}87}888990