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