• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.prf;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import com.google.crypto.tink.InsecureSecretKeyAccess;
23 import com.google.crypto.tink.internal.KeyTester;
24 import com.google.crypto.tink.util.SecretBytes;
25 import java.security.GeneralSecurityException;
26 import org.junit.Test;
27 import org.junit.experimental.theories.DataPoints;
28 import org.junit.experimental.theories.FromDataPoints;
29 import org.junit.experimental.theories.Theories;
30 import org.junit.experimental.theories.Theory;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(Theories.class)
34 public final class AesCmacPrfKeyTest {
35   @DataPoints("keySizes")
36   public static final int[] KEY_SIZES = new int[] {16, 32};
37 
38   @Theory
createAndGetProperties_succeeds(@romDataPoints"keySizes") int keySize)39   public void createAndGetProperties_succeeds(@FromDataPoints("keySizes") int keySize)
40       throws Exception {
41     AesCmacPrfParameters parameters = AesCmacPrfParameters.create(keySize);
42     assertThat(parameters.hasIdRequirement()).isFalse();
43     SecretBytes keyBytes = SecretBytes.randomBytes(keySize);
44     AesCmacPrfKey key = AesCmacPrfKey.create(parameters, keyBytes);
45 
46     assertThat(key.getParameters()).isEqualTo(parameters);
47     assertThat(key.getKeyBytes()).isEqualTo(keyBytes);
48     assertThat(key.getIdRequirementOrNull()).isNull();
49   }
50 
51   @Test
createWithKeySizeMismatch_fails()52   public void createWithKeySizeMismatch_fails() throws Exception {
53     assertThrows(
54         GeneralSecurityException.class,
55         () -> AesCmacPrfKey.create(AesCmacPrfParameters.create(16), SecretBytes.randomBytes(32)));
56   }
57 
58   @Test
equals()59   public void equals() throws Exception {
60     SecretBytes keyBytes = SecretBytes.randomBytes(16);
61     SecretBytes keyBytesCopy =
62         SecretBytes.copyFrom(
63             keyBytes.toByteArray(InsecureSecretKeyAccess.get()), InsecureSecretKeyAccess.get());
64     AesCmacPrfParameters parameters16 = AesCmacPrfParameters.create(16);
65     AesCmacPrfParameters parameters32 = AesCmacPrfParameters.create(32);
66 
67     new KeyTester()
68         .addEqualityGroup(
69             "16-byte key",
70             AesCmacPrfKey.create(parameters16, keyBytes),
71             // Same key built twice.
72             AesCmacPrfKey.create(parameters16, keyBytes),
73             // Same key built with a copy of the key bytes.
74             AesCmacPrfKey.create(parameters16, keyBytesCopy))
75         .addEqualityGroup(
76             "16-byte random key bytes",
77             AesCmacPrfKey.create(parameters16, SecretBytes.randomBytes(16)))
78         .addEqualityGroup(
79             "32-byte random key bytes",
80             AesCmacPrfKey.create(parameters32, SecretBytes.randomBytes(32)))
81         .addEqualityGroup(
82             "different key class",
83             HkdfPrfKey.builder()
84                 .setParameters(
85                     HkdfPrfParameters.builder()
86                         .setKeySizeBytes(16)
87                         .setHashType(HkdfPrfParameters.HashType.SHA256)
88                         .build())
89                 .setKeyBytes(keyBytes)
90                 .build())
91         .doTests();
92   }
93 }
94