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 48ba9dbe98
commit e44e97844c
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
import org.bouncycastle.openpgp.api.MessageEncryptionMechanism
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),
;
fun toMechanism(ciphermode: SymmetricKeyAlgorithm): MessageEncryptionMechanism =
MessageEncryptionMechanism.aead(ciphermode.algorithmId, this.algorithmId)
companion object {
@JvmStatic
fun fromId(id: Int): AEADAlgorithm? {

View file

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