1 /* 2 * Copyright (C) 2022 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 com.android.server.nearby.util.encryption; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import org.junit.Test; 22 /** 23 * Unit test for {@link CryptorMicImp} 24 */ 25 public final class CryptorMicImpTest { 26 private static final String TAG = "CryptorImpV1Test"; 27 private static final byte[] SALT = new byte[]{102, 22}; 28 private static final byte[] DATA = 29 new byte[]{107, -102, 101, 107, 20, 62, 2, 73, 113, 59, 8, -14, -58, 122}; 30 private static final byte[] AUTHENTICITY_KEY = 31 new byte[]{-89, 88, -50, -42, -99, 57, 84, -24, 121, 1, -104, -8, -26, -73, -36, 100}; 32 getEncryptedData()33 private static byte[] getEncryptedData() { 34 return new byte[]{112, 23, -111, 87, 122, -27, 45, -25, -35, 84, -89, 115, 61, 113}; 35 } 36 37 @Test test_encryption()38 public void test_encryption() throws Exception { 39 Cryptor v1Cryptor = CryptorMicImp.getInstance(); 40 byte[] encryptedData = 41 v1Cryptor.encrypt(DATA, CryptorMicImp.generateAdvNonce(SALT), AUTHENTICITY_KEY); 42 assertThat(encryptedData).isEqualTo(getEncryptedData()); 43 } 44 45 @Test test_decryption()46 public void test_decryption() throws Exception { 47 Cryptor v1Cryptor = CryptorMicImp.getInstance(); 48 byte[] decryptedData = 49 v1Cryptor.decrypt(getEncryptedData(), CryptorMicImp.generateAdvNonce(SALT), 50 AUTHENTICITY_KEY); 51 assertThat(decryptedData).isEqualTo(DATA); 52 } 53 54 @Test test_verify()55 public void test_verify() { 56 CryptorMicImp v1Cryptor = CryptorMicImp.getInstance(); 57 byte[] expectedTag = new byte[]{ 58 -80, -51, -101, -7, -65, 110, 37, 68, 122, -128, 57, -90, -115, -59, -61, 46}; 59 assertThat(v1Cryptor.verify(DATA, AUTHENTICITY_KEY, expectedTag)).isTrue(); 60 assertThat(v1Cryptor.verify(DATA, AUTHENTICITY_KEY, DATA)).isFalse(); 61 } 62 63 @Test test_generateHmacTag_sameResult()64 public void test_generateHmacTag_sameResult() { 65 CryptorMicImp v1Cryptor = CryptorMicImp.getInstance(); 66 byte[] res1 = v1Cryptor.generateHmacTag(DATA, AUTHENTICITY_KEY); 67 assertThat(res1) 68 .isEqualTo(v1Cryptor.generateHmacTag(DATA, AUTHENTICITY_KEY)); 69 } 70 71 @Test test_generateHmacTag_nullData()72 public void test_generateHmacTag_nullData() { 73 CryptorMicImp v1Cryptor = CryptorMicImp.getInstance(); 74 assertThat(v1Cryptor.generateHmacTag(/* data= */ null, AUTHENTICITY_KEY)).isNull(); 75 } 76 77 @Test test_generateHmacTag_nullKey()78 public void test_generateHmacTag_nullKey() { 79 CryptorMicImp v1Cryptor = CryptorMicImp.getInstance(); 80 assertThat(v1Cryptor.generateHmacTag(DATA, /* authenticityKey= */ null)).isNull(); 81 } 82 } 83