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; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.security.Key; 026import javax.crypto.spec.SecretKeySpec; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseConfiguration; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.testclassification.MiscTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040@Category({MiscTests.class, SmallTests.class}) 041public class TestEncryption { 042 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestEncryption.class); 046 047 private static final Logger LOG = LoggerFactory.getLogger(TestEncryption.class); 048 049 @Test 050 public void testSmallBlocks() throws Exception { 051 byte[] key = new byte[16]; 052 Bytes.random(key); 053 byte[] iv = new byte[16]; 054 Bytes.random(iv); 055 for (int size: new int[] { 4, 8, 16, 32, 64, 128, 256, 512 } ) { 056 checkTransformSymmetry(key, iv, getRandomBlock(size)); 057 } 058 } 059 060 @Test 061 public void testLargeBlocks() throws Exception { 062 byte[] key = new byte[16]; 063 Bytes.random(key); 064 byte[] iv = new byte[16]; 065 Bytes.random(iv); 066 for (int size: new int[] { 256 * 1024, 512 * 1024, 1024 * 1024 } ) { 067 checkTransformSymmetry(key, iv, getRandomBlock(size)); 068 } 069 } 070 071 @Test 072 public void testOddSizedBlocks() throws Exception { 073 byte[] key = new byte[16]; 074 Bytes.random(key); 075 byte[] iv = new byte[16]; 076 Bytes.random(iv); 077 for (int size: new int[] { 3, 7, 11, 23, 47, 79, 119, 175 } ) { 078 checkTransformSymmetry(key, iv, getRandomBlock(size)); 079 } 080 } 081 082 @Test 083 public void testTypicalHFileBlocks() throws Exception { 084 byte[] key = new byte[16]; 085 Bytes.random(key); 086 byte[] iv = new byte[16]; 087 Bytes.random(iv); 088 for (int size: new int[] { 4 * 1024, 8 * 1024, 64 * 1024, 128 * 1024 } ) { 089 checkTransformSymmetry(key, iv, getRandomBlock(size)); 090 } 091 } 092 093 private void checkTransformSymmetry(byte[] keyBytes, byte[] iv, byte[] plaintext) 094 throws Exception { 095 LOG.info("checkTransformSymmetry: AES, plaintext length = " + plaintext.length); 096 097 Configuration conf = HBaseConfiguration.create(); 098 String algorithm = 099 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES); 100 Cipher aes = Encryption.getCipher(conf, algorithm); 101 Key key = new SecretKeySpec(keyBytes, algorithm); 102 103 Encryptor e = aes.getEncryptor(); 104 e.setKey(key); 105 e.setIv(iv); 106 e.reset(); 107 ByteArrayOutputStream encOut = new ByteArrayOutputStream(); 108 Encryption.encrypt(encOut, plaintext, 0, plaintext.length, e); 109 byte[] encrypted = encOut.toByteArray(); 110 111 Decryptor d = aes.getDecryptor(); 112 d.setKey(key); 113 d.setIv(iv); 114 d.reset(); 115 ByteArrayInputStream encIn = new ByteArrayInputStream(encrypted); 116 ByteArrayOutputStream decOut = new ByteArrayOutputStream(); 117 Encryption.decrypt(decOut, encIn, plaintext.length, d); 118 119 byte[] result = decOut.toByteArray(); 120 assertEquals("Decrypted result has different length than plaintext", 121 result.length, plaintext.length); 122 assertTrue("Transformation was not symmetric", 123 Bytes.equals(result, plaintext)); 124 } 125 126 private byte[] getRandomBlock(int size) { 127 byte[] b = new byte[size]; 128 Bytes.random(b); 129 return b; 130 } 131 132}