PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/android/upstream/org/bouncycastle/jce/provider/JCEStreamCipher.java

https://bitbucket.org/festevezga/xobotos
Java | 526 lines | 313 code | 54 blank | 159 comment | 42 complexity | c611731d14ad00adb148ae8bf976b7b2 MD5 | raw file
  1. package org.bouncycastle.jce.provider;
  2. import java.security.AlgorithmParameters;
  3. import java.security.InvalidAlgorithmParameterException;
  4. import java.security.InvalidKeyException;
  5. import java.security.Key;
  6. import java.security.SecureRandom;
  7. import java.security.spec.AlgorithmParameterSpec;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.ShortBufferException;
  12. import javax.crypto.spec.IvParameterSpec;
  13. import javax.crypto.spec.PBEParameterSpec;
  14. // BEGIN android-removed
  15. // import javax.crypto.spec.RC2ParameterSpec;
  16. // import javax.crypto.spec.RC5ParameterSpec;
  17. // END android-removed
  18. import org.bouncycastle.crypto.BlockCipher;
  19. import org.bouncycastle.crypto.CipherParameters;
  20. import org.bouncycastle.crypto.DataLengthException;
  21. import org.bouncycastle.crypto.StreamBlockCipher;
  22. import org.bouncycastle.crypto.StreamCipher;
  23. // BEGIN android-removed
  24. // import org.bouncycastle.crypto.engines.BlowfishEngine;
  25. // import org.bouncycastle.crypto.engines.DESEngine;
  26. // import org.bouncycastle.crypto.engines.DESedeEngine;
  27. // END android-removed
  28. import org.bouncycastle.crypto.engines.RC4Engine;
  29. // BEGIN android-removed
  30. // import org.bouncycastle.crypto.engines.SkipjackEngine;
  31. // import org.bouncycastle.crypto.engines.TwofishEngine;
  32. // END android-removed
  33. import org.bouncycastle.crypto.modes.CFBBlockCipher;
  34. import org.bouncycastle.crypto.modes.OFBBlockCipher;
  35. import org.bouncycastle.crypto.params.KeyParameter;
  36. import org.bouncycastle.crypto.params.ParametersWithIV;
  37. public class JCEStreamCipher
  38. extends WrapCipherSpi implements PBE
  39. {
  40. //
  41. // specs we can handle.
  42. //
  43. private Class[] availableSpecs =
  44. {
  45. // BEGIN android-removed
  46. // RC2ParameterSpec.class,
  47. // RC5ParameterSpec.class,
  48. // END android-removed
  49. IvParameterSpec.class,
  50. PBEParameterSpec.class
  51. };
  52. private StreamCipher cipher;
  53. private ParametersWithIV ivParam;
  54. private int ivLength = 0;
  55. private PBEParameterSpec pbeSpec = null;
  56. private String pbeAlgorithm = null;
  57. protected JCEStreamCipher(
  58. StreamCipher engine,
  59. int ivLength)
  60. {
  61. cipher = engine;
  62. this.ivLength = ivLength;
  63. }
  64. protected JCEStreamCipher(
  65. BlockCipher engine,
  66. int ivLength)
  67. {
  68. this.ivLength = ivLength;
  69. cipher = new StreamBlockCipher(engine);
  70. }
  71. protected int engineGetBlockSize()
  72. {
  73. return 0;
  74. }
  75. protected byte[] engineGetIV()
  76. {
  77. return (ivParam != null) ? ivParam.getIV() : null;
  78. }
  79. protected int engineGetKeySize(
  80. Key key)
  81. {
  82. return key.getEncoded().length * 8;
  83. }
  84. protected int engineGetOutputSize(
  85. int inputLen)
  86. {
  87. return inputLen;
  88. }
  89. protected AlgorithmParameters engineGetParameters()
  90. {
  91. if (engineParams == null)
  92. {
  93. if (pbeSpec != null)
  94. {
  95. try
  96. {
  97. AlgorithmParameters engineParams = AlgorithmParameters.getInstance(pbeAlgorithm, BouncyCastleProvider.PROVIDER_NAME);
  98. engineParams.init(pbeSpec);
  99. return engineParams;
  100. }
  101. catch (Exception e)
  102. {
  103. return null;
  104. }
  105. }
  106. }
  107. return engineParams;
  108. }
  109. /**
  110. * should never be called.
  111. */
  112. protected void engineSetMode(
  113. String mode)
  114. {
  115. if (!mode.equalsIgnoreCase("ECB"))
  116. {
  117. throw new IllegalArgumentException("can't support mode " + mode);
  118. }
  119. }
  120. /**
  121. * should never be called.
  122. */
  123. protected void engineSetPadding(
  124. String padding)
  125. throws NoSuchPaddingException
  126. {
  127. if (!padding.equalsIgnoreCase("NoPadding"))
  128. {
  129. throw new NoSuchPaddingException("Padding " + padding + " unknown.");
  130. }
  131. }
  132. protected void engineInit(
  133. int opmode,
  134. Key key,
  135. AlgorithmParameterSpec params,
  136. SecureRandom random)
  137. throws InvalidKeyException, InvalidAlgorithmParameterException
  138. {
  139. CipherParameters param;
  140. this.pbeSpec = null;
  141. this.pbeAlgorithm = null;
  142. this.engineParams = null;
  143. //
  144. // basic key check
  145. //
  146. if (!(key instanceof SecretKey))
  147. {
  148. throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
  149. }
  150. if (key instanceof JCEPBEKey)
  151. {
  152. JCEPBEKey k = (JCEPBEKey)key;
  153. if (k.getOID() != null)
  154. {
  155. pbeAlgorithm = k.getOID().getId();
  156. }
  157. else
  158. {
  159. pbeAlgorithm = k.getAlgorithm();
  160. }
  161. if (k.getParam() != null)
  162. {
  163. param = k.getParam();
  164. pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
  165. }
  166. else if (params instanceof PBEParameterSpec)
  167. {
  168. param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
  169. pbeSpec = (PBEParameterSpec)params;
  170. }
  171. else
  172. {
  173. throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
  174. }
  175. if (k.getIvSize() != 0)
  176. {
  177. ivParam = (ParametersWithIV)param;
  178. }
  179. }
  180. else if (params == null)
  181. {
  182. param = new KeyParameter(key.getEncoded());
  183. }
  184. else if (params instanceof IvParameterSpec)
  185. {
  186. param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec)params).getIV());
  187. ivParam = (ParametersWithIV)param;
  188. }
  189. else
  190. {
  191. throw new IllegalArgumentException("unknown parameter type.");
  192. }
  193. if ((ivLength != 0) && !(param instanceof ParametersWithIV))
  194. {
  195. SecureRandom ivRandom = random;
  196. if (ivRandom == null)
  197. {
  198. ivRandom = new SecureRandom();
  199. }
  200. if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE))
  201. {
  202. byte[] iv = new byte[ivLength];
  203. ivRandom.nextBytes(iv);
  204. param = new ParametersWithIV(param, iv);
  205. ivParam = (ParametersWithIV)param;
  206. }
  207. else
  208. {
  209. throw new InvalidAlgorithmParameterException("no IV set when one expected");
  210. }
  211. }
  212. switch (opmode)
  213. {
  214. case Cipher.ENCRYPT_MODE:
  215. case Cipher.WRAP_MODE:
  216. cipher.init(true, param);
  217. break;
  218. case Cipher.DECRYPT_MODE:
  219. case Cipher.UNWRAP_MODE:
  220. cipher.init(false, param);
  221. break;
  222. default:
  223. System.out.println("eeek!");
  224. }
  225. }
  226. protected void engineInit(
  227. int opmode,
  228. Key key,
  229. AlgorithmParameters params,
  230. SecureRandom random)
  231. throws InvalidKeyException, InvalidAlgorithmParameterException
  232. {
  233. AlgorithmParameterSpec paramSpec = null;
  234. if (params != null)
  235. {
  236. for (int i = 0; i != availableSpecs.length; i++)
  237. {
  238. try
  239. {
  240. paramSpec = params.getParameterSpec(availableSpecs[i]);
  241. break;
  242. }
  243. catch (Exception e)
  244. {
  245. continue;
  246. }
  247. }
  248. if (paramSpec == null)
  249. {
  250. throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
  251. }
  252. }
  253. engineInit(opmode, key, paramSpec, random);
  254. engineParams = params;
  255. }
  256. protected void engineInit(
  257. int opmode,
  258. Key key,
  259. SecureRandom random)
  260. throws InvalidKeyException
  261. {
  262. try
  263. {
  264. engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
  265. }
  266. catch (InvalidAlgorithmParameterException e)
  267. {
  268. throw new InvalidKeyException(e.getMessage());
  269. }
  270. }
  271. protected byte[] engineUpdate(
  272. byte[] input,
  273. int inputOffset,
  274. int inputLen)
  275. {
  276. byte[] out = new byte[inputLen];
  277. cipher.processBytes(input, inputOffset, inputLen, out, 0);
  278. return out;
  279. }
  280. protected int engineUpdate(
  281. byte[] input,
  282. int inputOffset,
  283. int inputLen,
  284. byte[] output,
  285. int outputOffset)
  286. throws ShortBufferException
  287. {
  288. try
  289. {
  290. cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
  291. return inputLen;
  292. }
  293. catch (DataLengthException e)
  294. {
  295. throw new ShortBufferException(e.getMessage());
  296. }
  297. }
  298. protected byte[] engineDoFinal(
  299. byte[] input,
  300. int inputOffset,
  301. int inputLen)
  302. {
  303. if (inputLen != 0)
  304. {
  305. byte[] out = engineUpdate(input, inputOffset, inputLen);
  306. cipher.reset();
  307. return out;
  308. }
  309. cipher.reset();
  310. return new byte[0];
  311. }
  312. protected int engineDoFinal(
  313. byte[] input,
  314. int inputOffset,
  315. int inputLen,
  316. byte[] output,
  317. int outputOffset)
  318. {
  319. if (inputLen != 0)
  320. {
  321. cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
  322. }
  323. cipher.reset();
  324. return inputLen;
  325. }
  326. /*
  327. * The ciphers that inherit from us.
  328. */
  329. // BEGIN android-removed
  330. // /**
  331. // * DES
  332. // */
  333. // static public class DES_CFB8
  334. // extends JCEStreamCipher
  335. // {
  336. // public DES_CFB8()
  337. // {
  338. // super(new CFBBlockCipher(new DESEngine(), 8), 64);
  339. // }
  340. // }
  341. //
  342. // /**
  343. // * DESede
  344. // */
  345. // static public class DESede_CFB8
  346. // extends JCEStreamCipher
  347. // {
  348. // public DESede_CFB8()
  349. // {
  350. // super(new CFBBlockCipher(new DESedeEngine(), 8), 64);
  351. // }
  352. // }
  353. //
  354. // /**
  355. // * SKIPJACK
  356. // */
  357. // static public class Skipjack_CFB8
  358. // extends JCEStreamCipher
  359. // {
  360. // public Skipjack_CFB8()
  361. // {
  362. // super(new CFBBlockCipher(new SkipjackEngine(), 8), 64);
  363. // }
  364. // }
  365. //
  366. // /**
  367. // * Blowfish
  368. // */
  369. // static public class Blowfish_CFB8
  370. // extends JCEStreamCipher
  371. // {
  372. // public Blowfish_CFB8()
  373. // {
  374. // super(new CFBBlockCipher(new BlowfishEngine(), 8), 64);
  375. // }
  376. // }
  377. //
  378. // /**
  379. // * Twofish
  380. // */
  381. // static public class Twofish_CFB8
  382. // extends JCEStreamCipher
  383. // {
  384. // public Twofish_CFB8()
  385. // {
  386. // super(new CFBBlockCipher(new TwofishEngine(), 8), 128);
  387. // }
  388. // }
  389. //
  390. // /**
  391. // * DES
  392. // */
  393. // static public class DES_OFB8
  394. // extends JCEStreamCipher
  395. // {
  396. // public DES_OFB8()
  397. // {
  398. // super(new OFBBlockCipher(new DESEngine(), 8), 64);
  399. // }
  400. // }
  401. //
  402. // /**
  403. // * DESede
  404. // */
  405. // static public class DESede_OFB8
  406. // extends JCEStreamCipher
  407. // {
  408. // public DESede_OFB8()
  409. // {
  410. // super(new OFBBlockCipher(new DESedeEngine(), 8), 64);
  411. // }
  412. // }
  413. //
  414. // /**
  415. // * SKIPJACK
  416. // */
  417. // static public class Skipjack_OFB8
  418. // extends JCEStreamCipher
  419. // {
  420. // public Skipjack_OFB8()
  421. // {
  422. // super(new OFBBlockCipher(new SkipjackEngine(), 8), 64);
  423. // }
  424. // }
  425. //
  426. // /**
  427. // * Blowfish
  428. // */
  429. // static public class Blowfish_OFB8
  430. // extends JCEStreamCipher
  431. // {
  432. // public Blowfish_OFB8()
  433. // {
  434. // super(new OFBBlockCipher(new BlowfishEngine(), 8), 64);
  435. // }
  436. // }
  437. //
  438. // /**
  439. // * Twofish
  440. // */
  441. // static public class Twofish_OFB8
  442. // extends JCEStreamCipher
  443. // {
  444. // public Twofish_OFB8()
  445. // {
  446. // super(new OFBBlockCipher(new TwofishEngine(), 8), 128);
  447. // }
  448. // }
  449. // END android-removed
  450. /**
  451. * PBEWithSHAAnd128BitRC4
  452. */
  453. static public class PBEWithSHAAnd128BitRC4
  454. extends JCEStreamCipher
  455. {
  456. public PBEWithSHAAnd128BitRC4()
  457. {
  458. super(new RC4Engine(), 0);
  459. }
  460. }
  461. /**
  462. * PBEWithSHAAnd40BitRC4
  463. */
  464. static public class PBEWithSHAAnd40BitRC4
  465. extends JCEStreamCipher
  466. {
  467. public PBEWithSHAAnd40BitRC4()
  468. {
  469. super(new RC4Engine(), 0);
  470. }
  471. }
  472. }