PageRenderTime 1146ms CodeModel.GetById 23ms RepoModel.GetById 10ms app.codeStats 0ms

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

https://github.com/truedat101/fiskidagur
Java | 539 lines | 376 code | 61 blank | 102 comment | 6 complexity | 664eac893be2cd52850a9893fef15fe3 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. /**
  358. * MD2HMAC
  359. */
  360. public static class MD2HMAC
  361. extends JCEKeyGenerator
  362. {
  363. public MD2HMAC()
  364. {
  365. super("HMACMD2", 128, new CipherKeyGenerator());
  366. }
  367. }
  368. /**
  369. * MD4HMAC
  370. */
  371. public static class MD4HMAC
  372. extends JCEKeyGenerator
  373. {
  374. public MD4HMAC()
  375. {
  376. super("HMACMD4", 128, new CipherKeyGenerator());
  377. }
  378. }
  379. /**
  380. * MD5HMAC
  381. */
  382. public static class MD5HMAC
  383. extends JCEKeyGenerator
  384. {
  385. public MD5HMAC()
  386. {
  387. super("HMACMD5", 128, new CipherKeyGenerator());
  388. }
  389. }
  390. /**
  391. * RIPE128HMAC
  392. */
  393. public static class RIPEMD128HMAC
  394. extends JCEKeyGenerator
  395. {
  396. public RIPEMD128HMAC()
  397. {
  398. super("HMACRIPEMD128", 128, new CipherKeyGenerator());
  399. }
  400. }
  401. /**
  402. * RIPE160HMAC
  403. */
  404. public static class RIPEMD160HMAC
  405. extends JCEKeyGenerator
  406. {
  407. public RIPEMD160HMAC()
  408. {
  409. super("HMACRIPEMD160", 160, new CipherKeyGenerator());
  410. }
  411. }
  412. /**
  413. * HMACSHA1
  414. */
  415. public static class HMACSHA1
  416. extends JCEKeyGenerator
  417. {
  418. public HMACSHA1()
  419. {
  420. super("HMACSHA1", 160, new CipherKeyGenerator());
  421. }
  422. }
  423. /**
  424. * HMACSHA224
  425. */
  426. public static class HMACSHA224
  427. extends JCEKeyGenerator
  428. {
  429. public HMACSHA224()
  430. {
  431. super("HMACSHA224", 224, new CipherKeyGenerator());
  432. }
  433. }
  434. /**
  435. * HMACSHA256
  436. */
  437. public static class HMACSHA256
  438. extends JCEKeyGenerator
  439. {
  440. public HMACSHA256()
  441. {
  442. super("HMACSHA256", 256, new CipherKeyGenerator());
  443. }
  444. }
  445. /**
  446. * HMACSHA384
  447. */
  448. public static class HMACSHA384
  449. extends JCEKeyGenerator
  450. {
  451. public HMACSHA384()
  452. {
  453. super("HMACSHA384", 384, new CipherKeyGenerator());
  454. }
  455. }
  456. /**
  457. * HMACSHA512
  458. */
  459. public static class HMACSHA512
  460. extends JCEKeyGenerator
  461. {
  462. public HMACSHA512()
  463. {
  464. super("HMACSHA512", 512, new CipherKeyGenerator());
  465. }
  466. }
  467. /**
  468. * HMACTIGER
  469. */
  470. public static class HMACTIGER
  471. extends JCEKeyGenerator
  472. {
  473. public HMACTIGER()
  474. {
  475. super("HMACTIGER", 192, new CipherKeyGenerator());
  476. }
  477. }
  478. }