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.AESEngine; 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 031/** 032 * @author pheckel 033 * 034 */ 035public abstract class AesGcmCipherSpec extends CipherSpec { 036 private static final int MAC_SIZE = 128; 037 038 public AesGcmCipherSpec(int id, String algorithm, int keySize, int ivSize, boolean needsUnlimitedStrength) { 039 super(id, algorithm, keySize, ivSize, needsUnlimitedStrength); 040 } 041 042 @Override 043 public OutputStream newCipherOutputStream(OutputStream underlyingOutputStream, byte[] secretKey, byte[] iv) throws CipherException { 044 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); 045 cipher.init(true, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv)); 046 047 return new org.bouncycastle.crypto.io.CipherOutputStream(underlyingOutputStream, cipher); 048 } 049 050 @Override 051 public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException { 052 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); 053 cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv)); 054 055 return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher); 056 } 057}