|
@@ -21,7 +21,6 @@ package org.apache.hadoop.crypto.key;
|
|
|
import java.io.IOException;
|
|
|
import java.security.GeneralSecurityException;
|
|
|
import java.security.SecureRandom;
|
|
|
-
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
@@ -30,51 +29,109 @@ import com.google.common.base.Preconditions;
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
|
|
|
|
/**
|
|
|
- * A KeyProvider with Cytographic Extensions specifically for generating
|
|
|
- * Encrypted Keys as well as decrypting them
|
|
|
+ * A KeyProvider with Cryptographic Extensions specifically for generating
|
|
|
+ * and decrypting encrypted encryption keys.
|
|
|
*
|
|
|
*/
|
|
|
@InterfaceAudience.Private
|
|
|
public class KeyProviderCryptoExtension extends
|
|
|
KeyProviderExtension<KeyProviderCryptoExtension.CryptoExtension> {
|
|
|
|
|
|
+ /**
|
|
|
+ * Designates an encrypted encryption key, or EEK.
|
|
|
+ */
|
|
|
public static final String EEK = "EEK";
|
|
|
+ /**
|
|
|
+ * Designates a decrypted encrypted encryption key, that is, an encryption key
|
|
|
+ * (EK).
|
|
|
+ */
|
|
|
public static final String EK = "EK";
|
|
|
|
|
|
/**
|
|
|
- * This is a holder class whose instance contains the keyVersionName, iv
|
|
|
- * used to generate the encrypted Key and the encrypted KeyVersion
|
|
|
+ * An encrypted encryption key (EEK) and related information. An EEK must be
|
|
|
+ * decrypted using the key's encryption key before it can be used.
|
|
|
*/
|
|
|
public static class EncryptedKeyVersion {
|
|
|
- private String keyName;
|
|
|
- private String keyVersionName;
|
|
|
- private byte[] iv;
|
|
|
- private KeyVersion encryptedKey;
|
|
|
+ private String encryptionKeyName;
|
|
|
+ private String encryptionKeyVersionName;
|
|
|
+ private byte[] encryptedKeyIv;
|
|
|
+ private KeyVersion encryptedKeyVersion;
|
|
|
|
|
|
- protected EncryptedKeyVersion(String keyName, String keyVersionName,
|
|
|
- byte[] iv, KeyVersion encryptedKey) {
|
|
|
- this.keyName = keyName;
|
|
|
- this.keyVersionName = keyVersionName;
|
|
|
- this.iv = iv;
|
|
|
- this.encryptedKey = encryptedKey;
|
|
|
+ /**
|
|
|
+ * Create a new EncryptedKeyVersion.
|
|
|
+ *
|
|
|
+ * @param keyName Name of the encryption key used to
|
|
|
+ * encrypt the encrypted key.
|
|
|
+ * @param encryptionKeyVersionName Version name of the encryption key used
|
|
|
+ * to encrypt the encrypted key.
|
|
|
+ * @param encryptedKeyIv Initialization vector of the encrypted
|
|
|
+ * key. The IV of the encryption key used to
|
|
|
+ * encrypt the encrypted key is derived from
|
|
|
+ * this IV.
|
|
|
+ * @param encryptedKeyVersion The encrypted encryption key version.
|
|
|
+ */
|
|
|
+ protected EncryptedKeyVersion(String keyName,
|
|
|
+ String encryptionKeyVersionName, byte[] encryptedKeyIv,
|
|
|
+ KeyVersion encryptedKeyVersion) {
|
|
|
+ this.encryptionKeyName = keyName;
|
|
|
+ this.encryptionKeyVersionName = encryptionKeyVersionName;
|
|
|
+ this.encryptedKeyIv = encryptedKeyIv;
|
|
|
+ this.encryptedKeyVersion = encryptedKeyVersion;
|
|
|
}
|
|
|
|
|
|
- public String getKeyName() {
|
|
|
- return keyName;
|
|
|
+ /**
|
|
|
+ * @return Name of the encryption key used to encrypt the encrypted key.
|
|
|
+ */
|
|
|
+ public String getEncryptionKeyName() {
|
|
|
+ return encryptionKeyName;
|
|
|
}
|
|
|
|
|
|
- public String getKeyVersionName() {
|
|
|
- return keyVersionName;
|
|
|
+ /**
|
|
|
+ * @return Version name of the encryption key used to encrypt the encrypted
|
|
|
+ * key.
|
|
|
+ */
|
|
|
+ public String getEncryptionKeyVersionName() {
|
|
|
+ return encryptionKeyVersionName;
|
|
|
}
|
|
|
|
|
|
- public byte[] getIv() {
|
|
|
- return iv;
|
|
|
+ /**
|
|
|
+ * @return Initialization vector of the encrypted key. The IV of the
|
|
|
+ * encryption key used to encrypt the encrypted key is derived from this
|
|
|
+ * IV.
|
|
|
+ */
|
|
|
+ public byte[] getEncryptedKeyIv() {
|
|
|
+ return encryptedKeyIv;
|
|
|
}
|
|
|
|
|
|
- public KeyVersion getEncryptedKey() {
|
|
|
- return encryptedKey;
|
|
|
+ /**
|
|
|
+ * @return The encrypted encryption key version.
|
|
|
+ */
|
|
|
+ public KeyVersion getEncryptedKeyVersion() {
|
|
|
+ return encryptedKeyVersion;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Derive the initialization vector (IV) for the encryption key from the IV
|
|
|
+ * of the encrypted key. This derived IV is used with the encryption key to
|
|
|
+ * decrypt the encrypted key.
|
|
|
+ * <p/>
|
|
|
+ * The alternative to this is using the same IV for both the encryption key
|
|
|
+ * and the encrypted key. Even a simple symmetric transformation like this
|
|
|
+ * improves security by avoiding IV re-use. IVs will also be fairly unique
|
|
|
+ * among different EEKs.
|
|
|
+ *
|
|
|
+ * @param encryptedKeyIV of the encrypted key (i.e. {@link
|
|
|
+ * #getEncryptedKeyIv()})
|
|
|
+ * @return IV for the encryption key
|
|
|
+ */
|
|
|
+ protected static byte[] deriveIV(byte[] encryptedKeyIV) {
|
|
|
+ byte[] rIv = new byte[encryptedKeyIV.length];
|
|
|
+ // Do a simple XOR transformation to flip all the bits
|
|
|
+ for (int i = 0; i < encryptedKeyIV.length; i++) {
|
|
|
+ rIv[i] = (byte) (encryptedKeyIV[i] ^ 0xff);
|
|
|
+ }
|
|
|
+ return rIv;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -141,53 +198,56 @@ public class KeyProviderCryptoExtension extends
|
|
|
this.keyProvider = keyProvider;
|
|
|
}
|
|
|
|
|
|
- // the IV used to encrypt a EK typically will be the same IV used to
|
|
|
- // encrypt data with the EK. To avoid any chance of weakening the
|
|
|
- // encryption because the same IV is used, we simply XOR the IV thus we
|
|
|
- // are not using the same IV for 2 different encryptions (even if they
|
|
|
- // are done using different keys)
|
|
|
- private byte[] flipIV(byte[] iv) {
|
|
|
- byte[] rIv = new byte[iv.length];
|
|
|
- for (int i = 0; i < iv.length; i++) {
|
|
|
- rIv[i] = (byte) (iv[i] ^ 0xff);
|
|
|
- }
|
|
|
- return rIv;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public EncryptedKeyVersion generateEncryptedKey(String encryptionKeyName)
|
|
|
throws IOException, GeneralSecurityException {
|
|
|
- KeyVersion keyVer = keyProvider.getCurrentKey(encryptionKeyName);
|
|
|
- Preconditions.checkNotNull(keyVer, "No KeyVersion exists for key '%s' ",
|
|
|
- encryptionKeyName);
|
|
|
- byte[] newKey = new byte[keyVer.getMaterial().length];
|
|
|
- SecureRandom.getInstance("SHA1PRNG").nextBytes(newKey);
|
|
|
+ // Fetch the encryption key
|
|
|
+ KeyVersion encryptionKey = keyProvider.getCurrentKey(encryptionKeyName);
|
|
|
+ Preconditions.checkNotNull(encryptionKey,
|
|
|
+ "No KeyVersion exists for key '%s' ", encryptionKeyName);
|
|
|
+ // Generate random bytes for new key and IV
|
|
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
|
|
- byte[] iv = SecureRandom.getSeed(cipher.getBlockSize());
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyVer.getMaterial(),
|
|
|
- "AES"), new IvParameterSpec(flipIV(iv)));
|
|
|
- byte[] ek = cipher.doFinal(newKey);
|
|
|
+ SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
|
+ final byte[] newKey = new byte[encryptionKey.getMaterial().length];
|
|
|
+ random.nextBytes(newKey);
|
|
|
+ final byte[] iv = random.generateSeed(cipher.getBlockSize());
|
|
|
+ // Encryption key IV is derived from new key's IV
|
|
|
+ final byte[] encryptionIV = EncryptedKeyVersion.deriveIV(iv);
|
|
|
+ // Encrypt the new key
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE,
|
|
|
+ new SecretKeySpec(encryptionKey.getMaterial(), "AES"),
|
|
|
+ new IvParameterSpec(encryptionIV));
|
|
|
+ final byte[] encryptedKey = cipher.doFinal(newKey);
|
|
|
return new EncryptedKeyVersion(encryptionKeyName,
|
|
|
- keyVer.getVersionName(), iv,
|
|
|
- new KeyVersion(keyVer.getName(), EEK, ek));
|
|
|
+ encryptionKey.getVersionName(), iv,
|
|
|
+ new KeyVersion(encryptionKey.getName(), EEK, encryptedKey));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public KeyVersion decryptEncryptedKey(
|
|
|
EncryptedKeyVersion encryptedKeyVersion) throws IOException,
|
|
|
GeneralSecurityException {
|
|
|
- KeyVersion keyVer =
|
|
|
- keyProvider.getKeyVersion(encryptedKeyVersion.getKeyVersionName());
|
|
|
- Preconditions.checkNotNull(keyVer, "KeyVersion name '%s' does not exist",
|
|
|
- encryptedKeyVersion.getKeyVersionName());
|
|
|
- KeyVersion keyVersion = encryptedKeyVersion.getEncryptedKey();
|
|
|
+ // Fetch the encryption key material
|
|
|
+ final String encryptionKeyVersionName =
|
|
|
+ encryptedKeyVersion.getEncryptionKeyVersionName();
|
|
|
+ final KeyVersion encryptionKey =
|
|
|
+ keyProvider.getKeyVersion(encryptionKeyVersionName);
|
|
|
+ Preconditions.checkNotNull(encryptionKey,
|
|
|
+ "KeyVersion name '%s' does not exist", encryptionKeyVersionName);
|
|
|
+ final byte[] encryptionKeyMaterial = encryptionKey.getMaterial();
|
|
|
+ // Encryption key IV is determined from encrypted key's IV
|
|
|
+ final byte[] encryptionIV =
|
|
|
+ EncryptedKeyVersion.deriveIV(encryptedKeyVersion.getEncryptedKeyIv());
|
|
|
+ // Init the cipher with encryption key parameters
|
|
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
|
|
cipher.init(Cipher.DECRYPT_MODE,
|
|
|
- new SecretKeySpec(keyVersion.getMaterial(), "AES"),
|
|
|
- new IvParameterSpec(flipIV(encryptedKeyVersion.getIv())));
|
|
|
- byte[] ek =
|
|
|
- cipher.doFinal(encryptedKeyVersion.getEncryptedKey().getMaterial());
|
|
|
- return new KeyVersion(keyVer.getName(), EK, ek);
|
|
|
+ new SecretKeySpec(encryptionKeyMaterial, "AES"),
|
|
|
+ new IvParameterSpec(encryptionIV));
|
|
|
+ // Decrypt the encrypted key
|
|
|
+ final KeyVersion encryptedKV =
|
|
|
+ encryptedKeyVersion.getEncryptedKeyVersion();
|
|
|
+ final byte[] decryptedKey = cipher.doFinal(encryptedKV.getMaterial());
|
|
|
+ return new KeyVersion(encryptionKey.getName(), EK, decryptedKey);
|
|
|
}
|
|
|
|
|
|
@Override
|