comparison src/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children 8c1451f51a5e
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
12 * BlockCipherFactory. 12 * BlockCipherFactory.
13 * 13 *
14 * @author Christian Plattner 14 * @author Christian Plattner
15 * @version $Id: BlockCipherFactory.java 86 2014-04-07 14:15:18Z dkocher@sudo.ch $ 15 * @version $Id: BlockCipherFactory.java 86 2014-04-07 14:15:18Z dkocher@sudo.ch $
16 */ 16 */
17 public class BlockCipherFactory 17 public class BlockCipherFactory {
18 { 18 private static final class CipherEntry {
19 private static final class CipherEntry 19 String type;
20 { 20 int blocksize;
21 String type; 21 int keysize;
22 int blocksize; 22 String cipherClass;
23 int keysize;
24 String cipherClass;
25 23
26 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) 24 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) {
27 { 25 this.type = type;
28 this.type = type; 26 this.blocksize = blockSize;
29 this.blocksize = blockSize; 27 this.keysize = keySize;
30 this.keysize = keySize; 28 this.cipherClass = cipherClass;
31 this.cipherClass = cipherClass; 29 }
32 } 30 }
33 }
34 31
35 private static final List<CipherEntry> ciphers = new ArrayList<CipherEntry>(); 32 private static final List<CipherEntry> ciphers = new ArrayList<CipherEntry>();
36 33
37 static 34 static {
38 { 35 /* Higher Priority First */
39 /* Higher Priority First */ 36 ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
40 ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES")); 37 ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
41 ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); 38 ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
42 ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES")); 39 ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
43 ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish")); 40 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
41 ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
42 ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
43 ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
44 ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
45 ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
46 }
44 47
45 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES")); 48 public static String[] getDefaultCipherList() {
46 ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); 49 List<String> list = new ArrayList<String>(ciphers.size());
47 ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
48 ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
49 50
50 ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede")); 51 for (CipherEntry ce : ciphers) {
51 ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede")); 52 list.add(ce.type);
52 } 53 }
53 54
54 public static String[] getDefaultCipherList() 55 return list.toArray(new String[ciphers.size()]);
55 { 56 }
56 List<String> list = new ArrayList<String>(ciphers.size());
57 for (CipherEntry ce : ciphers)
58 {
59 list.add(ce.type);
60 }
61 return list.toArray(new String[ciphers.size()]);
62 }
63 57
64 public static void checkCipherList(String[] cipherCandidates) 58 public static void checkCipherList(String[] cipherCandidates) {
65 { 59 for (String cipherCandidate : cipherCandidates) {
66 for (String cipherCandidate : cipherCandidates) 60 getEntry(cipherCandidate);
67 { 61 }
68 getEntry(cipherCandidate); 62 }
69 }
70 }
71 63
72 // @SuppressWarnings("rawtypes") 64 // @SuppressWarnings("rawtypes")
73 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) 65 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) {
74 { 66 try {
75 try 67 CipherEntry ce = getEntry(type);
76 { 68 Class<?> cc = Class.forName(ce.cipherClass);
77 CipherEntry ce = getEntry(type); 69 BlockCipher bc = (BlockCipher) cc.newInstance();
78 Class<?> cc = Class.forName(ce.cipherClass);
79 BlockCipher bc = (BlockCipher) cc.newInstance();
80 70
81 if (type.endsWith("-cbc")) 71 if (type.endsWith("-cbc")) {
82 { 72 bc.init(encrypt, key);
83 bc.init(encrypt, key); 73 return new CBCMode(bc, iv, encrypt);
84 return new CBCMode(bc, iv, encrypt); 74 }
85 } 75 else if (type.endsWith("-ctr")) {
86 else if (type.endsWith("-ctr")) 76 bc.init(true, key);
87 { 77 return new CTRMode(bc, iv, encrypt);
88 bc.init(true, key); 78 }
89 return new CTRMode(bc, iv, encrypt);
90 }
91 throw new IllegalArgumentException("Cannot instantiate " + type);
92 }
93 catch (ClassNotFoundException e)
94 {
95 throw new IllegalArgumentException("Cannot instantiate " + type, e);
96 }
97 catch (InstantiationException e)
98 {
99 throw new IllegalArgumentException("Cannot instantiate " + type, e);
100 }
101 catch (IllegalAccessException e)
102 {
103 throw new IllegalArgumentException("Cannot instantiate " + type, e);
104 }
105 }
106 79
107 private static CipherEntry getEntry(String type) 80 throw new IllegalArgumentException("Cannot instantiate " + type);
108 { 81 }
109 for (CipherEntry ce : ciphers) 82 catch (ClassNotFoundException e) {
110 { 83 throw new IllegalArgumentException("Cannot instantiate " + type, e);
111 if (ce.type.equals(type)) 84 }
112 { 85 catch (InstantiationException e) {
113 return ce; 86 throw new IllegalArgumentException("Cannot instantiate " + type, e);
114 } 87 }
115 } 88 catch (IllegalAccessException e) {
116 throw new IllegalArgumentException("Unkown algorithm " + type); 89 throw new IllegalArgumentException("Cannot instantiate " + type, e);
117 } 90 }
91 }
118 92
119 public static int getBlockSize(String type) 93 private static CipherEntry getEntry(String type) {
120 { 94 for (CipherEntry ce : ciphers) {
121 CipherEntry ce = getEntry(type); 95 if (ce.type.equals(type)) {
122 return ce.blocksize; 96 return ce;
123 } 97 }
98 }
124 99
125 public static int getKeySize(String type) 100 throw new IllegalArgumentException("Unkown algorithm " + type);
126 { 101 }
127 CipherEntry ce = getEntry(type); 102
128 return ce.keysize; 103 public static int getBlockSize(String type) {
129 } 104 CipherEntry ce = getEntry(type);
105 return ce.blocksize;
106 }
107
108 public static int getKeySize(String type) {
109 CipherEntry ce = getEntry(type);
110 return ce.keysize;
111 }
130 } 112 }