Skip to content

Commit d1116a4

Browse files
author
gefeili
committed
#1794 Initial push for bukka's code
2 parents 11877fc + 1102d45 commit d1116a4

File tree

7 files changed

+717
-63
lines changed

7 files changed

+717
-63
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.bouncycastle.cms;
2+
3+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
4+
import org.bouncycastle.jcajce.io.CipherInputStream;
5+
import org.bouncycastle.operator.InputAEADDecryptor;
6+
7+
import javax.crypto.Cipher;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
12+
public class CMSInputAEADDecryptor
13+
implements InputAEADDecryptor
14+
{
15+
final AlgorithmIdentifier contentEncryptionAlgorithm;
16+
17+
final Cipher dataCipher;
18+
19+
private InputStream inputStream;
20+
21+
public CMSInputAEADDecryptor(AlgorithmIdentifier contentEncryptionAlgorithm, Cipher dataCipher)
22+
{
23+
this.contentEncryptionAlgorithm = contentEncryptionAlgorithm;
24+
this.dataCipher = dataCipher;
25+
}
26+
27+
public AlgorithmIdentifier getAlgorithmIdentifier()
28+
{
29+
return contentEncryptionAlgorithm;
30+
}
31+
32+
public InputStream getInputStream(InputStream dataIn)
33+
{
34+
inputStream = dataIn;
35+
return new CipherInputStream(dataIn, dataCipher);
36+
}
37+
38+
public OutputStream getAADStream()
39+
{
40+
return new AADStream(dataCipher);
41+
}
42+
43+
public byte[] getMAC()
44+
{
45+
if (inputStream instanceof InputStreamWithMAC)
46+
{
47+
return ((InputStreamWithMAC)inputStream).getMAC();
48+
}
49+
return null;
50+
}
51+
52+
private static class AADStream
53+
extends OutputStream
54+
{
55+
private Cipher cipher;
56+
private byte[] oneByte = new byte[1];
57+
58+
public AADStream(Cipher cipher)
59+
{
60+
this.cipher = cipher;
61+
}
62+
63+
public void write(byte[] buf, int off, int len)
64+
throws IOException
65+
{
66+
cipher.updateAAD(buf, off, len);
67+
}
68+
69+
public void write(int b)
70+
throws IOException
71+
{
72+
oneByte[0] = (byte)b;
73+
74+
cipher.updateAAD(oneByte);
75+
}
76+
}
77+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.bouncycastle.cms.jcajce;
2+
3+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
4+
import org.bouncycastle.cms.CMSException;
5+
import org.bouncycastle.cms.CMSInputAEADDecryptor;
6+
import org.bouncycastle.cms.RecipientOperator;
7+
8+
import javax.crypto.Cipher;
9+
import javax.crypto.SecretKey;
10+
import java.security.Key;
11+
12+
public class JceKEKAuthEnvelopedRecipient
13+
extends JceKEKRecipient
14+
{
15+
public JceKEKAuthEnvelopedRecipient(SecretKey recipientKey)
16+
{
17+
super(recipientKey);
18+
}
19+
20+
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey)
21+
throws CMSException
22+
{
23+
Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
24+
25+
final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm);
26+
27+
return new RecipientOperator(new CMSInputAEADDecryptor(contentEncryptionAlgorithm, dataCipher));
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.bouncycastle.cms.jcajce;
2+
3+
import org.bouncycastle.asn1.ASN1OctetString;
4+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
5+
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
6+
import org.bouncycastle.cms.CMSException;
7+
import org.bouncycastle.cms.CMSInputAEADDecryptor;
8+
import org.bouncycastle.cms.RecipientOperator;
9+
10+
import javax.crypto.Cipher;
11+
import java.security.Key;
12+
import java.security.PrivateKey;
13+
14+
public class JceKeyAgreeAuthEnvelopedRecipient
15+
extends JceKeyAgreeRecipient
16+
{
17+
public JceKeyAgreeAuthEnvelopedRecipient(PrivateKey recipientKey)
18+
{
19+
super(recipientKey);
20+
}
21+
22+
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, SubjectPublicKeyInfo senderPublicKey, ASN1OctetString userKeyingMaterial, byte[] encryptedContentKey)
23+
throws CMSException
24+
{
25+
Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, senderPublicKey, userKeyingMaterial, encryptedContentKey);
26+
27+
final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm);
28+
29+
return new RecipientOperator(new CMSInputAEADDecryptor(contentEncryptionAlgorithm, dataCipher));
30+
}
31+
}
Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package org.bouncycastle.cms.jcajce;
22

3-
import java.io.IOException;
4-
import java.io.InputStream;
5-
import java.io.OutputStream;
63
import java.security.Key;
74
import java.security.PrivateKey;
85

96
import javax.crypto.Cipher;
107

118
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
129
import org.bouncycastle.cms.CMSException;
13-
import org.bouncycastle.cms.InputStreamWithMAC;
10+
import org.bouncycastle.cms.CMSInputAEADDecryptor;
1411
import org.bouncycastle.cms.RecipientOperator;
15-
import org.bouncycastle.jcajce.io.CipherInputStream;
16-
import org.bouncycastle.operator.InputAEADDecryptor;
1712

1813
public class JceKeyTransAuthEnvelopedRecipient
1914
extends JceKeyTransRecipient
@@ -30,60 +25,6 @@ public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionA
3025

3126
final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm);
3227

33-
return new RecipientOperator(new InputAEADDecryptor()
34-
{
35-
private InputStream inputStream;
36-
37-
public AlgorithmIdentifier getAlgorithmIdentifier()
38-
{
39-
return contentEncryptionAlgorithm;
40-
}
41-
42-
public InputStream getInputStream(InputStream dataIn)
43-
{
44-
inputStream = dataIn;
45-
return new CipherInputStream(dataIn, dataCipher);
46-
}
47-
48-
public OutputStream getAADStream()
49-
{
50-
return new AADStream(dataCipher);
51-
}
52-
53-
public byte[] getMAC()
54-
{
55-
if (inputStream instanceof InputStreamWithMAC)
56-
{
57-
return ((InputStreamWithMAC)inputStream).getMAC();
58-
}
59-
return null;
60-
}
61-
});
62-
}
63-
64-
private static class AADStream
65-
extends OutputStream
66-
{
67-
private Cipher cipher;
68-
private byte[] oneByte = new byte[1];
69-
70-
public AADStream(Cipher cipher)
71-
{
72-
this.cipher = cipher;
73-
}
74-
75-
public void write(byte[] buf, int off, int len)
76-
throws IOException
77-
{
78-
cipher.updateAAD(buf, off, len);
79-
}
80-
81-
public void write(int b)
82-
throws IOException
83-
{
84-
oneByte[0] = (byte)b;
85-
86-
cipher.updateAAD(oneByte);
87-
}
28+
return new RecipientOperator(new CMSInputAEADDecryptor(contentEncryptionAlgorithm, dataCipher));
8829
}
8930
}

pkix/src/test/java/org/bouncycastle/cms/test/AllTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static Test suite()
2828
suite.addTest(NewAuthenticatedDataStreamTest.suite());
2929
suite.addTest(NewCompressedDataStreamTest.suite());
3030
suite.addTest(NewSignedDataStreamTest.suite());
31+
suite.addTest(NewAuthEnvelopedDataStreamTest.suite());
3132
suite.addTest(NewEnvelopedDataStreamTest.suite());
3233
suite.addTest(AuthEnvelopedDataTest.suite());
3334

0 commit comments

Comments
 (0)