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/XDHPrivateKeyImpl.java
41161 views
1
/*
2
* Copyright (c) 2018, 2021, 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.io.*;
29
import java.security.interfaces.XECPrivateKey;
30
import java.util.Optional;
31
import java.security.*;
32
import java.security.spec.*;
33
34
import sun.security.pkcs.PKCS8Key;
35
import sun.security.x509.AlgorithmId;
36
import sun.security.util.*;
37
38
public final class XDHPrivateKeyImpl extends PKCS8Key implements XECPrivateKey {
39
40
private static final long serialVersionUID = 1L;
41
42
private final AlgorithmParameterSpec paramSpec;
43
private byte[] k;
44
45
XDHPrivateKeyImpl(XECParameters params, byte[] k)
46
throws InvalidKeyException {
47
48
this.paramSpec = new NamedParameterSpec(params.getName());
49
this.k = k.clone();
50
51
this.algid = new AlgorithmId(params.getOid());
52
53
DerValue val = new DerValue(DerValue.tag_OctetString, k);
54
try {
55
this.key = val.toByteArray();
56
} catch (IOException ex) {
57
throw new AssertionError("Should not happen", ex);
58
} finally {
59
val.clear();
60
}
61
checkLength(params);
62
}
63
64
XDHPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {
65
super(encoded);
66
XECParameters params = XECParameters.get(
67
InvalidKeyException::new, algid);
68
paramSpec = new NamedParameterSpec(params.getName());
69
try {
70
DerInputStream derStream = new DerInputStream(key);
71
k = derStream.getOctetString();
72
} catch (IOException ex) {
73
throw new InvalidKeyException(ex);
74
}
75
checkLength(params);
76
}
77
78
void checkLength(XECParameters params) throws InvalidKeyException {
79
80
if (params.getBytes() != this.k.length) {
81
throw new InvalidKeyException(
82
"key length must be " + params.getBytes());
83
}
84
}
85
86
public byte[] getK() {
87
return k.clone();
88
}
89
90
@Override
91
public String getAlgorithm() {
92
return "XDH";
93
}
94
95
@Override
96
public AlgorithmParameterSpec getParams() {
97
return paramSpec;
98
}
99
100
@Override
101
public Optional<byte[]> getScalar() {
102
return Optional.of(getK());
103
}
104
}
105
106
107