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.streamingaead; 18 19 import com.google.crypto.tink.Configuration; 20 import com.google.crypto.tink.StreamingAead; 21 import com.google.crypto.tink.config.internal.TinkFipsUtil; 22 import com.google.crypto.tink.internal.InternalConfiguration; 23 import com.google.crypto.tink.internal.PrimitiveConstructor; 24 import com.google.crypto.tink.internal.PrimitiveRegistry; 25 import com.google.crypto.tink.subtle.AesCtrHmacStreaming; 26 import com.google.crypto.tink.subtle.AesGcmHkdfStreaming; 27 import java.security.GeneralSecurityException; 28 29 /** 30 * StreamingAeadConfigurationV0 contains the following algorithms for StreamingAEAD: 31 * 32 * <ul> 33 * <li>AesGcmHkdfStreaming 34 * <li>AesCtrHmacStreaming 35 * </ul> 36 */ 37 /* Placeholder for internally public; DO NOT CHANGE. */ class StreamingAeadConfigurationV0 { StreamingAeadConfigurationV0()38 private StreamingAeadConfigurationV0() {} 39 40 private static final InternalConfiguration INTERNAL_CONFIGURATION = create(); 41 create()42 private static InternalConfiguration create() { 43 try { 44 PrimitiveRegistry.Builder builder = PrimitiveRegistry.builder(); 45 46 // Register StreamingAead wrapper and concrete primitives. 47 StreamingAeadWrapper.registerToInternalPrimitiveRegistry(builder); 48 builder.registerPrimitiveConstructor( 49 PrimitiveConstructor.create( 50 AesGcmHkdfStreaming::create, AesGcmHkdfStreamingKey.class, StreamingAead.class)); 51 builder.registerPrimitiveConstructor( 52 PrimitiveConstructor.create( 53 AesCtrHmacStreaming::create, AesCtrHmacStreamingKey.class, StreamingAead.class)); 54 55 return InternalConfiguration.createFromPrimitiveRegistry(builder.build()); 56 } catch (GeneralSecurityException e) { 57 throw new IllegalStateException(e); 58 } 59 } 60 61 /** 62 * Returns an instance of the {@code StreamingAeadConfigurationV0}. 63 */ get()64 public static Configuration get() throws GeneralSecurityException { 65 if (TinkFipsUtil.useOnlyFips()) { 66 throw new GeneralSecurityException( 67 "Cannot use non-FIPS-compliant StreamingAead in FIPS mode"); 68 } 69 return INTERNAL_CONFIGURATION; 70 } 71 } 72