src/jdk.crypto.ec/share/classes/sun/security/ec/XDHKeyPairGenerator.java
author bpb
Fri, 14 Sep 2018 09:00:22 -0700
changeset 51746 07ae9da7a230
parent 50053 9bc1e6487cbb
child 56589 bafd8be2f970
permissions -rw-r--r--
8210741: Typo in Java API documentation of java.nio.file.Paths Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     1
/*
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     4
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    10
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    15
 * accompanied this code).
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    16
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    20
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    23
 * questions.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    24
 */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    25
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    26
package sun.security.ec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    27
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    28
import java.math.BigInteger;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    29
import java.security.KeyPairGeneratorSpi;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    30
import java.security.InvalidKeyException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    31
import java.security.InvalidParameterException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    32
import java.security.InvalidAlgorithmParameterException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    33
import java.security.KeyPair;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    34
import java.security.ProviderException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    35
import java.security.SecureRandom;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    37
import java.security.spec.NamedParameterSpec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    38
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    39
import sun.security.jca.JCAUtil;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    40
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    41
/**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    42
 * Key pair generator for the XDH key agreement algorithm.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    43
 */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    44
public class XDHKeyPairGenerator extends KeyPairGeneratorSpi {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    45
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    46
    private static final NamedParameterSpec DEFAULT_PARAM_SPEC
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    47
        = NamedParameterSpec.X25519;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    48
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    49
    private SecureRandom random = null;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    50
    private XECOperations ops = null;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    51
    private XECParameters lockedParams = null;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    52
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    53
    XDHKeyPairGenerator() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    54
        tryInitialize(DEFAULT_PARAM_SPEC);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    55
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    56
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    57
    private XDHKeyPairGenerator(NamedParameterSpec paramSpec) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    58
        tryInitialize(paramSpec);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    59
        lockedParams = ops.getParameters();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    60
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    61
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    62
    private void tryInitialize(NamedParameterSpec paramSpec) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    63
        try {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    64
            initialize(paramSpec, null);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    65
        } catch (InvalidAlgorithmParameterException ex) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    66
            String name = paramSpec.getName();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    67
            throw new ProviderException(name + " not supported");
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    68
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    69
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    70
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    71
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    72
    public void initialize(int keySize, SecureRandom random) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    73
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    74
        XECParameters params = XECParameters.getBySize(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    75
            InvalidParameterException::new, keySize);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    76
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    77
        initializeImpl(params, random);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    78
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    79
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    80
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    81
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    82
            throws InvalidAlgorithmParameterException {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    83
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    84
        XECParameters xecParams = XECParameters.get(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    85
            InvalidAlgorithmParameterException::new, params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    86
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    87
        initializeImpl(xecParams, random);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    88
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    89
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    90
    private void initializeImpl(XECParameters params, SecureRandom random) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    91
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    92
        if (lockedParams != null && lockedParams != params) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    93
            throw new InvalidParameterException("Parameters must be " +
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    94
                lockedParams.getName());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    95
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    96
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    97
        this.ops = new XECOperations(params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    98
        this.random = random == null ? JCAUtil.getSecureRandom() : random;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    99
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   100
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   101
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   102
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   103
    public KeyPair generateKeyPair() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   104
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   105
        byte[] privateKey = ops.generatePrivate(random);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   106
        // computePublic may modify the private key, so clone it first
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   107
        BigInteger publicKey = ops.computePublic(privateKey.clone());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   108
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   109
        try {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   110
            return new KeyPair(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   111
                new XDHPublicKeyImpl(ops.getParameters(), publicKey),
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   112
                new XDHPrivateKeyImpl(ops.getParameters(), privateKey)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   113
            );
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   114
        } catch (InvalidKeyException ex) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   115
            throw new ProviderException(ex);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   116
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   117
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   118
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   119
    static class X25519 extends XDHKeyPairGenerator {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   120
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   121
        public X25519() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   122
            super(NamedParameterSpec.X25519);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   123
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   124
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   125
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   126
    static class X448 extends XDHKeyPairGenerator {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   127
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   128
        public X448() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   129
            super(NamedParameterSpec.X448);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   130
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   131
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   132
}