PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/luni/src/test/java/libcore/javax/crypto/SecretKeyFactoryTest.java

https://github.com/MIPS/libcore
Java | 170 lines | 114 code | 17 blank | 39 comment | 0 complexity | 3dc3bce378348644bf1b44fd3ac5bc51 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package libcore.javax.crypto;
  17. import java.security.spec.InvalidKeySpecException;
  18. import java.security.spec.KeySpec;
  19. import java.util.Arrays;
  20. import javax.crypto.SecretKey;
  21. import javax.crypto.SecretKeyFactory;
  22. import javax.crypto.spec.PBEKeySpec;
  23. import junit.framework.TestCase;
  24. public class SecretKeyFactoryTest extends TestCase {
  25. private static final char[] PASSWORD = "google".toCharArray();
  26. /**
  27. * Salts should be random to reduce effectiveness of dictionary
  28. * attacks, but need not be kept secret from attackers. For more
  29. * information, see http://en.wikipedia.org/wiki/Salt_(cryptography)
  30. */
  31. private static final byte[] SALT = {0, 1, 2, 3, 4, 5, 6, 7};
  32. /**
  33. * The number of iterations should be higher for production
  34. * strength protection. The tolerable value may vary from device
  35. * to device, but 8192 should be acceptable for PBKDF2 on a Nexus One.
  36. */
  37. private static final int ITERATIONS = 1024;
  38. private static final int KEY_LENGTH = 128;
  39. public void test_PBKDF2_required_parameters() throws Exception {
  40. SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  41. // PBEKeySpec validates arguments most to be non-null, non-empty, postive, etc.
  42. // Focus on insufficient PBEKeySpecs
  43. // PBEKeySpecs password only constructor
  44. try {
  45. KeySpec ks = new PBEKeySpec(null);
  46. factory.generateSecret(ks);
  47. fail();
  48. } catch (InvalidKeySpecException expected) {
  49. }
  50. try {
  51. KeySpec ks = new PBEKeySpec(new char[0]);
  52. factory.generateSecret(ks);
  53. fail();
  54. } catch (InvalidKeySpecException expected) {
  55. }
  56. try {
  57. KeySpec ks = new PBEKeySpec(PASSWORD);
  58. factory.generateSecret(ks);
  59. fail();
  60. } catch (InvalidKeySpecException expected) {
  61. }
  62. // PBEKeySpecs constructor without key length
  63. try {
  64. KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS);
  65. factory.generateSecret(ks);
  66. fail();
  67. } catch (InvalidKeySpecException expected) {
  68. }
  69. try {
  70. KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS);
  71. factory.generateSecret(ks);
  72. fail();
  73. } catch (InvalidKeySpecException expected) {
  74. }
  75. try {
  76. KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
  77. factory.generateSecret(ks);
  78. fail();
  79. } catch (InvalidKeySpecException expected) {
  80. }
  81. try {
  82. KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS, KEY_LENGTH);
  83. factory.generateSecret(ks);
  84. fail();
  85. } catch (IllegalArgumentException expected) {
  86. }
  87. try {
  88. KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS, KEY_LENGTH);
  89. factory.generateSecret(ks);
  90. fail();
  91. } catch (IllegalArgumentException expected) {
  92. }
  93. KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS, KEY_LENGTH);
  94. factory.generateSecret(ks);
  95. }
  96. public void test_PBKDF2_b3059950() throws Exception {
  97. test_PBKDF2(PASSWORD, SALT, ITERATIONS, KEY_LENGTH,
  98. new byte[] {
  99. (byte)0x70, (byte)0x74, (byte)0xdb, (byte)0x72,
  100. (byte)0x35, (byte)0xd4, (byte)0x11, (byte)0x68,
  101. (byte)0x83, (byte)0x7c, (byte)0x14, (byte)0x1f,
  102. (byte)0xf6, (byte)0x4a, (byte)0xb0, (byte)0x54
  103. });
  104. }
  105. /**
  106. * 64-bit Test vector from RFC 3211
  107. *
  108. * See also org.bouncycastle.crypto.test.PKCS5Test
  109. */
  110. public void test_PBKDF2_rfc3211_64() throws Exception {
  111. char[] password = "password".toCharArray();
  112. byte[] salt = new byte[] {
  113. (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
  114. (byte)0x78, (byte)0x56, (byte)0x34, (byte)0x12
  115. };
  116. int iterations = 5;
  117. int keyLength = 64;
  118. byte[] expected = new byte[] {
  119. (byte)0xD1, (byte)0xDA, (byte)0xA7, (byte)0x86,
  120. (byte)0x15, (byte)0xF2, (byte)0x87, (byte)0xE6
  121. };
  122. test_PBKDF2(password, salt, iterations, keyLength, expected);
  123. }
  124. /**
  125. * 192-bit Test vector from RFC 3211
  126. *
  127. * See also org.bouncycastle.crypto.test.PKCS5Test
  128. */
  129. public void test_PBKDF2_rfc3211_192() throws Exception {
  130. char[] password = ("All n-entities must communicate with other "
  131. + "n-entities via n-1 entiteeheehees").toCharArray();
  132. byte[] salt = new byte[] {
  133. (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
  134. (byte)0x78, (byte)0x56, (byte)0x34, (byte)0x12
  135. };
  136. int iterations = 500;
  137. int keyLength = 192;
  138. byte[] expected = new byte[] {
  139. (byte)0x6a, (byte)0x89, (byte)0x70, (byte)0xbf, (byte)0x68, (byte)0xc9,
  140. (byte)0x2c, (byte)0xae, (byte)0xa8, (byte)0x4a, (byte)0x8d, (byte)0xf2,
  141. (byte)0x85, (byte)0x10, (byte)0x85, (byte)0x86, (byte)0x07, (byte)0x12,
  142. (byte)0x63, (byte)0x80, (byte)0xcc, (byte)0x47, (byte)0xab, (byte)0x2d
  143. };
  144. test_PBKDF2(password, salt, iterations, keyLength, expected);
  145. }
  146. private void test_PBKDF2(char[] password, byte[] salt, int iterations, int keyLength,
  147. byte[] expected) throws Exception {
  148. SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  149. KeySpec ks = new PBEKeySpec(password, salt, iterations, keyLength);
  150. SecretKey key = factory.generateSecret(ks);
  151. assertTrue(Arrays.equals(expected, key.getEncoded()));
  152. }
  153. }