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 TestAES {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestAES.class);
053
054  // Validation for AES in CTR mode with a 128 bit key
055  // From NIST Special Publication 800-38A
056  @Test
057  public void testAESAlgorithm() throws Exception {
058    Configuration conf = HBaseConfiguration.create();
059    Cipher aes = Encryption.getCipher(conf, "AES");
060    assertEquals(AES.KEY_LENGTH, aes.getKeyLength());
061    assertEquals(AES.IV_LENGTH, aes.getIvLength());
062    Encryptor e = aes.getEncryptor();
063    e.setKey(new SecretKeySpec(Bytes.fromHex("2b7e151628aed2a6abf7158809cf4f3c"), "AES"));
064    e.setIv(Bytes.fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"));
065
066    ByteArrayOutputStream out = new ByteArrayOutputStream();
067    OutputStream cout = e.createEncryptionStream(out);
068    cout.write(Bytes.fromHex("6bc1bee22e409f96e93d7e117393172a"));
069    cout.write(Bytes.fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
070    cout.write(Bytes.fromHex("30c81c46a35ce411e5fbc1191a0a52ef"));
071    cout.write(Bytes.fromHex("f69f2445df4f9b17ad2b417be66c3710"));
072    cout.close();
073
074    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
075    byte[] b = new byte[16];
076    IOUtils.readFully(in, b);
077    assertTrue("Failed #1", Bytes.equals(b, Bytes.fromHex("874d6191b620e3261bef6864990db6ce")));
078    IOUtils.readFully(in, b);
079    assertTrue("Failed #2", Bytes.equals(b, Bytes.fromHex("9806f66b7970fdff8617187bb9fffdff")));
080    IOUtils.readFully(in, b);
081    assertTrue("Failed #3", Bytes.equals(b, Bytes.fromHex("5ae4df3edbd5d35e5b4f09020db03eab")));
082    IOUtils.readFully(in, b);
083    assertTrue("Failed #4", Bytes.equals(b, Bytes.fromHex("1e031dda2fbe03d1792170a0f3009cee")));
084  }
085
086  @Test
087  public void testAlternateRNG() throws Exception {
088    Security.addProvider(new TestProvider());
089
090    Configuration conf = new Configuration();
091    conf.set(AES.RNG_ALGORITHM_KEY, "TestRNG");
092    conf.set(AES.RNG_PROVIDER_KEY, "TEST");
093    DefaultCipherProvider.getInstance().setConf(conf);
094
095    AES aes = new AES(DefaultCipherProvider.getInstance());
096    assertEquals("AES did not find alternate RNG", "TestRNG", aes.getRNG().getAlgorithm());
097  }
098
099  static class TestProvider extends Provider {
100    private static final long serialVersionUID = 1L;
101
102    public TestProvider() {
103      super("TEST", 1.0, "Test provider");
104      AccessController.doPrivileged(new PrivilegedAction<Object>() {
105        @Override
106        public Object run() {
107          put("SecureRandom.TestRNG", TestAES.class.getName() + "$TestRNG");
108          return null;
109        }
110      });
111    }
112  }
113
114  // Must be public for instantiation by the SecureRandom SPI
115  public static class TestRNG extends SecureRandomSpi {
116    private static final long serialVersionUID = 1L;
117    private SecureRandom rng;
118
119    public TestRNG() throws Exception {
120      rng = java.security.SecureRandom.getInstance("SHA1PRNG");
121    }
122
123    @Override
124    protected void engineSetSeed(byte[] seed) {
125      rng.setSeed(seed);
126    }
127
128    @Override
129    protected void engineNextBytes(byte[] bytes) {
130      rng.nextBytes(bytes);
131    }
132
133    @Override
134    protected byte[] engineGenerateSeed(int numBytes) {
135      return rng.generateSeed(numBytes);
136    }
137  }
138
139}