001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with this 004 * work for additional information regarding copyright ownership. The ASF 005 * licenses this file to you under the Apache License, Version 2.0 (the 006 * "License"); you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations under 015 * the License. 016 */ 017package org.apache.hadoop.hbase.io.crypto; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.OutputStream; 022import java.security.Key; 023 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** 027 * A common interface for a cryptographic algorithm. 028 */ 029@InterfaceAudience.Public 030public abstract class Cipher { 031 032 public static final int KEY_LENGTH = 16; 033 public static final int KEY_LENGTH_BITS = KEY_LENGTH * 8; 034 public static final int BLOCK_SIZE = 16; 035 public static final int IV_LENGTH = 16; 036 037 public static final String RNG_ALGORITHM_KEY = "hbase.crypto.algorithm.rng"; 038 public static final String RNG_PROVIDER_KEY = "hbase.crypto.algorithm.rng.provider"; 039 040 private final CipherProvider provider; 041 042 public Cipher(CipherProvider provider) { 043 this.provider = provider; 044 } 045 046 /** 047 * Return the provider for this Cipher 048 */ 049 public CipherProvider getProvider() { 050 return provider; 051 } 052 053 /** 054 * Return this Cipher's name 055 */ 056 public abstract String getName(); 057 058 /** 059 * Return the key length required by this cipher, in bytes 060 */ 061 public abstract int getKeyLength(); 062 063 /** 064 * Return the expected initialization vector length, in bytes, or 0 if not applicable 065 */ 066 public abstract int getIvLength(); 067 068 /** 069 * Create a random symmetric key 070 * @return the random symmetric key 071 */ 072 public abstract Key getRandomKey(); 073 074 /** 075 * Get an encryptor for encrypting data. 076 */ 077 public abstract Encryptor getEncryptor(); 078 079 /** 080 * Return a decryptor for decrypting data. 081 */ 082 public abstract Decryptor getDecryptor(); 083 084 /** 085 * Create an encrypting output stream given a context and IV 086 * @param out the output stream to wrap 087 * @param context the encryption context 088 * @param iv initialization vector 089 * @return the encrypting wrapper 090 * @throws IOException 091 */ 092 public abstract OutputStream createEncryptionStream(OutputStream out, Context context, 093 byte[] iv) 094 throws IOException; 095 096 /** 097 * Create an encrypting output stream given an initialized encryptor 098 * @param out the output stream to wrap 099 * @param encryptor the encryptor 100 * @return the encrypting wrapper 101 * @throws IOException 102 */ 103 public abstract OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor) 104 throws IOException; 105 106 /** 107 * Create a decrypting input stream given a context and IV 108 * @param in the input stream to wrap 109 * @param context the encryption context 110 * @param iv initialization vector 111 * @return the decrypting wrapper 112 * @throws IOException 113 */ 114 public abstract InputStream createDecryptionStream(InputStream in, Context context, 115 byte[] iv) 116 throws IOException; 117 118 /** 119 * Create a decrypting output stream given an initialized decryptor 120 * @param in the input stream to wrap 121 * @param decryptor the decryptor 122 * @return the decrypting wrapper 123 * @throws IOException 124 */ 125 public abstract InputStream createDecryptionStream(InputStream in, Decryptor decryptor) 126 throws IOException; 127 128}