1 /* 2 * Copyright 2014 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 org.apache.harmony.crypto.tests.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 import libcore.javax.crypto.MockKey; 29 import libcore.javax.crypto.MockKey2; 30 31 public class MockKeyAgreementSpi extends KeyAgreementSpi { 32 public static class SpecificKeyTypes extends MockKeyAgreementSpi { 33 @Override checkKeyType(Key key)34 public void checkKeyType(Key key) throws InvalidKeyException { 35 if (!(key instanceof MockKey)) { 36 throw new InvalidKeyException("Must be MockKey!"); 37 } 38 } 39 } 40 41 public static class SpecificKeyTypes2 extends MockKeyAgreementSpi { 42 @Override checkKeyType(Key key)43 public void checkKeyType(Key key) throws InvalidKeyException { 44 if (!(key instanceof MockKey2)) { 45 throw new InvalidKeyException("Must be MockKey2!"); 46 } 47 } 48 } 49 50 public static class AllKeyTypes extends MockKeyAgreementSpi { 51 } 52 checkKeyType(Key key)53 public void checkKeyType(Key key) throws InvalidKeyException { 54 } 55 56 @Override engineDoPhase(Key key, boolean lastPhase)57 protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, 58 IllegalStateException { 59 throw new UnsupportedOperationException(); 60 } 61 62 @Override engineGenerateSecret()63 protected byte[] engineGenerateSecret() throws IllegalStateException { 64 throw new UnsupportedOperationException(); 65 } 66 67 @Override engineGenerateSecret(byte[] sharedSecret, int offset)68 protected int engineGenerateSecret(byte[] sharedSecret, int offset) 69 throws IllegalStateException, ShortBufferException { 70 throw new UnsupportedOperationException(); 71 } 72 73 @Override engineGenerateSecret(String algorithm)74 protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException, 75 NoSuchAlgorithmException, InvalidKeyException { 76 throw new UnsupportedOperationException(); 77 } 78 79 @Override engineInit(Key key, SecureRandom random)80 protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException { 81 checkKeyType(key); 82 } 83 84 @Override engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random)85 protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) 86 throws InvalidKeyException, InvalidAlgorithmParameterException { 87 checkKeyType(key); 88 } 89 } 90