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