forked from apache/hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HADOOP-10693. Implementation of AES-CTR CryptoCodec using JNI to Open…
…SSL (hitliuyi via cmccabe) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/fs-encryption@1607768 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
15 changed files
with
267 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +0,0 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.crypto; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience; | ||
import org.apache.hadoop.classification.InterfaceStability; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
@InterfaceAudience.Private | ||
@InterfaceStability.Evolving | ||
public abstract class AESCTRCryptoCodec extends CryptoCodec { | ||
|
||
protected static final CipherSuite SUITE = CipherSuite.AES_CTR_NOPADDING; | ||
|
||
/** | ||
* For AES, the algorithm block is fixed size of 128 bits. | ||
* @see http://en.wikipedia.org/wiki/Advanced_Encryption_Standard | ||
*/ | ||
private static final int AES_BLOCK_SIZE = SUITE.getAlgorithmBlockSize(); | ||
private static final int CTR_OFFSET = 8; | ||
|
||
@Override | ||
public CipherSuite getCipherSuite() { | ||
return SUITE; | ||
} | ||
|
||
/** | ||
* The IV is produced by adding the initial IV to the counter. IV length | ||
* should be the same as {@link #AES_BLOCK_SIZE} | ||
*/ | ||
@Override | ||
public void calculateIV(byte[] initIV, long counter, byte[] IV) { | ||
Preconditions.checkArgument(initIV.length == AES_BLOCK_SIZE); | ||
Preconditions.checkArgument(IV.length == AES_BLOCK_SIZE); | ||
|
||
System.arraycopy(initIV, 0, IV, 0, CTR_OFFSET); | ||
long l = (initIV[CTR_OFFSET + 0] << 56) | ||
+ ((initIV[CTR_OFFSET + 1] & 0xFF) << 48) | ||
+ ((initIV[CTR_OFFSET + 2] & 0xFF) << 40) | ||
+ ((initIV[CTR_OFFSET + 3] & 0xFF) << 32) | ||
+ ((initIV[CTR_OFFSET + 4] & 0xFF) << 24) | ||
+ ((initIV[CTR_OFFSET + 5] & 0xFF) << 16) | ||
+ ((initIV[CTR_OFFSET + 6] & 0xFF) << 8) | ||
+ (initIV[CTR_OFFSET + 7] & 0xFF); | ||
l += counter; | ||
IV[CTR_OFFSET + 0] = (byte) (l >>> 56); | ||
IV[CTR_OFFSET + 1] = (byte) (l >>> 48); | ||
IV[CTR_OFFSET + 2] = (byte) (l >>> 40); | ||
IV[CTR_OFFSET + 3] = (byte) (l >>> 32); | ||
IV[CTR_OFFSET + 4] = (byte) (l >>> 24); | ||
IV[CTR_OFFSET + 5] = (byte) (l >>> 16); | ||
IV[CTR_OFFSET + 6] = (byte) (l >>> 8); | ||
IV[CTR_OFFSET + 7] = (byte) (l); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +0,0 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.crypto; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.security.GeneralSecurityException; | ||
import java.security.SecureRandom; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_KEY; | ||
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_SECURE_RANDOM_ALGORITHM_KEY; | ||
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_SECURE_RANDOM_ALGORITHM_DEFAULT; | ||
|
||
/** | ||
* Implement the AES-CTR crypto codec using JCE provider. | ||
*/ | ||
@InterfaceAudience.Private | ||
public class JCEAESCTRCryptoCodec extends AESCTRCryptoCodec { | ||
private Configuration conf; | ||
private String provider; | ||
private SecureRandom random; | ||
|
||
public JCEAESCTRCryptoCodec() { | ||
} | ||
|
||
@Override | ||
public Configuration getConf() { | ||
return conf; | ||
} | ||
|
||
@Override | ||
public void setConf(Configuration conf) { | ||
this.conf = conf; | ||
provider = conf.get(HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_KEY); | ||
final String secureRandomAlg = conf.get( | ||
HADOOP_SECURITY_SECURE_RANDOM_ALGORITHM_KEY, | ||
HADOOP_SECURITY_SECURE_RANDOM_ALGORITHM_DEFAULT); | ||
try { | ||
random = (provider != null) ? | ||
SecureRandom.getInstance(secureRandomAlg, provider) : | ||
SecureRandom.getInstance(secureRandomAlg); | ||
} catch (GeneralSecurityException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Encryptor createEncryptor() throws GeneralSecurityException { | ||
return new JCEAESCTRCipher(Cipher.ENCRYPT_MODE, provider); | ||
} | ||
|
||
@Override | ||
public Decryptor createDecryptor() throws GeneralSecurityException { | ||
return new JCEAESCTRCipher(Cipher.DECRYPT_MODE, provider); | ||
} | ||
|
||
@Override | ||
public void generateSecureRandom(byte[] bytes) { | ||
random.nextBytes(bytes); | ||
} | ||
|
||
private static class JCEAESCTRCipher implements Encryptor, Decryptor { | ||
private final Cipher cipher; | ||
private final int mode; | ||
private boolean contextReset = false; | ||
|
||
public JCEAESCTRCipher(int mode, String provider) | ||
throws GeneralSecurityException { | ||
this.mode = mode; | ||
if (provider == null || provider.isEmpty()) { | ||
cipher = Cipher.getInstance(SUITE.getName()); | ||
} else { | ||
cipher = Cipher.getInstance(SUITE.getName(), provider); | ||
} | ||
} | ||
|
||
@Override | ||
public void init(byte[] key, byte[] iv) throws IOException { | ||
Preconditions.checkNotNull(key); | ||
Preconditions.checkNotNull(iv); | ||
contextReset = false; | ||
try { | ||
cipher.init(mode, new SecretKeySpec(key, "AES"), | ||
new IvParameterSpec(iv)); | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
/** | ||
* AES-CTR will consume all of the input data. It requires enough space in | ||
* the destination buffer to encrypt entire input buffer. | ||
*/ | ||
@Override | ||
public void encrypt(ByteBuffer inBuffer, ByteBuffer outBuffer) | ||
throws IOException { | ||
process(inBuffer, outBuffer); | ||
} | ||
|
||
/** | ||
* AES-CTR will consume all of the input data. It requires enough space in | ||
* the destination buffer to decrypt entire input buffer. | ||
*/ | ||
@Override | ||
public void decrypt(ByteBuffer inBuffer, ByteBuffer outBuffer) | ||
throws IOException { | ||
process(inBuffer, outBuffer); | ||
} | ||
|
||
private void process(ByteBuffer inBuffer, ByteBuffer outBuffer) | ||
throws IOException { | ||
try { | ||
int inputSize = inBuffer.remaining(); | ||
// Cipher#update will maintain crypto context. | ||
int n = cipher.update(inBuffer, outBuffer); | ||
if (n < inputSize) { | ||
/** | ||
* Typically code will not get here. Cipher#update will consume all | ||
* input data and put result in outBuffer. | ||
* Cipher#doFinal will reset the crypto context. | ||
*/ | ||
contextReset = true; | ||
cipher.doFinal(inBuffer, outBuffer); | ||
} | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isContextReset() { | ||
return contextReset; | ||
} | ||
} | ||
} | ||
Oops, something went wrong.