Skip to content

Commit f8561f6

Browse files
author
gefeili
committed
Refactor around CMSInputAEADDecryptor
1 parent d1116a4 commit f8561f6

File tree

5 files changed

+28
-46
lines changed

5 files changed

+28
-46
lines changed

pkix/src/main/java/org/bouncycastle/cms/CMSInputAEADDecryptor.java renamed to pkix/src/main/java/org/bouncycastle/cms/jcajce/CMSInputAEADDecryptor.java

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
package org.bouncycastle.cms;
1+
package org.bouncycastle.cms.jcajce;
2+
3+
import java.io.InputStream;
4+
import java.io.OutputStream;
5+
6+
import javax.crypto.Cipher;
27

38
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
9+
import org.bouncycastle.cms.InputStreamWithMAC;
410
import org.bouncycastle.jcajce.io.CipherInputStream;
511
import org.bouncycastle.operator.InputAEADDecryptor;
612

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
13+
class CMSInputAEADDecryptor
14+
implements InputAEADDecryptor
1415
{
15-
final AlgorithmIdentifier contentEncryptionAlgorithm;
16+
private final AlgorithmIdentifier contentEncryptionAlgorithm;
1617

17-
final Cipher dataCipher;
18+
private final Cipher dataCipher;
1819

1920
private InputStream inputStream;
2021

21-
public CMSInputAEADDecryptor(AlgorithmIdentifier contentEncryptionAlgorithm, Cipher dataCipher)
22+
CMSInputAEADDecryptor(AlgorithmIdentifier contentEncryptionAlgorithm, Cipher dataCipher)
2223
{
2324
this.contentEncryptionAlgorithm = contentEncryptionAlgorithm;
2425
this.dataCipher = dataCipher;
@@ -37,7 +38,7 @@ public InputStream getInputStream(InputStream dataIn)
3738

3839
public OutputStream getAADStream()
3940
{
40-
return new AADStream(dataCipher);
41+
return new JceAADStream(dataCipher);
4142
}
4243

4344
public byte[] getMAC()
@@ -48,30 +49,4 @@ public byte[] getMAC()
4849
}
4950
return null;
5051
}
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-
}
7752
}

pkix/src/main/java/org/bouncycastle/cms/jcajce/JceAADStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class JceAADStream
99
extends OutputStream
1010
{
11-
private static final byte[] SINGLE_BYTE = new byte[1];
11+
private final byte[] SINGLE_BYTE = new byte[1];
1212
private Cipher cipher;
1313

1414
JceAADStream(Cipher cipher)

pkix/src/main/java/org/bouncycastle/cms/jcajce/JceKEKAuthEnvelopedRecipient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
44
import org.bouncycastle.cms.CMSException;
5-
import org.bouncycastle.cms.CMSInputAEADDecryptor;
65
import org.bouncycastle.cms.RecipientOperator;
76

87
import javax.crypto.Cipher;
98
import javax.crypto.SecretKey;
9+
1010
import java.security.Key;
1111

12+
/**
13+
* A recipient for CMS authenticated enveloped data encrypted with a KEK (Key Encryption Key).
14+
* Handles key extraction and decryption of the content.
15+
*/
1216
public class JceKEKAuthEnvelopedRecipient
13-
extends JceKEKRecipient
17+
extends JceKEKRecipient
1418
{
1519
public JceKEKAuthEnvelopedRecipient(SecretKey recipientKey)
1620
{
1721
super(recipientKey);
1822
}
1923

2024
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey)
21-
throws CMSException
25+
throws CMSException
2226
{
2327
Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
2428

pkix/src/main/java/org/bouncycastle/cms/jcajce/JceKeyAgreeAuthEnvelopedRecipient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
55
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
66
import org.bouncycastle.cms.CMSException;
7-
import org.bouncycastle.cms.CMSInputAEADDecryptor;
87
import org.bouncycastle.cms.RecipientOperator;
98

109
import javax.crypto.Cipher;
10+
1111
import java.security.Key;
1212
import java.security.PrivateKey;
1313

14+
/**
15+
* A recipient class for CMS authenticated enveloped data using key agreement (Key Agreement Recipient).
16+
* Handles private key-based key extraction and content decryption.
17+
*/
1418
public class JceKeyAgreeAuthEnvelopedRecipient
15-
extends JceKeyAgreeRecipient
19+
extends JceKeyAgreeRecipient
1620
{
1721
public JceKeyAgreeAuthEnvelopedRecipient(PrivateKey recipientKey)
1822
{
1923
super(recipientKey);
2024
}
2125

2226
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, SubjectPublicKeyInfo senderPublicKey, ASN1OctetString userKeyingMaterial, byte[] encryptedContentKey)
23-
throws CMSException
27+
throws CMSException
2428
{
2529
Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, senderPublicKey, userKeyingMaterial, encryptedContentKey);
2630

pkix/src/main/java/org/bouncycastle/cms/jcajce/JceKeyTransAuthEnvelopedRecipient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
99
import org.bouncycastle.cms.CMSException;
10-
import org.bouncycastle.cms.CMSInputAEADDecryptor;
1110
import org.bouncycastle.cms.RecipientOperator;
1211

1312
public class JceKeyTransAuthEnvelopedRecipient

0 commit comments

Comments
 (0)