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 34 import javax.crypto.BadPaddingException; 35 import javax.crypto.CipherSpi; 36 import javax.crypto.IllegalBlockSizeException; 37 import javax.crypto.NoSuchPaddingException; 38 import javax.crypto.ShortBufferException; 39 40 import org.apache.harmony.crypto.internal.nls.Messages; 41 42 /** 43 * CipherSpi implementation for javax.crypto.NullCipher 44 * 45 */ 46 public class NullCipherSpi extends CipherSpi { 47 48 @Override engineSetMode(String arg0)49 public void engineSetMode(String arg0) throws NoSuchAlgorithmException { 50 // Do nothing 51 } 52 53 @Override engineSetPadding(String arg0)54 public void engineSetPadding(String arg0) throws NoSuchPaddingException { 55 // Do nothing 56 } 57 58 @Override engineGetBlockSize()59 public int engineGetBlockSize() { 60 return 1; 61 } 62 63 @Override engineGetOutputSize(int inputLen)64 public int engineGetOutputSize(int inputLen) { 65 return inputLen; 66 } 67 68 @Override engineGetIV()69 public byte[] engineGetIV() { 70 return new byte[8]; // compatible with RI 71 } 72 73 @Override engineGetParameters()74 public AlgorithmParameters engineGetParameters() { 75 return null; 76 } 77 78 @Override engineInit(int opmode, Key key, SecureRandom random)79 public void engineInit(int opmode, Key key, SecureRandom random) 80 throws InvalidKeyException { 81 // Do nothing 82 } 83 84 @Override engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)85 public void engineInit(int opmode, Key key, AlgorithmParameterSpec params, 86 SecureRandom random) throws InvalidKeyException, 87 InvalidAlgorithmParameterException { 88 // Do nothing 89 } 90 91 @Override engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)92 public void engineInit(int opmode, Key key, AlgorithmParameters params, 93 SecureRandom random) throws InvalidKeyException, 94 InvalidAlgorithmParameterException { 95 // Do nothing 96 } 97 98 @Override engineUpdate(byte[] input, int inputOffset, int inputLen)99 public byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) { 100 if (input == null) { 101 return null; 102 } 103 byte[] result = new byte[inputLen]; 104 System.arraycopy(input, inputOffset, result, 0, inputLen); 105 return result; 106 } 107 108 @Override engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)109 public int engineUpdate(byte[] input, int inputOffset, int inputLen, 110 byte[] output, int outputOffset) throws ShortBufferException { 111 if (input == null) { 112 return 0; 113 } 114 System.arraycopy(input, inputOffset, output, outputOffset, inputLen); 115 return inputLen; 116 } 117 118 @Override engineUpdate(ByteBuffer input, ByteBuffer output)119 public int engineUpdate(ByteBuffer input, ByteBuffer output) 120 throws ShortBufferException { 121 if (input == null || output == null) { 122 throw new NullPointerException(); 123 } 124 int result = input.limit() - input.position(); 125 try { 126 output.put(input); 127 } catch (java.nio.BufferOverflowException e) { 128 throw new ShortBufferException(Messages.getString("crypto.0F", e)); //$NON-NLS-1$ 129 } 130 return result; 131 } 132 133 @Override engineDoFinal(byte[] input, int inputOffset, int inputLen)134 public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) 135 throws IllegalBlockSizeException, BadPaddingException { 136 if (input == null) { 137 return null; 138 } 139 return engineUpdate(input, inputOffset, inputLen); 140 } 141 142 @Override engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)143 public int engineDoFinal(byte[] input, int inputOffset, int inputLen, 144 byte[] output, int outputOffset) throws ShortBufferException, 145 IllegalBlockSizeException, BadPaddingException { 146 int result = engineUpdate(input, inputOffset, inputLen, output, 147 outputOffset); 148 return result; 149 } 150 151 @Override engineDoFinal(ByteBuffer input, ByteBuffer output)152 public int engineDoFinal(ByteBuffer input, ByteBuffer output) 153 throws ShortBufferException, IllegalBlockSizeException, 154 BadPaddingException { 155 return engineUpdate(input, output); 156 } 157 158 @Override engineWrap(Key key)159 public byte[] engineWrap(Key key) throws IllegalBlockSizeException, 160 InvalidKeyException { 161 throw new UnsupportedOperationException(Messages.getString("crypto.44")); //$NON-NLS-1$ 162 } 163 164 @Override engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)165 public Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, 166 int wrappedKeyType) throws InvalidKeyException, 167 NoSuchAlgorithmException { 168 throw new UnsupportedOperationException(Messages.getString("crypto.45")); //$NON-NLS-1$ 169 } 170 171 @Override engineGetKeySize(Key key)172 public int engineGetKeySize(Key key) throws InvalidKeyException { 173 throw new UnsupportedOperationException(Messages.getString("crypto.46")); //$NON-NLS-1$ 174 } 175 }