• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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.aead;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 import static org.junit.Assert.assertArrayEquals;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertNotNull;
23 
24 import com.google.crypto.tink.Aead;
25 import com.google.crypto.tink.BinaryKeysetWriter;
26 import com.google.crypto.tink.InsecureSecretKeyAccess;
27 import com.google.crypto.tink.KeysetHandle;
28 import com.google.crypto.tink.KeysetWriter;
29 import com.google.crypto.tink.LegacyKeysetSerialization;
30 import com.google.crypto.tink.RegistryConfiguration;
31 import java.io.ByteArrayOutputStream;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 @RunWith(JUnit4.class)
38 public final class XAesGcmKeyManagerTest {
39 
40   @BeforeClass
setUp()41   public static void setUp() throws Exception {
42     XAesGcmKeyManager.register(/* newKeyAllowed= */ true);
43   }
44 
45   @Test
xAesGcmKeyTypeIsRegistered()46   public void xAesGcmKeyTypeIsRegistered() throws Exception {
47     assertNotNull(
48         KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX));
49   }
50 
51   @Test
xAesGcmKeyCreator_generatesNewKey()52   public void xAesGcmKeyCreator_generatesNewKey() throws Exception {
53     XAesGcmKey key1 =
54         (XAesGcmKey)
55             KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX)
56                 .getPrimary()
57                 .getKey();
58     XAesGcmKey key2 =
59         (XAesGcmKey)
60             KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX)
61                 .getPrimary()
62                 .getKey();
63 
64     assertNotEquals(key1, key2);
65   }
66 
67   @Test
xAesGcmKeyNamesTemplates_areRegistered()68   public void xAesGcmKeyNamesTemplates_areRegistered() throws Exception {
69     assertNotNull(
70         KeysetHandle.newBuilder()
71             .addEntry(
72                 KeysetHandle.generateEntryFromParametersName("XAES_256_GCM_160_BIT_NONCE_NO_PREFIX")
73                     .withRandomId()
74                     .makePrimary())
75             .build());
76   }
77 
78   @Test
xAesGcmKeySerialization_isRegistered()79   public void xAesGcmKeySerialization_isRegistered() throws Exception {
80     KeysetHandle handle =
81         KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX);
82     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
83     KeysetWriter keysetWriter = BinaryKeysetWriter.withOutputStream(outputStream);
84     LegacyKeysetSerialization.serializeKeyset(handle, keysetWriter, InsecureSecretKeyAccess.get());
85   }
86 
87   @Test
xAesGcmPrimitiveCreation()88   public void xAesGcmPrimitiveCreation() throws Exception {
89     AeadConfig.register();
90     KeysetHandle handle =
91         KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX);
92     Aead xAesGcm = handle.getPrimitive(RegistryConfiguration.get(), Aead.class);
93     String plaintext = "plaintext";
94     String associatedData = "associatedData";
95     byte[] ciphertext = xAesGcm.encrypt(plaintext.getBytes(UTF_8), associatedData.getBytes(UTF_8));
96     assertArrayEquals(
97         xAesGcm.decrypt(ciphertext, associatedData.getBytes(UTF_8)), plaintext.getBytes(UTF_8));
98   }
99 }
100