aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/main/java/com/trilead/ssh2/crypto/cipher/BlockCipherFactory.java
blob: 6e386a5c8d6e0282129741e454559b5a9bc15f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.trilead.ssh2.crypto.cipher;

import java.util.Vector;

/**
 * BlockCipherFactory.
 * 
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: BlockCipherFactory.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
 */
public class BlockCipherFactory
{
	static class CipherEntry
	{
		String type;
		int blocksize;
		int keysize;
		String cipherClass;

		public CipherEntry(String type, int blockSize, int keySize, String cipherClass)
		{
			this.type = type;
			this.blocksize = blockSize;
			this.keysize = keySize;
			this.cipherClass = cipherClass;
		}
	}

	static Vector<CipherEntry> ciphers = new Vector<CipherEntry>();

	static
	{
		/* Higher Priority First */

		ciphers.addElement(new CipherEntry("aes256-ctr", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("aes192-ctr", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("aes128-ctr", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("blowfish-ctr", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));

		ciphers.addElement(new CipherEntry("aes256-cbc", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("aes192-cbc", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("aes128-cbc", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
		ciphers.addElement(new CipherEntry("blowfish-cbc", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));
		
		ciphers.addElement(new CipherEntry("3des-ctr", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
		ciphers.addElement(new CipherEntry("3des-cbc", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
	}

	public static String[] getDefaultCipherList()
	{
		String list[] = new String[ciphers.size()];
		for (int i = 0; i < ciphers.size(); i++)
		{
			CipherEntry ce = ciphers.elementAt(i);
			list[i] = new String(ce.type);
		}
		return list;
	}

	public static void checkCipherList(String[] cipherCandidates)
	{
		for (int i = 0; i < cipherCandidates.length; i++)
			getEntry(cipherCandidates[i]);
	}

	public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv)
	{
		try
		{
			CipherEntry ce = getEntry(type);
			Class cc = Class.forName(ce.cipherClass);
			BlockCipher bc = (BlockCipher) cc.newInstance();

			if (type.endsWith("-cbc"))
			{
				bc.init(encrypt, key);
				return new CBCMode(bc, iv, encrypt);
			}
			else if (type.endsWith("-ctr"))
			{
				bc.init(true, key);
				return new CTRMode(bc, iv, encrypt);
			}
			throw new IllegalArgumentException("Cannot instantiate " + type);
		}
		catch (Exception e)
		{
			throw new IllegalArgumentException("Cannot instantiate " + type);
		}
	}

	private static CipherEntry getEntry(String type)
	{
		for (int i = 0; i < ciphers.size(); i++)
		{
			CipherEntry ce = ciphers.elementAt(i);
			if (ce.type.equals(type))
				return ce;
		}
		throw new IllegalArgumentException("Unkown algorithm " + type);
	}

	public static int getBlockSize(String type)
	{
		CipherEntry ce = getEntry(type);
		return ce.blocksize;
	}

	public static int getKeySize(String type)
	{
		CipherEntry ce = getEntry(type);
		return ce.keysize;
	}
}