pgpainless/pgpainless-core/src/main/java/org/pgpainless/algorithm/PublicKeyAlgorithm.java

142 lines
3.7 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.algorithm;
2018-06-02 21:21:35 +02:00
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
2018-06-02 21:21:35 +02:00
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
2021-04-25 13:28:33 +02:00
/**
* Enumeration of public key algorithms as defined in RFC4880.
*
* @see <a href="https://tools.ietf.org/html/rfc4880#section-9.1">RFC4880: Public-Key Algorithms</a>
*/
2018-06-02 21:21:35 +02:00
public enum PublicKeyAlgorithm {
/**
* RSA capable of encryption and signatures.
*/
2021-04-26 13:38:12 +02:00
RSA_GENERAL (PublicKeyAlgorithmTags.RSA_GENERAL, true, true),
/**
* RSA with usage encryption.
*
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.5
*/
2021-04-25 13:28:33 +02:00
@Deprecated
2021-04-26 13:38:12 +02:00
RSA_ENCRYPT (PublicKeyAlgorithmTags.RSA_ENCRYPT, false, true),
/**
* RSA with usage of creating signatures.
*
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.5
*/
2021-04-25 13:28:33 +02:00
@Deprecated
2021-04-26 13:38:12 +02:00
RSA_SIGN (PublicKeyAlgorithmTags.RSA_SIGN, true, false),
/**
* ElGamal with usage encryption.
*/
2021-04-26 13:38:12 +02:00
ELGAMAL_ENCRYPT (PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT, false, true),
/**
* Digital Signature Algorithm.
*/
2021-04-26 13:38:12 +02:00
DSA (PublicKeyAlgorithmTags.DSA, true, false),
2018-06-02 21:21:35 +02:00
/**
2018-07-02 21:40:59 +02:00
* EC is deprecated.
2018-06-02 21:21:35 +02:00
* @deprecated use {@link #ECDH} instead.
*/
2021-04-25 13:28:33 +02:00
@Deprecated
2021-04-26 13:38:12 +02:00
EC (PublicKeyAlgorithmTags.EC, false, true),
/**
* Elliptic Curve Diffie-Hellman.
*/
2021-04-26 13:38:12 +02:00
ECDH (PublicKeyAlgorithmTags.ECDH, false, true),
/**
* Elliptic Curve Digital Signature Algorithm.
*/
2021-04-26 13:38:12 +02:00
ECDSA (PublicKeyAlgorithmTags.ECDSA, true, false),
2020-12-11 18:14:36 +01:00
/**
* ElGamal General.
*
2020-12-11 18:14:36 +01:00
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.8
*/
@Deprecated
2021-04-26 13:38:12 +02:00
ELGAMAL_GENERAL (PublicKeyAlgorithmTags.ELGAMAL_GENERAL, true, true),
/**
* Diffie-Hellman key exchange algorithm.
*/
2021-04-26 13:38:12 +02:00
DIFFIE_HELLMAN (PublicKeyAlgorithmTags.DIFFIE_HELLMAN, false, true),
/**
* Digital Signature Algorithm based on twisted Edwards Curves.
*/
2021-04-26 13:38:12 +02:00
EDDSA (PublicKeyAlgorithmTags.EDDSA, true, false),
2018-06-02 21:21:35 +02:00
;
private static final Map<Integer, PublicKeyAlgorithm> MAP = new ConcurrentHashMap<>();
2018-06-02 21:21:35 +02:00
static {
for (PublicKeyAlgorithm p : PublicKeyAlgorithm.values()) {
MAP.put(p.algorithmId, p);
}
}
2021-04-25 13:28:33 +02:00
/**
* Return the {@link PublicKeyAlgorithm} that corresponds to the provided algorithm id.
* If an invalid id is provided, null is returned.
*
* @param id numeric algorithm id
* @return algorithm
*/
2018-06-02 21:21:35 +02:00
public static PublicKeyAlgorithm fromId(int id) {
return MAP.get(id);
}
private final int algorithmId;
2021-04-26 13:38:12 +02:00
private final boolean signingCapable;
private final boolean encryptionCapable;
2018-06-02 21:21:35 +02:00
2021-04-26 13:38:12 +02:00
PublicKeyAlgorithm(int algorithmId, boolean signingCapable, boolean encryptionCapable) {
2018-06-02 21:21:35 +02:00
this.algorithmId = algorithmId;
2021-04-26 13:38:12 +02:00
this.signingCapable = signingCapable;
this.encryptionCapable = encryptionCapable;
2018-06-02 21:21:35 +02:00
}
2021-04-25 13:28:33 +02:00
/**
* Return the numeric identifier of the public key algorithm.
*
* @return id
*/
2018-06-02 21:21:35 +02:00
public int getAlgorithmId() {
return algorithmId;
}
2021-04-26 13:38:12 +02:00
/**
* Return true if this public key algorithm is able to create signatures.
*
* @return true if can sign
*/
public boolean isSigningCapable() {
return signingCapable;
}
/**
* Return true if this public key algorithm can be used as an encryption algorithm.
*
* @return true if can encrypt
*/
public boolean isEncryptionCapable() {
return encryptionCapable;
}
2018-06-02 21:21:35 +02:00
}