From 3c88bdde9b72aee5fcd0842cfcafdb7c0fde4ffd Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 8 Dec 2020 20:02:41 +0100 Subject: [PATCH] Refactor keytype related classes --- .../key/generation/KeyRingBuilder.java | 4 +- .../pgpainless/key/generation/type/EDDSA.java | 35 ------------- .../type/{curve => }/EllipticCurve.java | 2 +- .../type/{length => }/KeyLength.java | 2 +- .../key/generation/type/KeyType.java | 6 ++- .../pgpainless/key/generation/type/XDH.java | 35 ------------- .../key/generation/type/curve/EdDSACurve.java | 18 ------- .../key/generation/type/curve/XDHCurve.java | 18 ------- .../key/generation/type/{ => ecdh}/ECDH.java | 11 ++-- .../type/{curve => ecdh}/package-info.java | 6 +-- .../generation/type/{ => ecdsa}/ECDSA.java | 20 ++++++-- .../generation/type/ecdsa/package-info.java | 19 +++++++ .../key/generation/type/eddsa/EdDSA.java | 50 +++++++++++++++++++ .../key/generation/type/eddsa/EdDSACurve.java | 33 ++++++++++++ .../generation/type/eddsa/package-info.java | 19 +++++++ .../{length => elgamal}/ElGamalLength.java | 4 +- .../type/{ => elgamal}/ElGamal_ENCRYPT.java | 3 +- .../type/{ => elgamal}/ElGamal_GENERAL.java | 4 +- .../generation/type/elgamal/package-info.java | 19 +++++++ .../type/length/DiffieHellmanLength.java | 2 + .../key/generation/type/{ => rsa}/RSA.java | 4 +- .../type/{length => rsa}/RsaLength.java | 4 +- .../key/generation/type/rsa/package-info.java | 19 +++++++ .../key/generation/type/xdh/XDH.java | 50 +++++++++++++++++++ .../key/generation/type/xdh/XDHCurve.java | 33 ++++++++++++ .../key/generation/type/xdh/package-info.java | 19 +++++++ .../EncryptDecryptTest.java | 6 +-- .../encryption_signing/LengthTest.java | 2 +- .../GenerateEllipticCurveKeyTest.java | 34 +++++++++---- .../GenerateKeyWithAdditionalUserIdTest.java | 2 +- .../GenerateWithEmptyPassphrase.java | 2 +- .../key/modification/AddSubKeyTest.java | 4 +- .../java/org/pgpainless/util/BCUtilTest.java | 2 +- 33 files changed, 340 insertions(+), 151 deletions(-) delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EDDSA.java rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{curve => }/EllipticCurve.java (94%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{length => }/KeyLength.java (92%) delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/XDH.java delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EdDSACurve.java delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/XDHCurve.java rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{ => ecdh}/ECDH.java (82%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{curve => ecdh}/package-info.java (81%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{ => ecdsa}/ECDSA.java (63%) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/package-info.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSA.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSACurve.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/package-info.java rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{length => elgamal}/ElGamalLength.java (98%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{ => elgamal}/ElGamal_ENCRYPT.java (89%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{ => elgamal}/ElGamal_GENERAL.java (93%) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/package-info.java rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{ => rsa}/RSA.java (93%) rename pgpainless-core/src/main/java/org/pgpainless/key/generation/type/{length => rsa}/RsaLength.java (90%) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/package-info.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDH.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDHCurve.java create mode 100644 pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/package-info.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java index 5732cac8..d3426f5f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EDDSA.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EDDSA.java deleted file mode 100644 index 7ba30e8d..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EDDSA.java +++ /dev/null @@ -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()); - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EllipticCurve.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EllipticCurve.java similarity index 94% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EllipticCurve.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EllipticCurve.java index 471eb6bd..7921c6f0 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EllipticCurve.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/EllipticCurve.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/KeyLength.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyLength.java similarity index 92% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/KeyLength.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyLength.java index f624c9fb..35d84170 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/KeyLength.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyLength.java @@ -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 { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java index e12506d3..a39d2a85 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java @@ -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 { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/XDH.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/XDH.java deleted file mode 100644 index ae9361f6..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/XDH.java +++ /dev/null @@ -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()); - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EdDSACurve.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EdDSACurve.java deleted file mode 100644 index 2b7cb7bd..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/EdDSACurve.java +++ /dev/null @@ -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; - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/XDHCurve.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/XDHCurve.java deleted file mode 100644 index 6a35b661..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/XDHCurve.java +++ /dev/null @@ -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; - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDH.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/ECDH.java similarity index 82% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDH.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/ECDH.java index 73f9b81e..a6753cfd 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDH.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/ECDH.java @@ -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; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/package-info.java similarity index 81% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/package-info.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/package-info.java index bf7cff10..b6c623bc 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/curve/package-info.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdh/package-info.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDSA.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/ECDSA.java similarity index 63% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDSA.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/ECDSA.java index 414d9209..a9022f79 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ECDSA.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/ECDSA.java @@ -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()); + } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/package-info.java new file mode 100644 index 00000000..0e96e3e4 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ecdsa/package-info.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSA.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSA.java new file mode 100644 index 00000000..2517f40e --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSA.java @@ -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()); + } +} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSACurve.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSACurve.java new file mode 100644 index 00000000..ca168fcc --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/EdDSACurve.java @@ -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; + } +} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/package-info.java new file mode 100644 index 00000000..de21fc28 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/eddsa/package-info.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/ElGamalLength.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamalLength.java similarity index 98% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/ElGamalLength.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamalLength.java index 61a507d1..648b06ae 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/ElGamalLength.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamalLength.java @@ -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. * diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_ENCRYPT.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_ENCRYPT.java similarity index 89% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_ENCRYPT.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_ENCRYPT.java index bd7ae3aa..cb3008de 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_ENCRYPT.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_ENCRYPT.java @@ -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 { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_GENERAL.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_GENERAL.java similarity index 93% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_GENERAL.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_GENERAL.java index 7d7fd717..f12e0849 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/ElGamal_GENERAL.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/ElGamal_GENERAL.java @@ -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 { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/package-info.java new file mode 100644 index 00000000..3d9a5374 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/elgamal/package-info.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/DiffieHellmanLength.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/DiffieHellmanLength.java index f1953b30..1579b016 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/DiffieHellmanLength.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/DiffieHellmanLength.java @@ -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), diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/RSA.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RSA.java similarity index 93% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/RSA.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RSA.java index 5db1a982..67d6a051 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/RSA.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RSA.java @@ -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. diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/RsaLength.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RsaLength.java similarity index 90% rename from pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/RsaLength.java rename to pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RsaLength.java index 823f67ff..e585d23f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/length/RsaLength.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/RsaLength.java @@ -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 diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/package-info.java new file mode 100644 index 00000000..8937a34d --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/rsa/package-info.java @@ -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; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDH.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDH.java new file mode 100644 index 00000000..31e307ec --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDH.java @@ -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()); + } +} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDHCurve.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDHCurve.java new file mode 100644 index 00000000..04a7731e --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/XDHCurve.java @@ -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; + } +} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/package-info.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/package-info.java new file mode 100644 index 00000000..81b382b1 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/xdh/package-info.java @@ -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; diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java index 7e981261..6be7a8f3 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java @@ -45,10 +45,10 @@ import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.TestKeys; import org.pgpainless.key.generation.KeySpec; -import org.pgpainless.key.generation.type.ElGamal_GENERAL; +import org.pgpainless.key.generation.type.elgamal.ElGamal_GENERAL; import org.pgpainless.key.generation.type.KeyType; -import org.pgpainless.key.generation.type.length.ElGamalLength; -import org.pgpainless.key.generation.type.length.RsaLength; +import org.pgpainless.key.generation.type.elgamal.ElGamalLength; +import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.util.KeyRingUtils; diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/LengthTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/LengthTest.java index c13859bc..26654673 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/LengthTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/LengthTest.java @@ -33,7 +33,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.util.io.Streams; import org.pgpainless.PGPainless; import org.pgpainless.key.TestKeys; -import org.pgpainless.key.generation.type.length.RsaLength; +import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.util.KeyRingUtils; diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateEllipticCurveKeyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateEllipticCurveKeyTest.java index 2a6ac23f..a9d67a69 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateEllipticCurveKeyTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateEllipticCurveKeyTest.java @@ -1,3 +1,18 @@ +/* + * 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; import java.io.IOException; @@ -5,13 +20,13 @@ import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; -import org.pgpainless.key.collection.PGPKeyRing; -import org.pgpainless.key.generation.type.EDDSA; -import org.pgpainless.key.generation.type.XDH; -import org.pgpainless.key.generation.type.curve.EdDSACurve; -import org.pgpainless.key.generation.type.curve.XDHCurve; +import org.pgpainless.key.generation.type.eddsa.EdDSA; +import org.pgpainless.key.generation.type.xdh.XDH; +import org.pgpainless.key.generation.type.eddsa.EdDSACurve; +import org.pgpainless.key.generation.type.xdh.XDHCurve; import org.pgpainless.key.util.UserId; import org.pgpainless.util.ArmorUtils; @@ -19,17 +34,18 @@ public class GenerateEllipticCurveKeyTest { @Test public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { - PGPKeyRing keyRing = PGPainless.generateKeyRing() + PGPSecretKeyRing keyRing = PGPainless.generateKeyRing() .withSubKey(KeySpec.getBuilder(XDH.fromCurve(XDHCurve._X25519)) .withDefaultKeyFlags() .withDefaultAlgorithms()) - .withMasterKey(KeySpec.getBuilder(EDDSA.fromCurve(EdDSACurve._Ed25519)) + .withMasterKey(KeySpec.getBuilder(EdDSA.fromCurve(EdDSACurve._Ed25519)) .withDefaultKeyFlags() .withDefaultAlgorithms()) .withPrimaryUserId(UserId.onlyEmail("alice@wonderland.lit").toString()) .withoutPassphrase() .build(); - - System.out.println(ArmorUtils.toAsciiArmoredString(keyRing.getPublicKeys())); + // CHECKSTYLE:OFF + System.out.println(ArmorUtils.toAsciiArmoredString(keyRing)); + // CHECKSTYLE:ON } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java index 91bf3c27..2aca9b5a 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java @@ -31,7 +31,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.generation.type.KeyType; -import org.pgpainless.key.generation.type.length.RsaLength; +import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.util.KeyRingUtils; public class GenerateKeyWithAdditionalUserIdTest { diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java index 9d32a3b6..305bd734 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateWithEmptyPassphrase.java @@ -25,7 +25,7 @@ import org.bouncycastle.openpgp.PGPException; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.generation.type.KeyType; -import org.pgpainless.key.generation.type.length.RsaLength; +import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.util.Passphrase; /** diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java index 78ac951d..1ed4dd88 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java @@ -34,8 +34,8 @@ import org.pgpainless.PGPainless; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.key.TestKeys; import org.pgpainless.key.generation.KeySpec; -import org.pgpainless.key.generation.type.ECDSA; -import org.pgpainless.key.generation.type.curve.EllipticCurve; +import org.pgpainless.key.generation.type.ecdsa.ECDSA; +import org.pgpainless.key.generation.type.EllipticCurve; import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector; import org.pgpainless.util.Passphrase; diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java index ff8063c5..57f079ee 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java @@ -37,7 +37,7 @@ import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.type.KeyType; -import org.pgpainless.key.generation.type.length.RsaLength; +import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.util.KeyRingUtils; public class BCUtilTest {