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
17package org.apache.harmony.crypto.tests.javax.crypto;
18
19import java.security.InvalidAlgorithmParameterException;
20import java.security.InvalidKeyException;
21import java.security.Key;
22import java.security.NoSuchAlgorithmException;
23import java.security.SecureRandom;
24import java.security.spec.AlgorithmParameterSpec;
25import javax.crypto.KeyAgreementSpi;
26import javax.crypto.SecretKey;
27import javax.crypto.ShortBufferException;
28import libcore.javax.crypto.MockKey;
29import libcore.javax.crypto.MockKey2;
30
31public class MockKeyAgreementSpi extends KeyAgreementSpi {
32    public static class SpecificKeyTypes extends MockKeyAgreementSpi {
33        @Override
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
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
53    public void checkKeyType(Key key) throws InvalidKeyException {
54    }
55
56    @Override
57    protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException,
58            IllegalStateException {
59        throw new UnsupportedOperationException();
60    }
61
62    @Override
63    protected byte[] engineGenerateSecret() throws IllegalStateException {
64        throw new UnsupportedOperationException();
65    }
66
67    @Override
68    protected int engineGenerateSecret(byte[] sharedSecret, int offset)
69            throws IllegalStateException, ShortBufferException {
70        throw new UnsupportedOperationException();
71    }
72
73    @Override
74    protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException,
75            NoSuchAlgorithmException, InvalidKeyException {
76        throw new UnsupportedOperationException();
77    }
78
79    @Override
80    protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
81        checkKeyType(key);
82    }
83
84    @Override
85    protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random)
86            throws InvalidKeyException, InvalidAlgorithmParameterException {
87        checkKeyType(key);
88    }
89}
90