1/*
2 * Copyright (C) 2009 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 tests.security;
18
19import java.security.AlgorithmParameters;
20import java.security.InvalidAlgorithmParameterException;
21import java.security.InvalidKeyException;
22import java.security.Key;
23import java.security.NoSuchAlgorithmException;
24import java.util.Arrays;
25import javax.crypto.BadPaddingException;
26import javax.crypto.Cipher;
27import javax.crypto.IllegalBlockSizeException;
28import javax.crypto.KeyGenerator;
29import javax.crypto.NoSuchPaddingException;
30import junit.framework.Assert;
31
32public class AlgorithmParameterSymmetricHelper extends TestHelper<AlgorithmParameters> {
33
34    private static final String plainData = "some data to encrypt and decrypt";
35    private final String algorithmName;
36    private final int keySize;
37    private String blockmode;
38
39    public AlgorithmParameterSymmetricHelper(String algorithmName, int keySize) {
40        this.algorithmName = algorithmName;
41        this.keySize = keySize;
42    }
43
44    public AlgorithmParameterSymmetricHelper(String algorithmName, String blockmode, int keySize) {
45        this(algorithmName, keySize);
46        this.blockmode = blockmode;
47    }
48
49    @Override
50    public void test(AlgorithmParameters parameters) {
51
52        KeyGenerator generator = null;
53        try {
54            generator = KeyGenerator.getInstance(algorithmName);
55        } catch (NoSuchAlgorithmException e) {
56            Assert.fail(e.getMessage());
57        }
58
59        generator.init(keySize);
60
61        Key key = generator.generateKey();
62
63
64        Cipher cipher = null;
65        try {
66            String transformation = algorithmName;
67            if (blockmode != null)
68            {
69                transformation += "/" + blockmode;
70            }
71            cipher = Cipher.getInstance(transformation);
72        } catch (NoSuchAlgorithmException e) {
73            Assert.fail(e.getMessage());
74        } catch (NoSuchPaddingException e) {
75            Assert.fail(e.getMessage());
76        }
77
78        try {
79            cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
80        } catch (InvalidKeyException e) {
81            Assert.fail(e.getMessage());
82        } catch (InvalidAlgorithmParameterException e) {
83            Assert.fail(e.getMessage());
84        }
85
86        byte[] bs = null;
87        try {
88            bs = cipher.doFinal(plainData.getBytes());
89        } catch (IllegalBlockSizeException e) {
90            Assert.fail(e.getMessage());
91        } catch (BadPaddingException e) {
92            Assert.fail(e.getMessage());
93        }
94
95        try {
96            cipher.init(Cipher.DECRYPT_MODE, key, parameters);
97        } catch (InvalidKeyException e) {
98            Assert.fail(e.getMessage());
99        } catch (InvalidAlgorithmParameterException e) {
100            Assert.fail(e.getMessage());
101        }
102
103        byte[] decrypted = null;
104        try {
105            decrypted = cipher.doFinal(bs);
106        } catch (IllegalBlockSizeException e) {
107            Assert.fail(e.getMessage());
108        } catch (BadPaddingException e) {
109            Assert.fail(e.getMessage());
110        }
111
112        Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
113    }
114}
115