PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/external/bouncycastle/bcprov/src/main/java/org/bouncycastle/jce/provider/JCEStreamCipher.java

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