|
22 | 22 | import org.bouncycastle.asn1.cms.GCMParameters; |
23 | 23 | import org.bouncycastle.asn1.cms.Time; |
24 | 24 | import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
| 25 | +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
25 | 26 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
26 | 27 | import org.bouncycastle.cms.CMSAttributeTableGenerationException; |
27 | 28 | import org.bouncycastle.cms.CMSAttributeTableGenerator; |
@@ -207,6 +208,53 @@ public AttributeTable getAttributes(Map parameters) |
207 | 208 | assertEquals("Hello, world!", Strings.fromByteArray(recData)); |
208 | 209 | } |
209 | 210 |
|
| 211 | + public void testChacha20Poly1305() |
| 212 | + throws Exception |
| 213 | + { |
| 214 | + if (!CMSTestUtil.isAeadAvailable()) |
| 215 | + { |
| 216 | + return; |
| 217 | + } |
| 218 | + byte[] message = Strings.toByteArray("Hello, world!"); |
| 219 | + OutputEncryptor candidate = new JceCMSContentEncryptorBuilder(PKCSObjectIdentifiers.id_alg_AEADChaCha20Poly1305).setProvider(BC).build(); |
| 220 | + |
| 221 | + assertEquals(PKCSObjectIdentifiers.id_alg_AEADChaCha20Poly1305, candidate.getAlgorithmIdentifier().getAlgorithm()); |
| 222 | + //assertNotNull(GCMParameters.getInstance(candidate.getAlgorithmIdentifier().getParameters())); |
| 223 | + |
| 224 | + assertTrue(candidate instanceof OutputAEADEncryptor); |
| 225 | + |
| 226 | + OutputAEADEncryptor macProvider = (OutputAEADEncryptor)candidate; |
| 227 | + |
| 228 | + CMSAuthEnvelopedDataGenerator authGen = new CMSAuthEnvelopedDataGenerator(); |
| 229 | + |
| 230 | + authGen.setAuthenticatedAttributeGenerator(new CMSAttributeTableGenerator() |
| 231 | + { |
| 232 | + public AttributeTable getAttributes(Map parameters) |
| 233 | + throws CMSAttributeTableGenerationException |
| 234 | + { |
| 235 | + Hashtable<ASN1ObjectIdentifier, Attribute> attrs = new Hashtable<ASN1ObjectIdentifier, Attribute>(); |
| 236 | + Attribute testAttr = new Attribute(CMSAttributes.signingTime, |
| 237 | + new DERSet(new Time(new Date()))); |
| 238 | + attrs.put(testAttr.getAttrType(), testAttr); |
| 239 | + return new AttributeTable(attrs); |
| 240 | + } |
| 241 | + }); |
| 242 | + |
| 243 | + authGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert)); |
| 244 | + |
| 245 | + CMSAuthEnvelopedData authData = authGen.generate(new CMSProcessableByteArray(message), macProvider); |
| 246 | + |
| 247 | + CMSAuthEnvelopedData encAuthData = new CMSAuthEnvelopedData(authData.getEncoded()); |
| 248 | + |
| 249 | + RecipientInformationStore recipients = encAuthData.getRecipientInfos(); |
| 250 | + |
| 251 | + RecipientInformation recipient = (RecipientInformation)recipients.getRecipients().iterator().next(); |
| 252 | + |
| 253 | + byte[] recData = recipient.getContent(new JceKeyTransAuthEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)); |
| 254 | + |
| 255 | + assertEquals("Hello, world!", Strings.fromByteArray(recData)); |
| 256 | + } |
| 257 | + |
210 | 258 | public void testGCMwithHKDF() |
211 | 259 | throws Exception |
212 | 260 | { |
|
0 commit comments