1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 06:11:08 +01:00

Refactor keytype related classes

This commit is contained in:
Paul Schaub 2020-12-08 20:02:41 +01:00
parent 4550425609
commit 3c88bdde9b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
33 changed files with 340 additions and 151 deletions

View file

@ -53,8 +53,8 @@ import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.generation.type.EllipticCurve;
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.util.UserId;
import org.pgpainless.provider.ProviderFactory;
import org.pgpainless.util.Passphrase;

View file

@ -1,35 +0,0 @@
package org.pgpainless.key.generation.type;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.curve.EdDSACurve;
public class EDDSA implements KeyType {
private final EdDSACurve curve;
private EDDSA(EdDSACurve curve) {
this.curve = curve;
}
public static EDDSA fromCurve(EdDSACurve curve) {
return new EDDSA(curve);
}
@Override
public String getName() {
return "EdDSA";
}
@Override
public PublicKeyAlgorithm getAlgorithm() {
return PublicKeyAlgorithm.EDDSA;
}
@Override
public AlgorithmParameterSpec getAlgorithmSpec() {
return new ECNamedCurveGenParameterSpec(curve.getName());
}
}

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.curve;
package org.pgpainless.key.generation.type;
import javax.annotation.Nonnull;

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.length;
package org.pgpainless.key.generation.type;
public interface KeyLength {

View file

@ -18,8 +18,10 @@ package org.pgpainless.key.generation.type;
import java.security.spec.AlgorithmParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.generation.type.ecdh.ECDH;
import org.pgpainless.key.generation.type.ecdsa.ECDSA;
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.generation.type.rsa.RSA;
public interface KeyType {

View file

@ -1,35 +0,0 @@
package org.pgpainless.key.generation.type;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.curve.XDHCurve;
public class XDH implements KeyType {
private XDHCurve curve;
private XDH(XDHCurve curve) {
this.curve = curve;
}
public static XDH fromCurve(XDHCurve curve) {
return new XDH(curve);
}
@Override
public String getName() {
return "XDH";
}
@Override
public PublicKeyAlgorithm getAlgorithm() {
return PublicKeyAlgorithm.ECDH;
}
@Override
public AlgorithmParameterSpec getAlgorithmSpec() {
return new ECNamedCurveGenParameterSpec(curve.getName());
}
}

View file

@ -1,18 +0,0 @@
package org.pgpainless.key.generation.type.curve;
import javax.annotation.Nonnull;
public enum EdDSACurve {
_Ed25519("ed25519"),
;
final String name;
EdDSACurve(@Nonnull String curveName) {
this.name = curveName;
}
public String getName() {
return name;
}
}

View file

@ -1,18 +0,0 @@
package org.pgpainless.key.generation.type.curve;
import javax.annotation.Nonnull;
public enum XDHCurve {
_X25519("X25519"),
;
final String name;
XDHCurve(@Nonnull String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright 2018 Paul Schaub.
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,20 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type;
package org.pgpainless.key.generation.type.ecdh;
import javax.annotation.Nonnull;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.EllipticCurve;
public class ECDH implements KeyType {
public final class ECDH implements KeyType {
private final EllipticCurve curve;
ECDH(EllipticCurve curve) {
private ECDH(EllipticCurve curve) {
this.curve = curve;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright 2018 Paul Schaub.
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,6 +14,6 @@
* limitations under the License.
*/
/**
* Classes related to elliptic curve cryptography.
* Classes related to ECDH.
*/
package org.pgpainless.key.generation.type.curve;
package org.pgpainless.key.generation.type.ecdh;

View file

@ -13,18 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type;
package org.pgpainless.key.generation.type.ecdsa;
import java.security.spec.AlgorithmParameterSpec;
import javax.annotation.Nonnull;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.curve.EllipticCurve;
import org.pgpainless.key.generation.type.EllipticCurve;
import org.pgpainless.key.generation.type.KeyType;
public class ECDSA extends ECDH {
public final class ECDSA implements KeyType {
ECDSA(@Nonnull EllipticCurve curve) {
super(curve);
private final EllipticCurve curve;
private ECDSA(@Nonnull EllipticCurve curve) {
this.curve = curve;
}
public static ECDSA fromCurve(@Nonnull EllipticCurve curve) {
@ -40,4 +45,9 @@ public class ECDSA extends ECDH {
public PublicKeyAlgorithm getAlgorithm() {
return PublicKeyAlgorithm.ECDSA;
}
@Override
public AlgorithmParameterSpec getAlgorithmSpec() {
return new ECNamedCurveGenParameterSpec(curve.getName());
}
}

View file

@ -0,0 +1,19 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes related to ECDSA.
*/
package org.pgpainless.key.generation.type.ecdsa;

View file

@ -0,0 +1,50 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.eddsa;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.KeyType;
public final class EdDSA implements KeyType {
private final EdDSACurve curve;
private EdDSA(EdDSACurve curve) {
this.curve = curve;
}
public static EdDSA fromCurve(EdDSACurve curve) {
return new EdDSA(curve);
}
@Override
public String getName() {
return "EdDSA";
}
@Override
public PublicKeyAlgorithm getAlgorithm() {
return PublicKeyAlgorithm.EDDSA;
}
@Override
public AlgorithmParameterSpec getAlgorithmSpec() {
return new ECNamedCurveGenParameterSpec(curve.getName());
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.eddsa;
import javax.annotation.Nonnull;
public enum EdDSACurve {
_Ed25519("ed25519"),
;
final String name;
EdDSACurve(@Nonnull String curveName) {
this.name = curveName;
}
public String getName() {
return name;
}
}

View file

@ -0,0 +1,19 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes related to EdDSA.
*/
package org.pgpainless.key.generation.type.eddsa;

View file

@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.length;
package org.pgpainless.key.generation.type.elgamal;
import java.math.BigInteger;
import org.pgpainless.key.generation.type.KeyLength;
/**
* The following primes are taken from RFC-3526.
*

View file

@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type;
package org.pgpainless.key.generation.type.elgamal;
import javax.annotation.Nonnull;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.length.ElGamalLength;
public class ElGamal_ENCRYPT extends ElGamal_GENERAL {

View file

@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type;
package org.pgpainless.key.generation.type.elgamal;
import javax.annotation.Nonnull;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ElGamalParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.length.ElGamalLength;
import org.pgpainless.key.generation.type.KeyType;
public class ElGamal_GENERAL implements KeyType {

View file

@ -0,0 +1,19 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes related to ElGamal.
*/
package org.pgpainless.key.generation.type.elgamal;

View file

@ -15,6 +15,8 @@
*/
package org.pgpainless.key.generation.type.length;
import org.pgpainless.key.generation.type.KeyLength;
public enum DiffieHellmanLength implements KeyLength {
_1024(1024),

View file

@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type;
package org.pgpainless.key.generation.type.rsa;
import javax.annotation.Nonnull;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.RSAKeyGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.key.generation.type.KeyType;
/**
* Key type that specifies the RSA_GENERAL algorithm.

View file

@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.length;
package org.pgpainless.key.generation.type.rsa;
import org.pgpainless.key.generation.type.KeyLength;
public enum RsaLength implements KeyLength {
@Deprecated

View file

@ -0,0 +1,19 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes related to RSA.
*/
package org.pgpainless.key.generation.type.rsa;

View file

@ -0,0 +1,50 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.xdh;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.KeyType;
public final class XDH implements KeyType {
private final XDHCurve curve;
private XDH(XDHCurve curve) {
this.curve = curve;
}
public static XDH fromCurve(XDHCurve curve) {
return new XDH(curve);
}
@Override
public String getName() {
return "XDH";
}
@Override
public PublicKeyAlgorithm getAlgorithm() {
return PublicKeyAlgorithm.ECDH;
}
@Override
public AlgorithmParameterSpec getAlgorithmSpec() {
return new ECNamedCurveGenParameterSpec(curve.getName());
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.generation.type.xdh;
import javax.annotation.Nonnull;
public enum XDHCurve {
_X25519("X25519"),
;
final String name;
XDHCurve(@Nonnull String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View file

@ -0,0 +1,19 @@
/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes related to Diffie-Hellman on the X25519 curve.
*/
package org.pgpainless.key.generation.type.xdh;