• 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.streamingaead;
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.aead.XChaCha20Poly1305Key;
24 import com.google.crypto.tink.internal.KeyTester;
25 import com.google.crypto.tink.util.SecretBytes;
26 import java.security.GeneralSecurityException;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 @RunWith(JUnit4.class)
32 public final class AesGcmHkdfStreamingKeyTest {
33 
34   @Test
basicBuild_compareParameters_works()35   public void basicBuild_compareParameters_works() throws Exception {
36     AesGcmHkdfStreamingParameters parameters =
37         AesGcmHkdfStreamingParameters.builder()
38             .setKeySizeBytes(19)
39             .setDerivedAesGcmKeySizeBytes(16)
40             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256)
41             .setCiphertextSegmentSizeBytes(1024 * 1024)
42             .build();
43     SecretBytes bytes = SecretBytes.randomBytes(19);
44     AesGcmHkdfStreamingKey key = AesGcmHkdfStreamingKey.create(parameters, bytes);
45 
46     assertThat(key.getParameters()).isEqualTo(parameters);
47     assertThat(key.getInitialKeyMaterial()).isEqualTo(bytes);
48   }
49 
50   @Test
build_wrongKeySize_throws()51   public void build_wrongKeySize_throws() throws Exception {
52     AesGcmHkdfStreamingParameters parameters =
53         AesGcmHkdfStreamingParameters.builder()
54             .setKeySizeBytes(19)
55             .setDerivedAesGcmKeySizeBytes(16)
56             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256)
57             .setCiphertextSegmentSizeBytes(1024 * 1024)
58             .build();
59     SecretBytes bytes = SecretBytes.randomBytes(18);
60     assertThrows(
61         GeneralSecurityException.class, () -> AesGcmHkdfStreamingKey.create(parameters, bytes));
62   }
63 
64   @Test
testEqualities()65   public void testEqualities() throws Exception {
66     SecretBytes keyBytes33 = SecretBytes.randomBytes(33);
67     SecretBytes keyBytes33Copy =
68         SecretBytes.copyFrom(
69             keyBytes33.toByteArray(InsecureSecretKeyAccess.get()), InsecureSecretKeyAccess.get());
70     SecretBytes keyBytes33Diff = SecretBytes.randomBytes(33);
71 
72     AesGcmHkdfStreamingParameters parameters33 =
73         AesGcmHkdfStreamingParameters.builder()
74             .setKeySizeBytes(33)
75             .setDerivedAesGcmKeySizeBytes(32)
76             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256)
77             .setCiphertextSegmentSizeBytes(1024 * 1024)
78             .build();
79     AesGcmHkdfStreamingParameters parameters33Copy =
80         AesGcmHkdfStreamingParameters.builder()
81             .setKeySizeBytes(33)
82             .setDerivedAesGcmKeySizeBytes(32)
83             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256)
84             .setCiphertextSegmentSizeBytes(1024 * 1024)
85             .build();
86     AesGcmHkdfStreamingParameters parametersDifferentHashType =
87         AesGcmHkdfStreamingParameters.builder()
88             .setKeySizeBytes(33)
89             .setDerivedAesGcmKeySizeBytes(32)
90             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA1)
91             .setCiphertextSegmentSizeBytes(1024 * 1024)
92             .build();
93 
94     new KeyTester()
95         .addEqualityGroup(
96             "33 byte key",
97             AesGcmHkdfStreamingKey.create(parameters33, keyBytes33),
98             AesGcmHkdfStreamingKey.create(parameters33, keyBytes33Copy),
99             AesGcmHkdfStreamingKey.create(parameters33Copy, keyBytes33))
100         .addEqualityGroup(
101             "different key",
102             AesGcmHkdfStreamingKey.create(parameters33, keyBytes33Diff),
103             AesGcmHkdfStreamingKey.create(parameters33Copy, keyBytes33Diff))
104         .addEqualityGroup(
105             "different parameters",
106             AesGcmHkdfStreamingKey.create(parametersDifferentHashType, keyBytes33))
107         .doTests();
108   }
109 
110   @Test
testDifferentKeyTypesEquality_fails()111   public void testDifferentKeyTypesEquality_fails() throws Exception {
112     AesGcmHkdfStreamingParameters parameters =
113         AesGcmHkdfStreamingParameters.builder()
114             .setKeySizeBytes(32)
115             .setDerivedAesGcmKeySizeBytes(32)
116             .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256)
117             .setCiphertextSegmentSizeBytes(1024 * 1024)
118             .build();
119     AesGcmHkdfStreamingKey key =
120         AesGcmHkdfStreamingKey.create(parameters, SecretBytes.randomBytes(32));
121 
122     XChaCha20Poly1305Key xChaCha20Poly1305Key =
123         XChaCha20Poly1305Key.create(SecretBytes.randomBytes(32));
124 
125     assertThat(key.equalsKey(xChaCha20Poly1305Key)).isFalse();
126   }
127 }
128