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

Add AEADAlkgorithm.toMechanism(SymAlg) shortcut method

This commit is contained in:
Paul Schaub 2025-05-12 12:32:06 +02:00
parent 49a507b8d1
commit df95860f24
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 11 additions and 12 deletions

View file

@ -4,6 +4,8 @@
package org.pgpainless.algorithm package org.pgpainless.algorithm
import org.bouncycastle.openpgp.api.MessageEncryptionMechanism
enum class AEADAlgorithm(val algorithmId: Int, val ivLength: Int, val tagLength: Int) { enum class AEADAlgorithm(val algorithmId: Int, val ivLength: Int, val tagLength: Int) {
/** /**
@ -27,6 +29,9 @@ enum class AEADAlgorithm(val algorithmId: Int, val ivLength: Int, val tagLength:
GCM(3, 12, 16), GCM(3, 12, 16),
; ;
fun toMechanism(ciphermode: SymmetricKeyAlgorithm): MessageEncryptionMechanism =
MessageEncryptionMechanism.aead(ciphermode.algorithmId, this.algorithmId)
companion object { companion object {
@JvmStatic @JvmStatic
fun fromId(id: Int): AEADAlgorithm? { fun fromId(id: Int): AEADAlgorithm? {

View file

@ -21,7 +21,6 @@ import java.util.Set;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.api.MessageEncryptionMechanism;
import org.bouncycastle.openpgp.api.OpenPGPCertificate; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
@ -338,14 +337,13 @@ public class EncryptDecryptTest {
.onOutputStream(bOut) .onOutputStream(bOut)
.withOptions(ProducerOptions.encrypt( .withOptions(ProducerOptions.encrypt(
EncryptionOptions.encryptCommunications() EncryptionOptions.encryptCommunications()
.overrideEncryptionMechanism(MessageEncryptionMechanism.aead(SymmetricKeyAlgorithm.AES_192.getAlgorithmId(), AEADAlgorithm.OCB.getAlgorithmId())) .overrideEncryptionMechanism(AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192))
.addRecipient(keyWithoutSEIPD2Feature.toCertificate()))); .addRecipient(keyWithoutSEIPD2Feature.toCertificate())));
eOut.write(testMessage.getBytes(StandardCharsets.UTF_8)); eOut.write(testMessage.getBytes(StandardCharsets.UTF_8));
eOut.close(); eOut.close();
EncryptionResult result = eOut.getResult(); EncryptionResult result = eOut.getResult();
assertEquals(MessageEncryptionMechanism.aead( assertEquals(AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192),
SymmetricKeyAlgorithm.AES_192.getAlgorithmId(), AEADAlgorithm.OCB.getAlgorithmId()),
result.getEncryptionMechanism()); result.getEncryptionMechanism());
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray()); ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
@ -357,7 +355,7 @@ public class EncryptDecryptTest {
decIn.close(); decIn.close();
MessageMetadata metadata = decIn.getMetadata(); MessageMetadata metadata = decIn.getMetadata();
assertEquals(MessageEncryptionMechanism.aead(SymmetricKeyAlgorithm.AES_192.getAlgorithmId(), AEADAlgorithm.OCB.getAlgorithmId()), assertEquals(AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192),
metadata.getEncryptionMechanism()); metadata.getEncryptionMechanism());
} }
@ -371,10 +369,7 @@ public class EncryptDecryptTest {
.withOptions( .withOptions(
ProducerOptions.encrypt(EncryptionOptions.encryptCommunications() ProducerOptions.encrypt(EncryptionOptions.encryptCommunications()
.overrideEncryptionMechanism( .overrideEncryptionMechanism(
MessageEncryptionMechanism.aead( AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192))
SymmetricKeyAlgorithm.AES_192.getAlgorithmId(),
AEADAlgorithm.OCB.getAlgorithmId()
))
.addMessagePassphrase(Passphrase.fromPassword("sw0rdf1sh")) .addMessagePassphrase(Passphrase.fromPassword("sw0rdf1sh"))
)); ));
@ -382,8 +377,7 @@ public class EncryptDecryptTest {
eOut.close(); eOut.close();
EncryptionResult result = eOut.getResult(); EncryptionResult result = eOut.getResult();
assertEquals(MessageEncryptionMechanism.aead( assertEquals(AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192),
SymmetricKeyAlgorithm.AES_192.getAlgorithmId(), AEADAlgorithm.OCB.getAlgorithmId()),
result.getEncryptionMechanism()); result.getEncryptionMechanism());
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray()); ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
@ -394,7 +388,7 @@ public class EncryptDecryptTest {
Streams.drain(decIn); Streams.drain(decIn);
decIn.close(); decIn.close();
MessageMetadata metadata = decIn.getMetadata(); MessageMetadata metadata = decIn.getMetadata();
assertEquals(MessageEncryptionMechanism.aead(SymmetricKeyAlgorithm.AES_192.getAlgorithmId(), AEADAlgorithm.OCB.getAlgorithmId()), assertEquals(AEADAlgorithm.OCB.toMechanism(SymmetricKeyAlgorithm.AES_192),
metadata.getEncryptionMechanism()); metadata.getEncryptionMechanism());
} }