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 Boris V. Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.crypto.internal;
022:
023: import java.nio.ByteBuffer;
024: import java.security.AlgorithmParameters;
025: import java.security.InvalidAlgorithmParameterException;
026: import java.security.InvalidKeyException;
027: import java.security.Key;
028: import java.security.NoSuchAlgorithmException;
029: import java.security.SecureRandom;
030: import java.security.spec.AlgorithmParameterSpec;
031:
032: import javax.crypto.BadPaddingException;
033: import javax.crypto.CipherSpi;
034: import javax.crypto.IllegalBlockSizeException;
035: import javax.crypto.NoSuchPaddingException;
036: import javax.crypto.ShortBufferException;
037:
038: import org.apache.harmony.crypto.internal.nls.Messages;
039:
040: /**
041: * CipherSpi implementation for javax.crypto.NullCipher
042: *
043: */
044: public class NullCipherSpi extends CipherSpi {
045:
046: @Override
047: public void engineSetMode(String arg0)
048: throws NoSuchAlgorithmException {
049: // Do nothing
050: }
051:
052: @Override
053: public void engineSetPadding(String arg0)
054: throws NoSuchPaddingException {
055: // Do nothing
056: }
057:
058: @Override
059: public int engineGetBlockSize() {
060: return 1;
061: }
062:
063: @Override
064: public int engineGetOutputSize(int inputLen) {
065: return inputLen;
066: }
067:
068: @Override
069: public byte[] engineGetIV() {
070: return new byte[8]; // compatible with RI
071: }
072:
073: @Override
074: public AlgorithmParameters engineGetParameters() {
075: return null;
076: }
077:
078: @Override
079: public void engineInit(int opmode, Key key, SecureRandom random)
080: throws InvalidKeyException {
081: // Do nothing
082: }
083:
084: @Override
085: public void engineInit(int opmode, Key key,
086: AlgorithmParameterSpec params, SecureRandom random)
087: throws InvalidKeyException,
088: InvalidAlgorithmParameterException {
089: // Do nothing
090: }
091:
092: @Override
093: public void engineInit(int opmode, Key key,
094: AlgorithmParameters params, SecureRandom random)
095: throws InvalidKeyException,
096: InvalidAlgorithmParameterException {
097: // Do nothing
098: }
099:
100: @Override
101: public byte[] engineUpdate(byte[] input, int inputOffset,
102: int inputLen) {
103: if (input == null) {
104: return null;
105: }
106: byte[] result = new byte[inputLen];
107: System.arraycopy(input, inputOffset, result, 0, inputLen);
108: return result;
109: }
110:
111: @Override
112: public int engineUpdate(byte[] input, int inputOffset,
113: int inputLen, byte[] output, int outputOffset)
114: throws ShortBufferException {
115: if (input == null) {
116: return 0;
117: }
118: System.arraycopy(input, inputOffset, output, outputOffset,
119: inputLen);
120: return inputLen;
121: }
122:
123: @Override
124: public int engineUpdate(ByteBuffer input, ByteBuffer output)
125: throws ShortBufferException {
126: if (input == null || output == null) {
127: throw new NullPointerException();
128: }
129: int result = input.limit() - input.position();
130: try {
131: output.put(input);
132: } catch (java.nio.BufferOverflowException e) {
133: throw new ShortBufferException(Messages.getString(
134: "crypto.0F", e)); //$NON-NLS-1$
135: }
136: return result;
137: }
138:
139: @Override
140: public byte[] engineDoFinal(byte[] input, int inputOffset,
141: int inputLen) throws IllegalBlockSizeException,
142: BadPaddingException {
143: if (input == null) {
144: return null;
145: }
146: return engineUpdate(input, inputOffset, inputLen);
147: }
148:
149: @Override
150: public int engineDoFinal(byte[] input, int inputOffset,
151: int inputLen, byte[] output, int outputOffset)
152: throws ShortBufferException, IllegalBlockSizeException,
153: BadPaddingException {
154: int result = engineUpdate(input, inputOffset, inputLen, output,
155: outputOffset);
156: return result;
157: }
158:
159: @Override
160: public int engineDoFinal(ByteBuffer input, ByteBuffer output)
161: throws ShortBufferException, IllegalBlockSizeException,
162: BadPaddingException {
163: return engineUpdate(input, output);
164: }
165:
166: @Override
167: public byte[] engineWrap(Key key) throws IllegalBlockSizeException,
168: InvalidKeyException {
169: throw new UnsupportedOperationException(Messages
170: .getString("crypto.44")); //$NON-NLS-1$
171: }
172:
173: @Override
174: public Key engineUnwrap(byte[] wrappedKey,
175: String wrappedKeyAlgorithm, int wrappedKeyType)
176: throws InvalidKeyException, NoSuchAlgorithmException {
177: throw new UnsupportedOperationException(Messages
178: .getString("crypto.45")); //$NON-NLS-1$
179: }
180:
181: @Override
182: public int engineGetKeySize(Key key) throws InvalidKeyException {
183: throw new UnsupportedOperationException(Messages
184: .getString("crypto.46")); //$NON-NLS-1$
185: }
186: }
|