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 23package org.apache.harmony.crypto.internal; 24 25import java.nio.ByteBuffer; 26import java.security.AlgorithmParameters; 27import java.security.InvalidAlgorithmParameterException; 28import java.security.InvalidKeyException; 29import java.security.Key; 30import java.security.NoSuchAlgorithmException; 31import java.security.SecureRandom; 32import java.security.spec.AlgorithmParameterSpec; 33 34import javax.crypto.BadPaddingException; 35import javax.crypto.CipherSpi; 36import javax.crypto.IllegalBlockSizeException; 37import javax.crypto.NoSuchPaddingException; 38import javax.crypto.ShortBufferException; 39 40import org.apache.harmony.crypto.internal.nls.Messages; 41 42/** 43 * CipherSpi implementation for javax.crypto.NullCipher 44 * 45 */ 46public class NullCipherSpi extends CipherSpi { 47 48 @Override 49 public void engineSetMode(String arg0) throws NoSuchAlgorithmException { 50 // Do nothing 51 } 52 53 @Override 54 public void engineSetPadding(String arg0) throws NoSuchPaddingException { 55 // Do nothing 56 } 57 58 @Override 59 public int engineGetBlockSize() { 60 return 1; 61 } 62 63 @Override 64 public int engineGetOutputSize(int inputLen) { 65 return inputLen; 66 } 67 68 @Override 69 public byte[] engineGetIV() { 70 return new byte[8]; // compatible with RI 71 } 72 73 @Override 74 public AlgorithmParameters engineGetParameters() { 75 return null; 76 } 77 78 @Override 79 public void engineInit(int opmode, Key key, SecureRandom random) 80 throws InvalidKeyException { 81 // Do nothing 82 } 83 84 @Override 85 public void engineInit(int opmode, Key key, AlgorithmParameterSpec params, 86 SecureRandom random) throws InvalidKeyException, 87 InvalidAlgorithmParameterException { 88 // Do nothing 89 } 90 91 @Override 92 public void engineInit(int opmode, Key key, AlgorithmParameters params, 93 SecureRandom random) throws InvalidKeyException, 94 InvalidAlgorithmParameterException { 95 // Do nothing 96 } 97 98 @Override 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 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 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 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 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 152 public int engineDoFinal(ByteBuffer input, ByteBuffer output) 153 throws ShortBufferException, IllegalBlockSizeException, 154 BadPaddingException { 155 return engineUpdate(input, output); 156 } 157 158 @Override 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 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 172 public int engineGetKeySize(Key key) throws InvalidKeyException { 173 throw new UnsupportedOperationException(Messages.getString("crypto.46")); //$NON-NLS-1$ 174 } 175}