• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 Google Inc.
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 com.google.common.truth.Truth.assertThat;
20 import static java.nio.charset.StandardCharsets.UTF_8;
21 
22 import com.google.crypto.tink.Aead;
23 import com.google.crypto.tink.Key;
24 import com.google.crypto.tink.KeyTemplate;
25 import com.google.crypto.tink.KeyTemplates;
26 import com.google.crypto.tink.KeysetHandle;
27 import com.google.crypto.tink.Parameters;
28 import com.google.crypto.tink.RegistryConfiguration;
29 import com.google.crypto.tink.aead.ChaCha20Poly1305Parameters.Variant;
30 import com.google.crypto.tink.internal.KeyManagerRegistry;
31 import com.google.crypto.tink.subtle.ChaCha20Poly1305;
32 import com.google.crypto.tink.util.SecretBytes;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.experimental.theories.DataPoints;
36 import org.junit.experimental.theories.FromDataPoints;
37 import org.junit.experimental.theories.Theories;
38 import org.junit.experimental.theories.Theory;
39 import org.junit.runner.RunWith;
40 
41 /** Test for ChaCha20Poly1305KeyManager. */
42 @RunWith(Theories.class)
43 public class ChaCha20Poly1305KeyManagerTest {
44   @Before
register()45   public void register() throws Exception {
46     AeadConfig.register();
47   }
48 
49   @Test
testKeyManagerRegistered()50   public void testKeyManagerRegistered() throws Exception {
51     assertThat(
52             KeyManagerRegistry.globalInstance()
53                 .getKeyManager(
54                     "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key", Aead.class))
55         .isNotNull();
56   }
57 
58   @Test
testChaCha20Poly1305Template()59   public void testChaCha20Poly1305Template() throws Exception {
60     KeyTemplate template = ChaCha20Poly1305KeyManager.chaCha20Poly1305Template();
61     assertThat(template.toParameters())
62         .isEqualTo(ChaCha20Poly1305Parameters.create(ChaCha20Poly1305Parameters.Variant.TINK));
63   }
64 
65   @Test
testRawChaCha20Poly1305Template()66   public void testRawChaCha20Poly1305Template() throws Exception {
67     KeyTemplate template = ChaCha20Poly1305KeyManager.rawChaCha20Poly1305Template();
68     assertThat(template.toParameters()).isEqualTo(ChaCha20Poly1305Parameters.create());
69   }
70 
71   @Test
testKeyTemplatesWork()72   public void testKeyTemplatesWork() throws Exception {
73     Parameters p = ChaCha20Poly1305KeyManager.chaCha20Poly1305Template().toParameters();
74     assertThat(KeysetHandle.generateNew(p).getAt(0).getKey().getParameters()).isEqualTo(p);
75 
76     p = ChaCha20Poly1305KeyManager.rawChaCha20Poly1305Template().toParameters();
77     assertThat(KeysetHandle.generateNew(p).getAt(0).getKey().getParameters()).isEqualTo(p);
78   }
79 
80   @DataPoints("templateNames")
81   public static final String[] KEY_TEMPLATES =
82       new String[] {"CHACHA20_POLY1305", "CHACHA20_POLY1305_RAW"};
83 
84   @Theory
testTemplates(@romDataPoints"templateNames") String templateName)85   public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception {
86     KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName));
87     assertThat(h.size()).isEqualTo(1);
88     assertThat(h.getAt(0).getKey().getParameters())
89         .isEqualTo(KeyTemplates.get(templateName).toParameters());
90   }
91 
92   @Test
callingCreateTwiceGivesDifferentKeys()93   public void callingCreateTwiceGivesDifferentKeys() throws Exception {
94     Parameters p = ChaCha20Poly1305KeyManager.chaCha20Poly1305Template().toParameters();
95     Key key = KeysetHandle.generateNew(p).getAt(0).getKey();
96     for (int i = 0; i < 1000; ++i) {
97       assertThat(KeysetHandle.generateNew(p).getAt(0).getKey().equalsKey(key)).isFalse();
98     }
99   }
100 
101   @Test
getPrimitiveKeysetHandle()102   public void getPrimitiveKeysetHandle() throws Exception {
103     com.google.crypto.tink.aead.ChaCha20Poly1305Key key =
104         com.google.crypto.tink.aead.ChaCha20Poly1305Key.create(
105             Variant.TINK, SecretBytes.randomBytes(32), 42);
106     KeysetHandle keysetHandle =
107         KeysetHandle.newBuilder().addEntry(KeysetHandle.importKey(key).makePrimary()).build();
108     byte[] plaintext = "plaintext".getBytes(UTF_8);
109     byte[] aad = "aad".getBytes(UTF_8);
110 
111     Aead aead = keysetHandle.getPrimitive(RegistryConfiguration.get(), Aead.class);
112     Aead directAead = ChaCha20Poly1305.create(key);
113 
114     assertThat(aead.decrypt(directAead.encrypt(plaintext, aad), aad)).isEqualTo(plaintext);
115     assertThat(directAead.decrypt(aead.encrypt(plaintext, aad), aad)).isEqualTo(plaintext);
116   }
117 }
118