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

/src/main/java/org/bouncycastle/jce/provider/JCEKeyGenerator.java

https://github.com/MIPS/external-bouncycastle
Java | 538 lines | 167 code | 38 blank | 333 comment | 5 complexity | f90b41e763c39cd8f6b9012d99b9edda MD5 | raw file
  1. package org.bouncycastle.jce.provider;
  2. import org.bouncycastle.crypto.CipherKeyGenerator;
  3. import org.bouncycastle.crypto.KeyGenerationParameters;
  4. import org.bouncycastle.crypto.generators.DESKeyGenerator;
  5. import org.bouncycastle.crypto.generators.DESedeKeyGenerator;
  6. import javax.crypto.KeyGeneratorSpi;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import java.security.InvalidAlgorithmParameterException;
  10. import java.security.InvalidParameterException;
  11. import java.security.SecureRandom;
  12. import java.security.spec.AlgorithmParameterSpec;
  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. engine.init(new KeyGenerationParameters(random, defaultKeySize));
  43. uninitialised = false;
  44. }
  45. }
  46. protected void engineInit(
  47. int keySize,
  48. SecureRandom random)
  49. {
  50. try
  51. {
  52. engine.init(new KeyGenerationParameters(random, keySize));
  53. uninitialised = false;
  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(new SecureRandom(), defaultKeySize));
  65. uninitialised = false;
  66. }
  67. return 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(new SecureRandom(), defaultKeySize));
  110. uninitialised = false;
  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. // BEGIN android-removed
  129. // /**
  130. // * generate a desEDE key in the a-b-c format.
  131. // */
  132. // public static class DESede3
  133. // extends JCEKeyGenerator
  134. // {
  135. // public DESede3()
  136. // {
  137. // super("DESede3", 192, new DESedeKeyGenerator());
  138. // }
  139. // }
  140. //
  141. // /**
  142. // * SKIPJACK
  143. // */
  144. // public static class Skipjack
  145. // extends JCEKeyGenerator
  146. // {
  147. // public Skipjack()
  148. // {
  149. // super("SKIPJACK", 80, new CipherKeyGenerator());
  150. // }
  151. // }
  152. // END android-removed
  153. /**
  154. * Blowfish
  155. */
  156. public static class Blowfish
  157. extends JCEKeyGenerator
  158. {
  159. public Blowfish()
  160. {
  161. super("Blowfish", 128, new CipherKeyGenerator());
  162. }
  163. }
  164. // BEGIN android-removed
  165. // /**
  166. // * Twofish
  167. // */
  168. // public static class Twofish
  169. // extends JCEKeyGenerator
  170. // {
  171. // public Twofish()
  172. // {
  173. // super("Twofish", 256, new CipherKeyGenerator());
  174. // }
  175. // }
  176. //
  177. // /**
  178. // * RC2
  179. // */
  180. // public static class RC2
  181. // extends JCEKeyGenerator
  182. // {
  183. // public RC2()
  184. // {
  185. // super("RC2", 128, new CipherKeyGenerator());
  186. // }
  187. // }
  188. // END android-removed
  189. /**
  190. * RC4
  191. */
  192. public static class RC4
  193. extends JCEKeyGenerator
  194. {
  195. public RC4()
  196. {
  197. super("RC4", 128, new CipherKeyGenerator());
  198. }
  199. }
  200. // BEGIN android-removed
  201. // /**
  202. // * RC5
  203. // */
  204. // public static class RC5
  205. // extends JCEKeyGenerator
  206. // {
  207. // public RC5()
  208. // {
  209. // super("RC5", 128, new CipherKeyGenerator());
  210. // }
  211. // }
  212. //
  213. // /**
  214. // * RC5
  215. // */
  216. // public static class RC564
  217. // extends JCEKeyGenerator
  218. // {
  219. // public RC564()
  220. // {
  221. // super("RC5-64", 256, new CipherKeyGenerator());
  222. // }
  223. // }
  224. //
  225. // /**
  226. // * RC6
  227. // */
  228. // public static class RC6
  229. // extends JCEKeyGenerator
  230. // {
  231. // public RC6()
  232. // {
  233. // super("RC6", 256, new CipherKeyGenerator());
  234. // }
  235. // }
  236. //
  237. // /**
  238. // * GOST28147
  239. // */
  240. // public static class GOST28147
  241. // extends JCEKeyGenerator
  242. // {
  243. // public GOST28147()
  244. // {
  245. // super("GOST28147", 256, new CipherKeyGenerator());
  246. // }
  247. // }
  248. // /**
  249. // * Rijndael
  250. // */
  251. // public static class Rijndael
  252. // extends JCEKeyGenerator
  253. // {
  254. // public Rijndael()
  255. // {
  256. // super("Rijndael", 192, new CipherKeyGenerator());
  257. // }
  258. // }
  259. //
  260. // /**
  261. // * Serpent
  262. // */
  263. // public static class Serpent
  264. // extends JCEKeyGenerator
  265. // {
  266. // public Serpent()
  267. // {
  268. // super("Serpent", 192, new CipherKeyGenerator());
  269. // }
  270. // }
  271. //
  272. //
  273. //
  274. // /**
  275. // * CAST6
  276. // */
  277. // public static class CAST6
  278. // extends JCEKeyGenerator
  279. // {
  280. // public CAST6()
  281. // {
  282. // super("CAST6", 256, new CipherKeyGenerator());
  283. // }
  284. // }
  285. //
  286. // /**
  287. // * TEA
  288. // */
  289. // public static class TEA
  290. // extends JCEKeyGenerator
  291. // {
  292. // public TEA()
  293. // {
  294. // super("TEA", 128, new CipherKeyGenerator());
  295. // }
  296. // }
  297. //
  298. // /**
  299. // * XTEA
  300. // */
  301. // public static class XTEA
  302. // extends JCEKeyGenerator
  303. // {
  304. // public XTEA()
  305. // {
  306. // super("XTEA", 128, new CipherKeyGenerator());
  307. // }
  308. // }
  309. //
  310. // /**
  311. // * Salsa20
  312. // */
  313. // public static class Salsa20
  314. // extends JCEKeyGenerator
  315. // {
  316. // public Salsa20()
  317. // {
  318. // super("Salsa20", 128, new CipherKeyGenerator());
  319. // }
  320. // }
  321. //
  322. // /**
  323. // * HC128
  324. // */
  325. // public static class HC128
  326. // extends JCEKeyGenerator
  327. // {
  328. // public HC128()
  329. // {
  330. // super("HC128", 128, new CipherKeyGenerator());
  331. // }
  332. // }
  333. //
  334. // /**
  335. // * HC256
  336. // */
  337. // public static class HC256
  338. // extends JCEKeyGenerator
  339. // {
  340. // public HC256()
  341. // {
  342. // super("HC256", 256, new CipherKeyGenerator());
  343. // }
  344. // }
  345. //
  346. // /**
  347. // * VMPC
  348. // */
  349. // public static class VMPC
  350. // extends JCEKeyGenerator
  351. // {
  352. // public VMPC()
  353. // {
  354. // super("VMPC", 128, new CipherKeyGenerator());
  355. // }
  356. // }
  357. //
  358. // /**
  359. // * VMPC-KSA3
  360. // */
  361. // public static class VMPCKSA3
  362. // extends JCEKeyGenerator
  363. // {
  364. // public VMPCKSA3()
  365. // {
  366. // super("VMPC-KSA3", 128, new CipherKeyGenerator());
  367. // }
  368. // }
  369. // END android-removed
  370. // HMAC Related secret keys..
  371. // BEGIN android-removed
  372. // /**
  373. // * MD2HMAC
  374. // */
  375. // public static class MD2HMAC
  376. // extends JCEKeyGenerator
  377. // {
  378. // public MD2HMAC()
  379. // {
  380. // super("HMACMD2", 128, new CipherKeyGenerator());
  381. // }
  382. // }
  383. //
  384. //
  385. // /**
  386. // * MD4HMAC
  387. // */
  388. // public static class MD4HMAC
  389. // extends JCEKeyGenerator
  390. // {
  391. // public MD4HMAC()
  392. // {
  393. // super("HMACMD4", 128, new CipherKeyGenerator());
  394. // }
  395. // }
  396. // END android-removed
  397. /**
  398. * MD5HMAC
  399. */
  400. public static class MD5HMAC
  401. extends JCEKeyGenerator
  402. {
  403. public MD5HMAC()
  404. {
  405. super("HMACMD5", 128, new CipherKeyGenerator());
  406. }
  407. }
  408. // /**
  409. // * RIPE128HMAC
  410. // */
  411. // public static class RIPEMD128HMAC
  412. // extends JCEKeyGenerator
  413. // {
  414. // public RIPEMD128HMAC()
  415. // {
  416. // super("HMACRIPEMD128", 128, new CipherKeyGenerator());
  417. // }
  418. // }
  419. // /**
  420. // * RIPE160HMAC
  421. // */
  422. // public static class RIPEMD160HMAC
  423. // extends JCEKeyGenerator
  424. // {
  425. // public RIPEMD160HMAC()
  426. // {
  427. // super("HMACRIPEMD160", 160, new CipherKeyGenerator());
  428. // }
  429. // }
  430. /**
  431. * HMACSHA1
  432. */
  433. public static class HMACSHA1
  434. extends JCEKeyGenerator
  435. {
  436. public HMACSHA1()
  437. {
  438. super("HMACSHA1", 160, new CipherKeyGenerator());
  439. }
  440. }
  441. // BEGIN android-removed
  442. // /**
  443. // * HMACSHA224
  444. // */
  445. // public static class HMACSHA224
  446. // extends JCEKeyGenerator
  447. // {
  448. // public HMACSHA224()
  449. // {
  450. // super("HMACSHA224", 224, new CipherKeyGenerator());
  451. // }
  452. // }
  453. // END android-removed
  454. /**
  455. * HMACSHA256
  456. */
  457. public static class HMACSHA256
  458. extends JCEKeyGenerator
  459. {
  460. public HMACSHA256()
  461. {
  462. super("HMACSHA256", 256, new CipherKeyGenerator());
  463. }
  464. }
  465. /**
  466. * HMACSHA384
  467. */
  468. public static class HMACSHA384
  469. extends JCEKeyGenerator
  470. {
  471. public HMACSHA384()
  472. {
  473. super("HMACSHA384", 384, new CipherKeyGenerator());
  474. }
  475. }
  476. /**
  477. * HMACSHA512
  478. */
  479. public static class HMACSHA512
  480. extends JCEKeyGenerator
  481. {
  482. public HMACSHA512()
  483. {
  484. super("HMACSHA512", 512, new CipherKeyGenerator());
  485. }
  486. }
  487. // BEGIN android-removed
  488. // /**
  489. // * HMACTIGER
  490. // */
  491. // public static class HMACTIGER
  492. // extends JCEKeyGenerator
  493. // {
  494. // public HMACTIGER()
  495. // {
  496. // super("HMACTIGER", 192, new CipherKeyGenerator());
  497. // }
  498. // }
  499. // END android-removed
  500. }