AbstractOpenSslFeedbackCipher.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.nio.ByteBuffer;
  20. import java.security.InvalidAlgorithmParameterException;
  21. import java.security.spec.AlgorithmParameterSpec;

  22. import javax.crypto.BadPaddingException;
  23. import javax.crypto.IllegalBlockSizeException;
  24. import javax.crypto.ShortBufferException;

  25. import org.apache.commons.crypto.utils.Utils;

  26. /**
  27.  * This class represents a block cipher in one of its modes.
  28.  */
  29. abstract class AbstractOpenSslFeedbackCipher {

  30.     protected long context;
  31.     protected final int algorithmMode;
  32.     protected final int padding;

  33.     protected int cipherMode = OpenSsl.DECRYPT_MODE;

  34.     AbstractOpenSslFeedbackCipher(final long context, final int algorithmMode, final int padding) {
  35.         this.context = context;
  36.         this.algorithmMode = algorithmMode;
  37.         this.padding = padding;
  38.     }

  39.     public void checkState() {
  40.         Utils.checkState(context != 0, "Cipher context is invalid.");
  41.     }

  42.     public void clean() {
  43.         if (context != 0) {
  44.             OpenSslNative.clean(context);
  45.             context = 0;
  46.         }
  47.     }

  48.     abstract int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
  49.             throws ShortBufferException, IllegalBlockSizeException, BadPaddingException;

  50.     abstract int doFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException;

  51.     abstract void init(int mode, byte[] key, AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException;

  52.     abstract int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException;

  53.     abstract int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException;

  54.     abstract void updateAAD(byte[] aad);
  55. }