1717import org .bouncycastle .bcpg .HashAlgorithmTags ;
1818import org .bouncycastle .bcpg .PublicKeyAlgorithmTags ;
1919import org .bouncycastle .bcpg .PublicKeyPacket ;
20+ import org .bouncycastle .bcpg .S2K ;
2021import org .bouncycastle .bcpg .SymmetricKeyAlgorithmTags ;
2122import org .bouncycastle .crypto .AsymmetricCipherKeyPair ;
2223import org .bouncycastle .crypto .CryptoServicesRegistrar ;
2526import org .bouncycastle .crypto .digests .SHA256Digest ;
2627import org .bouncycastle .crypto .engines .AESEngine ;
2728import org .bouncycastle .crypto .engines .RFC3394WrapEngine ;
29+ import org .bouncycastle .crypto .generators .Ed25519KeyPairGenerator ;
2830import org .bouncycastle .crypto .generators .HKDFBytesGenerator ;
2931import org .bouncycastle .crypto .params .AsymmetricKeyParameter ;
32+ import org .bouncycastle .crypto .params .Ed25519KeyGenerationParameters ;
3033import org .bouncycastle .crypto .params .HKDFParameters ;
3134import org .bouncycastle .crypto .params .KeyParameter ;
3235import org .bouncycastle .crypto .params .X25519PrivateKeyParameters ;
5154import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
5255import org .bouncycastle .openpgp .jcajce .JcaPGPObjectFactory ;
5356import org .bouncycastle .openpgp .operator .PBESecretKeyDecryptor ;
57+ import org .bouncycastle .openpgp .operator .PBESecretKeyEncryptor ;
5458import org .bouncycastle .openpgp .operator .PGPContentVerifier ;
5559import org .bouncycastle .openpgp .operator .PGPDigestCalculator ;
5660import org .bouncycastle .openpgp .operator .PGPDigestCalculatorProvider ;
61+ import org .bouncycastle .openpgp .operator .bc .BcAEADSecretKeyEncryptorBuilder ;
5762import org .bouncycastle .openpgp .operator .bc .BcKeyFingerprintCalculator ;
5863import org .bouncycastle .openpgp .operator .bc .BcPBESecretKeyDecryptorBuilder ;
5964import org .bouncycastle .openpgp .operator .bc .BcPGPContentVerifierBuilderProvider ;
7984import org .bouncycastle .util .encoders .Hex ;
8085import org .bouncycastle .util .test .SimpleTest ;
8186import org .bouncycastle .util .test .UncloseableOutputStream ;
87+ import org .junit .Assert ;
8288
8389public 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