1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
31
32
33
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
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 }