CipherHelper.java revision f979bbd1277c77ca945ad981e7864fb4e9f6ae05
1f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson/*
2f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Copyright (C) 2009 The Android Open Source Project
3f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
4f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * you may not use this file except in compliance with the License.
6f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * You may obtain a copy of the License at
7f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
8f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
9f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
10f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * See the License for the specific language governing permissions and
14f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * limitations under the License.
15f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson */
16f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
17f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpackage tests.security;
18f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
19f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.InvalidKeyException;
20f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.Key;
21f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.NoSuchAlgorithmException;
22f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.BadPaddingException;
23f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.Cipher;
24f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.IllegalBlockSizeException;
25f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.NoSuchPaddingException;
26f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport junit.framework.Assert;
27f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
28f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpublic abstract class CipherHelper<T> extends TestHelper<T> {
29f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
30f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String algorithmName;
31f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String plainData;
32f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final int mode1;
33f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final int mode2;
34f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
35f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public CipherHelper(String algorithmName, String plainData, int mode1, int mode2) {
36f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.algorithmName = algorithmName;
37f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.plainData = plainData;
38f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.mode1 = mode1;
39f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.mode2 = mode2;
40f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
41f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
42f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public void test(Key encryptKey, Key decryptKey) {
43f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        Cipher cipher = null;
44f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
45f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            cipher = Cipher.getInstance(algorithmName);
46f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (NoSuchAlgorithmException e) {
47f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
48f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (NoSuchPaddingException e) {
49f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
50f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
51f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
52f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            cipher.init(mode1, encryptKey);
53f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (InvalidKeyException e) {
54f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
55f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
56f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
57f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        byte[] encrypted = crypt(cipher, plainData.getBytes());
58f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
59f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
60f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            cipher.init(mode2, decryptKey);
61f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (InvalidKeyException e) {
62f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
63f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
64f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
65f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        byte[] decrypted = crypt(cipher, encrypted);
66f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
67f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        String decryptedString = new String(decrypted);
68f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
69f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        Assert.assertEquals("transformed data does not match", plainData,
70f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson                decryptedString);
71f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
72f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
73f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public byte[] crypt(Cipher cipher, byte[] input) {
74f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        try {
75f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            return cipher.doFinal(input);
76f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (IllegalBlockSizeException e) {
77f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
78f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        } catch (BadPaddingException e) {
79f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson            Assert.fail(e.getMessage());
80f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        }
81f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        return null;
82f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
83f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson}
84