forked from bcgit/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEncryptionMechanism.java
More file actions
158 lines (142 loc) · 4.82 KB
/
Copy pathMessageEncryptionMechanism.java
File metadata and controls
158 lines (142 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.bouncycastle.openpgp.api;
import org.bouncycastle.bcpg.AEADAlgorithmTags;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
/**
* Encryption mode (SEIPDv1 / SEIPDv2 / OED) and algorithms.
*/
public class MessageEncryptionMechanism
{
private final EncryptedDataPacketType mode;
private final int symmetricKeyAlgorithm;
private final int aeadAlgorithm;
/**
* Create a {@link MessageEncryptionMechanism} tuple.
*
* @param mode encryption mode (packet type)
* @param symmetricKeyAlgorithm symmetric key algorithm for message encryption
* @param aeadAlgorithm aead algorithm for message encryption
*/
private MessageEncryptionMechanism(EncryptedDataPacketType mode,
int symmetricKeyAlgorithm,
int aeadAlgorithm)
{
this.mode = mode;
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
this.aeadAlgorithm = aeadAlgorithm;
}
public EncryptedDataPacketType getMode()
{
return mode;
}
public int getSymmetricKeyAlgorithm()
{
return symmetricKeyAlgorithm;
}
public int getAeadAlgorithm()
{
return aeadAlgorithm;
}
/**
* The data will not be encrypted.
* Useful for sign-only operations.
*
* @return unencrypted encryption setup
*/
public static MessageEncryptionMechanism unencrypted()
{
int none = 0;
return new MessageEncryptionMechanism(null,
SymmetricKeyAlgorithmTags.NULL, none);
}
@Deprecated
public static MessageEncryptionMechanism legacyEncryptedNonIntegrityProtected(int symmetricKeyAlgorithm)
{
int none = 0;
return new MessageEncryptionMechanism(EncryptedDataPacketType.SED, symmetricKeyAlgorithm, none);
}
/**
* The data will be encrypted and integrity protected using a SEIPDv1 packet.
*
* @param symmetricKeyAlgorithm symmetric cipher algorithm for message encryption
* @return sym. enc. integrity protected encryption setup
*/
public static MessageEncryptionMechanism integrityProtected(int symmetricKeyAlgorithm)
{
int none = 0;
return new MessageEncryptionMechanism(EncryptedDataPacketType.SEIPDv1, symmetricKeyAlgorithm, none);
}
/**
* The data will be OCB-encrypted as specified by the non-standard LibrePGP document.
*
* @param symmetricKeyAlgorithm symmetric key algorithm which will be combined with OCB to form
* an OCB-encrypted data packet
* @return LibrePGP OCB encryption setup
*/
public static MessageEncryptionMechanism librePgp(int symmetricKeyAlgorithm)
{
return new MessageEncryptionMechanism(EncryptedDataPacketType.LIBREPGP_OED,
symmetricKeyAlgorithm, AEADAlgorithmTags.OCB);
}
/**
* The data will be AEAD-encrypted using the method described in RFC9580.
*
* @param symmetricKeyAlgorithm symmetric cipher algorithm
* @param aeadAlgorithm AEAD algorithm
* @return AEAD encryption setup
*/
public static MessageEncryptionMechanism aead(int symmetricKeyAlgorithm, int aeadAlgorithm)
{
return new MessageEncryptionMechanism(EncryptedDataPacketType.SEIPDv2, symmetricKeyAlgorithm, aeadAlgorithm);
}
public static MessageEncryptionMechanism aead(PreferredAEADCiphersuites.Combination combination)
{
return aead(combination.getSymmetricAlgorithm(), combination.getAeadAlgorithm());
}
/**
* Return true, if the message will be encrypted.
*
* @return is encrypted
*/
public boolean isEncrypted()
{
return symmetricKeyAlgorithm != SymmetricKeyAlgorithmTags.NULL;
}
@Override
public int hashCode()
{
return mode.hashCode()
+ 13 * symmetricKeyAlgorithm
+ 17 * aeadAlgorithm;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (this == obj)
{
return true;
}
if (!(obj instanceof MessageEncryptionMechanism))
{
return false;
}
MessageEncryptionMechanism m = (MessageEncryptionMechanism)obj;
return getMode() == m.getMode()
&& getSymmetricKeyAlgorithm() == m.getSymmetricKeyAlgorithm()
&& getAeadAlgorithm() == m.getAeadAlgorithm();
}
@Override
public String toString()
{
String out = mode.name() + "[cipher: " + symmetricKeyAlgorithm;
if (mode == EncryptedDataPacketType.SEIPDv2 || mode == EncryptedDataPacketType.LIBREPGP_OED)
{
out += " aead: " + aeadAlgorithm;
}
return out + "]";
}
}