1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.keyderivation; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.crypto.tink.internal.TinkBugException.exceptionIsBug; 21 import static org.junit.Assert.assertThrows; 22 23 import com.google.crypto.tink.Parameters; 24 import com.google.crypto.tink.aead.XChaCha20Poly1305Parameters; 25 import com.google.crypto.tink.mac.HmacParameters; 26 import com.google.crypto.tink.prf.AesCmacPrfParameters; 27 import com.google.crypto.tink.prf.HmacPrfParameters; 28 import com.google.crypto.tink.prf.PrfParameters; 29 import java.security.GeneralSecurityException; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 @RunWith(JUnit4.class) 35 public final class PrfBasedKeyDerivationParametersTest { 36 private static final PrfParameters PRF_PARAMETERS_1 = 37 exceptionIsBug(() -> AesCmacPrfParameters.create(16)); 38 private static final PrfParameters PRF_PARAMETERS_1_COPY = 39 exceptionIsBug(() -> AesCmacPrfParameters.create(16)); 40 private static final PrfParameters PRF_PARAMETERS_2 = 41 exceptionIsBug( 42 () -> 43 HmacPrfParameters.builder() 44 .setKeySizeBytes(16) 45 .setHashType(HmacPrfParameters.HashType.SHA256) 46 .build()); 47 private static final Parameters DERIVED_PARAMETERS_1 = 48 exceptionIsBug(() -> XChaCha20Poly1305Parameters.create()); 49 private static final Parameters DERIVED_PARAMETERS_1_COPY = 50 exceptionIsBug(() -> XChaCha20Poly1305Parameters.create()); 51 private static final Parameters DERIVED_PARAMETERS_2 = 52 exceptionIsBug( 53 () -> 54 HmacParameters.builder() 55 .setKeySizeBytes(16) 56 .setTagSizeBytes(16) 57 .setVariant(HmacParameters.Variant.NO_PREFIX) 58 .setHashType(HmacParameters.HashType.SHA1) 59 .build()); 60 61 @Test testCreateAndGet_works()62 public void testCreateAndGet_works() throws Exception { 63 PrfBasedKeyDerivationParameters params = 64 PrfBasedKeyDerivationParameters.builder() 65 .setPrfParameters(PRF_PARAMETERS_1) 66 .setDerivedKeyParameters(DERIVED_PARAMETERS_1) 67 .build(); 68 assertThat(params.getPrfParameters()).isEqualTo(PRF_PARAMETERS_1); 69 assertThat(params.getDerivedKeyParameters()).isEqualTo(DERIVED_PARAMETERS_1); 70 } 71 72 @Test test_missingPrfParameters_throws()73 public void test_missingPrfParameters_throws() throws Exception { 74 PrfBasedKeyDerivationParameters.Builder builder = 75 PrfBasedKeyDerivationParameters.builder().setDerivedKeyParameters(DERIVED_PARAMETERS_1); 76 assertThrows(GeneralSecurityException.class, builder::build); 77 } 78 79 @Test test_missingDerivedKeyParameters_throws()80 public void test_missingDerivedKeyParameters_throws() throws Exception { 81 PrfBasedKeyDerivationParameters.Builder builder = 82 PrfBasedKeyDerivationParameters.builder().setPrfParameters(PRF_PARAMETERS_1); 83 assertThrows(GeneralSecurityException.class, builder::build); 84 } 85 86 @Test test_equals_hashCode_works()87 public void test_equals_hashCode_works() throws Exception { 88 PrfBasedKeyDerivationParameters params11 = 89 PrfBasedKeyDerivationParameters.builder() 90 .setPrfParameters(PRF_PARAMETERS_1) 91 .setDerivedKeyParameters(DERIVED_PARAMETERS_1) 92 .build(); 93 PrfBasedKeyDerivationParameters params11Copy = 94 PrfBasedKeyDerivationParameters.builder() 95 .setPrfParameters(PRF_PARAMETERS_1_COPY) 96 .setDerivedKeyParameters(DERIVED_PARAMETERS_1_COPY) 97 .build(); 98 PrfBasedKeyDerivationParameters params12 = 99 PrfBasedKeyDerivationParameters.builder() 100 .setPrfParameters(PRF_PARAMETERS_1) 101 .setDerivedKeyParameters(DERIVED_PARAMETERS_2) 102 .build(); 103 PrfBasedKeyDerivationParameters params21 = 104 PrfBasedKeyDerivationParameters.builder() 105 .setPrfParameters(PRF_PARAMETERS_2) 106 .setDerivedKeyParameters(DERIVED_PARAMETERS_1) 107 .build(); 108 PrfBasedKeyDerivationParameters params22 = 109 PrfBasedKeyDerivationParameters.builder() 110 .setPrfParameters(PRF_PARAMETERS_2) 111 .setDerivedKeyParameters(DERIVED_PARAMETERS_2) 112 .build(); 113 114 assertThat(params11).isEqualTo(params11Copy); 115 116 assertThat(params11).isNotEqualTo(params12); 117 assertThat(params11).isNotEqualTo(params21); 118 assertThat(params11).isNotEqualTo(params22); 119 120 assertThat(params12).isNotEqualTo(params11); 121 assertThat(params12).isNotEqualTo(params21); 122 assertThat(params12).isNotEqualTo(params22); 123 124 assertThat(params21).isNotEqualTo(params11); 125 assertThat(params21).isNotEqualTo(params12); 126 assertThat(params21).isNotEqualTo(params22); 127 128 assertThat(params22).isNotEqualTo(params11); 129 assertThat(params22).isNotEqualTo(params12); 130 assertThat(params22).isNotEqualTo(params21); 131 132 assertThat(params11.hashCode()).isEqualTo(params11Copy.hashCode()); 133 assertThat(params11.hashCode()).isNotEqualTo(params12.hashCode()); 134 assertThat(params11.hashCode()).isNotEqualTo(params21.hashCode()); 135 assertThat(params11.hashCode()).isNotEqualTo(params22.hashCode()); 136 } 137 } 138