1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 03:17:58 +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;

View file

@ -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;

View file

@ -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;

View file

@ -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
}
}

View file

@ -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 {

View file

@ -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;
/**

View file

@ -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;

View file

@ -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 {