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