001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (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 General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.crypto.specs;
019
020import java.io.InputStream;
021import java.io.OutputStream;
022
023import org.bouncycastle.crypto.engines.TwofishEngine;
024import org.bouncycastle.crypto.modes.AEADBlockCipher;
025import org.bouncycastle.crypto.modes.GCMBlockCipher;
026import org.bouncycastle.crypto.params.AEADParameters;
027import org.bouncycastle.crypto.params.KeyParameter;
028import org.syncany.crypto.CipherException;
029import org.syncany.crypto.CipherSpec;
030
031public abstract class TwofishGcmCipherSpec extends CipherSpec {
032        private static final int MAC_SIZE = 128;                
033
034        public TwofishGcmCipherSpec(int id, String algorithm, int keySize, int ivSize, boolean needsUnlimitedStrength) {
035                super(id, algorithm, keySize, ivSize, needsUnlimitedStrength);
036        }
037                
038        @Override
039        public OutputStream newCipherOutputStream(OutputStream underlyingOutputStream, byte[] secretKey, byte[] iv) throws CipherException {
040                AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine()); 
041                cipher.init(true, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
042                
043                return new org.bouncycastle.crypto.io.CipherOutputStream(underlyingOutputStream, cipher);
044        }
045
046        @Override
047        public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
048                AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine()); 
049                cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
050                
051                return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
052        }
053}