Home | History | Annotate | Download | only in cipher
      1 package ch.ethz.ssh2.crypto.cipher;
      2 
      3 /*
      4  This file was shamelessly taken (and modified) from the Bouncy Castle Crypto package.
      5  Their licence file states the following:
      6 
      7  Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle
      8  (http://www.bouncycastle.org)
      9 
     10  Permission is hereby granted, free of charge, to any person obtaining a copy
     11  of this software and associated documentation files (the "Software"), to deal
     12  in the Software without restriction, including without limitation the rights
     13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     14  copies of the Software, and to permit persons to whom the Software is
     15  furnished to do so, subject to the following conditions:
     16 
     17  The above copyright notice and this permission notice shall be included in
     18  all copies or substantial portions of the Software.
     19 
     20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     26  THE SOFTWARE.
     27  */
     28 
     29 /**
     30  * DESede.
     31  *
     32  * @author See comments in the source file
     33  * @version 2.50, 03/15/10
     34  *
     35  */
     36 public class DESede extends DES
     37 {
     38 	private int[] key1 = null;
     39 	private int[] key2 = null;
     40 	private int[] key3 = null;
     41 
     42 	private boolean encrypt;
     43 
     44 	/**
     45 	 * standard constructor.
     46 	 */
     47 	public DESede()
     48 	{
     49 	}
     50 
     51 	/**
     52 	 * initialise a DES cipher.
     53 	 *
     54 	 * @param encrypting
     55 	 *            whether or not we are for encryption.
     56 	 * @param key
     57 	 *            the parameters required to set up the cipher.
     58 	 * @exception IllegalArgumentException
     59 	 *                if the params argument is inappropriate.
     60 	 */
     61 	@Override
     62 	public void init(boolean encrypting, byte[] key)
     63 	{
     64 		key1 = generateWorkingKey(encrypting, key, 0);
     65 		key2 = generateWorkingKey(!encrypting, key, 8);
     66 		key3 = generateWorkingKey(encrypting, key, 16);
     67 
     68 		encrypt = encrypting;
     69 	}
     70 
     71 	@Override
     72 	public String getAlgorithmName()
     73 	{
     74 		return "DESede";
     75 	}
     76 
     77 	@Override
     78 	public int getBlockSize()
     79 	{
     80 		return 8;
     81 	}
     82 
     83 	@Override
     84 	public void transformBlock(byte[] in, int inOff, byte[] out, int outOff)
     85 	{
     86 		if (key1 == null)
     87 		{
     88 			throw new IllegalStateException("DESede engine not initialised!");
     89 		}
     90 
     91 		if (encrypt)
     92 		{
     93 			desFunc(key1, in, inOff, out, outOff);
     94 			desFunc(key2, out, outOff, out, outOff);
     95 			desFunc(key3, out, outOff, out, outOff);
     96 		}
     97 		else
     98 		{
     99 			desFunc(key3, in, inOff, out, outOff);
    100 			desFunc(key2, out, outOff, out, outOff);
    101 			desFunc(key1, out, outOff, out, outOff);
    102 		}
    103 	}
    104 
    105 	@Override
    106 	public void reset()
    107 	{
    108 	}
    109 }
    110