• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 /**
19 * @author Boris V. Kuznetsov
20 * @version $Revision$
21 */
22 
23 package org.apache.harmony.crypto.internal;
24 
25 import java.nio.ByteBuffer;
26 import java.security.AlgorithmParameters;
27 import java.security.InvalidAlgorithmParameterException;
28 import java.security.InvalidKeyException;
29 import java.security.Key;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.SecureRandom;
32 import java.security.spec.AlgorithmParameterSpec;
33 import javax.crypto.BadPaddingException;
34 import javax.crypto.CipherSpi;
35 import javax.crypto.IllegalBlockSizeException;
36 import javax.crypto.NoSuchPaddingException;
37 import javax.crypto.ShortBufferException;
38 
39 /**
40  * CipherSpi implementation for javax.crypto.NullCipher
41  *
42  */
43 public class NullCipherSpi extends CipherSpi {
44 
45     @Override
engineSetMode(String arg0)46     public void engineSetMode(String arg0) throws NoSuchAlgorithmException {
47         // Do nothing
48     }
49 
50     @Override
engineSetPadding(String arg0)51     public void engineSetPadding(String arg0) throws NoSuchPaddingException {
52         // Do nothing
53     }
54 
55     @Override
engineGetBlockSize()56     public int engineGetBlockSize() {
57         return 1;
58     }
59 
60     @Override
engineGetOutputSize(int inputLen)61     public int engineGetOutputSize(int inputLen) {
62         return inputLen;
63     }
64 
65     @Override
engineGetIV()66     public byte[] engineGetIV() {
67         return new byte[8]; // compatible with RI
68     }
69 
70     @Override
engineGetParameters()71     public AlgorithmParameters engineGetParameters() {
72         return null;
73     }
74 
75     @Override
engineInit(int opmode, Key key, SecureRandom random)76     public void engineInit(int opmode, Key key, SecureRandom random)
77             throws InvalidKeyException {
78         // Do nothing
79     }
80 
81     @Override
engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)82     public void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
83             SecureRandom random) throws InvalidKeyException,
84             InvalidAlgorithmParameterException {
85         // Do nothing
86     }
87 
88     @Override
engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)89     public void engineInit(int opmode, Key key, AlgorithmParameters params,
90             SecureRandom random) throws InvalidKeyException,
91             InvalidAlgorithmParameterException {
92         // Do nothing
93     }
94 
95     @Override
engineUpdate(byte[] input, int inputOffset, int inputLen)96     public byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
97         if (input == null) {
98             return null;
99         }
100         byte[] result = new byte[inputLen];
101         System.arraycopy(input, inputOffset, result, 0, inputLen);
102         return result;
103     }
104 
105     @Override
engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)106     public int engineUpdate(byte[] input, int inputOffset, int inputLen,
107             byte[] output, int outputOffset) throws ShortBufferException {
108         if (input == null) {
109             return 0;
110         }
111         System.arraycopy(input, inputOffset, output, outputOffset, inputLen);
112         return inputLen;
113     }
114 
115     @Override
engineUpdate(ByteBuffer input, ByteBuffer output)116     public int engineUpdate(ByteBuffer input, ByteBuffer output)
117             throws ShortBufferException {
118         if (input == null || output == null) {
119             throw new NullPointerException();
120         }
121         int result = input.limit() - input.position();
122         try {
123             output.put(input);
124         } catch (java.nio.BufferOverflowException e) {
125             throw new ShortBufferException("output buffer too small");
126         }
127         return result;
128     }
129 
130     @Override
engineDoFinal(byte[] input, int inputOffset, int inputLen)131     public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
132             throws IllegalBlockSizeException, BadPaddingException {
133         if (input == null) {
134             return null;
135         }
136         return engineUpdate(input, inputOffset, inputLen);
137     }
138 
139     @Override
engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)140     public int engineDoFinal(byte[] input, int inputOffset, int inputLen,
141             byte[] output, int outputOffset) throws ShortBufferException,
142             IllegalBlockSizeException, BadPaddingException {
143         int result = engineUpdate(input, inputOffset, inputLen, output,
144                 outputOffset);
145         return result;
146     }
147 
148     @Override
engineDoFinal(ByteBuffer input, ByteBuffer output)149     public int engineDoFinal(ByteBuffer input, ByteBuffer output)
150             throws ShortBufferException, IllegalBlockSizeException,
151             BadPaddingException {
152         return engineUpdate(input, output);
153     }
154 
155     @Override
engineWrap(Key key)156     public byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
157         throw new UnsupportedOperationException();
158     }
159 
160     @Override
engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)161     public Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
162             int wrappedKeyType) throws InvalidKeyException,
163             NoSuchAlgorithmException {
164         throw new UnsupportedOperationException();
165     }
166 
167     @Override
engineGetKeySize(Key key)168     public int engineGetKeySize(Key key) throws InvalidKeyException {
169         throw new UnsupportedOperationException();
170     }
171 }
172