Skip to content

Commit 6bcb85d

Browse files
author
gefeili
committed
Add OperatorBcTest.testBcAEADSecretKeyEncryptorBuilder
1 parent ad7fdce commit 6bcb85d

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

pg/src/test/java/org/bouncycastle/openpgp/test/OperatorBcTest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.bouncycastle.bcpg.HashAlgorithmTags;
1818
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
1919
import org.bouncycastle.bcpg.PublicKeyPacket;
20+
import org.bouncycastle.bcpg.S2K;
2021
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
2122
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
2223
import org.bouncycastle.crypto.CryptoServicesRegistrar;
@@ -25,8 +26,10 @@
2526
import org.bouncycastle.crypto.digests.SHA256Digest;
2627
import org.bouncycastle.crypto.engines.AESEngine;
2728
import org.bouncycastle.crypto.engines.RFC3394WrapEngine;
29+
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
2830
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
2931
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
32+
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
3033
import org.bouncycastle.crypto.params.HKDFParameters;
3134
import org.bouncycastle.crypto.params.KeyParameter;
3235
import org.bouncycastle.crypto.params.X25519PrivateKeyParameters;
@@ -51,9 +54,11 @@
5154
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
5255
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
5356
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
57+
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
5458
import org.bouncycastle.openpgp.operator.PGPContentVerifier;
5559
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
5660
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
61+
import org.bouncycastle.openpgp.operator.bc.BcAEADSecretKeyEncryptorBuilder;
5762
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
5863
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder;
5964
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
@@ -79,6 +84,7 @@
7984
import org.bouncycastle.util.encoders.Hex;
8085
import org.bouncycastle.util.test.SimpleTest;
8186
import org.bouncycastle.util.test.UncloseableOutputStream;
87+
import org.junit.Assert;
8288

8389
public class OperatorBcTest
8490
extends SimpleTest
@@ -100,6 +106,7 @@ public String getName()
100106
public void performTest()
101107
throws Exception
102108
{
109+
testBcAEADSecretKeyEncryptorBuilder();
103110
testX25519HKDF();
104111
testKeyRings();
105112
testBcPGPKeyPair();
@@ -309,7 +316,7 @@ public void initialize(KeyPairGenerator gen)
309316
}
310317
});
311318
}
312-
319+
313320
private void testCreateKeyPairEC(int algorithm, String name, final String curveName)
314321
throws Exception
315322
{
@@ -469,7 +476,7 @@ private void keyringTest(String algorithmName1, String ed_str, int ed_num, Strin
469476
{
470477
count++;
471478
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), vKey);
472-
// TODO: appears to be failing on CI system
479+
// TODO: appears to be failing on CI system
473480
if (!sig.verifyCertification(vKey, sKey))
474481
{
475482
fail("failed to verify sub-key signature.");
@@ -637,6 +644,38 @@ public void testX25519HKDF()
637644
isTrue(Arrays.areEqual(output, expectedDecryptedSessionKey));
638645
}
639646

640-
647+
public void testBcAEADSecretKeyEncryptorBuilder()
648+
throws Exception
649+
{
650+
Ed25519KeyPairGenerator gen = new Ed25519KeyPairGenerator();
651+
gen.init(new Ed25519KeyGenerationParameters(new SecureRandom()));
652+
AsymmetricCipherKeyPair kp = gen.generateKeyPair();
653+
Date creationTime = new Date();
654+
SecureRandom random = new SecureRandom();
655+
for (int version : new int[]{PublicKeyPacket.VERSION_4, PublicKeyPacket.VERSION_6})
656+
{
657+
PGPKeyPair keyPair = new BcPGPKeyPair(version, PublicKeyAlgorithmTags.Ed25519, kp, creationTime);
658+
659+
BcAEADSecretKeyEncryptorBuilder bcEncBuilder = new BcAEADSecretKeyEncryptorBuilder(
660+
AEADAlgorithmTags.OCB, SymmetricKeyAlgorithmTags.AES_256,
661+
S2K.Argon2Params.memoryConstrainedParameters());
662+
663+
bcEncBuilder.build(
664+
"passphrase".toCharArray(),
665+
keyPair.getPublicKey().getPublicKeyPacket());
666+
PBESecretKeyEncryptor encryptor = bcEncBuilder.build(
667+
"Yin".toCharArray(),
668+
keyPair.getPublicKey().getPublicKeyPacket());
669+
byte[] key = new byte[16];
670+
random.nextBytes(key);
671+
byte[] input1 = new byte[64];
672+
random.nextBytes(input1);
673+
674+
byte[] input2 = Arrays.copyOfRange(input1, 32, 64);
675+
byte[] output1 = encryptor.encryptKeyData(key, input1, 32, 32);
676+
byte[] output2 = encryptor.encryptKeyData(key, input2, 0, 32);
677+
Assert.assertTrue(Arrays.areEqual(output1, output2));
678+
}
679+
}
641680

642681
}

0 commit comments

Comments
 (0)