xref: /aosp_15_r20/external/tink/java_src/src/main/java/com/google/crypto/tink/internal/InternalConfiguration.java (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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.internal;
18 
19 import com.google.crypto.tink.Configuration;
20 import com.google.crypto.tink.Key;
21 import com.google.crypto.tink.PrimitiveSet;
22 import com.google.crypto.tink.proto.KeyData;
23 import java.security.GeneralSecurityException;
24 
25 /**
26  * Abstract class representing the real configuration API, i.e. all algorithms that Tink
27  * understands. Internal. Users should not access these methods since the operations are to be used
28  * by internal KeysetHandle operations only.
29  */
30 public abstract class InternalConfiguration extends Configuration {
31   /**
32    * Creates a primitive from a key in the old (proto) format.
33    */
getLegacyPrimitive(KeyData keyData, Class<P> primitiveClass)34   public abstract <P> P getLegacyPrimitive(KeyData keyData, Class<P> primitiveClass)
35       throws GeneralSecurityException;
36 
37   /**
38    * Given a key and a desired primitive class, creates the required primitive.
39    */
getPrimitive(Key key, Class<P> primitiveClass)40   public abstract <P> P getPrimitive(Key key, Class<P> primitiveClass)
41       throws GeneralSecurityException;
42 
43   /**
44    * Wraps the primitives in the primitive set into the provided class.
45    *
46    * @throws GeneralSecurityException if the wrapper for the provided pair
47    * (input class, wrapped class) is not registered
48    */
wrap(PrimitiveSet<B> primitiveSet, Class<P> clazz)49   public abstract <B, P> P wrap(PrimitiveSet<B> primitiveSet, Class<P> clazz)
50       throws GeneralSecurityException;
51 
52   /**
53    * Given the target class, reveals primitive set of what type should be provided to the
54    * {@link InternalConfiguration.wrap} method in order to get a wrapped object of the target class.
55    */
getInputPrimitiveClass(Class<?> wrapperClassObject)56   public abstract Class<?> getInputPrimitiveClass(Class<?> wrapperClassObject)
57       throws GeneralSecurityException;
58 
createFromPrimitiveRegistry(PrimitiveRegistry registry)59   public static InternalConfiguration createFromPrimitiveRegistry(PrimitiveRegistry registry) {
60     return new InternalConfigurationImpl(registry);
61   }
62 
63   /**
64    * Implementation of the configuration API.
65    */
66   private static class InternalConfigurationImpl extends InternalConfiguration {
67     /**
68      * Immutable registry instance.
69      */
70     private final PrimitiveRegistry registry;
71 
InternalConfigurationImpl(PrimitiveRegistry registry)72     private InternalConfigurationImpl(PrimitiveRegistry registry) {
73       this.registry = registry;
74     }
75 
76     @Override
getLegacyPrimitive(KeyData keyData, Class<P> primitiveClass)77     public <P> P getLegacyPrimitive(KeyData keyData, Class<P> primitiveClass)
78         throws GeneralSecurityException {
79       throw new UnsupportedOperationException("Not implemented");
80     }
81 
82     @Override
getPrimitive(Key key, Class<P> primitiveClass)83     public <P> P getPrimitive(Key key, Class<P> primitiveClass) throws GeneralSecurityException {
84       return registry.getPrimitive(key, primitiveClass);
85     }
86 
87     @Override
getInputPrimitiveClass(Class<?> wrapperClassObject)88     public Class<?> getInputPrimitiveClass(Class<?> wrapperClassObject)
89         throws GeneralSecurityException {
90       return registry.getInputPrimitiveClass(wrapperClassObject);
91     }
92 
93     @Override
wrap(PrimitiveSet<B> primitiveSet, Class<P> clazz)94     public <B, P> P wrap(PrimitiveSet<B> primitiveSet, Class<P> clazz)
95         throws GeneralSecurityException {
96       return registry.wrap(primitiveSet, clazz);
97     }
98   }
99 }
100