001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.crypto.CipherParameters;
004: import org.bouncycastle.crypto.Mac;
005: import org.bouncycastle.crypto.digests.MD2Digest;
006: import org.bouncycastle.crypto.digests.MD4Digest;
007: import org.bouncycastle.crypto.digests.MD5Digest;
008: import org.bouncycastle.crypto.digests.RIPEMD128Digest;
009: import org.bouncycastle.crypto.digests.RIPEMD160Digest;
010: import org.bouncycastle.crypto.digests.SHA1Digest;
011: import org.bouncycastle.crypto.digests.SHA224Digest;
012: import org.bouncycastle.crypto.digests.SHA256Digest;
013: import org.bouncycastle.crypto.digests.SHA384Digest;
014: import org.bouncycastle.crypto.digests.SHA512Digest;
015: import org.bouncycastle.crypto.digests.TigerDigest;
016: import org.bouncycastle.crypto.engines.DESEngine;
017: import org.bouncycastle.crypto.engines.DESedeEngine;
018: import org.bouncycastle.crypto.engines.IDEAEngine;
019: import org.bouncycastle.crypto.engines.RC2Engine;
020: import org.bouncycastle.crypto.engines.RC532Engine;
021: import org.bouncycastle.crypto.engines.SkipjackEngine;
022: import org.bouncycastle.crypto.macs.CBCBlockCipherMac;
023: import org.bouncycastle.crypto.macs.CFBBlockCipherMac;
024: import org.bouncycastle.crypto.macs.GOST28147Mac;
025: import org.bouncycastle.crypto.macs.HMac;
026: import org.bouncycastle.crypto.macs.ISO9797Alg3Mac;
027: import org.bouncycastle.crypto.macs.OldHMac;
028: import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
029: import org.bouncycastle.crypto.params.KeyParameter;
030: import org.bouncycastle.crypto.params.ParametersWithIV;
031:
032: import javax.crypto.MacSpi;
033: import javax.crypto.spec.IvParameterSpec;
034: import javax.crypto.spec.PBEParameterSpec;
035: import java.security.InvalidAlgorithmParameterException;
036: import java.security.InvalidKeyException;
037: import java.security.Key;
038: import java.security.spec.AlgorithmParameterSpec;
039:
040: public class JCEMac extends MacSpi implements PBE {
041: private Mac macEngine;
042:
043: private int pbeType = PKCS12;
044: private int pbeHash = SHA1;
045: private int keySize = 160;
046:
047: protected JCEMac(Mac macEngine) {
048: this .macEngine = macEngine;
049: }
050:
051: protected JCEMac(Mac macEngine, int pbeType, int pbeHash,
052: int keySize) {
053: this .macEngine = macEngine;
054: this .pbeType = pbeType;
055: this .pbeHash = pbeHash;
056: this .keySize = keySize;
057: }
058:
059: protected void engineInit(Key key, AlgorithmParameterSpec params)
060: throws InvalidKeyException,
061: InvalidAlgorithmParameterException {
062: CipherParameters param;
063:
064: if (key == null) {
065: throw new InvalidKeyException("key is null");
066: }
067:
068: if (key instanceof JCEPBEKey) {
069: JCEPBEKey k = (JCEPBEKey) key;
070:
071: if (k.getParam() != null) {
072: param = k.getParam();
073: } else if (params instanceof PBEParameterSpec) {
074: param = PBE.Util.makePBEMacParameters(k, params);
075: } else {
076: throw new InvalidAlgorithmParameterException(
077: "PBE requires PBE parameters to be set.");
078: }
079: } else if (params instanceof IvParameterSpec) {
080: param = new ParametersWithIV(new KeyParameter(key
081: .getEncoded()), ((IvParameterSpec) params).getIV());
082: } else if (params == null) {
083: param = new KeyParameter(key.getEncoded());
084: } else {
085: throw new InvalidAlgorithmParameterException(
086: "unknown parameter type.");
087: }
088:
089: macEngine.init(param);
090: }
091:
092: protected int engineGetMacLength() {
093: return macEngine.getMacSize();
094: }
095:
096: protected void engineReset() {
097: macEngine.reset();
098: }
099:
100: protected void engineUpdate(byte input) {
101: macEngine.update(input);
102: }
103:
104: protected void engineUpdate(byte[] input, int offset, int len) {
105: macEngine.update(input, offset, len);
106: }
107:
108: protected byte[] engineDoFinal() {
109: byte[] out = new byte[engineGetMacLength()];
110:
111: macEngine.doFinal(out, 0);
112:
113: return out;
114: }
115:
116: /**
117: * the classes that extend directly off us.
118: */
119:
120: /**
121: * DES
122: */
123: public static class DES extends JCEMac {
124: public DES() {
125: super (new CBCBlockCipherMac(new DESEngine()));
126: }
127: }
128:
129: /**
130: * DESede
131: */
132: public static class DESede extends JCEMac {
133: public DESede() {
134: super (new CBCBlockCipherMac(new DESedeEngine()));
135: }
136: }
137:
138: /**
139: * SKIPJACK
140: */
141: public static class Skipjack extends JCEMac {
142: public Skipjack() {
143: super (new CBCBlockCipherMac(new SkipjackEngine()));
144: }
145: }
146:
147: /**
148: * IDEA
149: */
150: public static class IDEA extends JCEMac {
151: public IDEA() {
152: super (new CBCBlockCipherMac(new IDEAEngine()));
153: }
154: }
155:
156: /**
157: * RC2
158: */
159: public static class RC2 extends JCEMac {
160: public RC2() {
161: super (new CBCBlockCipherMac(new RC2Engine()));
162: }
163: }
164:
165: /**
166: * RC5
167: */
168: public static class RC5 extends JCEMac {
169: public RC5() {
170: super (new CBCBlockCipherMac(new RC532Engine()));
171: }
172: }
173:
174: /**
175: * GOST28147
176: */
177: public static class GOST28147 extends JCEMac {
178: public GOST28147() {
179: super (new GOST28147Mac());
180: }
181: }
182:
183: /**
184: * DES
185: */
186: public static class DESCFB8 extends JCEMac {
187: public DESCFB8() {
188: super (new CFBBlockCipherMac(new DESEngine()));
189: }
190: }
191:
192: /**
193: * DESede
194: */
195: public static class DESedeCFB8 extends JCEMac {
196: public DESedeCFB8() {
197: super (new CFBBlockCipherMac(new DESedeEngine()));
198: }
199: }
200:
201: /**
202: * SKIPJACK
203: */
204: public static class SkipjackCFB8 extends JCEMac {
205: public SkipjackCFB8() {
206: super (new CFBBlockCipherMac(new SkipjackEngine()));
207: }
208: }
209:
210: /**
211: * IDEACFB8
212: */
213: public static class IDEACFB8 extends JCEMac {
214: public IDEACFB8() {
215: super (new CFBBlockCipherMac(new IDEAEngine()));
216: }
217: }
218:
219: /**
220: * RC2CFB8
221: */
222: public static class RC2CFB8 extends JCEMac {
223: public RC2CFB8() {
224: super (new CFBBlockCipherMac(new RC2Engine()));
225: }
226: }
227:
228: /**
229: * RC5CFB8
230: */
231: public static class RC5CFB8 extends JCEMac {
232: public RC5CFB8() {
233: super (new CFBBlockCipherMac(new RC532Engine()));
234: }
235: }
236:
237: /**
238: * DESede64
239: */
240: public static class DESede64 extends JCEMac {
241: public DESede64() {
242: super (new CBCBlockCipherMac(new DESedeEngine(), 64));
243: }
244: }
245:
246: /**
247: * DESede64with7816-4Padding
248: */
249: public static class DESede64with7816d4 extends JCEMac {
250: public DESede64with7816d4() {
251: super (new CBCBlockCipherMac(new DESedeEngine(), 64,
252: new ISO7816d4Padding()));
253: }
254: }
255:
256: /**
257: * DES9797Alg3with7816-4Padding
258: */
259: public static class DES9797Alg3with7816d4 extends JCEMac {
260: public DES9797Alg3with7816d4() {
261: super (new ISO9797Alg3Mac(new DESEngine(),
262: new ISO7816d4Padding()));
263: }
264: }
265:
266: /**
267: * DES9797Alg3
268: */
269: public static class DES9797Alg3 extends JCEMac {
270: public DES9797Alg3() {
271: super (new ISO9797Alg3Mac(new DESEngine()));
272: }
273: }
274:
275: /**
276: * MD2 HMac
277: */
278: public static class MD2 extends JCEMac {
279: public MD2() {
280: super (new HMac(new MD2Digest()));
281: }
282: }
283:
284: /**
285: * MD4 HMac
286: */
287: public static class MD4 extends JCEMac {
288: public MD4() {
289: super (new HMac(new MD4Digest()));
290: }
291: }
292:
293: /**
294: * MD5 HMac
295: */
296: public static class MD5 extends JCEMac {
297: public MD5() {
298: super (new HMac(new MD5Digest()));
299: }
300: }
301:
302: /**
303: * SHA1 HMac
304: */
305: public static class SHA1 extends JCEMac {
306: public SHA1() {
307: super (new HMac(new SHA1Digest()));
308: }
309: }
310:
311: /**
312: * SHA-224 HMac
313: */
314: public static class SHA224 extends JCEMac {
315: public SHA224() {
316: super (new HMac(new SHA224Digest()));
317: }
318: }
319:
320: /**
321: * SHA-256 HMac
322: */
323: public static class SHA256 extends JCEMac {
324: public SHA256() {
325: super (new HMac(new SHA256Digest()));
326: }
327: }
328:
329: /**
330: * SHA-384 HMac
331: */
332: public static class SHA384 extends JCEMac {
333: public SHA384() {
334: super (new HMac(new SHA384Digest()));
335: }
336: }
337:
338: public static class OldSHA384 extends JCEMac {
339: public OldSHA384() {
340: super (new OldHMac(new SHA384Digest()));
341: }
342: }
343:
344: /**
345: * SHA-512 HMac
346: */
347: public static class SHA512 extends JCEMac {
348: public SHA512() {
349: super (new HMac(new SHA512Digest()));
350: }
351: }
352:
353: /**
354: * SHA-512 HMac
355: */
356: public static class OldSHA512 extends JCEMac {
357: public OldSHA512() {
358: super (new OldHMac(new SHA512Digest()));
359: }
360: }
361:
362: /**
363: * RIPEMD128 HMac
364: */
365: public static class RIPEMD128 extends JCEMac {
366: public RIPEMD128() {
367: super (new HMac(new RIPEMD128Digest()));
368: }
369: }
370:
371: /**
372: * RIPEMD160 HMac
373: */
374: public static class RIPEMD160 extends JCEMac {
375: public RIPEMD160() {
376: super (new HMac(new RIPEMD160Digest()));
377: }
378: }
379:
380: /**
381: * Tiger HMac
382: */
383: public static class Tiger extends JCEMac {
384: public Tiger() {
385: super (new HMac(new TigerDigest()));
386: }
387: }
388:
389: //
390: // PKCS12 states that the same algorithm should be used
391: // for the key generation as is used in the HMAC, so that
392: // is what we do here.
393: //
394:
395: /**
396: * PBEWithHmacRIPEMD160
397: */
398: public static class PBEWithRIPEMD160 extends JCEMac {
399: public PBEWithRIPEMD160() {
400: super (new HMac(new RIPEMD160Digest()), PKCS12, RIPEMD160,
401: 160);
402: }
403: }
404:
405: /**
406: * PBEWithHmacSHA
407: */
408: public static class PBEWithSHA extends JCEMac {
409: public PBEWithSHA() {
410: super (new HMac(new SHA1Digest()), PKCS12, SHA1, 160);
411: }
412: }
413:
414: /**
415: * PBEWithHmacTiger
416: */
417: public static class PBEWithTiger extends JCEMac {
418: public PBEWithTiger() {
419: super (new HMac(new TigerDigest()), PKCS12, TIGER, 192);
420: }
421: }
422: }
|