• 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 com.google.crypto.tink.Aead;
20 import com.google.crypto.tink.Configuration;
21 import com.google.crypto.tink.aead.internal.ChaCha20Poly1305Jce;
22 import com.google.crypto.tink.aead.internal.XAesGcm;
23 import com.google.crypto.tink.aead.internal.XChaCha20Poly1305Jce;
24 import com.google.crypto.tink.aead.subtle.AesGcmSiv;
25 import com.google.crypto.tink.config.internal.TinkFipsUtil;
26 import com.google.crypto.tink.internal.InternalConfiguration;
27 import com.google.crypto.tink.internal.PrimitiveConstructor;
28 import com.google.crypto.tink.internal.PrimitiveRegistry;
29 import com.google.crypto.tink.subtle.AesEaxJce;
30 import com.google.crypto.tink.subtle.AesGcmJce;
31 import com.google.crypto.tink.subtle.ChaCha20Poly1305;
32 import com.google.crypto.tink.subtle.EncryptThenAuthenticate;
33 import com.google.crypto.tink.subtle.XChaCha20Poly1305;
34 import java.security.GeneralSecurityException;
35 
36 /**
37  * AeadConfigurationV0 contains the following algorithms for Aead:
38  *
39  * <ul>
40  *   <li>AesCtrHmac
41  *   <li>AesGcm
42  *   <li>AesGcmSiv
43  *   <li>AesEax
44  *   <li>ChaCha20Poly1305
45  *   <li>XChaCha20Poly1305
46  * </ul>
47  */
48 /* Placeholder for internally public; DO NOT CHANGE. */ class AeadConfigurationV0 {
AeadConfigurationV0()49   private AeadConfigurationV0() {}
50 
51   private static final InternalConfiguration INTERNAL_CONFIGURATION = create();
52 
create()53   private static InternalConfiguration create() {
54     try {
55       PrimitiveRegistry.Builder builder = PrimitiveRegistry.builder();
56 
57       // Register {@code Aead} wrapper and concrete primitives.
58       AeadWrapper.registerToInternalPrimitiveRegistry(builder);
59       builder.registerPrimitiveConstructor(
60           PrimitiveConstructor.create(
61               EncryptThenAuthenticate::create, AesCtrHmacAeadKey.class, Aead.class));
62       builder.registerPrimitiveConstructor(
63           PrimitiveConstructor.create(AesGcmJce::create, AesGcmKey.class, Aead.class));
64       builder.registerPrimitiveConstructor(
65           PrimitiveConstructor.create(AesGcmSiv::create, AesGcmSivKey.class, Aead.class));
66       builder.registerPrimitiveConstructor(
67           PrimitiveConstructor.create(AesEaxJce::create, AesEaxKey.class, Aead.class));
68       builder.registerPrimitiveConstructor(
69           PrimitiveConstructor.create(
70               AeadConfigurationV0::createChaCha20Poly1305,
71               ChaCha20Poly1305Key.class,
72               Aead.class));
73       builder.registerPrimitiveConstructor(
74           PrimitiveConstructor.create(
75               AeadConfigurationV0::createXChaCha20Poly1305,
76               XChaCha20Poly1305Key.class,
77               Aead.class));
78       builder.registerPrimitiveConstructor(
79           PrimitiveConstructor.create(XAesGcm::create, XAesGcmKey.class, Aead.class));
80 
81       return InternalConfiguration.createFromPrimitiveRegistry(builder.build());
82     } catch (GeneralSecurityException e) {
83       throw new IllegalStateException(e);
84     }
85   }
86 
87   /** Returns an instance of the {@code AeadConfigurationV0}. */
get()88   public static Configuration get() throws GeneralSecurityException {
89     if (TinkFipsUtil.useOnlyFips()) {
90       throw new GeneralSecurityException(
91           "Cannot use non-FIPS-compliant AeadConfigurationV0 in FIPS mode");
92     }
93     return INTERNAL_CONFIGURATION;
94   }
95 
createChaCha20Poly1305(ChaCha20Poly1305Key key)96   private static Aead createChaCha20Poly1305(ChaCha20Poly1305Key key)
97       throws GeneralSecurityException {
98     if (ChaCha20Poly1305Jce.isSupported()) {
99       return ChaCha20Poly1305Jce.create(key);
100     }
101     return ChaCha20Poly1305.create(key);
102   }
103 
createXChaCha20Poly1305(XChaCha20Poly1305Key key)104   private static Aead createXChaCha20Poly1305(XChaCha20Poly1305Key key)
105       throws GeneralSecurityException {
106     if (XChaCha20Poly1305Jce.isSupported()) {
107       return XChaCha20Poly1305Jce.create(key);
108     }
109     return XChaCha20Poly1305.create(key);
110   }
111 }
112