PBEKeySpec.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* PBEKeySpec.java -- Wrapper for password-based keys.
  2. Copyright (C) 2004, 2006 Free Software Foundation, Inc.
  3. This file is 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, or (at your option)
  7. 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; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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 javax.crypto.spec;
  32. import java.security.spec.KeySpec;
  33. /**
  34. * A wrapper for a password-based key, used for password-based
  35. * encryption (PBE).
  36. *
  37. * <p>Examples of password-based encryption algorithms include:
  38. *
  39. * <ul>
  40. * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/">PKCS #5
  41. * - Password-Based Cryptography Standard</a></li>
  42. * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-12/">PKCS
  43. * #12 - Personal Information Exchange Syntax Standard</a></li>
  44. * </ul>
  45. *
  46. * @author Casey Marshall (csm@gnu.org)
  47. * @since 1.4
  48. * @see javax.crypto.SecretKeyFactory
  49. * @see PBEParameterSpec
  50. */
  51. public class PBEKeySpec implements KeySpec
  52. {
  53. // Fields.
  54. // ------------------------------------------------------------------------
  55. /** The iteration count. */
  56. private int iterationCount;
  57. /** The generated key length. */
  58. private int keyLength;
  59. /** The password. */
  60. private char[] password;
  61. /** The salt. */
  62. private byte[] salt;
  63. /** The password state */
  64. private boolean passwordValid = true;
  65. // Constructors.
  66. // ------------------------------------------------------------------------
  67. /**
  68. * Create a new PBE key spec with just a password.
  69. * <p>
  70. * A copy of the password argument is stored instead of the argument itself.
  71. *
  72. * @param password The password char array.
  73. */
  74. public PBEKeySpec(char[] password)
  75. {
  76. setPassword(password);
  77. // load the default values for unspecified variables.
  78. salt = null;
  79. iterationCount = 0;
  80. keyLength = 0;
  81. }
  82. /**
  83. * Create a PBE key spec with a password, salt, and iteration count.
  84. * <p>
  85. * A copy of the password and salt arguments are stored instead of the
  86. * arguments themselves.
  87. *
  88. * @param password The password char array.
  89. * @param salt The salt bytes.
  90. * @param iterationCount The iteration count.
  91. * @throws NullPointerException If salt is null
  92. * @throws IllegalArgumentException If salt is an empty array, or
  93. * iterationCount is negative
  94. */
  95. public PBEKeySpec(char[] password, byte[] salt, int iterationCount)
  96. {
  97. setPassword(password);
  98. setSalt(salt);
  99. setIterationCount(iterationCount);
  100. // load default values into unspecified variables.
  101. keyLength = 0;
  102. }
  103. /**
  104. * Create a PBE key spec with a password, salt, iteration count, and key
  105. * length.
  106. * <p>
  107. * A copy of the password and salt arguments are stored instead of the
  108. * arguments themselves.
  109. *
  110. * @param password The password char array.
  111. * @param salt The salt bytes.
  112. * @param iterationCount The iteration count.
  113. * @param keyLength The generated key length.
  114. * @throws NullPointerException If salt is null
  115. * @throws IllegalArgumentException If salt is an empty array, if
  116. * iterationCount or keyLength is negative
  117. */
  118. public PBEKeySpec(char[] password, byte[] salt, int iterationCount,
  119. int keyLength)
  120. {
  121. setPassword(password);
  122. setSalt(salt);
  123. setIterationCount(iterationCount);
  124. setKeyLength(keyLength);
  125. }
  126. // Instance methods.
  127. // ------------------------------------------------------------------------
  128. /**
  129. * Clear the password array by filling it with null characters.
  130. * <p>
  131. * This clears the stored copy of the password, not the original char array
  132. * used to create the password.
  133. */
  134. public final void clearPassword()
  135. {
  136. if (password == null)
  137. return;
  138. for (int i = 0; i < password.length; i++)
  139. password[i] = '\u0000';
  140. // since the password is cleared, it is no longer valid
  141. passwordValid = false;
  142. }
  143. /**
  144. * Get the iteration count, or 0 if it has not been specified.
  145. *
  146. * @return The iteration count, or 0 if it has not been specified.
  147. */
  148. public final int getIterationCount()
  149. {
  150. return iterationCount;
  151. }
  152. /**
  153. * Get the generated key length, or 0 if it has not been specified.
  154. *
  155. * @return The key length, or 0 if it has not been specified.
  156. */
  157. public final int getKeyLength()
  158. {
  159. return keyLength;
  160. }
  161. /**
  162. * Get the password character array copy.
  163. * <p>
  164. * This returns a copy of the password, not the password itself.
  165. *
  166. * @return a clone of the password.
  167. * @throws IllegalStateException If {@link #clearPassword()} has already been
  168. * called.
  169. */
  170. public final char[] getPassword()
  171. {
  172. if (! passwordValid)
  173. throw new IllegalStateException("clearPassword() has been called, the "
  174. + "password is no longer valid");
  175. return (char[]) password.clone();
  176. }
  177. /**
  178. * Get the salt bytes array copy.
  179. * <p>
  180. * This returns a copy of the salt, not the salt itself.
  181. *
  182. * @return The salt.
  183. */
  184. public final byte[] getSalt()
  185. {
  186. if (salt != null)
  187. return (byte[]) salt.clone();
  188. return null;
  189. }
  190. /**
  191. * Set the password char array.
  192. * <p>
  193. * A copy of the password argument is stored instead of the argument itself.
  194. *
  195. * @param password The password to be set
  196. */
  197. private void setPassword(char[] password)
  198. {
  199. if (password != null)
  200. this.password = (char[]) password.clone();
  201. else
  202. this.password = new char[0];
  203. passwordValid = true;
  204. }
  205. /**
  206. * Set the salt byte array.
  207. * <p>
  208. * A copy of the salt arguments is stored instead of the argument itself.
  209. *
  210. * @param salt The salt to be set.
  211. * @throws NullPointerException If the salt is null.
  212. * @throws IllegalArgumentException If the salt is an empty array.
  213. */
  214. private void setSalt(byte[] salt)
  215. {
  216. if (salt.length == 0)
  217. throw new IllegalArgumentException("salt MUST NOT be an empty byte array");
  218. this.salt = (byte[]) salt.clone();
  219. }
  220. /**
  221. * Set the iterationCount.
  222. *
  223. * @param iterationCount The iteration count to be set.
  224. * @throws IllegalArgumentException If the iterationCount is negative.
  225. */
  226. private void setIterationCount(int iterationCount)
  227. {
  228. if (iterationCount < 0)
  229. throw new IllegalArgumentException("iterationCount MUST be positive");
  230. this.iterationCount = iterationCount;
  231. }
  232. /**
  233. * Set the keyLength.
  234. *
  235. * @param keyLength The keyLength to be set.
  236. * @throws IllegalArgumentException if the keyLength is negative.
  237. */
  238. private void setKeyLength(int keyLength)
  239. {
  240. if (keyLength < 0)
  241. throw new IllegalArgumentException("keyLength MUST be positive");
  242. this.keyLength = keyLength;
  243. }
  244. }