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.commons.crypto.cipher;
019
020import java.io.Closeable;
021import java.nio.ByteBuffer;
022import java.security.InvalidAlgorithmParameterException;
023import java.security.InvalidKeyException;
024import java.security.Key;
025import java.security.spec.AlgorithmParameterSpec;
026
027import javax.crypto.BadPaddingException;
028import javax.crypto.IllegalBlockSizeException;
029import javax.crypto.ShortBufferException;
030
031/**
032 * The interface of cryptographic cipher for encryption and decryption.
033 *
034 * <p>
035 * Note that implementations must provide a constructor that has 2 parameters: a Properties instance and a String (transformation)
036 * </p>
037 */
038public interface CryptoCipher extends Closeable {
039
040    /**
041     * Encrypts or decrypts data in a single-part operation, or finishes a
042     * multiple-part operation.
043     *
044     * @param input the input byte array
045     * @param inputOffset the offset in input where the input starts
046     * @param inputLen the input length
047     * @param output the byte array for the result
048     * @param outputOffset the offset in output where the result is stored
049     * @return the number of bytes stored in output
050     * @throws ShortBufferException if the given output byte array is too small
051     *         to hold the result
052     * @throws BadPaddingException if this cipher is in decryption mode, and
053     *         (un)padding has been requested, but the decrypted data is not
054     *         bounded by the appropriate padding bytes
055     * @throws IllegalBlockSizeException if this cipher is a block cipher, no
056     *         padding has been requested (only in encryption mode), and the
057     *         total input length of the data processed by this cipher is not a
058     *         multiple of block size; or if this encryption algorithm is unable
059     *         to process the input data provided.
060     */
061    int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
062            int outputOffset) throws ShortBufferException,
063            IllegalBlockSizeException, BadPaddingException;
064
065    /**
066     * Encrypts or decrypts data in a single-part operation, or finishes a
067     * multiple-part operation.
068     *
069     * @param inBuffer the input ByteBuffer
070     * @param outBuffer the output ByteBuffer
071     * @return int number of bytes stored in {@code output}
072     * @throws BadPaddingException if this cipher is in decryption mode, and
073     *         (un)padding has been requested, but the decrypted data is not
074     *         bounded by the appropriate padding bytes
075     * @throws IllegalBlockSizeException if this cipher is a block cipher, no
076     *         padding has been requested (only in encryption mode), and the
077     *         total input length of the data processed by this cipher is not a
078     *         multiple of block size; or if this encryption algorithm is unable
079     *         to process the input data provided.
080     * @throws ShortBufferException if the given output buffer is too small to
081     *         hold the result
082     */
083    int doFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
084            throws ShortBufferException, IllegalBlockSizeException,
085            BadPaddingException;
086
087    /**
088     * Returns the algorithm name of this {@code CryptoCipher} object.
089     *
090     * <p>
091     * This is the same name that was specified in one of the
092     * {@code CryptoCipherFactory#getInstance} calls that created this
093     * {@code CryptoCipher} object..
094     * </p>
095     *
096     * @return the algorithm name of this {@code CryptoCipher} object.
097     */
098    String getAlgorithm();
099
100    /**
101     * Returns the block size (in bytes).
102     *
103     * @return the block size (in bytes), or 0 if the underlying algorithm is
104     * not a block cipher
105     */
106    int getBlockSize();
107
108    /**
109     * Initializes the cipher with mode, key and iv.
110     *
111     * @param mode {@link javax.crypto.Cipher#ENCRYPT_MODE} or
112     *             {@link javax.crypto.Cipher#DECRYPT_MODE}
113     * @param key crypto key for the cipher
114     * @param params the algorithm parameters
115     * @throws InvalidKeyException if the given key is inappropriate for
116     *         initializing this cipher, or its keysize exceeds the maximum
117     *         allowable keysize (as determined from the configured jurisdiction
118     *         policy files).
119     * @throws InvalidAlgorithmParameterException if the given algorithm
120     *         parameters are inappropriate for this cipher, or this cipher
121     *         requires algorithm parameters and {@code params} is null, or
122     *         the given algorithm parameters imply a cryptographic strength
123     *         that would exceed the legal limits (as determined from the
124     *         configured jurisdiction policy files).
125     */
126    void init(int mode, Key key, AlgorithmParameterSpec params)
127            throws InvalidKeyException, InvalidAlgorithmParameterException;
128
129    /**
130     * Continues a multiple-part encryption/decryption operation. The data is
131     * encrypted or decrypted, depending on how this cipher was initialized.
132     *
133     * @param input the input byte array
134     * @param inputOffset the offset in input where the input starts
135     * @param inputLen the input length
136     * @param output the byte array for the result
137     * @param outputOffset the offset in output where the result is stored
138     * @return the number of bytes stored in output
139     * @throws ShortBufferException if there is insufficient space in the output
140     *         byte array
141     */
142    int update(byte[] input, int inputOffset, int inputLen, byte[] output,
143            int outputOffset) throws ShortBufferException;
144
145    /**
146     * Continues a multiple-part encryption/decryption operation. The data is
147     * encrypted or decrypted, depending on how this cipher was initialized.
148     *
149     * @param inBuffer the input ByteBuffer
150     * @param outBuffer the output ByteBuffer
151     * @return int number of bytes stored in {@code output}
152     * @throws ShortBufferException if there is insufficient space in the output
153     *         buffer
154     */
155    int update(ByteBuffer inBuffer, ByteBuffer outBuffer)
156            throws ShortBufferException;
157
158    /**
159     * Continues a multi-part update of the Additional Authentication
160     * Data (AAD).
161     * <p>
162     * Calls to this method provide AAD to the cipher when operating in
163     * modes such as AEAD (GCM).  If this cipher is operating in
164     * GCM mode, all AAD must be supplied before beginning
165     * operations on the ciphertext (via the {@code update} and
166     * {@code doFinal} methods).
167     * </p>
168     *
169     * @param aad the buffer containing the Additional Authentication Data
170     *
171     * @throws IllegalArgumentException if the {@code aad}
172     * byte array is null
173     * @throws IllegalStateException if this cipher is in a wrong state
174     * (e.g., has not been initialized), does not accept AAD, or if
175     * operating in either GCM or CCM mode and one of the {@code update}
176     * methods has already been called for the active
177     * encryption/decryption operation
178     * @throws UnsupportedOperationException if the corresponding method
179     * has not been overridden by an implementation
180     *
181     */
182    default void updateAAD(final byte[] aad)
183            throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException {
184      throw new UnsupportedOperationException();
185    }
186
187    /**
188     * Continues a multi-part update of the Additional Authentication
189     * Data (AAD).
190     * <p>
191     * Calls to this method provide AAD to the cipher when operating in
192     * modes such as AEAD (GCM).  If this cipher is operating in
193     * GCM mode, all AAD must be supplied before beginning
194     * operations on the ciphertext (via the {@code update} and
195     * {@code doFinal} methods).
196     * </p>
197     *
198     * @param aad the buffer containing the Additional Authentication Data
199     *
200     * @throws IllegalArgumentException if the {@code aad}
201     * byte array is null
202     * @throws IllegalStateException if this cipher is in a wrong state
203     * (e.g., has not been initialized), does not accept AAD, or if
204     * operating in either GCM or CCM mode and one of the {@code update}
205     * methods has already been called for the active
206     * encryption/decryption operation
207     * @throws UnsupportedOperationException if the corresponding method
208     * has not been overridden by an implementation
209     *
210     */
211    default void updateAAD(final ByteBuffer aad)
212            throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException {
213      throw new UnsupportedOperationException();
214    }
215}