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.asymmetric; 019 020 import org.bouncycastle.crypto.BufferedAsymmetricBlockCipher; 021 import org.bouncycastle.crypto.CipherParameters; 022 import org.bouncycastle.crypto.CryptoException; 023 import org.bouncycastle.crypto.DataLengthException; 024 import org.cumulus4j.crypto.AbstractCipher; 025 import org.cumulus4j.crypto.CipherOperationMode; 026 027 /** 028 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de 029 */ 030 public class AsymmetricBlockCipherImpl 031 extends AbstractCipher 032 { 033 private BufferedAsymmetricBlockCipher delegate; 034 035 public AsymmetricBlockCipherImpl(String transformation, BufferedAsymmetricBlockCipher delegate) { 036 super(transformation); 037 this.delegate = delegate; 038 } 039 040 @Override 041 public void _init(CipherOperationMode mode, CipherParameters parameters) { 042 delegate.init(CipherOperationMode.ENCRYPT == mode, parameters); 043 } 044 045 @Override 046 public int getInputBlockSize() { 047 return delegate.getInputBlockSize(); 048 } 049 050 @Override 051 public int getOutputBlockSize() { 052 return delegate.getOutputBlockSize(); 053 } 054 055 @Override 056 public void reset() { 057 // does not exist in delegate => not necessary?! 058 } 059 060 @Override 061 public int getUpdateOutputSize(int length) { 062 return getOutputSize(length); // this is not correct and very pessimistic, but for now, it is at least sth. that shouldn't produce any errors (the result should be >= the real value). 063 } 064 065 @Override 066 public int getOutputSize(int length) { 067 return getOutputBlockSize(); // Copied this from org.bouncycastle.jce.provider.JCERSACipher. 068 } 069 070 @Override 071 public int update(byte in, byte[] out, int outOff) 072 throws DataLengthException, IllegalStateException, CryptoException 073 { 074 delegate.processByte(in); 075 return 0; 076 } 077 078 @Override 079 public int update(byte[] in, int inOff, int inLen, byte[] out, int outOff) 080 throws DataLengthException, IllegalStateException, CryptoException 081 { 082 delegate.processBytes(in, inOff, inLen); 083 return 0; 084 } 085 086 @Override 087 public int doFinal(byte[] out, int outOff) 088 throws DataLengthException, IllegalStateException, CryptoException 089 { 090 byte[] encrypted = delegate.doFinal(); 091 System.arraycopy(encrypted, 0, out, outOff, encrypted.length); 092 return encrypted.length; 093 } 094 095 @Override 096 public int getIVSize() { 097 return 0; 098 } 099 100 // @Override 101 // public AsymmetricCipherKeyPairGenerator createKeyPairGenerator(boolean initWithDefaults) 102 // { 103 // String algorithmName = CryptoRegistry.splitTransformation(getTransformation())[0]; 104 // try { 105 // return CryptoRegistry.sharedInstance().createKeyPairGenerator(algorithmName, initWithDefaults); 106 // } catch (NoSuchAlgorithmException e) { 107 // throw new RuntimeException(e); // We should be able to provide an Asymmetric...KeyPairGenerator for every Cipher => RuntimeException 108 // } 109 // } 110 // 111 // @Override 112 // public SecretKeyGenerator createSecretKeyGenerator(boolean initWithDefaults) 113 // throws UnsupportedOperationException 114 // { 115 // throw new UnsupportedOperationException("This is an ASYMMETRIC cipher! Cannot get an appropriate secret key generator."); 116 // } 117 }