PBKDF2SecretKeyFactory.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* PBKDF2SecretKeyFactory.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.jce;
  32. import java.security.spec.InvalidKeySpecException;
  33. import java.security.spec.KeySpec;
  34. import java.util.HashMap;
  35. import javax.crypto.SecretKey;
  36. import javax.crypto.SecretKeyFactorySpi;
  37. import javax.crypto.spec.PBEKeySpec;
  38. import javax.crypto.spec.SecretKeySpec;
  39. import gnu.javax.crypto.prng.IPBE;
  40. import gnu.java.security.prng.IRandom;
  41. import gnu.java.security.prng.LimitReachedException;
  42. import gnu.javax.crypto.prng.PRNGFactory;
  43. public abstract class PBKDF2SecretKeyFactory
  44. extends SecretKeyFactorySpi
  45. {
  46. protected String macName;
  47. private static final int DEFAULT_ITERATION_COUNT = 1000;
  48. private static final int DEFAULT_KEY_LEN = 32;
  49. protected PBKDF2SecretKeyFactory(String macName)
  50. {
  51. this.macName = macName;
  52. }
  53. protected SecretKey engineGenerateSecret(KeySpec spec)
  54. throws InvalidKeySpecException
  55. {
  56. if (! (spec instanceof PBEKeySpec))
  57. throw new InvalidKeySpecException("not a PBEKeySpec");
  58. IRandom kdf = PRNGFactory.getInstance("PBKDF2-" + macName);
  59. HashMap attr = new HashMap();
  60. attr.put(IPBE.PASSWORD, ((PBEKeySpec) spec).getPassword());
  61. byte[] salt = ((PBEKeySpec) spec).getSalt();
  62. if (salt == null)
  63. salt = new byte[0];
  64. attr.put(IPBE.SALT, salt);
  65. int ic = ((PBEKeySpec) spec).getIterationCount();
  66. if (ic <= 0)
  67. ic = DEFAULT_ITERATION_COUNT;
  68. attr.put(IPBE.ITERATION_COUNT, Integer.valueOf(ic));
  69. kdf.init(attr);
  70. int len = ((PBEKeySpec) spec).getKeyLength();
  71. if (len <= 0)
  72. len = DEFAULT_KEY_LEN;
  73. byte[] dk = new byte[len];
  74. try
  75. {
  76. kdf.nextBytes(dk, 0, len);
  77. }
  78. catch (LimitReachedException lre)
  79. {
  80. throw new IllegalArgumentException(lre.toString());
  81. }
  82. return new SecretKeySpec(dk, "PBKDF2");
  83. }
  84. protected KeySpec engineGetKeySpec(SecretKey key, Class clazz)
  85. throws InvalidKeySpecException
  86. {
  87. throw new InvalidKeySpecException("not supported");
  88. }
  89. protected SecretKey engineTranslateKey(SecretKey key)
  90. {
  91. return new SecretKeySpec(key.getEncoded(), key.getAlgorithm());
  92. }
  93. public static class HMacHaval
  94. extends PBKDF2SecretKeyFactory
  95. {
  96. public HMacHaval()
  97. {
  98. super("HMAC-HAVAL");
  99. }
  100. }
  101. public static class HMacMD2
  102. extends PBKDF2SecretKeyFactory
  103. {
  104. public HMacMD2()
  105. {
  106. super("HMAC-MD2");
  107. }
  108. }
  109. public static class HMacMD4
  110. extends PBKDF2SecretKeyFactory
  111. {
  112. public HMacMD4()
  113. {
  114. super("HMAC-MD4");
  115. }
  116. }
  117. public static class HMacMD5
  118. extends PBKDF2SecretKeyFactory
  119. {
  120. public HMacMD5()
  121. {
  122. super("HMAC-MD5");
  123. }
  124. }
  125. public static class HMacRipeMD128
  126. extends PBKDF2SecretKeyFactory
  127. {
  128. public HMacRipeMD128()
  129. {
  130. super("HMAC-RIPEMD128");
  131. }
  132. }
  133. public static class HMacRipeMD160
  134. extends PBKDF2SecretKeyFactory
  135. {
  136. public HMacRipeMD160()
  137. {
  138. super("HMAC-RIPEMD160");
  139. }
  140. }
  141. public static class HMacSHA1
  142. extends PBKDF2SecretKeyFactory
  143. {
  144. public HMacSHA1()
  145. {
  146. super("HMAC-SHA1");
  147. }
  148. }
  149. public static class HMacSHA256
  150. extends PBKDF2SecretKeyFactory
  151. {
  152. public HMacSHA256()
  153. {
  154. super("HMAC-SHA256");
  155. }
  156. }
  157. public static class HMacSHA384
  158. extends PBKDF2SecretKeyFactory
  159. {
  160. public HMacSHA384()
  161. {
  162. super("HMAC-SHA384");
  163. }
  164. }
  165. public static class HMacSHA512
  166. extends PBKDF2SecretKeyFactory
  167. {
  168. public HMacSHA512()
  169. {
  170. super("HMAC-SHA512");
  171. }
  172. }
  173. public static class HMacTiger
  174. extends PBKDF2SecretKeyFactory
  175. {
  176. public HMacTiger()
  177. {
  178. super("HMAC-TIGER");
  179. }
  180. }
  181. public static class HMacWhirlpool
  182. extends PBKDF2SecretKeyFactory
  183. {
  184. public HMacWhirlpool()
  185. {
  186. super("HMAC-WHIRLPOOL");
  187. }
  188. }
  189. }