001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.crypto.internal.mac;
019    
020    import org.bouncycastle.crypto.BlockCipher;
021    import org.bouncycastle.crypto.Digest;
022    import org.bouncycastle.crypto.digests.MD2Digest;
023    import org.bouncycastle.crypto.digests.MD4Digest;
024    import org.bouncycastle.crypto.digests.MD5Digest;
025    import org.bouncycastle.crypto.digests.RIPEMD128Digest;
026    import org.bouncycastle.crypto.digests.RIPEMD160Digest;
027    import org.bouncycastle.crypto.digests.SHA1Digest;
028    import org.bouncycastle.crypto.digests.SHA224Digest;
029    import org.bouncycastle.crypto.digests.SHA256Digest;
030    import org.bouncycastle.crypto.digests.SHA384Digest;
031    import org.bouncycastle.crypto.digests.SHA512Digest;
032    import org.bouncycastle.crypto.digests.TigerDigest;
033    import org.bouncycastle.crypto.engines.DESEngine;
034    import org.bouncycastle.crypto.engines.RC2Engine;
035    import org.bouncycastle.crypto.macs.CBCBlockCipherMac;
036    import org.bouncycastle.crypto.macs.CFBBlockCipherMac;
037    import org.bouncycastle.crypto.macs.GOST28147Mac;
038    import org.bouncycastle.crypto.macs.HMac;
039    import org.bouncycastle.crypto.macs.ISO9797Alg3Mac;
040    import org.bouncycastle.crypto.macs.OldHMac;
041    import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
042    import org.cumulus4j.crypto.AbstractMACCalculatorFactory;
043    import org.cumulus4j.crypto.MACCalculator;
044    
045    /**
046     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
047     */
048    public abstract class MACCalculatorFactoryImpl
049    extends AbstractMACCalculatorFactory
050    {
051            public static class DES
052            extends MACCalculatorFactoryImpl
053            {
054                    @Override
055                    public MACCalculator _createMACCalculator() {
056                            BlockCipher cipher = new DESEngine();
057                            return new MACCalculatorImpl(new CBCBlockCipherMac(cipher), cipher.getBlockSize(), cipher.getBlockSize());
058                    }
059            }
060    
061            public static class DES64
062            extends MACCalculatorFactoryImpl
063            {
064                    @Override
065                    public MACCalculator _createMACCalculator() {
066                            BlockCipher cipher = new DESEngine();
067                            return new MACCalculatorImpl(new CBCBlockCipherMac(cipher, 64), cipher.getBlockSize(), cipher.getBlockSize());
068                    }
069            }
070    
071            public static class RC2
072            extends MACCalculatorFactoryImpl
073            {
074                    @Override
075                    public MACCalculator _createMACCalculator() {
076                            BlockCipher cipher = new RC2Engine();
077                            return new MACCalculatorImpl(new CBCBlockCipherMac(cipher), cipher.getBlockSize(), cipher.getBlockSize());
078                    }
079            }
080    
081            public static class GOST28147
082            extends MACCalculatorFactoryImpl
083            {
084                    @Override
085                    public MACCalculator _createMACCalculator() {
086                            return new MACCalculatorImpl(new GOST28147Mac(), 32, 0); // IV not supported - throws exception when passing ParameterWithIV
087                    }
088            }
089    
090            public static class DESCFB8
091            extends MACCalculatorFactoryImpl
092            {
093                    @Override
094                    public MACCalculator _createMACCalculator() {
095                            BlockCipher cipher = new DESEngine();
096                            return new MACCalculatorImpl(new CFBBlockCipherMac(cipher), cipher.getBlockSize(), cipher.getBlockSize());
097                    }
098            }
099    
100            public static class RC2CFB8
101            extends MACCalculatorFactoryImpl
102            {
103                    @Override
104                    public MACCalculator _createMACCalculator() {
105                            BlockCipher cipher = new RC2Engine();
106                            return new MACCalculatorImpl(new CFBBlockCipherMac(cipher), cipher.getBlockSize(), cipher.getBlockSize());
107                    }
108            }
109    
110            public static class DES9797Alg3with7816d4
111            extends MACCalculatorFactoryImpl
112            {
113                    @Override
114                    public MACCalculator _createMACCalculator() {
115                            BlockCipher cipher = new DESEngine();
116                            return new MACCalculatorImpl(new ISO9797Alg3Mac(cipher, new ISO7816d4Padding()), 24, cipher.getBlockSize());
117                    }
118            }
119    
120            public static class DES9797Alg3
121            extends MACCalculatorFactoryImpl
122            {
123                    @Override
124                    public MACCalculator _createMACCalculator() {
125                            BlockCipher cipher = new DESEngine();
126                            return new MACCalculatorImpl(new ISO9797Alg3Mac(cipher), 24, cipher.getBlockSize());
127                    }
128            }
129    
130            public static class MD2
131            extends MACCalculatorFactoryImpl
132            {
133                    @Override
134                    public MACCalculator _createMACCalculator() {
135                            Digest digest = new MD2Digest();
136                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
137                    }
138            }
139    
140            public static class MD4
141            extends MACCalculatorFactoryImpl
142            {
143                    @Override
144                    public MACCalculator _createMACCalculator() {
145                            Digest digest = new MD4Digest();
146                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
147                    }
148            }
149    
150            public static class MD5
151            extends MACCalculatorFactoryImpl
152            {
153                    @Override
154                    public MACCalculator _createMACCalculator() {
155                            MD5Digest digest = new MD5Digest();
156                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
157                    }
158            }
159    
160            public static class SHA1
161            extends MACCalculatorFactoryImpl
162            {
163                    @Override
164                    public MACCalculator _createMACCalculator() {
165                            SHA1Digest digest = new SHA1Digest();
166                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
167                    }
168            }
169    
170            public static class SHA224
171            extends MACCalculatorFactoryImpl
172            {
173                    @Override
174                    public MACCalculator _createMACCalculator() {
175                            SHA224Digest digest = new SHA224Digest();
176                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
177                    }
178            }
179    
180            public static class SHA256
181            extends MACCalculatorFactoryImpl
182            {
183                    @Override
184                    public MACCalculator _createMACCalculator() {
185                            SHA256Digest digest = new SHA256Digest();
186                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
187                    }
188            }
189    
190            public static class SHA384
191            extends MACCalculatorFactoryImpl
192            {
193                    @Override
194                    public MACCalculator _createMACCalculator() {
195                            SHA384Digest digest = new SHA384Digest();
196                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
197                    }
198            }
199    
200            /**
201             * @deprecated See {@link OldHMac}.
202             */
203            @Deprecated
204            public static class OldSHA384
205            extends MACCalculatorFactoryImpl
206            {
207                    @Override
208                    public MACCalculator _createMACCalculator() {
209                            SHA384Digest digest = new SHA384Digest();
210                            return new MACCalculatorImpl(new OldHMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
211                    }
212            }
213    
214            public static class SHA512
215            extends MACCalculatorFactoryImpl
216            {
217                    @Override
218                    public MACCalculator _createMACCalculator() {
219                            SHA512Digest digest = new SHA512Digest();
220                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
221                    }
222            }
223    
224            /**
225             * @deprecated See {@link OldHMac}.
226             */
227            @Deprecated
228            public static class OldSHA512
229            extends MACCalculatorFactoryImpl
230            {
231                    @Override
232                    public MACCalculator _createMACCalculator() {
233                            SHA512Digest digest = new SHA512Digest();
234                            return new MACCalculatorImpl(new OldHMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
235                    }
236            }
237    
238            public static class RIPEMD128
239            extends MACCalculatorFactoryImpl
240            {
241                    @Override
242                    public MACCalculator _createMACCalculator() {
243                            RIPEMD128Digest digest = new RIPEMD128Digest();
244                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
245                    }
246            }
247    
248            public static class RIPEMD160
249            extends MACCalculatorFactoryImpl
250            {
251                    @Override
252                    public MACCalculator _createMACCalculator() {
253                            RIPEMD160Digest digest = new RIPEMD160Digest();
254                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
255                    }
256            }
257    
258            public static class Tiger
259            extends MACCalculatorFactoryImpl
260            {
261                    @Override
262                    public MACCalculator _createMACCalculator() {
263                            TigerDigest digest = new TigerDigest();
264                            return new MACCalculatorImpl(new HMac(digest), digest.getDigestSize(), 0); // IV not supported - throws exception when passing ParameterWithIV
265                    }
266            }
267    
268    // TODO implement the following 3 later.
269    //      //
270    //      // PKCS12 states that the same algorithm should be used
271    //      // for the key generation as is used in the HMAC, so that
272    //      // is what we do here.
273    //      //
274    //
275    //      public static class PBEWithRIPEMD160
276    //      extends MACCalculatorFactoryImpl
277    //      {
278    //              public PBEWithRIPEMD160()
279    //              {
280    //                      super(new HMac(new RIPEMD160Digest()), PKCS12, RIPEMD160, 160);
281    //              }
282    //      }
283    //
284    //      public static class PBEWithSHA
285    //      extends MACCalculatorFactoryImpl
286    //      {
287    //              public PBEWithSHA()
288    //              {
289    //                      super(new HMac(new SHA1Digest()), PKCS12, SHA1, 160);
290    //              }
291    //      }
292    //
293    //      public static class PBEWithTiger
294    //      extends MACCalculatorFactoryImpl
295    //      {
296    //              public PBEWithTiger()
297    //              {
298    //                      super(new HMac(new TigerDigest()), PKCS12, TIGER, 192);
299    //              }
300    //      }
301    }