1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

PublicKeyAlgorithm: Ask PublicKeyUtils for algorithm capabilities, add persistent symmetric key algorithm ids

This commit is contained in:
Paul Schaub 2025-03-15 12:56:42 +01:00
parent a95ebce07b
commit 6cfa87201b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -4,19 +4,17 @@
package org.pgpainless.algorithm package org.pgpainless.algorithm
import org.bouncycastle.bcpg.PublicKeyUtils
/** /**
* Enumeration of public key algorithms as defined in RFC4880. * Enumeration of public key algorithms as defined in RFC4880.
* *
* See [RFC4880: Public-Key Algorithms](https://tools.ietf.org/html/rfc4880#section-9.1) * See [RFC4880: Public-Key Algorithms](https://tools.ietf.org/html/rfc4880#section-9.1)
*/ */
enum class PublicKeyAlgorithm( enum class PublicKeyAlgorithm(val algorithmId: Int) {
val algorithmId: Int,
val signingCapable: Boolean,
val encryptionCapable: Boolean
) {
/** RSA capable of encryption and signatures. */ /** RSA capable of encryption and signatures. */
RSA_GENERAL(1, true, true), RSA_GENERAL(1),
/** /**
* RSA with usage encryption. * RSA with usage encryption.
@ -25,7 +23,7 @@ enum class PublicKeyAlgorithm(
* notice</a> * notice</a>
*/ */
@Deprecated("RSA_ENCRYPT is deprecated in favor of RSA_GENERAL", ReplaceWith("RSA_GENERAL")) @Deprecated("RSA_ENCRYPT is deprecated in favor of RSA_GENERAL", ReplaceWith("RSA_GENERAL"))
RSA_ENCRYPT(2, false, true), RSA_ENCRYPT(2),
/** /**
* RSA with usage of creating signatures. * RSA with usage of creating signatures.
@ -34,19 +32,19 @@ enum class PublicKeyAlgorithm(
* notice</a> * notice</a>
*/ */
@Deprecated("RSA_SIGN is deprecated in favor of RSA_GENERAL", ReplaceWith("RSA_GENERAL")) @Deprecated("RSA_SIGN is deprecated in favor of RSA_GENERAL", ReplaceWith("RSA_GENERAL"))
RSA_SIGN(3, true, false), RSA_SIGN(3),
/** ElGamal with usage encryption. */ /** ElGamal with usage encryption. */
ELGAMAL_ENCRYPT(16, false, true), ELGAMAL_ENCRYPT(16),
/** Digital Signature Algorithm. */ /** Digital Signature Algorithm. */
DSA(17, true, false), DSA(17),
/** Elliptic Curve Diffie-Hellman. */ /** Elliptic Curve Diffie-Hellman. */
ECDH(18, false, true), ECDH(18),
/** Elliptic Curve Digital Signature Algorithm. */ /** Elliptic Curve Digital Signature Algorithm. */
ECDSA(19, true, false), ECDSA(19),
/** /**
* ElGamal General. * ElGamal General.
@ -54,26 +52,50 @@ enum class PublicKeyAlgorithm(
* @deprecated see <a href="https://tools.ietf.org/html/rfc4880#section-13.8">Deprecation * @deprecated see <a href="https://tools.ietf.org/html/rfc4880#section-13.8">Deprecation
* notice</a> * notice</a>
*/ */
@Deprecated("ElGamal is deprecated") ELGAMAL_GENERAL(20, true, true), @Deprecated("ElGamal is deprecated") ELGAMAL_GENERAL(20),
/** Diffie-Hellman key exchange algorithm. */ /** Diffie-Hellman key exchange algorithm. */
DIFFIE_HELLMAN(21, false, true), DIFFIE_HELLMAN(21),
/** Digital Signature Algorithm based on twisted Edwards Curves. */ /** Digital Signature Algorithm based on twisted Edwards Curves. */
EDDSA_LEGACY(22, true, false), EDDSA_LEGACY(22),
/** X25519 encryption algorithm. */ /** X25519 encryption algorithm. */
X25519(25, false, true), X25519(25),
/** X448 encryption algorithm. */ /** X448 encryption algorithm. */
X448(26, false, true), X448(26),
/** Ed25519 signature algorithm. */ /** Ed25519 signature algorithm. */
ED25519(27, true, false), ED25519(27),
/** Ed448 signature algorithm. */ /** Ed448 signature algorithm. */
ED448(28, true, false), ED448(28),
;
/**
* AEAD can be used as a persistent key symmetric encryption algorithm for message encryption.
*
* @see
* [Persistent Symmetric Keys in OpenPGP](https://datatracker.ietf.org/doc/draft-ietf-openpgp-persistent-symmetric-keys/)
*/
AEAD(128) {
override val signingCapable = false
override val encryptionCapable = true
},
/**
* HMAC can be used as a persistent key symmetric signing algorithm for message signing.
*
* @see
* [Persistent Symmetric Keys in OpenPGP](https://datatracker.ietf.org/doc/draft-ietf-openpgp-persistent-symmetric-keys/)
*/
HMAC(129) {
override val signingCapable = true
override val encryptionCapable = false
};
open val signingCapable: Boolean = PublicKeyUtils.isSigningAlgorithm(algorithmId)
open val encryptionCapable: Boolean = PublicKeyUtils.isEncryptionAlgorithm(algorithmId)
fun isSigningCapable(): Boolean = signingCapable fun isSigningCapable(): Boolean = signingCapable