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;
18 
19 import androidx.annotation.Nullable;
20 
21 import com.google.auto.value.AutoValue;
22 
23 import java.util.Arrays;
24 import java.util.Objects;
25 
26 /** Holds the encapsulated shared secret generated by the HPKE setup base operation */
27 @AutoValue
28 public abstract class EncapsulatedSharedSecret {
29     /** Get the bytes held by this object */
30     @Nullable
31     @SuppressWarnings("mutable")
getBytes()32     abstract byte[] getBytes();
33 
34     /** Serialize this object to bytes. */
serializeToBytes()35     public byte[] serializeToBytes() {
36         byte[] originalBytes = getBytes();
37         return Objects.isNull(originalBytes)
38                 ? null
39                 : Arrays.copyOf(originalBytes, originalBytes.length);
40     }
41 
42     /** Create a {@link EncapsulatedSharedSecret} object with the given bytes */
create(byte[] bytes)43     public static EncapsulatedSharedSecret create(byte[] bytes) {
44         return Objects.isNull(bytes)
45                 ? new AutoValue_EncapsulatedSharedSecret(null)
46                 : new AutoValue_EncapsulatedSharedSecret(Arrays.copyOf(bytes, bytes.length));
47     }
48 }
49