aboutsummaryrefslogtreecommitdiff
path: root/tests/iketests/src/java/com/android/internal/net/ipsec/ike/crypto/IkeCombinedModeCipherTest.java
blob: a3b2253e600fc510c8323e34ce127546ae6f1de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.net.ipsec.ike.crypto;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.net.IpSecAlgorithm;
import android.net.ipsec.ike.SaProposal;

import com.android.internal.net.TestUtils;
import com.android.internal.net.ipsec.ike.message.IkeMessage;
import com.android.internal.net.ipsec.ike.message.IkeSaPayload.EncryptionTransform;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Arrays;
import java.util.Random;

import javax.crypto.AEADBadTagException;

@RunWith(JUnit4.class)
public final class IkeCombinedModeCipherTest {
    private static final String IV = "fbd69d9de2dafc5e";
    private static final String ENCRYPTED_PADDED_DATA_WITH_CHECKSUM =
            "f4109834e9f3559758c05edf119917521b885f67f0d14ced43";
    private static final String UNENCRYPTED_PADDED_DATA = "000000080000400f00";
    private static final String ADDITIONAL_AUTH_DATA =
            "77c708b4523e39a471dc683c1d4f21362e202508000000060000004129000025";
    private static final String KEY =
            "7C04513660DEC572D896105254EF92608054F8E6EE19E79CE52AB8697B2B5F2C2AA90C29";

    private static final int AES_GCM_IV_LEN = 8;
    private static final int AES_GCM_16_CHECKSUM_LEN = 128;

    private IkeCombinedModeCipher mAesGcm16Cipher;

    private byte[] mAesGcmKey;
    private byte[] mIv;
    private byte[] mEncryptedPaddedDataWithChecksum;
    private byte[] mUnencryptedPaddedData;
    private byte[] mAdditionalAuthData;

    @Before
    public void setUp() {
        mAesGcm16Cipher =
                (IkeCombinedModeCipher)
                        IkeCipher.create(
                                new EncryptionTransform(
                                        SaProposal.ENCRYPTION_ALGORITHM_AES_GCM_16,
                                        SaProposal.KEY_LEN_AES_256),
                                IkeMessage.getSecurityProvider());

        mAesGcmKey = TestUtils.hexStringToByteArray(KEY);
        mIv = TestUtils.hexStringToByteArray(IV);
        mEncryptedPaddedDataWithChecksum =
                TestUtils.hexStringToByteArray(ENCRYPTED_PADDED_DATA_WITH_CHECKSUM);
        mUnencryptedPaddedData = TestUtils.hexStringToByteArray(UNENCRYPTED_PADDED_DATA);
        mAdditionalAuthData = TestUtils.hexStringToByteArray(ADDITIONAL_AUTH_DATA);
    }

    @Test
    public void testBuild() throws Exception {
        assertTrue(mAesGcm16Cipher.isAead());
        assertEquals(AES_GCM_IV_LEN, mAesGcm16Cipher.generateIv().length);
    }

    @Test
    public void testGenerateRandomIv() throws Exception {
        assertFalse(Arrays.equals(mAesGcm16Cipher.generateIv(), mAesGcm16Cipher.generateIv()));
    }

    @Test
    public void testEncrypt() throws Exception {
        byte[] calculatedData =
                mAesGcm16Cipher.encrypt(
                        mUnencryptedPaddedData, mAdditionalAuthData, mAesGcmKey, mIv);

        assertArrayEquals(mEncryptedPaddedDataWithChecksum, calculatedData);
    }

    @Test
    public void testDecrypt() throws Exception {
        byte[] calculatedData =
                mAesGcm16Cipher.decrypt(
                        mEncryptedPaddedDataWithChecksum, mAdditionalAuthData, mAesGcmKey, mIv);

        assertArrayEquals(mUnencryptedPaddedData, calculatedData);
    }

    @Test
    public void testEncryptWithWrongKeyLen() throws Exception {
        byte[] encryptionKey = TestUtils.hexStringToByteArray(KEY + "00");

        try {
            mAesGcm16Cipher.encrypt(
                    mUnencryptedPaddedData, mAdditionalAuthData, encryptionKey, mIv);
            fail("Expected to fail because encryption key has wrong length.");
        } catch (IllegalArgumentException expected) {

        }
    }

    @Test
    public void testDecrypWithWrongKey() throws Exception {
        byte[] encryptionKey = new byte[mAesGcmKey.length];
        new Random().nextBytes(encryptionKey);

        try {
            mAesGcm16Cipher.decrypt(
                    mEncryptedPaddedDataWithChecksum, mAdditionalAuthData, encryptionKey, mIv);
            fail("Expected to fail because decryption key is wrong");
        } catch (AEADBadTagException expected) {

        }
    }

    @Test
    public void testBuildIpSecAlgorithm() throws Exception {
        IpSecAlgorithm ipsecAlgorithm = mAesGcm16Cipher.buildIpSecAlgorithmWithKey(mAesGcmKey);

        IpSecAlgorithm expectedIpSecAlgorithm =
                new IpSecAlgorithm(
                        IpSecAlgorithm.AUTH_CRYPT_AES_GCM, mAesGcmKey, AES_GCM_16_CHECKSUM_LEN);

        assertTrue(IpSecAlgorithm.equals(expectedIpSecAlgorithm, ipsecAlgorithm));
    }

    @Test
    public void buildIpSecAlgorithmWithInvalidKey() throws Exception {
        byte[] encryptionKey = TestUtils.hexStringToByteArray(KEY + "00");

        try {
            mAesGcm16Cipher.buildIpSecAlgorithmWithKey(encryptionKey);
            fail("Expected to fail because encryption key has wrong length.");
        } catch (IllegalArgumentException expected) {

        }
    }
}