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 java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.security.Key;
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 n
090   */
091  public abstract OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv)
092    throws IOException;
093
094  /**
095   * Create an encrypting output stream given an initialized encryptor
096   * @param out       the output stream to wrap
097   * @param encryptor the encryptor
098   * @return the encrypting wrapper n
099   */
100  public abstract OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor)
101    throws IOException;
102
103  /**
104   * Create a decrypting input stream given a context and IV
105   * @param in      the input stream to wrap
106   * @param context the encryption context
107   * @param iv      initialization vector
108   * @return the decrypting wrapper n
109   */
110  public abstract InputStream createDecryptionStream(InputStream in, Context context, byte[] iv)
111    throws IOException;
112
113  /**
114   * Create a decrypting output stream given an initialized decryptor
115   * @param in        the input stream to wrap
116   * @param decryptor the decryptor
117   * @return the decrypting wrapper n
118   */
119  public abstract InputStream createDecryptionStream(InputStream in, Decryptor decryptor)
120    throws IOException;
121
122}