PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ch/ethz/ssh2/crypto/cipher/BlockCipherFactory.java

https://bitbucket.org/indeni/ganymed-ssh2
Java | 134 lines | 104 code | 15 blank | 15 comment | 7 complexity | 070c584cad95e48d0780f75a0e12d34c MD5 | raw file
  1. /*
  2. * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
  3. * Please refer to the LICENSE.txt for licensing details.
  4. */
  5. package ch.ethz.ssh2.crypto.cipher;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Vector;
  9. /**
  10. * BlockCipherFactory.
  11. *
  12. * @author Christian Plattner
  13. * @version $Id$
  14. */
  15. public class BlockCipherFactory
  16. {
  17. private static final class CipherEntry
  18. {
  19. String type;
  20. int blocksize;
  21. int keysize;
  22. String cipherClass;
  23. public CipherEntry(String type, int blockSize, int keySize, String cipherClass)
  24. {
  25. this.type = type;
  26. this.blocksize = blockSize;
  27. this.keysize = keySize;
  28. this.cipherClass = cipherClass;
  29. }
  30. }
  31. private static final List<CipherEntry> ciphers = new Vector<CipherEntry>();
  32. static
  33. {
  34. /* Higher Priority First */
  35. ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
  36. ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
  37. ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
  38. ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
  39. ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"));
  40. ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"));
  41. ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"));
  42. ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"));
  43. ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
  44. ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"));
  45. }
  46. public static String[] getDefaultCipherList()
  47. {
  48. List<String> list = new ArrayList<String>(ciphers.size());
  49. for (CipherEntry ce : ciphers)
  50. {
  51. list.add(ce.type);
  52. }
  53. return list.toArray(new String[ciphers.size()]);
  54. }
  55. public static void checkCipherList(String[] cipherCandidates)
  56. {
  57. for (String cipherCandidate : cipherCandidates)
  58. {
  59. getEntry(cipherCandidate);
  60. }
  61. }
  62. // @SuppressWarnings("rawtypes")
  63. public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv)
  64. {
  65. try
  66. {
  67. CipherEntry ce = getEntry(type);
  68. Class cc = Class.forName(ce.cipherClass);
  69. BlockCipher bc = (BlockCipher) cc.newInstance();
  70. if (type.endsWith("-cbc"))
  71. {
  72. bc.init(encrypt, key);
  73. return new CBCMode(bc, iv, encrypt);
  74. }
  75. else if (type.endsWith("-ctr"))
  76. {
  77. bc.init(true, key);
  78. return new CTRMode(bc, iv, encrypt);
  79. }
  80. // MODIFIED BY INDENI:
  81. // Having two exceptions with the same text
  82. // may be confusing during debugging.
  83. throw new IllegalArgumentException("Unsupported block cipher type: " + type);
  84. }
  85. catch (ClassNotFoundException e)
  86. {
  87. throw new IllegalArgumentException("Cannot instantiate " + type + ", class not found exception\n", e);
  88. }
  89. catch (InstantiationException e)
  90. {
  91. throw new IllegalArgumentException("Cannot instantiate " + type + ", instantiation exception\n", e);
  92. }
  93. catch (IllegalAccessException e)
  94. {
  95. throw new IllegalArgumentException("Cannot instantiate " + type + ", illegal access\n", e);
  96. }
  97. }
  98. private static CipherEntry getEntry(String type)
  99. {
  100. for (CipherEntry ce : ciphers)
  101. {
  102. if (ce.type.equals(type))
  103. {
  104. return ce;
  105. }
  106. }
  107. throw new IllegalArgumentException("Unkown algorithm " + type);
  108. }
  109. public static int getBlockSize(String type)
  110. {
  111. CipherEntry ce = getEntry(type);
  112. return ce.blocksize;
  113. }
  114. public static int getKeySize(String type)
  115. {
  116. CipherEntry ce = getEntry(type);
  117. return ce.keysize;
  118. }
  119. }