View Javadoc
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  
20  import java.nio.ByteBuffer;
21  import java.security.InvalidAlgorithmParameterException;
22  import java.security.spec.AlgorithmParameterSpec;
23  
24  import javax.crypto.BadPaddingException;
25  import javax.crypto.IllegalBlockSizeException;
26  import javax.crypto.ShortBufferException;
27  import javax.crypto.spec.IvParameterSpec;
28  
29  /**
30   * This class do the real work(Encryption/Decryption) for non-authenticated modes, such as CTR, CBC.
31   * <p>
32   * It will call the OpenSSL API to implement encryption/decryption
33   * </p>
34   */
35  final class OpenSslCommonMode extends AbstractOpenSslFeedbackCipher {
36  
37      OpenSslCommonMode(final long context, final int algorithmMode, final int padding) {
38          super(context, algorithmMode, padding);
39      }
40  
41      @Override
42      public int doFinal(final byte[] input, final int inputOffset, final int inputLen, final byte[] output, final int outputOffset)
43              throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
44          checkState();
45          final int outputLength = output.length;
46  
47          int len = OpenSslNative.updateByteArray(context, input, inputOffset, inputLen, output, outputOffset, outputLength - outputOffset);
48  
49          len += OpenSslNative.doFinalByteArray(context, output, outputOffset + len, outputLength - outputOffset - len);
50  
51          return len;
52      }
53  
54      @Override
55      public int doFinal(final ByteBuffer input, final ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
56          checkState();
57  
58          int totalLen = 0;
59          int len = OpenSslNative.update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining());
60          totalLen += len;
61  
62          input.position(input.limit());
63          output.position(output.position() + len);
64  
65          len = OpenSslNative.doFinal(context, output, output.position(), output.remaining());
66          totalLen += len;
67  
68          output.position(output.position() + len);
69  
70          return totalLen;
71      }
72  
73      @Override
74      public void init(final int mode, final byte[] key, final AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException {
75          this.cipherMode = mode;
76          final byte[] iv;
77          if (!(params instanceof IvParameterSpec)) {
78              // other AlgorithmParameterSpec is not supported now.
79              throw new InvalidAlgorithmParameterException("Illegal parameters");
80          }
81          iv = ((IvParameterSpec) params).getIV();
82          context = OpenSslNative.init(context, mode, algorithmMode, padding, key, iv);
83      }
84  
85      @Override
86      public int update(final byte[] input, final int inputOffset, final int inputLen, final byte[] output, final int outputOffset) throws ShortBufferException {
87          checkState();
88  
89          return OpenSslNative.updateByteArray(context, input, inputOffset, inputLen, output, outputOffset, output.length - outputOffset);
90      }
91  
92      @Override
93      public int update(final ByteBuffer input, final ByteBuffer output) throws ShortBufferException {
94          checkState();
95  
96          final int len = OpenSslNative.update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining());
97          input.position(input.limit());
98          output.position(output.position() + len);
99  
100         return len;
101     }
102 
103     @Override
104     public void updateAAD(final byte[] aad) {
105         throw new UnsupportedOperationException("The underlying Cipher implementation does not support this method");
106     }
107 }