/platform/libcore/security/src/main/java/org/bouncycastle/jce/provider/JCEKeyGenerator.java

https://github.com/lems111/Intercept-CM6-Kernel · Java · 541 lines · 368 code · 61 blank · 112 comment · 6 complexity · bae0f496835ecddacc47d52e3ba6dfc7 MD5 · raw file

  1. package org.bouncycastle.jce.provider;
  2. import java.security.InvalidAlgorithmParameterException;
  3. import java.security.InvalidParameterException;
  4. import java.security.SecureRandom;
  5. import java.security.spec.AlgorithmParameterSpec;
  6. import javax.crypto.KeyGeneratorSpi;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import org.bouncycastle.crypto.CipherKeyGenerator;
  10. import org.bouncycastle.crypto.KeyGenerationParameters;
  11. import org.bouncycastle.crypto.generators.DESKeyGenerator;
  12. import org.bouncycastle.crypto.generators.DESedeKeyGenerator;
  13. public class JCEKeyGenerator
  14. extends KeyGeneratorSpi
  15. {
  16. protected String algName;
  17. protected int keySize;
  18. protected int defaultKeySize;
  19. protected CipherKeyGenerator engine;
  20. protected boolean uninitialised = true;
  21. protected JCEKeyGenerator(
  22. String algName,
  23. int defaultKeySize,
  24. CipherKeyGenerator engine)
  25. {
  26. this.algName = algName;
  27. this.keySize = this.defaultKeySize = defaultKeySize;
  28. this.engine = engine;
  29. }
  30. protected void engineInit(
  31. AlgorithmParameterSpec params,
  32. SecureRandom random)
  33. throws InvalidAlgorithmParameterException
  34. {
  35. throw new InvalidAlgorithmParameterException("Not Implemented");
  36. }
  37. protected void engineInit(
  38. SecureRandom random)
  39. {
  40. if (random != null)
  41. {
  42. uninitialised = false;
  43. engine.init(new KeyGenerationParameters(random, defaultKeySize));
  44. }
  45. }
  46. protected void engineInit(
  47. int keySize,
  48. SecureRandom random)
  49. {
  50. uninitialised = false;
  51. try
  52. {
  53. engine.init(new KeyGenerationParameters(random, keySize));
  54. }
  55. catch (IllegalArgumentException e)
  56. {
  57. throw new InvalidParameterException(e.getMessage());
  58. }
  59. }
  60. protected SecretKey engineGenerateKey()
  61. {
  62. if (uninitialised)
  63. {
  64. engine.init(new KeyGenerationParameters(
  65. new SecureRandom(), defaultKeySize));
  66. }
  67. return (SecretKey)(new SecretKeySpec(engine.generateKey(), algName));
  68. }
  69. /**
  70. * the generators that are defined directly off us.
  71. */
  72. /**
  73. * DES
  74. */
  75. public static class DES
  76. extends JCEKeyGenerator
  77. {
  78. public DES()
  79. {
  80. super("DES", 64, new DESKeyGenerator());
  81. }
  82. }
  83. /**
  84. * DESede - the default for this is to generate a key in
  85. * a-b-a format that's 24 bytes long but has 16 bytes of
  86. * key material (the first 8 bytes is repeated as the last
  87. * 8 bytes). If you give it a size, you'll get just what you
  88. * asked for.
  89. */
  90. public static class DESede
  91. extends JCEKeyGenerator
  92. {
  93. private boolean keySizeSet = false;
  94. public DESede()
  95. {
  96. super("DESede", 192, new DESedeKeyGenerator());
  97. }
  98. protected void engineInit(
  99. int keySize,
  100. SecureRandom random)
  101. {
  102. super.engineInit(keySize, random);
  103. keySizeSet = true;
  104. }
  105. protected SecretKey engineGenerateKey()
  106. {
  107. if (uninitialised)
  108. {
  109. engine.init(new KeyGenerationParameters(
  110. new SecureRandom(), defaultKeySize));
  111. }
  112. //
  113. // if no key size has been defined generate a 24 byte key in
  114. // the a-b-a format
  115. //
  116. if (!keySizeSet)
  117. {
  118. byte[] k = engine.generateKey();
  119. System.arraycopy(k, 0, k, 16, 8);
  120. return (SecretKey)(new SecretKeySpec(k, algName));
  121. }
  122. else
  123. {
  124. return (SecretKey)(new SecretKeySpec(engine.generateKey(), algName));
  125. }
  126. }
  127. }
  128. /**
  129. * generate a desEDE key in the a-b-c format.
  130. */
  131. public static class DESede3
  132. extends JCEKeyGenerator
  133. {
  134. private boolean keySizeSet = false;
  135. public DESede3()
  136. {
  137. super("DESede3", 192, new DESedeKeyGenerator());
  138. }
  139. protected void engineInit(
  140. int keySize,
  141. SecureRandom random)
  142. {
  143. super.engineInit(keySize, random);
  144. keySizeSet = true;
  145. }
  146. protected SecretKey engineGenerateKey()
  147. {
  148. if (uninitialised)
  149. {
  150. engine.init(new KeyGenerationParameters(
  151. new SecureRandom(), defaultKeySize));
  152. }
  153. return (SecretKey)(new SecretKeySpec(engine.generateKey(), algName));
  154. }
  155. }
  156. /**
  157. * SKIPJACK
  158. */
  159. public static class Skipjack
  160. extends JCEKeyGenerator
  161. {
  162. public Skipjack()
  163. {
  164. super("SKIPJACK", 80, new CipherKeyGenerator());
  165. }
  166. }
  167. /**
  168. * Blowfish
  169. */
  170. public static class Blowfish
  171. extends JCEKeyGenerator
  172. {
  173. public Blowfish()
  174. {
  175. super("Blowfish", 448, new CipherKeyGenerator());
  176. }
  177. }
  178. /**
  179. * Twofish
  180. */
  181. public static class Twofish
  182. extends JCEKeyGenerator
  183. {
  184. public Twofish()
  185. {
  186. super("Twofish", 256, new CipherKeyGenerator());
  187. }
  188. }
  189. /**
  190. * RC2
  191. */
  192. public static class RC2
  193. extends JCEKeyGenerator
  194. {
  195. public RC2()
  196. {
  197. super("RC2", 128, new CipherKeyGenerator());
  198. }
  199. }
  200. /**
  201. * RC4
  202. */
  203. public static class RC4
  204. extends JCEKeyGenerator
  205. {
  206. public RC4()
  207. {
  208. super("RC4", 128, new CipherKeyGenerator());
  209. }
  210. }
  211. /**
  212. * RC5
  213. */
  214. public static class RC5
  215. extends JCEKeyGenerator
  216. {
  217. public RC5()
  218. {
  219. super("RC5", 128, new CipherKeyGenerator());
  220. }
  221. }
  222. /**
  223. * RC5
  224. */
  225. public static class RC564
  226. extends JCEKeyGenerator
  227. {
  228. public RC564()
  229. {
  230. super("RC5-64", 256, new CipherKeyGenerator());
  231. }
  232. }
  233. /**
  234. * RC6
  235. */
  236. public static class RC6
  237. extends JCEKeyGenerator
  238. {
  239. public RC6()
  240. {
  241. super("RC6", 256, new CipherKeyGenerator());
  242. }
  243. }
  244. /**
  245. * AES
  246. */
  247. public static class AES
  248. extends JCEKeyGenerator
  249. {
  250. public AES()
  251. {
  252. super("AES", 192, new CipherKeyGenerator());
  253. }
  254. }
  255. public static class AES128
  256. extends JCEKeyGenerator
  257. {
  258. public AES128()
  259. {
  260. super("AES", 128, new CipherKeyGenerator());
  261. }
  262. }
  263. public static class AES192
  264. extends JCEKeyGenerator
  265. {
  266. public AES192()
  267. {
  268. super("AES", 192, new CipherKeyGenerator());
  269. }
  270. }
  271. public static class AES256
  272. extends JCEKeyGenerator
  273. {
  274. public AES256()
  275. {
  276. super("AES", 256, new CipherKeyGenerator());
  277. }
  278. }
  279. /**
  280. * GOST28147
  281. */
  282. public static class GOST28147
  283. extends JCEKeyGenerator
  284. {
  285. public GOST28147()
  286. {
  287. super("GOST28147", 256, new CipherKeyGenerator());
  288. }
  289. }
  290. /**
  291. * Rijndael
  292. */
  293. public static class Rijndael
  294. extends JCEKeyGenerator
  295. {
  296. public Rijndael()
  297. {
  298. super("Rijndael", 192, new CipherKeyGenerator());
  299. }
  300. }
  301. /**
  302. * Serpent
  303. */
  304. public static class Serpent
  305. extends JCEKeyGenerator
  306. {
  307. public Serpent()
  308. {
  309. super("Serpent", 192, new CipherKeyGenerator());
  310. }
  311. }
  312. /**
  313. * Camellia
  314. */
  315. public static class Camellia
  316. extends JCEKeyGenerator
  317. {
  318. public Camellia()
  319. {
  320. super("Camellia", 256, new CipherKeyGenerator());
  321. }
  322. }
  323. /**
  324. * CAST5
  325. */
  326. public static class CAST5
  327. extends JCEKeyGenerator
  328. {
  329. public CAST5()
  330. {
  331. super("CAST5", 128, new CipherKeyGenerator());
  332. }
  333. }
  334. /**
  335. * CAST6
  336. */
  337. public static class CAST6
  338. extends JCEKeyGenerator
  339. {
  340. public CAST6()
  341. {
  342. super("CAST6", 256, new CipherKeyGenerator());
  343. }
  344. }
  345. /**
  346. * IDEA
  347. */
  348. public static class IDEA
  349. extends JCEKeyGenerator
  350. {
  351. public IDEA()
  352. {
  353. super("IDEA", 128, new CipherKeyGenerator());
  354. }
  355. }
  356. // HMAC Related secret keys..
  357. // BEGIN android-removed
  358. // /**
  359. // * MD2HMAC
  360. // */
  361. // public static class MD2HMAC
  362. // extends JCEKeyGenerator
  363. // {
  364. // public MD2HMAC()
  365. // {
  366. // super("HMACMD2", 128, new CipherKeyGenerator());
  367. // }
  368. // }
  369. // END android-removed
  370. /**
  371. * MD4HMAC
  372. */
  373. public static class MD4HMAC
  374. extends JCEKeyGenerator
  375. {
  376. public MD4HMAC()
  377. {
  378. super("HMACMD4", 128, new CipherKeyGenerator());
  379. }
  380. }
  381. /**
  382. * MD5HMAC
  383. */
  384. public static class MD5HMAC
  385. extends JCEKeyGenerator
  386. {
  387. public MD5HMAC()
  388. {
  389. super("HMACMD5", 128, new CipherKeyGenerator());
  390. }
  391. }
  392. /**
  393. * RIPE128HMAC
  394. */
  395. public static class RIPEMD128HMAC
  396. extends JCEKeyGenerator
  397. {
  398. public RIPEMD128HMAC()
  399. {
  400. super("HMACRIPEMD128", 128, new CipherKeyGenerator());
  401. }
  402. }
  403. /**
  404. * RIPE160HMAC
  405. */
  406. public static class RIPEMD160HMAC
  407. extends JCEKeyGenerator
  408. {
  409. public RIPEMD160HMAC()
  410. {
  411. super("HMACRIPEMD160", 160, new CipherKeyGenerator());
  412. }
  413. }
  414. /**
  415. * HMACSHA1
  416. */
  417. public static class HMACSHA1
  418. extends JCEKeyGenerator
  419. {
  420. public HMACSHA1()
  421. {
  422. super("HMACSHA1", 160, new CipherKeyGenerator());
  423. }
  424. }
  425. /**
  426. * HMACSHA224
  427. */
  428. public static class HMACSHA224
  429. extends JCEKeyGenerator
  430. {
  431. public HMACSHA224()
  432. {
  433. super("HMACSHA224", 224, new CipherKeyGenerator());
  434. }
  435. }
  436. /**
  437. * HMACSHA256
  438. */
  439. public static class HMACSHA256
  440. extends JCEKeyGenerator
  441. {
  442. public HMACSHA256()
  443. {
  444. super("HMACSHA256", 256, new CipherKeyGenerator());
  445. }
  446. }
  447. /**
  448. * HMACSHA384
  449. */
  450. public static class HMACSHA384
  451. extends JCEKeyGenerator
  452. {
  453. public HMACSHA384()
  454. {
  455. super("HMACSHA384", 384, new CipherKeyGenerator());
  456. }
  457. }
  458. /**
  459. * HMACSHA512
  460. */
  461. public static class HMACSHA512
  462. extends JCEKeyGenerator
  463. {
  464. public HMACSHA512()
  465. {
  466. super("HMACSHA512", 512, new CipherKeyGenerator());
  467. }
  468. }
  469. /**
  470. * HMACTIGER
  471. */
  472. public static class HMACTIGER
  473. extends JCEKeyGenerator
  474. {
  475. public HMACTIGER()
  476. {
  477. super("HMACTIGER", 192, new CipherKeyGenerator());
  478. }
  479. }
  480. }