1 package org.bouncycastle.crypto.engines;
2 
3 import org.bouncycastle.crypto.BlockCipher;
4 import org.bouncycastle.crypto.CipherParameters;
5 import org.bouncycastle.crypto.DataLengthException;
6 import org.bouncycastle.crypto.OutputLengthException;
7 
8 /**
9  * The no-op engine that just copies bytes through, irrespective of whether encrypting and decrypting.
10  * Provided for the sake of completeness.
11  */
12 public class NullEngine implements BlockCipher
13 {
14     private boolean initialised;
15     protected static final int DEFAULT_BLOCK_SIZE = 1;
16     private final int blockSize;
17 
18     /**
19      * Constructs a null engine with a block size of 1 byte.
20      */
NullEngine()21     public NullEngine()
22     {
23         this(DEFAULT_BLOCK_SIZE);
24     }
25 
26     /**
27      * Constructs a null engine with a specific block size.
28      *
29      * @param blockSize the block size in bytes.
30      */
NullEngine(int blockSize)31     public NullEngine(int blockSize)
32     {
33         this.blockSize = blockSize;
34     }
35 
36     /* (non-Javadoc)
37      * @see org.bouncycastle.crypto.BlockCipher#init(boolean, org.bouncycastle.crypto.CipherParameters)
38      */
init(boolean forEncryption, CipherParameters params)39     public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException
40     {
41         // we don't mind any parameters that may come in
42         this.initialised = true;
43     }
44 
45     /* (non-Javadoc)
46      * @see org.bouncycastle.crypto.BlockCipher#getAlgorithmName()
47      */
getAlgorithmName()48     public String getAlgorithmName()
49     {
50         return "Null";
51     }
52 
53     /* (non-Javadoc)
54      * @see org.bouncycastle.crypto.BlockCipher#getBlockSize()
55      */
getBlockSize()56     public int getBlockSize()
57     {
58         return blockSize;
59     }
60 
61     /* (non-Javadoc)
62      * @see org.bouncycastle.crypto.BlockCipher#processBlock(byte[], int, byte[], int)
63      */
processBlock(byte[] in, int inOff, byte[] out, int outOff)64     public int processBlock(byte[] in, int inOff, byte[] out, int outOff)
65         throws DataLengthException, IllegalStateException
66     {
67         if (!initialised)
68         {
69             throw new IllegalStateException("Null engine not initialised");
70         }
71         if ((inOff + blockSize) > in.length)
72         {
73             throw new DataLengthException("input buffer too short");
74         }
75 
76         if ((outOff + blockSize) > out.length)
77         {
78             throw new OutputLengthException("output buffer too short");
79         }
80 
81         for (int i = 0; i < blockSize; ++i)
82         {
83             out[outOff + i] = in[inOff + i];
84         }
85 
86         return blockSize;
87     }
88 
89     /* (non-Javadoc)
90      * @see org.bouncycastle.crypto.BlockCipher#reset()
91      */
reset()92     public void reset()
93     {
94         // nothing needs to be done
95     }
96 }
97