Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHKeyPairGenerator.java
41161 views
/*1* Copyright (c) 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ec;2627import java.math.BigInteger;28import java.security.KeyPairGeneratorSpi;29import java.security.InvalidKeyException;30import java.security.InvalidParameterException;31import java.security.InvalidAlgorithmParameterException;32import java.security.KeyPair;33import java.security.ProviderException;34import java.security.SecureRandom;35import java.security.spec.AlgorithmParameterSpec;36import java.security.spec.NamedParameterSpec;37import java.util.Arrays;3839import sun.security.jca.JCAUtil;4041/**42* Key pair generator for the XDH key agreement algorithm.43*/44public class XDHKeyPairGenerator extends KeyPairGeneratorSpi {4546private static final NamedParameterSpec DEFAULT_PARAM_SPEC47= NamedParameterSpec.X25519;4849private SecureRandom random = null;50private XECOperations ops = null;51private XECParameters lockedParams = null;5253XDHKeyPairGenerator() {54tryInitialize(DEFAULT_PARAM_SPEC);55}5657private XDHKeyPairGenerator(NamedParameterSpec paramSpec) {58tryInitialize(paramSpec);59lockedParams = ops.getParameters();60}6162private void tryInitialize(NamedParameterSpec paramSpec) {63try {64initialize(paramSpec, null);65} catch (InvalidAlgorithmParameterException ex) {66String name = paramSpec.getName();67throw new ProviderException(name + " not supported");68}69}7071@Override72public void initialize(int keySize, SecureRandom random) {7374XECParameters params = XECParameters.getBySize(75InvalidParameterException::new, keySize);7677initializeImpl(params, random);78}7980@Override81public void initialize(AlgorithmParameterSpec params, SecureRandom random)82throws InvalidAlgorithmParameterException {8384XECParameters xecParams = XECParameters.get(85InvalidAlgorithmParameterException::new, params);8687initializeImpl(xecParams, random);88}8990private void initializeImpl(XECParameters params, SecureRandom random) {9192if (lockedParams != null && lockedParams != params) {93throw new InvalidParameterException("Parameters must be " +94lockedParams.getName());95}9697this.ops = new XECOperations(params);98this.random = random == null ? JCAUtil.getSecureRandom() : random;99}100101102@Override103public KeyPair generateKeyPair() {104105byte[] privateKey = ops.generatePrivate(random);106// computePublic may modify the private key, so clone it first107byte[] cloned = privateKey.clone();108BigInteger publicKey = ops.computePublic(cloned);109Arrays.fill(cloned, (byte)0);110111try {112return new KeyPair(113new XDHPublicKeyImpl(ops.getParameters(), publicKey),114new XDHPrivateKeyImpl(ops.getParameters(), privateKey)115);116} catch (InvalidKeyException ex) {117throw new ProviderException(ex);118} finally {119Arrays.fill(privateKey, (byte)0);120}121}122123static class X25519 extends XDHKeyPairGenerator {124125public X25519() {126super(NamedParameterSpec.X25519);127}128}129130static class X448 extends XDHKeyPairGenerator {131132public X448() {133super(NamedParameterSpec.X448);134}135}136}137138139