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.Cipher;
     29 import javax.crypto.CipherSpi;
     30 import javax.crypto.IllegalBlockSizeException;
     31 import javax.crypto.NoSuchPaddingException;
     32 import javax.crypto.ShortBufferException;
     33 
     34 /**
     35  * Mock CipherSpi used by {@link CipherTest}.
     36  */
     37 public class MockCipherSpi extends CipherSpi {
     38     public static class SpecificKeyTypes extends MockCipherSpi {
     39         @Override
     40         public void checkKeyType(Key key) throws InvalidKeyException {
     41             if (!(key instanceof MockKey)) {
     42                 throw new InvalidKeyException("Must be MockKey!");
     43             }
     44         }
     45     }
     46 
     47     public static class SpecificKeyTypes2 extends MockCipherSpi {
     48         @Override
     49         public void checkKeyType(Key key) throws InvalidKeyException {
     50             System.err.println("Checking key of type " + key.getClass().getName());
     51             if (!(key instanceof MockKey2)) {
     52                 throw new InvalidKeyException("Must be MockKey2!");
     53             }
     54         }
     55     }
     56 
     57     public static class SpecificAlgorithmParameterSpecTypes extends MockCipherSpi {
     58         @Override
     59         public void checkAlgorithmParameterSpec(AlgorithmParameterSpec aps)
     60                 throws InvalidAlgorithmParameterException {
     61             if (!(aps instanceof MockAlgorithmParameterSpec)) {
     62                 throw new InvalidAlgorithmParameterException("Must be "
     63                         + MockAlgorithmParameterSpec.class.getName());
     64             }
     65         }
     66     }
     67 
     68     public static class SpecificAlgorithmParameterSpecTypes2 extends MockCipherSpi {
     69         @Override
     70         public void checkAlgorithmParameterSpec(AlgorithmParameterSpec aps)
     71                 throws InvalidAlgorithmParameterException {
     72             if (!(aps instanceof MockAlgorithmParameterSpec2)) {
     73                 throw new InvalidAlgorithmParameterException("Must be "
     74                         + MockAlgorithmParameterSpec2.class.getName());
     75             }
     76         }
     77     }
     78 
     79     public static class SpecificAlgorithmParameterAesAlgorithm extends MockCipherSpi {
     80         @Override
     81         public void checkAlgorithmParameters(AlgorithmParameters ap)
     82                 throws InvalidAlgorithmParameterException {
     83             if (!ap.getAlgorithm().equals("AES")) {
     84                 throw new InvalidAlgorithmParameterException("Must be AES");
     85             }
     86         }
     87     }
     88 
     89     public static class SpecificAlgorithmParametersDesAlgorithm extends MockCipherSpi {
     90         @Override
     91         public void checkAlgorithmParameters(AlgorithmParameters ap)
     92                 throws InvalidAlgorithmParameterException {
     93             if ((!ap.getAlgorithm().equals("DES"))) {
     94                 throw new InvalidAlgorithmParameterException("Must be DES");
     95             }
     96         }
     97     }
     98 
     99     public static class AllKeyTypes extends MockCipherSpi {
    100     }
    101 
    102     public static class MustInitWithAlgorithmParameterSpec_RejectsAll extends MockCipherSpi {
    103         @Override
    104         protected void engineInit(int opmode, Key key, SecureRandom random)
    105                 throws InvalidKeyException {
    106             throw new AssertionError("Must have AlgorithmParameterSpec");
    107         }
    108 
    109         @Override
    110         protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    111                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    112             throw new InvalidAlgorithmParameterException("expected rejection");
    113         }
    114 
    115         @Override
    116         protected void engineInit(int opmode, Key key, AlgorithmParameters params,
    117                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    118             throw new AssertionError("Must have AlgorithmParameterSpec");
    119         }
    120     }
    121 
    122     public static class MustInitWithAlgorithmParameters_RejectsAll extends MockCipherSpi {
    123         @Override
    124         protected void engineInit(int opmode, Key key, SecureRandom random)
    125                 throws InvalidKeyException {
    126             throw new AssertionError("Must have AlgorithmParameterSpec");
    127         }
    128 
    129         @Override
    130         protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    131                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    132             throw new AssertionError("Must have AlgorithmParameterSpec");
    133         }
    134 
    135         @Override
    136         protected void engineInit(int opmode, Key key, AlgorithmParameters params,
    137                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    138             throw new InvalidAlgorithmParameterException("expected rejection");
    139         }
    140     }
    141 
    142     public static class MustInitWithAlgorithmParameters_ThrowsNull extends MockCipherSpi {
    143         @Override
    144         protected void engineInit(int opmode, Key key, SecureRandom random)
    145                 throws InvalidKeyException {
    146             throw new NullPointerException("expected rejection");
    147         }
    148 
    149         @Override
    150         protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    151                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    152             throw new NullPointerException("expected rejection");
    153         }
    154 
    155         @Override
    156         protected void engineInit(int opmode, Key key, AlgorithmParameters params,
    157                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    158             throw new NullPointerException("expected rejection");
    159         }
    160     }
    161 
    162     public static class MustInitForEncryptModeOrRejects extends MockCipherSpi {
    163         @Override
    164         protected void engineInit(int opmode, Key key, SecureRandom random)
    165                 throws InvalidKeyException {
    166             if (opmode != Cipher.ENCRYPT_MODE) {
    167                 throw new InvalidKeyException("expected rejection");
    168             }
    169         }
    170 
    171         @Override
    172         protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    173                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    174             if (opmode != Cipher.ENCRYPT_MODE) {
    175                 throw new InvalidKeyException("expected rejection");
    176             }
    177         }
    178 
    179         @Override
    180         protected void engineInit(int opmode, Key key, AlgorithmParameters params,
    181                 SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    182             if (opmode != Cipher.ENCRYPT_MODE) {
    183                 throw new InvalidKeyException("expected rejection");
    184             }
    185         }
    186     }
    187 
    188     public void checkKeyType(Key key) throws InvalidKeyException {
    189     }
    190 
    191     public void checkAlgorithmParameterSpec(AlgorithmParameterSpec aps)
    192             throws InvalidAlgorithmParameterException {
    193     }
    194 
    195     public void checkAlgorithmParameters(AlgorithmParameters ap)
    196             throws InvalidAlgorithmParameterException {
    197     }
    198 
    199 
    200     @Override
    201     protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
    202         if (!"FOO".equals(mode)) {
    203             throw new UnsupportedOperationException("not implemented");
    204         }
    205     }
    206 
    207     @Override
    208     protected void engineSetPadding(String padding) throws NoSuchPaddingException {
    209         if (!"FOO".equals(padding)) {
    210             throw new UnsupportedOperationException("not implemented");
    211         }
    212     }
    213 
    214     @Override
    215     protected int engineGetBlockSize() {
    216         throw new UnsupportedOperationException("not implemented");
    217     }
    218 
    219     @Override
    220     protected int engineGetOutputSize(int inputLen) {
    221         throw new UnsupportedOperationException("not implemented");
    222     }
    223 
    224     @Override
    225     protected byte[] engineGetIV() {
    226         throw new UnsupportedOperationException("not implemented");
    227     }
    228 
    229     @Override
    230     protected AlgorithmParameters engineGetParameters() {
    231         throw new UnsupportedOperationException("not implemented");
    232     }
    233 
    234     @Override
    235     protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException {
    236         checkKeyType(key);
    237     }
    238 
    239     @Override
    240     protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
    241             SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    242         checkKeyType(key);
    243         checkAlgorithmParameterSpec(params);
    244     }
    245 
    246     @Override
    247     protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
    248             throws InvalidKeyException, InvalidAlgorithmParameterException {
    249         checkKeyType(key);
    250         checkAlgorithmParameters(params);
    251     }
    252 
    253     @Override
    254     protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
    255         throw new UnsupportedOperationException("not implemented");
    256     }
    257 
    258     @Override
    259     protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output,
    260             int outputOffset) throws ShortBufferException {
    261         throw new UnsupportedOperationException("not implemented");
    262     }
    263 
    264     @Override
    265     protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
    266             throws IllegalBlockSizeException, BadPaddingException {
    267         throw new UnsupportedOperationException("not implemented");
    268     }
    269 
    270     @Override
    271     protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output,
    272             int outputOffset) throws ShortBufferException, IllegalBlockSizeException,
    273             BadPaddingException {
    274         throw new UnsupportedOperationException("not implemented");
    275     }
    276 
    277     public static class MockAlgorithmParameterSpec implements AlgorithmParameterSpec {
    278     }
    279 
    280     public static class MockAlgorithmParameterSpec2 implements AlgorithmParameterSpec {
    281     }
    282 }
    283