1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.Strings; 4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/** 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * super class for all Password Based Encryption (PBE) parameter generator classes. 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic abstract class PBEParametersGenerator 9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam protected byte[] password; 11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam protected byte[] salt; 12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam protected int iterationCount; 13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * base constructor. 16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam protected PBEParametersGenerator() 18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * initialise the PBE generator. 23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param password the password converted into bytes (see below). 25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param salt the salt to be mixed with the password. 26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param iterationCount the number of iterations the "mixing" function 27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * is to be applied for. 28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public void init( 30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] password, 31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] salt, 32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam int iterationCount) 33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.password = password; 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.salt = salt; 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.iterationCount = iterationCount; 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return the password byte array. 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return the password byte array. 43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] getPassword() 45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return password; 47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return the salt byte array. 51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return the salt byte array. 53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] getSalt() 55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return salt; 57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return the iteration count. 61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return the iteration count. 63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public int getIterationCount() 65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return iterationCount; 67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * generate derived parameters for a key of length keySize. 71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param keySize the length, in bits, of the key required. 73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return a parameters object representing a key. 74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public abstract CipherParameters generateDerivedParameters(int keySize); 76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * generate derived parameters for a key of length keySize, and 79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * an initialisation vector (IV) of length ivSize. 80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param keySize the length, in bits, of the key required. 82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param ivSize the length, in bits, of the iv required. 83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return a parameters object representing a key and an IV. 84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public abstract CipherParameters generateDerivedParameters(int keySize, int ivSize); 86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * generate derived parameters for a key of length keySize, specifically 89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * for use with a MAC. 90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param keySize the length, in bits, of the key required. 92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return a parameters object representing a key. 93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public abstract CipherParameters generateDerivedMacParameters(int keySize); 95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * converts a password to a byte array according to the scheme in 98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * PKCS5 (ascii, no padding) 99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 1004c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom * @param password a character array representing the password. 101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return a byte array representing the password. 102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static byte[] PKCS5PasswordToBytes( 104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam char[] password) 105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] bytes = new byte[password.length]; 107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != bytes.length; i++) 109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bytes[i] = (byte)password[i]; 111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return bytes; 114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * converts a password to a byte array according to the scheme in 118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * PKCS5 (UTF-8, no padding) 119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 1204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom * @param password a character array representing the password. 121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @return a byte array representing the password. 122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public static byte[] PKCS5PasswordToUTF8Bytes( 124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom char[] password) 125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 126c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return Strings.toUTF8ByteArray(password); 127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom /** 130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * converts a password to a byte array according to the scheme in 131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * PKCS12 (unicode, big endian, 2 zero pad bytes at the end). 132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * 133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @param password a character array representing the password. 134c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @return a byte array representing the password. 135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom */ 136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static byte[] PKCS12PasswordToBytes( 137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam char[] password) 138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam // BEGIN android-changed 140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (password != null && password.length > 0) 141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam // +1 for extra 2 pad bytes. 143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] bytes = new byte[(password.length + 1) * 2]; 144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam for (int i = 0; i != password.length; i ++) 146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bytes[i * 2] = (byte)(password[i] >>> 8); 148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bytes[i * 2 + 1] = (byte)password[i]; 149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return bytes; 152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam else 154b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 155b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return new byte[0]; 156b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 157b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam // END android-changed 158b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 159b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 160