Home | History | Annotate | Download | only in crypto
      1 /*
      2  * Copyright 2015 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.InvalidAlgorithmParameterException;
     20 import java.security.InvalidKeyException;
     21 import java.security.Key;
     22 import java.security.NoSuchAlgorithmException;
     23 import java.security.SecureRandom;
     24 import java.security.spec.AlgorithmParameterSpec;
     25 import javax.crypto.KeyAgreementSpi;
     26 import javax.crypto.SecretKey;
     27 import javax.crypto.ShortBufferException;
     28 
     29 /**
     30  * Mock KeyAgreementSpi used by {@link KeyAgreementTest}.
     31  */
     32 public class MockKeyAgreementSpi extends KeyAgreementSpi {
     33     public static class SpecificKeyTypes extends MockKeyAgreementSpi {
     34         @Override
     35         public void checkKeyType(Key key) throws InvalidKeyException {
     36             if (!(key instanceof MockKey)) {
     37                 throw new InvalidKeyException("Must be MockKey!");
     38             }
     39         }
     40     }
     41 
     42     public static class SpecificKeyTypes2 extends MockKeyAgreementSpi {
     43         @Override
     44         public void checkKeyType(Key key) throws InvalidKeyException {
     45             System.err.println("Checking key of type " + key.getClass().getName());
     46             if (!(key instanceof MockKey2)) {
     47                 throw new InvalidKeyException("Must be MockKey2!");
     48             }
     49         }
     50     }
     51 
     52     public static class AllKeyTypes extends MockKeyAgreementSpi {
     53     }
     54 
     55     public void checkKeyType(Key key) throws InvalidKeyException {
     56     }
     57 
     58     @Override
     59     protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException,
     60             IllegalStateException {
     61         throw new UnsupportedOperationException("not implemented");
     62     }
     63 
     64     @Override
     65     protected byte[] engineGenerateSecret() throws IllegalStateException {
     66         throw new UnsupportedOperationException("not implemented");
     67     }
     68 
     69     @Override
     70     protected int engineGenerateSecret(byte[] sharedSecret, int offset)
     71             throws IllegalStateException, ShortBufferException {
     72         throw new UnsupportedOperationException("not implemented");
     73     }
     74 
     75     @Override
     76     protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException,
     77             NoSuchAlgorithmException, InvalidKeyException {
     78         throw new UnsupportedOperationException("not implemented");
     79     }
     80 
     81     @Override
     82     protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
     83         checkKeyType(key);
     84     }
     85 
     86     @Override
     87     protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random)
     88             throws InvalidKeyException, InvalidAlgorithmParameterException {
     89         checkKeyType(key);
     90     }
     91 }
     92