Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAPublicKeyImpl.java
41162 views
1
/*
2
* Copyright (c) 2020, 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.ed;
27
28
import java.math.BigInteger;
29
import java.security.InvalidKeyException;
30
import java.security.KeyRep;
31
import java.security.interfaces.EdECPublicKey;
32
import java.security.spec.EdECPoint;
33
import java.security.spec.NamedParameterSpec;
34
import java.util.Arrays;
35
36
import sun.security.util.BitArray;
37
import sun.security.x509.AlgorithmId;
38
import sun.security.x509.X509Key;
39
40
public final class EdDSAPublicKeyImpl extends X509Key implements EdECPublicKey {
41
42
private static final long serialVersionUID = 1L;
43
44
private final EdECPoint point;
45
private final NamedParameterSpec paramSpec;
46
47
public EdDSAPublicKeyImpl(EdDSAParameters params, EdECPoint point)
48
throws InvalidKeyException {
49
this.paramSpec = new NamedParameterSpec(params.getName());
50
this.algid = new AlgorithmId(params.getOid());
51
this.point = point;
52
53
byte[] encodedPoint = point.getY().toByteArray();
54
reverse(encodedPoint);
55
// array may be too large or too small, depending on the value
56
encodedPoint = Arrays.copyOf(encodedPoint, params.getKeyLength());
57
// set the high-order bit of the encoded point
58
byte msb = (byte) (point.isXOdd() ? 0x80 : 0);
59
encodedPoint[encodedPoint.length - 1] |= msb;
60
setKey(new BitArray(encodedPoint.length * 8, encodedPoint));
61
62
checkLength(params);
63
}
64
65
public EdDSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
66
decode(encoded);
67
68
EdDSAParameters params =
69
EdDSAParameters.get(InvalidKeyException::new, algid);
70
this.paramSpec = new NamedParameterSpec(params.getName());
71
// construct the EdECPoint representation
72
byte[] encodedPoint = getKey().toByteArray();
73
byte msb = encodedPoint[encodedPoint.length - 1];
74
encodedPoint[encodedPoint.length - 1] &= (byte) 0x7F;
75
boolean xOdd = (msb & 0x80) != 0;
76
reverse(encodedPoint);
77
BigInteger y = new BigInteger(1, encodedPoint);
78
this.point = new EdECPoint(xOdd, y);
79
80
checkLength(params);
81
}
82
83
void checkLength(EdDSAParameters params) throws InvalidKeyException {
84
if (params.getKeyLength() * 8 != getKey().length()) {
85
throw new InvalidKeyException(
86
"key length must be " + params.getKeyLength());
87
}
88
}
89
90
public byte[] getEncodedPoint() {
91
return getKey().toByteArray();
92
}
93
94
@Override
95
public EdECPoint getPoint() {
96
return point;
97
}
98
99
@Override
100
public NamedParameterSpec getParams() {
101
return paramSpec;
102
}
103
104
@Override
105
public String getAlgorithm() {
106
return "EdDSA";
107
}
108
109
protected Object writeReplace() throws java.io.ObjectStreamException {
110
return new KeyRep(KeyRep.Type.PUBLIC, getAlgorithm(), getFormat(),
111
getEncoded());
112
}
113
114
private static void swap(byte[] arr, int i, int j) {
115
byte tmp = arr[i];
116
arr[i] = arr[j];
117
arr[j] = tmp;
118
}
119
120
private static void reverse(byte [] arr) {
121
int i = 0;
122
int j = arr.length - 1;
123
124
while (i < j) {
125
swap(arr, i, j);
126
i++;
127
j--;
128
}
129
}
130
}
131
132