Home | History | Annotate | Download | only in crypto
      1 /*
      2  * Copyright 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.javax.crypto;
     18 
     19 import java.security.AlgorithmParameters;
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.InvalidKeyException;
     22 import java.security.Key;
     23 import java.security.NoSuchAlgorithmException;
     24 import java.security.SecureRandom;
     25 import java.security.spec.AlgorithmParameterSpec;
     26 
     27 import javax.crypto.BadPaddingException;
     28 import javax.crypto.CipherSpi;
     29 import javax.crypto.IllegalBlockSizeException;
     30 import javax.crypto.NoSuchPaddingException;
     31 import javax.crypto.ShortBufferException;
     32 
     33 /**
     34  * Mock CipherSpi used by {@link CipherTest}.
     35  */
     36 public class MockCipherSpi extends CipherSpi {
     37     public static class SpecificKeyTypes extends MockCipherSpi {
     38         @Override
     39         public void checkKeyType(Key key) throws InvalidKeyException {
     40             if (!(key instanceof MockKey)) {
     41                 throw new InvalidKeyException("Must be MockKey!");
     42             }
     43         }
     44     }
     45 
     46     public static class SpecificKeyTypes2 extends MockCipherSpi {
     47         @Override
     48         public void checkKeyType(Key key) throws InvalidKeyException {
     49             System.err.println("Checking key of type " + key.getClass().getName());
     50             if (!(key instanceof MockKey2)) {
     51                 throw new InvalidKeyException("Must be MockKey2!");
     52             }
     53         }
     54     }
     55 
     56     public static class AllKeyTypes extends MockCipherSpi {
     57     }
     58 
     59     public void checkKeyType(Key key) throws InvalidKeyException {
     60     }
     61 
     62     @Override
     63     protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
     64         if (!"FOO".equals(mode)) {
     65             throw new UnsupportedOperationException("not implemented");
     66         }
     67     }
     68 
     69     @Override
     70     protected void engineSetPadding(String padding) throws NoSuchPaddingException {
     71         if (!"FOO".equals(padding)) {
     72             throw new UnsupportedOperationException("not implemented");
     73         }
     74     }
     75 
     76     @Override
     77     protected int engineGetBlockSize() {
     78         throw new UnsupportedOperationException("not implemented");
     79     }
     80 
     81     @Override
     82     protected int engineGetOutputSize(int inputLen) {
     83         throw new UnsupportedOperationException("not implemented");
     84     }
     85 
     86     @Override
     87     protected byte[] engineGetIV() {
     88         throw new UnsupportedOperationException("not implemented");
     89     }
     90 
     91     @Override
     92     protected AlgorithmParameters engineGetParameters() {
     93         throw new UnsupportedOperationException("not implemented");
     94     }
     95 
     96     @Override
     97     protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException {
     98         checkKeyType(key);
     99     }
    100 
    101     @Override
    102     protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    103             SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    104         checkKeyType(key);
    105     }
    106 
    107     @Override
    108     protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
    109             throws InvalidKeyException, InvalidAlgorithmParameterException {
    110         checkKeyType(key);
    111     }
    112 
    113     @Override
    114     protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
    115         throw new UnsupportedOperationException("not implemented");
    116     }
    117 
    118     @Override
    119     protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output,
    120             int outputOffset) throws ShortBufferException {
    121         throw new UnsupportedOperationException("not implemented");
    122     }
    123 
    124     @Override
    125     protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
    126             throws IllegalBlockSizeException, BadPaddingException {
    127         throw new UnsupportedOperationException("not implemented");
    128     }
    129 
    130     @Override
    131     protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
    132             int outputOffset) throws ShortBufferException, IllegalBlockSizeException,
    133             BadPaddingException {
    134         throw new UnsupportedOperationException("not implemented");
    135     }
    136 }
    137