-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathEdDSAKeyFactory.java
More file actions
267 lines (236 loc) · 10.5 KB
/
EdDSAKeyFactory.java
File metadata and controls
267 lines (236 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright IBM Corp. 2023, 2024
*
* This code is free software; you can redistribute it and/or modify it
* under the terms provided by IBM in the LICENSE file that accompanied
* this code, including the "Classpath" Exception described therein.
*/
package com.ibm.crypto.plus.provider;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactorySpi;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.EdECPrivateKey;
import java.security.interfaces.EdECPublicKey;
import java.security.spec.EdECPrivateKeySpec;
import java.security.spec.EdECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.NamedParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
public class EdDSAKeyFactory extends KeyFactorySpi {
private NamedParameterSpec params = null;
private OpenJCEPlusProvider provider = null;
private String configAlgName = "EdDSA";
private EdDSAKeyFactory(OpenJCEPlusProvider provider, NamedParameterSpec paramSpec, String algName) {
super();
this.params = paramSpec;
this.provider = provider;
this.configAlgName = algName;
}
EdDSAKeyFactory(OpenJCEPlusProvider provider) {
super();
this.provider = provider;
}
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("Key must not be null");
}
if (key instanceof EdECPublicKey) {
EdECPublicKey publicKey = (EdECPublicKey) key;
NamedParameterSpec params = publicKey.getParams();
checkLockedParams(params);
if (key instanceof com.ibm.crypto.plus.provider.EdDSAPublicKeyImpl) {
return key;
} else {
try {
return new EdDSAPublicKeyImpl(provider, params, publicKey.getPoint(), configAlgName);
} catch (InvalidAlgorithmParameterException iape) {
throw new InvalidKeyException(iape);
}
}
} else if (key instanceof EdECPrivateKey) {
EdDSAPrivateKeyImpl privKey = null;
EdECPrivateKey privateKey = (EdECPrivateKey) key;
NamedParameterSpec params = privateKey.getParams();
checkLockedParams(params);
byte[] privateKeyBytes = privateKey.getBytes().get();
if (privateKeyBytes == null) {
throw new InvalidKeyException("No private key data");
}
if (key instanceof com.ibm.crypto.plus.provider.EdDSAPrivateKeyImpl) {
privKey = (com.ibm.crypto.plus.provider.EdDSAPrivateKeyImpl) key;
} else {
try {
privKey = new EdDSAPrivateKeyImpl(provider, params, privateKeyBytes, this.configAlgName);
} catch (InvalidAlgorithmParameterException iape) {
throw new InvalidKeyException(iape);
}
}
return privKey;
} else if (key instanceof PublicKey && key.getFormat().equals("X.509")) {
EdDSAPublicKeyImpl result = new EdDSAPublicKeyImpl(provider, key.getEncoded(), configAlgName);
checkLockedParams(result.getParams());
return result;
} else if (key instanceof PrivateKey && key.getFormat().equals("PKCS#8")) {
byte[] encoded = key.getEncoded();
try {
EdDSAPrivateKeyImpl result = new EdDSAPrivateKeyImpl(provider, encoded, this.configAlgName);
checkLockedParams(result.getParams());
return result;
} catch (Exception e) {
throw new InvalidKeyException("Unsupported key type or format");
} finally {
Arrays.fill(encoded, (byte) 0);
}
} else {
throw new InvalidKeyException("Unsupported key type or format");
}
}
private void checkLockedParams(NamedParameterSpec spec) throws InvalidKeyException {
if (this.params != null && !this.params.getName().equals(spec.getName())) {
throw new InvalidKeyException("Wrong algorithm type");
}
}
@Override
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
try {
return generatePublicImpl(keySpec);
} catch (InvalidKeyException ex) {
throw new InvalidKeySpecException(ex);
}
}
@Override
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
try {
return generatePrivateImpl(keySpec);
} catch (InvalidKeyException ex) {
throw new InvalidKeySpecException(ex);
}
}
private PublicKey generatePublicImpl(KeySpec keySpec)
throws InvalidKeyException, InvalidKeySpecException {
if (keySpec instanceof X509EncodedKeySpec) {
X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
EdDSAPublicKeyImpl result = new EdDSAPublicKeyImpl(provider, x509Spec.getEncoded(), this.configAlgName);
checkLockedParams(result.getParams());
return result;
} else if (keySpec instanceof EdECPublicKeySpec) {
EdECPublicKeySpec publicKeySpec = (EdECPublicKeySpec) keySpec;
NamedParameterSpec params = publicKeySpec.getParams();
checkLockedParams(params);
try {
return new EdDSAPublicKeyImpl(provider, params, publicKeySpec.getPoint(), this.configAlgName);
} catch (InvalidAlgorithmParameterException iape) {
throw new InvalidKeySpecException(iape);
}
} else {
throw new InvalidKeySpecException(
"Only X509EncodedKeySpec and EdECPublicKeySpec are supported");
}
}
private PrivateKey generatePrivateImpl(KeySpec keySpec)
throws InvalidKeyException, InvalidKeySpecException {
if (keySpec instanceof PKCS8EncodedKeySpec) {
PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec) keySpec;
byte[] encoded = pkcsSpec.getEncoded();
try {
EdDSAPrivateKeyImpl result = new EdDSAPrivateKeyImpl(provider, encoded, this.configAlgName);
checkLockedParams(result.getParams());
return result;
} catch (Exception e) {
throw new InvalidKeyException("Unsupported key type or format");
} finally {
Arrays.fill(encoded, (byte) 0);
}
} else if (keySpec instanceof EdECPrivateKeySpec) {
EdECPrivateKeySpec privateKeySpec = (EdECPrivateKeySpec) keySpec;
NamedParameterSpec params = privateKeySpec.getParams();
checkLockedParams(params);
byte[] bytes = privateKeySpec.getBytes();
try {
return new EdDSAPrivateKeyImpl(provider, params, bytes, this.configAlgName);
} catch (InvalidAlgorithmParameterException iape) {
throw new InvalidKeySpecException(iape);
} finally {
Arrays.fill(bytes, (byte) 0);
}
} else {
throw new InvalidKeySpecException(
"Only PKCS8EncodedKeySpec and EdECPrivateKeySpec supported");
}
}
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
throws InvalidKeySpecException {
if (key instanceof EdECPublicKey) {
try {
checkLockedParams(((EdECPublicKey) key).getParams());
} catch (InvalidKeyException ex) {
throw new InvalidKeySpecException(ex);
}
if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {
if (!key.getFormat().equals("X.509")) {
throw new InvalidKeySpecException("Format is not X.509");
}
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
} else if (keySpec.isAssignableFrom(EdECPublicKeySpec.class)) {
EdECPublicKey edKey = (EdECPublicKey) key;
return keySpec.cast(new EdECPublicKeySpec(edKey.getParams(), edKey.getPoint()));
} else {
throw new InvalidKeySpecException(
"KeySpec must be X509EncodedKeySpec or EdECPublicKeySpec");
}
} else if (key instanceof EdECPrivateKey) {
try {
checkLockedParams(((EdECPrivateKey) key).getParams());
} catch (InvalidKeyException ex) {
throw new InvalidKeySpecException(ex);
}
if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) {
if (!key.getFormat().equals("PKCS#8")) {
throw new InvalidKeySpecException("Format is not PKCS#8");
}
byte[] encoded = key.getEncoded();
try {
return keySpec.cast(new PKCS8EncodedKeySpec(encoded));
} finally {
Arrays.fill(encoded, (byte) 0);
}
} else if (keySpec.isAssignableFrom(EdECPrivateKeySpec.class)) {
EdECPrivateKey edKey = (EdECPrivateKey) key;
byte[] scalar = edKey.getBytes()
.orElseThrow(() -> new InvalidKeySpecException("No private key value"));
try {
return keySpec.cast(new EdECPrivateKeySpec(edKey.getParams(), scalar));
} finally {
Arrays.fill(scalar, (byte) 0);
}
} else {
throw new InvalidKeySpecException(
"KeySpec must be PKCS8EncodedKeySpec or EdECPrivateKeySpec");
}
} else {
throw new InvalidKeySpecException("Unsupported key type");
}
}
public static final class Ed25519 extends EdDSAKeyFactory {
public Ed25519(OpenJCEPlusProvider provider) {
super(provider, new NamedParameterSpec("Ed25519"), "Ed25519");
}
}
public static final class Ed448 extends EdDSAKeyFactory {
public Ed448(OpenJCEPlusProvider provider) {
super(provider, new NamedParameterSpec("Ed448"), "Ed448");
}
}
public static final class EdDSA extends EdDSAKeyFactory {
public EdDSA(OpenJCEPlusProvider provider) {
super(provider);
}
}
}