Mercurial > 510Connectbot
annotate app/src/main/java/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java @ 524:609b142218bc
Added tag stable-1.9.4-7 for changeset 775a81a78a2b
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 31 May 2024 21:14:55 -0600 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 /* |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 * Please refer to the LICENSE.txt for licensing details. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 */ |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 package ch.ethz.ssh2.crypto.cipher; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 import java.util.ArrayList; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 import java.util.List; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 import java.util.Vector; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
11 /** |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 * BlockCipherFactory. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
13 * |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 * @author Christian Plattner |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
15 * @version $Id: BlockCipherFactory.java 86 2014-04-07 14:15:18Z dkocher@sudo.ch $ |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
16 */ |
307 | 17 public class BlockCipherFactory { |
18 private static final class CipherEntry { | |
19 String type; | |
20 int blocksize; | |
21 int keysize; | |
22 String cipherClass; | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
23 |
307 | 24 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) { |
25 this.type = type; | |
26 this.blocksize = blockSize; | |
27 this.keysize = keySize; | |
28 this.cipherClass = cipherClass; | |
29 } | |
30 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
31 |
307 | 32 private static final List<CipherEntry> ciphers = new ArrayList<CipherEntry>(); |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
33 |
307 | 34 static { |
375
8c1451f51a5e
kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents:
307
diff
changeset
|
35 // Higher priority (stronger) first |
307 | 36 ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES")); |
375
8c1451f51a5e
kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents:
307
diff
changeset
|
37 ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); |
8c1451f51a5e
kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents:
307
diff
changeset
|
38 ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES")); |
307 | 39 ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish")); |
375
8c1451f51a5e
kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents:
307
diff
changeset
|
40 ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES")); |
8c1451f51a5e
kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents:
307
diff
changeset
|
41 ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); |
307 | 42 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "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 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
47 |
307 | 48 public static String[] getDefaultCipherList() { |
49 List<String> list = new ArrayList<String>(ciphers.size()); | |
50 | |
51 for (CipherEntry ce : ciphers) { | |
52 list.add(ce.type); | |
53 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
54 |
307 | 55 return list.toArray(new String[ciphers.size()]); |
56 } | |
57 | |
58 public static void checkCipherList(String[] cipherCandidates) { | |
59 for (String cipherCandidate : cipherCandidates) { | |
60 getEntry(cipherCandidate); | |
61 } | |
62 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
63 |
307 | 64 // @SuppressWarnings("rawtypes") |
65 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) { | |
66 try { | |
67 CipherEntry ce = getEntry(type); | |
68 Class<?> cc = Class.forName(ce.cipherClass); | |
69 BlockCipher bc = (BlockCipher) cc.newInstance(); | |
70 | |
71 if (type.endsWith("-cbc")) { | |
72 bc.init(encrypt, key); | |
73 return new CBCMode(bc, iv, encrypt); | |
74 } | |
75 else if (type.endsWith("-ctr")) { | |
76 bc.init(true, key); | |
77 return new CTRMode(bc, iv, encrypt); | |
78 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
79 |
307 | 80 throw new IllegalArgumentException("Cannot instantiate " + type); |
81 } | |
82 catch (ClassNotFoundException e) { | |
83 throw new IllegalArgumentException("Cannot instantiate " + type, e); | |
84 } | |
85 catch (InstantiationException e) { | |
86 throw new IllegalArgumentException("Cannot instantiate " + type, e); | |
87 } | |
88 catch (IllegalAccessException e) { | |
89 throw new IllegalArgumentException("Cannot instantiate " + type, e); | |
90 } | |
91 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
92 |
307 | 93 private static CipherEntry getEntry(String type) { |
94 for (CipherEntry ce : ciphers) { | |
95 if (ce.type.equals(type)) { | |
96 return ce; | |
97 } | |
98 } | |
99 | |
100 throw new IllegalArgumentException("Unkown algorithm " + type); | |
101 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
102 |
307 | 103 public static int getBlockSize(String type) { |
104 CipherEntry ce = getEntry(type); | |
105 return ce.blocksize; | |
106 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
107 |
307 | 108 public static int getKeySize(String type) { |
109 CipherEntry ce = getEntry(type); | |
110 return ce.keysize; | |
111 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
112 } |