Skip to content

Commit

Permalink
Fix checkstyle errors in various v6 related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Aug 22, 2024
1 parent 93679b1 commit 2597de2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class OCBEncryptedDataPacketTest extends AbstractPacketTest {
public class OCBEncryptedDataPacketTest
extends AbstractPacketTest
{
@Override
public String getName() {
public String getName()
{
return "OCBEncryptedDataPacketTest";
}

@Override
public void performTest() throws Exception {
public void performTest()
throws Exception
{
parseTestVector();
parseUnsupportedPacketVersion();
}

private void parseTestVector() throws IOException {
private void parseTestVector()
throws IOException
{
String testVector = "" +
"d45301090210c265ff63a61ed8af00fa" +
"43866be8eb9eef77241518a3d60e387b" +
Expand All @@ -39,7 +46,9 @@ private void parseTestVector() throws IOException {
isEncodingEqual("IV mismatch", Hex.decode("C265FF63A61ED8AF00FA43866BE8EB"), p.getIV());
}

private void parseUnsupportedPacketVersion() throws IOException {
private void parseUnsupportedPacketVersion()
throws IOException
{
// Test vector with modified packet version 99
String testVector = "" +
"d45399090210c265ff63a61ed8af00fa" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private void testParseV6OnePassSignaturePacket()
issuerFp, ops.getFingerprint());
isTrue("OPS packet key-ID mismatch",
// key-ID are the first 8 octets of the fingerprint
// -DM Hex.toHexString
Hex.toHexString(issuerFp).startsWith(Long.toHexString(ops.getKeyID())));
isEncodingEqual("OPS packet salt mismatch",
salt, ops.getSalt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void performTest()
parseUnknownPublicKey();
}

private void parseUnknownPublicKey() throws ParseException, IOException {
private void parseUnknownPublicKey()
throws ParseException, IOException
{
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
parser.setTimeZone(TimeZone.getTimeZone("UTC"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ private void testUnlockKeyWithWrongPassphraseJca()
{
sk.extractPrivateKey(jceDecBuilder.build("Yang".toCharArray()));
fail("Expected PGPException due to wrong passphrase");
} catch (PGPException e)
}
catch (PGPException e)
{
// expected
}
Expand Down
45 changes: 8 additions & 37 deletions pg/src/test/java/org/bouncycastle/openpgp/test/PGPAeadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.test.DumpUtil;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Exceptions;
import org.bouncycastle.util.Pack;
Expand Down Expand Up @@ -174,11 +175,9 @@ private void knownV5TestVectorDecryptionTests()
throws IOException, PGPException
{
// test known-good V5 test vectors
System.out.println("Test V5 BC Decryption");
testBcDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
System.out.println("Test V5 JCA Decryption");
testJceDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
Expand All @@ -187,12 +186,10 @@ private void knownV5TestVectorDecryptionTests()
private void knownV6TestVectorDecryptionTests()
throws IOException, PGPException
{
// Test known-good V6 test vectors TODO: decryption tests should be working...
System.out.println("Test V6 BC Decryption");
// Test known-good V6 test vectors TODO: decryption tests
testBcDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
System.out.println("Test V6 JCA Decryption");
testJceDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
Expand All @@ -201,36 +198,28 @@ private void knownV6TestVectorDecryptionTests()
private void testBcRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
throws PGPException, IOException
{
System.out.println("Test BC RoundTrip " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
String armored = testBcEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
System.out.println(armored);
testBcDecryption(armored, password, plaintext);
}

private void testJceRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
throws PGPException, IOException
{
System.out.println("Test JCE RoundTrip " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
String armored = testJceEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
System.out.println(armored);
testJceDecryption(armored, password, plaintext);
}

private void testBcJceRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
throws PGPException, IOException
{
System.out.println("Test BC encrypt, JCE decrypt " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
String armored = testBcEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
System.out.println(armored);
testJceDecryption(armored, password, plaintext);
}

private void testJceBcRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
throws PGPException, IOException
{
System.out.println("Test JCE encrypt, BC decrypt " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
String armored = testJceEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
System.out.println(armored);
testBcDecryption(armored, password, plaintext);
}

Expand Down Expand Up @@ -365,7 +354,9 @@ private void testBcDecryption(String armoredMessage, char[] password, byte[] exp

if (o != null)
{
// -DM System.out.println
System.out.println("Unexpected trailing packet.");
// -DM System.out.println
System.out.println(o);
}
}
Expand Down Expand Up @@ -421,7 +412,9 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex

if (o != null)
{
// -DM System.out.println
System.out.println("Unexpected trailing packet.");
// -DM System.out.println
System.out.println(o);
}
}
Expand All @@ -435,30 +428,8 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex

public static void printHex(byte[] bytes)
{
boolean separate = true;
boolean prefix = true;
String hex = Hex.toHexString(bytes);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hex.length() / 2; i++)
{
if (prefix && i % 8 == 0)
{
sb.append("0x").append(Hex.toHexString(Pack.intToBigEndian(i & 0xFFFFF))).append(" ");
}
sb.append(hex.substring(i * 2, i * 2 + 2));
if (separate)
{
if ((i + 1) % 8 == 0)
{
sb.append('\n');
}
else
{
sb.append(' ');
}
}
}
System.out.println(sb);
// -DM System.out.println
System.out.println(DumpUtil.hexdump(bytes));
}

private static String algNames(int aeadAlg, int symAlg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ private void verifySignaturesOnKey(String armoredKey)
}
else
{
// -DM System.out.println
System.out.println("Did not find signing key for DK sig");
}
}
Expand All @@ -432,6 +433,7 @@ private void verifySignaturesOnKey(String armoredKey)
}
else
{
// -DM System.out.println
System.out.println("Did not find signing key for UID sig for " + uid);
}
}
Expand All @@ -453,6 +455,8 @@ private void verifySignaturesOnKey(String armoredKey)
}
else
{
// -DM System.out.println
// -DM Hex.toHexString
System.out.println("Did not find singing key for subkey " + Hex.toHexString(subkey.getFingerprint()) + " binding signature");
}
}
Expand All @@ -473,15 +477,17 @@ private PGPPublicKey getSigningKeyFor(PGPKeyRing keys, PGPSignature sig)
for (SignatureSubpacket p : sig.getHashedSubPackets().getSubpackets(SignatureSubpacketTags.ISSUER_FINGERPRINT))
{
IssuerFingerprint fp = (IssuerFingerprint) p;
if (Arrays.areEqual(k.getFingerprint(), fp.getFingerprint())) {
if (Arrays.areEqual(k.getFingerprint(), fp.getFingerprint()))
{
return k;
}
}

for (SignatureSubpacket p : sig.getUnhashedSubPackets().getSubpackets(SignatureSubpacketTags.ISSUER_FINGERPRINT))
{
IssuerFingerprint fp = (IssuerFingerprint) p;
if (Arrays.areEqual(k.getFingerprint(), fp.getFingerprint())) {
if (Arrays.areEqual(k.getFingerprint(), fp.getFingerprint()))
{
return k;
}
}
Expand Down

0 comments on commit 2597de2

Please sign in to comment.