001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.crypto.aes; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.io.OutputStream; 026import java.security.AccessController; 027import java.security.PrivilegedAction; 028import java.security.Provider; 029import java.security.SecureRandom; 030import java.security.SecureRandomSpi; 031import java.security.Security; 032import javax.crypto.spec.SecretKeySpec; 033import org.apache.commons.io.IOUtils; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.HBaseConfiguration; 037import org.apache.hadoop.hbase.io.crypto.Cipher; 038import org.apache.hadoop.hbase.io.crypto.DefaultCipherProvider; 039import org.apache.hadoop.hbase.io.crypto.Encryption; 040import org.apache.hadoop.hbase.io.crypto.Encryptor; 041import org.apache.hadoop.hbase.testclassification.MiscTests; 042import org.apache.hadoop.hbase.testclassification.SmallTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047 048@Category({ MiscTests.class, SmallTests.class }) 049public class TestCommonsAES { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestCommonsAES.class); 054 055 // Validation for AES in CTR mode with a 128 bit key 056 // From NIST Special Publication 800-38A 057 @Test 058 public void testAESAlgorithm() throws Exception { 059 Configuration conf = HBaseConfiguration.create(); 060 Cipher aes = Encryption.getCipher(conf, "AES"); 061 assertEquals(CommonsCryptoAES.KEY_LENGTH, aes.getKeyLength()); 062 assertEquals(CommonsCryptoAES.IV_LENGTH, aes.getIvLength()); 063 Encryptor e = aes.getEncryptor(); 064 e.setKey(new SecretKeySpec(Bytes.fromHex("2b7e151628aed2a6abf7158809cf4f3c"), "AES")); 065 e.setIv(Bytes.fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")); 066 067 ByteArrayOutputStream out = new ByteArrayOutputStream(); 068 OutputStream cout = e.createEncryptionStream(out); 069 cout.write(Bytes.fromHex("6bc1bee22e409f96e93d7e117393172a")); 070 cout.write(Bytes.fromHex("ae2d8a571e03ac9c9eb76fac45af8e51")); 071 cout.write(Bytes.fromHex("30c81c46a35ce411e5fbc1191a0a52ef")); 072 cout.write(Bytes.fromHex("f69f2445df4f9b17ad2b417be66c3710")); 073 cout.close(); 074 075 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 076 byte[] b = new byte[16]; 077 IOUtils.readFully(in, b); 078 assertTrue("Failed #1", Bytes.equals(b, Bytes.fromHex("874d6191b620e3261bef6864990db6ce"))); 079 IOUtils.readFully(in, b); 080 assertTrue("Failed #2", Bytes.equals(b, Bytes.fromHex("9806f66b7970fdff8617187bb9fffdff"))); 081 IOUtils.readFully(in, b); 082 assertTrue("Failed #3", Bytes.equals(b, Bytes.fromHex("5ae4df3edbd5d35e5b4f09020db03eab"))); 083 IOUtils.readFully(in, b); 084 assertTrue("Failed #4", Bytes.equals(b, Bytes.fromHex("1e031dda2fbe03d1792170a0f3009cee"))); 085 } 086 087 @Test 088 public void testAlternateRNG() throws Exception { 089 Security.addProvider(new TestProvider()); 090 091 Configuration conf = new Configuration(); 092 conf.set(AES.RNG_ALGORITHM_KEY, "TestRNG"); 093 conf.set(AES.RNG_PROVIDER_KEY, "TEST"); 094 DefaultCipherProvider.getInstance().setConf(conf); 095 096 AES aes = new AES(DefaultCipherProvider.getInstance()); 097 assertEquals("AES did not find alternate RNG", "TestRNG", aes.getRNG().getAlgorithm()); 098 } 099 100 static class TestProvider extends Provider { 101 private static final long serialVersionUID = 1L; 102 103 public TestProvider() { 104 super("TEST", 1.0, "Test provider"); 105 AccessController.doPrivileged(new PrivilegedAction<Object>() { 106 @Override 107 public Object run() { 108 put("SecureRandom.TestRNG", TestCommonsAES.class.getName() + "$TestRNG"); 109 return null; 110 } 111 }); 112 } 113 } 114 115 // Must be public for instantiation by the SecureRandom SPI 116 public static class TestRNG extends SecureRandomSpi { 117 private static final long serialVersionUID = 1L; 118 private SecureRandom rng; 119 120 public TestRNG() throws Exception { 121 rng = SecureRandom.getInstance("SHA1PRNG"); 122 } 123 124 @Override 125 protected void engineSetSeed(byte[] seed) { 126 rng.setSeed(seed); 127 } 128 129 @Override 130 protected void engineNextBytes(byte[] bytes) { 131 rng.nextBytes(bytes); 132 } 133 134 @Override 135 protected byte[] engineGenerateSeed(int numBytes) { 136 return rng.generateSeed(numBytes); 137 } 138 } 139 140}