• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.ohttp.algorithms;
18 
19 import com.android.adservices.ohttp.AeadNativeRef;
20 
21 import com.google.auto.value.AutoValue;
22 
23 import java.util.function.Supplier;
24 
25 /** Specifies the algorithm spec of an Authenticated Encryption with Associated Data algorithm */
26 @AutoValue
27 public abstract class AeadAlgorithmSpec {
28     // As defined in https://www.rfc-editor.org/rfc/rfc9180#name-authenticated-encryption-wi
29     public static final int AES_256_GCM_IDENTIFIER = 0x0002;
30 
31     private static final String AES_256_GCM = "AES_256_GCM";
32 
33     // The variables as defined in https://www.rfc-editor.org/rfc/rfc9180#name-aead-identifiers
34 
35     /** Get the identifier for the algorithm */
identifier()36     public abstract int identifier();
37 
38     /** Get the name of the algorithm */
name()39     public abstract String name();
40 
41     /** Get the length in bytes of a key for this algorithm */
keyLength()42     public abstract int keyLength();
43 
44     /** Get the length in bytes of a nonce for this algorithm */
nonceLength()45     public abstract int nonceLength();
46 
47     /** Get the length in bytes of an authentication tag for this algorithm */
tagLength()48     public abstract int tagLength();
49 
50     /** Gets the supplier of the Aead algorithm native reference */
aeadNativeRefSupplier()51     public abstract Supplier<AeadNativeRef> aeadNativeRefSupplier();
52 
aes256Gcm()53     private static AeadAlgorithmSpec aes256Gcm() {
54         return new AutoValue_AeadAlgorithmSpec(
55                 AES_256_GCM_IDENTIFIER,
56                 AES_256_GCM,
57                 /* keyLength= */ 32,
58                 /* nonceLength= */ 12,
59                 /* tagLength= */ 16,
60                 () -> AeadNativeRef.getHpkeAeadAes256GcmReference());
61     }
62 
63     /**
64      * Gets the AEAD algorithm corresponding to the identifier
65      *
66      * <p>As specified in https://www.rfc-editor.org/rfc/rfc9180#name-authenticated-encryption-wi
67      */
get(int identifier)68     public static AeadAlgorithmSpec get(int identifier) throws UnsupportedHpkeAlgorithmException {
69         if (identifier == AES_256_GCM_IDENTIFIER) {
70             return aes256Gcm();
71         }
72 
73         throw new UnsupportedHpkeAlgorithmException("Only AEAD ID = 0X0002 supported");
74     }
75 }
76