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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
package org.bouncycastle.jcajce.provider.symmetric.util;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.digests.GOST3411Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.TigerDigest;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.params.DESParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
public interface PBE
{
//
// PBE Based encryption constants - by default we do PKCS12 with SHA-1
//
static final int MD5 = 0;
static final int SHA1 = 1;
static final int RIPEMD160 = 2;
static final int TIGER = 3;
static final int SHA256 = 4;
static final int MD2 = 5;
static final int GOST3411 = 6;
static final int PKCS5S1 = 0;
static final int PKCS5S2 = 1;
static final int PKCS12 = 2;
static final int OPENSSL = 3;
/**
* uses the appropriate mixer to generate the key and IV if necessary.
*/
static class Util
{
static private PBEParametersGenerator makePBEGenerator(
int type,
int hash)
{
PBEParametersGenerator generator;
if (type == PKCS5S1)
{
switch (hash)
{
case MD2:
generator = new PKCS5S1ParametersGenerator(new MD2Digest());
break;
case MD5:
generator = new PKCS5S1ParametersGenerator(new MD5Digest());
break;
case SHA1:
generator = new PKCS5S1ParametersGenerator(new SHA1Digest());
break;
default:
throw new IllegalStateException("PKCS5 scheme 1 only supports MD2, MD5 and SHA1.");
}
}
else if (type == PKCS5S2)
{
generator = new PKCS5S2ParametersGenerator();
}
else if (type == PKCS12)
{
switch (hash)
{
case MD2:
generator = new PKCS12ParametersGenerator(new MD2Digest());
break;
case MD5:
generator = new PKCS12ParametersGenerator(new MD5Digest());
break;
case SHA1:
generator = new PKCS12ParametersGenerator(new SHA1Digest());
break;
case RIPEMD160:
generator = new PKCS12ParametersGenerator(new RIPEMD160Digest());
break;
case TIGER:
generator = new PKCS12ParametersGenerator(new TigerDigest());
break;
case SHA256:
generator = new PKCS12ParametersGenerator(new SHA256Digest());
break;
case GOST3411:
generator = new PKCS12ParametersGenerator(new GOST3411Digest());
break;
default:
throw new IllegalStateException("unknown digest scheme for PBE encryption.");
}
}
else
{
generator = new OpenSSLPBEParametersGenerator();
}
return generator;
}
/**
* construct a key and iv (if necessary) suitable for use with a
* Cipher.
*/
public static CipherParameters makePBEParameters(
BCPBEKey pbeKey,
AlgorithmParameterSpec spec,
String targetAlgorithm)
{
if ((spec == null) || !(spec instanceof PBEParameterSpec))
{
throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
}
PBEParameterSpec pbeParam = (PBEParameterSpec)spec;
PBEParametersGenerator generator = makePBEGenerator(pbeKey.getType(), pbeKey.getDigest());
byte[] key = pbeKey.getEncoded();
CipherParameters param;
if (pbeKey.shouldTryWrongPKCS12())
{
key = new byte[2];
}
generator.init(key, pbeParam.getSalt(), pbeParam.getIterationCount());
if (pbeKey.getIvSize() != 0)
{
param = generator.generateDerivedParameters(pbeKey.getKeySize(), pbeKey.getIvSize());
}
else
{
param = generator.generateDerivedParameters(pbeKey.getKeySize());
}
if (targetAlgorithm.startsWith("DES"))
{
if (param instanceof ParametersWithIV)
{
KeyParameter kParam = (KeyParameter)((ParametersWithIV)param).getParameters();
DESParameters.setOddParity(kParam.getKey());
}
else
{
KeyParameter kParam = (KeyParameter)param;
DESParameters.setOddParity(kParam.getKey());
}
}
for (int i = 0; i != key.length; i++)
{
key[i] = 0;
}
return param;
}
/**
* generate a PBE based key suitable for a MAC algorithm, the
* key size is chosen according the MAC size, or the hashing algorithm,
* whichever is greater.
*/
public static CipherParameters makePBEMacParameters(
BCPBEKey pbeKey,
AlgorithmParameterSpec spec)
{
if ((spec == null) || !(spec instanceof PBEParameterSpec))
{
throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
}
PBEParameterSpec pbeParam = (PBEParameterSpec)spec;
PBEParametersGenerator generator = makePBEGenerator(pbeKey.getType(), pbeKey.getDigest());
byte[] key = pbeKey.getEncoded();
CipherParameters param;
if (pbeKey.shouldTryWrongPKCS12())
{
key = new byte[2];
}
generator.init(key, pbeParam.getSalt(), pbeParam.getIterationCount());
param = generator.generateDerivedMacParameters(pbeKey.getKeySize());
for (int i = 0; i != key.length; i++)
{
key[i] = 0;
}
return param;
}
/**
* construct a key and iv (if necessary) suitable for use with a
* Cipher.
*/
public static CipherParameters makePBEParameters(
PBEKeySpec keySpec,
int type,
int hash,
int keySize,
int ivSize)
{
PBEParametersGenerator generator = makePBEGenerator(type, hash);
byte[] key;
CipherParameters param;
if (type == PKCS12)
{
key = PBEParametersGenerator.PKCS12PasswordToBytes(keySpec.getPassword());
}
else
{
key = PBEParametersGenerator.PKCS5PasswordToBytes(keySpec.getPassword());
}
generator.init(key, keySpec.getSalt(), keySpec.getIterationCount());
if (ivSize != 0)
{
param = generator.generateDerivedParameters(keySize, ivSize);
}
else
{
param = generator.generateDerivedParameters(keySize);
}
for (int i = 0; i != key.length; i++)
{
key[i] = 0;
}
return param;
}
/**
* generate a PBE based key suitable for a MAC algorithm, the
* key size is chosen according the MAC size, or the hashing algorithm,
* whichever is greater.
*/
public static CipherParameters makePBEMacParameters(
PBEKeySpec keySpec,
int type,
int hash,
int keySize)
{
PBEParametersGenerator generator = makePBEGenerator(type, hash);
byte[] key;
CipherParameters param;
if (type == PKCS12)
{
key = PBEParametersGenerator.PKCS12PasswordToBytes(keySpec.getPassword());
}
else
{
key = PBEParametersGenerator.PKCS5PasswordToBytes(keySpec.getPassword());
}
generator.init(key, keySpec.getSalt(), keySpec.getIterationCount());
param = generator.generateDerivedMacParameters(keySize);
for (int i = 0; i != key.length; i++)
{
key[i] = 0;
}
return param;
}
}
}
|