1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-16 16:44:50 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/key/generation/type/KeyType.java

119 lines
3.5 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.generation.type;
2018-06-02 21:21:35 +02:00
import java.security.spec.AlgorithmParameterSpec;
2018-06-02 21:21:35 +02:00
import org.pgpainless.algorithm.PublicKeyAlgorithm;
2020-12-11 18:16:31 +01:00
import org.pgpainless.key.generation.type.ecc.EllipticCurve;
import org.pgpainless.key.generation.type.ecc.ecdh.ECDH;
import org.pgpainless.key.generation.type.ecc.ecdsa.ECDSA;
import org.pgpainless.key.generation.type.eddsa.EdDSA;
import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
2020-12-08 20:02:41 +01:00
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.generation.type.rsa.RSA;
2020-12-11 22:09:49 +01:00
import org.pgpainless.key.generation.type.xdh.XDH;
2021-05-31 13:59:56 +02:00
import org.pgpainless.key.generation.type.xdh.XDHSpec;
2018-06-02 21:21:35 +02:00
public interface KeyType {
2018-06-02 21:21:35 +02:00
2020-12-11 18:53:10 +01:00
/**
* Return the encryption algorithm name.
*
* @return algorithm name.
*/
2018-06-02 21:21:35 +02:00
String getName();
2020-12-11 18:53:10 +01:00
/**
* Return the public key algorithm.
*
* @return public key algorithm
*/
2018-06-02 21:21:35 +02:00
PublicKeyAlgorithm getAlgorithm();
/**
* Return the strength of the key in bits.
2021-12-28 13:32:50 +01:00
* @return strength of the key in bits
*/
int getBitStrength();
2020-12-11 18:53:10 +01:00
/**
* Return an implementation of {@link AlgorithmParameterSpec} that can be used to generate the key.
*
* @return algorithm parameter spec
*/
AlgorithmParameterSpec getAlgorithmSpec();
2020-11-07 18:24:12 +01:00
2020-12-11 18:53:10 +01:00
/**
* Return true if the key that is generated from this type is able to carry the SIGN_DATA key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#SIGN_DATA}.
*
* @return true if the key can sign.
*/
2021-04-26 13:38:12 +02:00
default boolean canSign() {
return getAlgorithm().isSigningCapable();
}
/**
* Return true if the key that is generated from this type is able to carry the CERTIFY_OTHER key flag.
2020-12-11 18:53:10 +01:00
* See {@link org.pgpainless.algorithm.KeyFlag#CERTIFY_OTHER}.
*
* @return true if the key is able to certify other keys
*/
default boolean canCertify() {
return canSign();
}
/**
* Return true if the key that is generated from this type is able to carry the AUTHENTICATION key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#AUTHENTICATION}.
*
* @return true if the key is able to be used for authentication purposes.
2020-12-11 18:53:10 +01:00
*/
default boolean canAuthenticate() {
return canSign();
}
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_COMMS key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}.
*
* @return true if the key can encrypt communication
*/
2021-04-26 13:38:12 +02:00
default boolean canEncryptCommunication() {
return getAlgorithm().isEncryptionCapable();
}
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_STORAGE key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_STORAGE}.
*
* @return true if the key can encrypt for storage
*/
default boolean canEncryptStorage() {
2021-05-03 14:11:59 +02:00
return getAlgorithm().isEncryptionCapable();
}
2020-12-11 18:16:31 +01:00
2020-11-07 18:24:12 +01:00
static KeyType RSA(RsaLength length) {
return RSA.withLength(length);
}
static KeyType ECDH(EllipticCurve curve) {
return ECDH.fromCurve(curve);
}
static KeyType ECDSA(EllipticCurve curve) {
return ECDSA.fromCurve(curve);
}
2020-12-11 18:16:31 +01:00
static KeyType EDDSA(EdDSACurve curve) {
return EdDSA.fromCurve(curve);
}
2020-12-11 22:09:49 +01:00
2021-05-31 13:59:56 +02:00
static KeyType XDH(XDHSpec curve) {
return XDH.fromSpec(curve);
2020-12-11 22:09:49 +01:00
}
2018-06-02 21:21:35 +02:00
}