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.InvalidKeyException;
|
|
30 |
import java.security.KeyRep;
|
|
31 |
import java.security.PublicKey;
|
|
32 |
import java.security.interfaces.XECPublicKey;
|
|
33 |
import java.security.spec.AlgorithmParameterSpec;
|
|
34 |
import java.security.spec.NamedParameterSpec;
|
|
35 |
import java.util.Arrays;
|
|
36 |
|
|
37 |
import sun.security.util.BitArray;
|
|
38 |
import sun.security.x509.AlgorithmId;
|
|
39 |
import sun.security.x509.X509Key;
|
|
40 |
|
|
41 |
public final class XDHPublicKeyImpl extends X509Key implements XECPublicKey {
|
|
42 |
|
|
43 |
private static final long serialVersionUID = 1L;
|
|
44 |
|
|
45 |
private final BigInteger u;
|
|
46 |
private final NamedParameterSpec paramSpec;
|
|
47 |
|
|
48 |
XDHPublicKeyImpl(XECParameters params, BigInteger u)
|
|
49 |
throws InvalidKeyException {
|
|
50 |
|
|
51 |
this.paramSpec = new NamedParameterSpec(params.getName());
|
|
52 |
this.algid = new AlgorithmId(params.getOid());
|
|
53 |
this.u = u.mod(params.getP());
|
|
54 |
|
|
55 |
byte[] u_arr = this.u.toByteArray();
|
|
56 |
reverse(u_arr);
|
|
57 |
// u_arr may be too large or too small, depending on the value of u
|
|
58 |
u_arr = Arrays.copyOf(u_arr, params.getBytes());
|
|
59 |
|
|
60 |
setKey(new BitArray(u_arr.length * 8, u_arr));
|
|
61 |
|
|
62 |
checkLength(params);
|
|
63 |
}
|
|
64 |
|
|
65 |
XDHPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
|
|
66 |
decode(encoded);
|
|
67 |
|
|
68 |
XECParameters params =
|
|
69 |
XECParameters.get(InvalidKeyException::new, algid);
|
|
70 |
this.paramSpec = new NamedParameterSpec(params.getName());
|
|
71 |
// construct the BigInteger representation
|
|
72 |
byte[] u_arr = getKey().toByteArray();
|
|
73 |
reverse(u_arr);
|
|
74 |
|
|
75 |
// clear the extra bits
|
|
76 |
int bitsMod8 = params.getBits() % 8;
|
|
77 |
if (bitsMod8 != 0) {
|
|
78 |
int mask = (1 << bitsMod8) - 1;
|
|
79 |
u_arr[0] &= mask;
|
|
80 |
}
|
|
81 |
|
|
82 |
this.u = new BigInteger(1, u_arr);
|
|
83 |
|
|
84 |
checkLength(params);
|
|
85 |
}
|
|
86 |
|
|
87 |
void checkLength(XECParameters params) throws InvalidKeyException {
|
|
88 |
|
|
89 |
if (params.getBytes() * 8 != getKey().length()) {
|
|
90 |
throw new InvalidKeyException(
|
|
91 |
"key length must be " + params.getBytes());
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
@Override
|
|
96 |
public BigInteger getU() {
|
|
97 |
return u;
|
|
98 |
}
|
|
99 |
|
|
100 |
@Override
|
|
101 |
public AlgorithmParameterSpec getParams() {
|
|
102 |
return paramSpec;
|
|
103 |
}
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public String getAlgorithm() {
|
|
107 |
return "XDH";
|
|
108 |
}
|
|
109 |
|
|
110 |
protected Object writeReplace() throws java.io.ObjectStreamException {
|
|
111 |
return new KeyRep(KeyRep.Type.PUBLIC,
|
|
112 |
getAlgorithm(),
|
|
113 |
getFormat(),
|
|
114 |
getEncoded());
|
|
115 |
}
|
|
116 |
|
|
117 |
private static void swap(byte[] arr, int i, int j) {
|
|
118 |
byte tmp = arr[i];
|
|
119 |
arr[i] = arr[j];
|
|
120 |
arr[j] = tmp;
|
|
121 |
}
|
|
122 |
|
|
123 |
private static void reverse(byte [] arr) {
|
|
124 |
int i = 0;
|
|
125 |
int j = arr.length - 1;
|
|
126 |
|
|
127 |
while (i < j) {
|
|
128 |
swap(arr, i, j);
|
|
129 |
i++;
|
|
130 |
j--;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|