| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa; |
| 13 |
|
| 14 |
import java.security.InvalidKeyException; |
| 15 |
import java.security.Key; |
| 16 |
import java.security.KeyFactorySpi; |
| 17 |
import java.security.PrivateKey; |
| 18 |
import java.security.PublicKey; |
| 19 |
import java.security.spec.InvalidKeySpecException; |
| 20 |
import java.security.spec.KeySpec; |
| 21 |
import java.security.spec.PKCS8EncodedKeySpec; |
| 22 |
import java.security.spec.X509EncodedKeySpec; |
| 23 |
|
| 24 |
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec; |
| 25 |
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class KeyFactory extends KeyFactorySpi { |
| 32 |
|
| 33 |
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) |
| 34 |
throws InvalidKeySpecException { |
| 35 |
if (keySpec instanceof EdDSAPrivateKeySpec) { |
| 36 |
return new EdDSAPrivateKey((EdDSAPrivateKeySpec) keySpec); |
| 37 |
} |
| 38 |
if (keySpec instanceof PKCS8EncodedKeySpec) { |
| 39 |
return new EdDSAPrivateKey((PKCS8EncodedKeySpec) keySpec); |
| 40 |
} |
| 41 |
throw new InvalidKeySpecException("key spec not recognised: " + keySpec.getClass()); |
| 42 |
} |
| 43 |
|
| 44 |
protected PublicKey engineGeneratePublic(KeySpec keySpec) |
| 45 |
throws InvalidKeySpecException { |
| 46 |
if (keySpec instanceof EdDSAPublicKeySpec) { |
| 47 |
return new EdDSAPublicKey((EdDSAPublicKeySpec) keySpec); |
| 48 |
} |
| 49 |
if (keySpec instanceof X509EncodedKeySpec) { |
| 50 |
return new EdDSAPublicKey((X509EncodedKeySpec) keySpec); |
| 51 |
} |
| 52 |
throw new InvalidKeySpecException("key spec not recognised: " + keySpec.getClass()); |
| 53 |
} |
| 54 |
|
| 55 |
@SuppressWarnings("unchecked") |
| 56 |
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) |
| 57 |
throws InvalidKeySpecException { |
| 58 |
if (keySpec.isAssignableFrom(EdDSAPublicKeySpec.class) && key instanceof EdDSAPublicKey) { |
| 59 |
EdDSAPublicKey k = (EdDSAPublicKey) key; |
| 60 |
if (k.getParams() != null) { |
| 61 |
return (T) new EdDSAPublicKeySpec(k.getA(), k.getParams()); |
| 62 |
} |
| 63 |
} else if (keySpec.isAssignableFrom(EdDSAPrivateKeySpec.class) && key instanceof EdDSAPrivateKey) { |
| 64 |
EdDSAPrivateKey k = (EdDSAPrivateKey) key; |
| 65 |
if (k.getParams() != null) { |
| 66 |
return (T) new EdDSAPrivateKeySpec(k.getSeed(), k.getH(), k.geta(), k.getA(), k.getParams()); |
| 67 |
} |
| 68 |
} |
| 69 |
throw new InvalidKeySpecException("not implemented yet " + key + " " + keySpec); |
| 70 |
} |
| 71 |
|
| 72 |
protected Key engineTranslateKey(Key key) throws InvalidKeyException { |
| 73 |
throw new InvalidKeyException("No other EdDSA key providers known"); |
| 74 |
} |
| 75 |
} |