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