CryptoCipher.java

  1.  /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.commons.crypto.cipher;

  19. import java.io.Closeable;
  20. import java.nio.ByteBuffer;
  21. import java.security.InvalidAlgorithmParameterException;
  22. import java.security.InvalidKeyException;
  23. import java.security.Key;
  24. import java.security.spec.AlgorithmParameterSpec;

  25. import javax.crypto.BadPaddingException;
  26. import javax.crypto.IllegalBlockSizeException;
  27. import javax.crypto.ShortBufferException;

  28. /**
  29.  * The interface of cryptographic cipher for encryption and decryption.
  30.  *
  31.  * <p>
  32.  * Note that implementations must provide a constructor that has 2 parameters: a Properties instance and a String (transformation)
  33.  * </p>
  34.  */
  35. public interface CryptoCipher extends Closeable {

  36.     /**
  37.      * Encrypts or decrypts data in a single-part operation, or finishes a
  38.      * multiple-part operation.
  39.      *
  40.      * @param input the input byte array
  41.      * @param inputOffset the offset in input where the input starts
  42.      * @param inputLen the input length
  43.      * @param output the byte array for the result
  44.      * @param outputOffset the offset in output where the result is stored
  45.      * @return the number of bytes stored in output
  46.      * @throws ShortBufferException if the given output byte array is too small
  47.      *         to hold the result
  48.      * @throws BadPaddingException if this cipher is in decryption mode, and
  49.      *         (un)padding has been requested, but the decrypted data is not
  50.      *         bounded by the appropriate padding bytes
  51.      * @throws IllegalBlockSizeException if this cipher is a block cipher, no
  52.      *         padding has been requested (only in encryption mode), and the
  53.      *         total input length of the data processed by this cipher is not a
  54.      *         multiple of block size; or if this encryption algorithm is unable
  55.      *         to process the input data provided.
  56.      */
  57.     int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
  58.             int outputOffset) throws ShortBufferException,
  59.             IllegalBlockSizeException, BadPaddingException;

  60.     /**
  61.      * Encrypts or decrypts data in a single-part operation, or finishes a
  62.      * multiple-part operation.
  63.      *
  64.      * @param inBuffer the input ByteBuffer
  65.      * @param outBuffer the output ByteBuffer
  66.      * @return int number of bytes stored in {@code output}
  67.      * @throws BadPaddingException if this cipher is in decryption mode, and
  68.      *         (un)padding has been requested, but the decrypted data is not
  69.      *         bounded by the appropriate padding bytes
  70.      * @throws IllegalBlockSizeException if this cipher is a block cipher, no
  71.      *         padding has been requested (only in encryption mode), and the
  72.      *         total input length of the data processed by this cipher is not a
  73.      *         multiple of block size; or if this encryption algorithm is unable
  74.      *         to process the input data provided.
  75.      * @throws ShortBufferException if the given output buffer is too small to
  76.      *         hold the result
  77.      */
  78.     int doFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
  79.             throws ShortBufferException, IllegalBlockSizeException,
  80.             BadPaddingException;

  81.     /**
  82.      * Returns the algorithm name of this {@code CryptoCipher} object.
  83.      *
  84.      * <p>
  85.      * This is the same name that was specified in one of the
  86.      * {@code CryptoCipherFactory#getInstance} calls that created this
  87.      * {@code CryptoCipher} object..
  88.      * </p>
  89.      *
  90.      * @return the algorithm name of this {@code CryptoCipher} object.
  91.      */
  92.     String getAlgorithm();

  93.     /**
  94.      * Returns the block size (in bytes).
  95.      *
  96.      * @return the block size (in bytes), or 0 if the underlying algorithm is
  97.      * not a block cipher
  98.      */
  99.     int getBlockSize();

  100.     /**
  101.      * Initializes the cipher with mode, key and iv.
  102.      *
  103.      * @param mode {@link javax.crypto.Cipher#ENCRYPT_MODE} or
  104.      *             {@link javax.crypto.Cipher#DECRYPT_MODE}
  105.      * @param key crypto key for the cipher
  106.      * @param params the algorithm parameters
  107.      * @throws InvalidKeyException if the given key is inappropriate for
  108.      *         initializing this cipher, or its keysize exceeds the maximum
  109.      *         allowable keysize (as determined from the configured jurisdiction
  110.      *         policy files).
  111.      * @throws InvalidAlgorithmParameterException if the given algorithm
  112.      *         parameters are inappropriate for this cipher, or this cipher
  113.      *         requires algorithm parameters and {@code params} is null, or
  114.      *         the given algorithm parameters imply a cryptographic strength
  115.      *         that would exceed the legal limits (as determined from the
  116.      *         configured jurisdiction policy files).
  117.      */
  118.     void init(int mode, Key key, AlgorithmParameterSpec params)
  119.             throws InvalidKeyException, InvalidAlgorithmParameterException;

  120.     /**
  121.      * Continues a multiple-part encryption/decryption operation. The data is
  122.      * encrypted or decrypted, depending on how this cipher was initialized.
  123.      *
  124.      * @param input the input byte array
  125.      * @param inputOffset the offset in input where the input starts
  126.      * @param inputLen the input length
  127.      * @param output the byte array for the result
  128.      * @param outputOffset the offset in output where the result is stored
  129.      * @return the number of bytes stored in output
  130.      * @throws ShortBufferException if there is insufficient space in the output
  131.      *         byte array
  132.      */
  133.     int update(byte[] input, int inputOffset, int inputLen, byte[] output,
  134.             int outputOffset) throws ShortBufferException;

  135.     /**
  136.      * Continues a multiple-part encryption/decryption operation. The data is
  137.      * encrypted or decrypted, depending on how this cipher was initialized.
  138.      *
  139.      * @param inBuffer the input ByteBuffer
  140.      * @param outBuffer the output ByteBuffer
  141.      * @return int number of bytes stored in {@code output}
  142.      * @throws ShortBufferException if there is insufficient space in the output
  143.      *         buffer
  144.      */
  145.     int update(ByteBuffer inBuffer, ByteBuffer outBuffer)
  146.             throws ShortBufferException;

  147.     /**
  148.      * Continues a multi-part update of the Additional Authentication
  149.      * Data (AAD).
  150.      * <p>
  151.      * Calls to this method provide AAD to the cipher when operating in
  152.      * modes such as AEAD (GCM).  If this cipher is operating in
  153.      * GCM mode, all AAD must be supplied before beginning
  154.      * operations on the ciphertext (via the {@code update} and
  155.      * {@code doFinal} methods).
  156.      * </p>
  157.      *
  158.      * @param aad the buffer containing the Additional Authentication Data
  159.      *
  160.      * @throws IllegalArgumentException if the {@code aad}
  161.      * byte array is null
  162.      * @throws IllegalStateException if this cipher is in a wrong state
  163.      * (e.g., has not been initialized), does not accept AAD, or if
  164.      * operating in either GCM or CCM mode and one of the {@code update}
  165.      * methods has already been called for the active
  166.      * encryption/decryption operation
  167.      * @throws UnsupportedOperationException if the corresponding method
  168.      * has not been overridden by an implementation
  169.      *
  170.      */
  171.     default void updateAAD(final byte[] aad)
  172.             throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException {
  173.       throw new UnsupportedOperationException();
  174.     }

  175.     /**
  176.      * Continues a multi-part update of the Additional Authentication
  177.      * Data (AAD).
  178.      * <p>
  179.      * Calls to this method provide AAD to the cipher when operating in
  180.      * modes such as AEAD (GCM).  If this cipher is operating in
  181.      * GCM mode, all AAD must be supplied before beginning
  182.      * operations on the ciphertext (via the {@code update} and
  183.      * {@code doFinal} methods).
  184.      * </p>
  185.      *
  186.      * @param aad the buffer containing the Additional Authentication Data
  187.      *
  188.      * @throws IllegalArgumentException if the {@code aad}
  189.      * byte array is null
  190.      * @throws IllegalStateException if this cipher is in a wrong state
  191.      * (e.g., has not been initialized), does not accept AAD, or if
  192.      * operating in either GCM or CCM mode and one of the {@code update}
  193.      * methods has already been called for the active
  194.      * encryption/decryption operation
  195.      * @throws UnsupportedOperationException if the corresponding method
  196.      * has not been overridden by an implementation
  197.      *
  198.      */
  199.     default void updateAAD(final ByteBuffer aad)
  200.             throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException {
  201.       throw new UnsupportedOperationException();
  202.     }
  203. }