001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.security.AlgorithmParameters;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.InvalidKeyException;
026: import java.security.Key;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.SecureRandom;
029: import java.security.spec.AlgorithmParameterSpec;
030: import java.nio.ByteBuffer;
031:
032: import org.apache.harmony.crypto.internal.nls.Messages;
033:
034: /**
035: * @com.intel.drl.spec_ref
036: *
037: */
038:
039: public abstract class CipherSpi {
040:
041: /**
042: * @com.intel.drl.spec_ref
043: *
044: */
045: public CipherSpi() {
046: }
047:
048: /**
049: * @com.intel.drl.spec_ref
050: *
051: */
052: protected abstract void engineSetMode(String mode)
053: throws NoSuchAlgorithmException;
054:
055: /**
056: * @com.intel.drl.spec_ref
057: *
058: */
059: protected abstract void engineSetPadding(String padding)
060: throws NoSuchPaddingException;
061:
062: /**
063: * @com.intel.drl.spec_ref
064: *
065: */
066: protected abstract int engineGetBlockSize();
067:
068: /**
069: * @com.intel.drl.spec_ref
070: *
071: */
072: protected abstract int engineGetOutputSize(int inputLen);
073:
074: /**
075: * @com.intel.drl.spec_ref
076: *
077: */
078: protected abstract byte[] engineGetIV();
079:
080: /**
081: * @com.intel.drl.spec_ref
082: *
083: */
084: protected abstract AlgorithmParameters engineGetParameters();
085:
086: /**
087: * @com.intel.drl.spec_ref
088: *
089: */
090: protected abstract void engineInit(int opmode, Key key,
091: SecureRandom random) throws InvalidKeyException;
092:
093: /**
094: * @com.intel.drl.spec_ref
095: *
096: */
097: protected abstract void engineInit(int opmode, Key key,
098: AlgorithmParameterSpec params, SecureRandom random)
099: throws InvalidKeyException,
100: InvalidAlgorithmParameterException;
101:
102: /**
103: * @com.intel.drl.spec_ref
104: *
105: */
106: protected abstract void engineInit(int opmode, Key key,
107: AlgorithmParameters params, SecureRandom random)
108: throws InvalidKeyException,
109: InvalidAlgorithmParameterException;
110:
111: /**
112: * @com.intel.drl.spec_ref
113: *
114: */
115: protected abstract byte[] engineUpdate(byte[] input,
116: int inputOffset, int inputLen);
117:
118: /**
119: * @com.intel.drl.spec_ref
120: *
121: */
122: protected abstract int engineUpdate(byte[] input, int inputOffset,
123: int inputLen, byte[] output, int outputOffset)
124: throws ShortBufferException;
125:
126: /**
127: * @com.intel.drl.spec_ref
128: *
129: */
130: protected int engineUpdate(ByteBuffer input, ByteBuffer output)
131: throws ShortBufferException {
132: if (input == null) {
133: throw new NullPointerException(Messages
134: .getString("crypto.0C")); //$NON-NLS-1$
135: }
136: if (output == null) {
137: throw new NullPointerException(Messages
138: .getString("crypto.0D")); //$NON-NLS-1$
139: }
140: int position = input.position();
141: int limit = input.limit();
142: if ((limit - position) <= 0) {
143: return 0;
144: }
145: byte[] bInput;
146: byte[] bOutput;
147: if (input.hasArray()) {
148: bInput = input.array();
149: int offset = input.arrayOffset();
150: bOutput = engineUpdate(bInput, offset + position, limit
151: - position);
152: input.position(limit);
153: } else {
154: bInput = new byte[limit - position];
155: input.get(bInput);
156: bOutput = engineUpdate(bInput, 0, limit - position);
157: }
158: if (output.remaining() < bOutput.length) {
159: throw new ShortBufferException(Messages
160: .getString("crypto.0E")); //$NON-NLS-1$
161: }
162: try {
163: output.put(bOutput);
164: } catch (java.nio.BufferOverflowException e) {
165: throw new ShortBufferException(Messages.getString(
166: "crypto.0F", e)); //$NON-NLS-1$
167: }
168: return bOutput.length;
169: }
170:
171: /**
172: * @com.intel.drl.spec_ref
173: *
174: */
175: protected abstract byte[] engineDoFinal(byte[] input,
176: int inputOffset, int inputLen)
177: throws IllegalBlockSizeException, BadPaddingException;
178:
179: /**
180: * @com.intel.drl.spec_ref
181: *
182: */
183: protected abstract int engineDoFinal(byte[] input, int inputOffset,
184: int inputLen, byte[] output, int outputOffset)
185: throws ShortBufferException, IllegalBlockSizeException,
186: BadPaddingException;
187:
188: /**
189: * @com.intel.drl.spec_ref
190: *
191: */
192: protected int engineDoFinal(ByteBuffer input, ByteBuffer output)
193: throws ShortBufferException, IllegalBlockSizeException,
194: BadPaddingException {
195: if (input == null) {
196: throw new NullPointerException(Messages
197: .getString("crypto.0C")); //$NON-NLS-1$
198: }
199: if (output == null) {
200: throw new NullPointerException(Messages
201: .getString("crypto.0D")); //$NON-NLS-1$
202: }
203: int position = input.position();
204: int limit = input.limit();
205:
206: if ((limit - position) <= 0) {
207: return 0;
208: }
209: byte[] bInput;
210: byte[] bOutput;
211:
212: if (input.hasArray()) {
213: bInput = input.array();
214: int offset = input.arrayOffset();
215: bOutput = engineDoFinal(bInput, offset + position, limit
216: - position);
217: input.position(limit);
218: } else {
219: bInput = new byte[limit - position];
220: input.get(bInput);
221: bOutput = engineDoFinal(bInput, 0, limit - position);
222: }
223: if (output.remaining() < bOutput.length) {
224: throw new ShortBufferException(Messages
225: .getString("crypto.0E")); //$NON-NLS-1$
226: }
227: try {
228: output.put(bOutput);
229: } catch (java.nio.BufferOverflowException e) {
230: throw new ShortBufferException(Messages.getString(
231: "crypto.0F", e)); //$NON-NLS-1$
232: }
233: return bOutput.length;
234: }
235:
236: /**
237: * @com.intel.drl.spec_ref
238: *
239: */
240: protected byte[] engineWrap(Key key)
241: throws IllegalBlockSizeException, InvalidKeyException {
242: throw new UnsupportedOperationException(Messages
243: .getString("crypto.10")); //$NON-NLS-1$
244: }
245:
246: /**
247: * @com.intel.drl.spec_ref
248: *
249: */
250: protected Key engineUnwrap(byte[] wrappedKey,
251: String wrappedKeyAlgorithm, int wrappedKeyType)
252: throws InvalidKeyException, NoSuchAlgorithmException {
253: throw new UnsupportedOperationException(Messages
254: .getString("crypto.11")); //$NON-NLS-1$
255: }
256:
257: /**
258: * @com.intel.drl.spec_ref
259: *
260: */
261: protected int engineGetKeySize(Key key) throws InvalidKeyException {
262: throw new UnsupportedOperationException(Messages
263: .getString("crypto.12")); //$NON-NLS-1$
264: }
265: }
|