comparison src/com/trilead/ssh2/crypto/cipher/BlockCipherFactory.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.crypto.cipher;
3
4 import java.util.Vector;
5
6 /**
7 * BlockCipherFactory.
8 *
9 * @author Christian Plattner, plattner@trilead.com
10 * @version $Id: BlockCipherFactory.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
11 */
12 public class BlockCipherFactory {
13 static class CipherEntry {
14 String type;
15 int blocksize;
16 int keysize;
17 String cipherClass;
18
19 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) {
20 this.type = type;
21 this.blocksize = blockSize;
22 this.keysize = keySize;
23 this.cipherClass = cipherClass;
24 }
25 }
26
27 static Vector<CipherEntry> ciphers = new Vector<CipherEntry>();
28
29 static {
30 /* Higher Priority First */
31 ciphers.addElement(new CipherEntry("aes256-ctr", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
32 ciphers.addElement(new CipherEntry("aes192-ctr", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
33 ciphers.addElement(new CipherEntry("aes128-ctr", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
34 ciphers.addElement(new CipherEntry("blowfish-ctr", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));
35 ciphers.addElement(new CipherEntry("aes256-cbc", 16, 32, "com.trilead.ssh2.crypto.cipher.AES"));
36 ciphers.addElement(new CipherEntry("aes192-cbc", 16, 24, "com.trilead.ssh2.crypto.cipher.AES"));
37 ciphers.addElement(new CipherEntry("aes128-cbc", 16, 16, "com.trilead.ssh2.crypto.cipher.AES"));
38 ciphers.addElement(new CipherEntry("blowfish-cbc", 8, 16, "com.trilead.ssh2.crypto.cipher.BlowFish"));
39 ciphers.addElement(new CipherEntry("3des-ctr", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
40 ciphers.addElement(new CipherEntry("3des-cbc", 8, 24, "com.trilead.ssh2.crypto.cipher.DESede"));
41 }
42
43 public static String[] getDefaultCipherList() {
44 String list[] = new String[ciphers.size()];
45
46 for (int i = 0; i < ciphers.size(); i++) {
47 CipherEntry ce = ciphers.elementAt(i);
48 list[i] = new String(ce.type);
49 }
50
51 return list;
52 }
53
54 public static void checkCipherList(String[] cipherCandidates) {
55 for (int i = 0; i < cipherCandidates.length; i++)
56 getEntry(cipherCandidates[i]);
57 }
58
59 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) {
60 try {
61 CipherEntry ce = getEntry(type);
62 Class cc = Class.forName(ce.cipherClass);
63 BlockCipher bc = (BlockCipher) cc.newInstance();
64
65 if (type.endsWith("-cbc")) {
66 bc.init(encrypt, key);
67 return new CBCMode(bc, iv, encrypt);
68 }
69 else if (type.endsWith("-ctr")) {
70 bc.init(true, key);
71 return new CTRMode(bc, iv, encrypt);
72 }
73
74 throw new IllegalArgumentException("Cannot instantiate " + type);
75 }
76 catch (Exception e) {
77 throw new IllegalArgumentException("Cannot instantiate " + type);
78 }
79 }
80
81 private static CipherEntry getEntry(String type) {
82 for (int i = 0; i < ciphers.size(); i++) {
83 CipherEntry ce = ciphers.elementAt(i);
84
85 if (ce.type.equals(type))
86 return ce;
87 }
88
89 throw new IllegalArgumentException("Unkown algorithm " + type);
90 }
91
92 public static int getBlockSize(String type) {
93 CipherEntry ce = getEntry(type);
94 return ce.blocksize;
95 }
96
97 public static int getKeySize(String type) {
98 CipherEntry ce = getEntry(type);
99 return ce.keysize;
100 }
101 }