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.data.adselection;
18 
19 import android.annotation.IntDef;
20 
21 import com.android.adservices.service.adselection.encryption.AdSelectionEncryptionKey;
22 
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 
26 /** Constants used in the EncryptionKey Datastore. */
27 public final class EncryptionKeyConstants {
28     /** IntDef to classify different key types. */
29     @IntDef(
30             value = {
31                 EncryptionKeyType.ENCRYPTION_KEY_TYPE_INVALID,
32                 EncryptionKeyType.ENCRYPTION_KEY_TYPE_AUCTION,
33                 EncryptionKeyType.ENCRYPTION_KEY_TYPE_QUERY,
34                 EncryptionKeyType.ENCRYPTION_KEY_TYPE_JOIN
35             })
36     @Retention(RetentionPolicy.SOURCE)
37     public @interface EncryptionKeyType {
38         int ENCRYPTION_KEY_TYPE_INVALID = 0;
39         int ENCRYPTION_KEY_TYPE_AUCTION = 1;
40         int ENCRYPTION_KEY_TYPE_QUERY = 2;
41         int ENCRYPTION_KEY_TYPE_JOIN = 3;
42     }
43 
44     /** Convert AdSelectionEncryptionKeyType to the encryptionKeyType used in DB. */
45     @EncryptionKeyType
from( @dSelectionEncryptionKey.AdSelectionEncryptionKeyType int adSelectionEncryptionKeyType)46     public static int from(
47             @AdSelectionEncryptionKey.AdSelectionEncryptionKeyType
48                     int adSelectionEncryptionKeyType) {
49         switch (adSelectionEncryptionKeyType) {
50             case AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.AUCTION:
51                 return EncryptionKeyType.ENCRYPTION_KEY_TYPE_AUCTION;
52             case AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.JOIN:
53                 return EncryptionKeyType.ENCRYPTION_KEY_TYPE_JOIN;
54             case AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.UNASSIGNED:
55                 // Intentional fallthrough
56             default:
57                 return EncryptionKeyType.ENCRYPTION_KEY_TYPE_INVALID;
58         }
59     }
60 
61     /** Convert DBEncryptionKeyType to AdSelectionEncryptionType. */
62     @AdSelectionEncryptionKey.AdSelectionEncryptionKeyType
toAdSelectionEncryptionKeyType(@ncryptionKeyType int dbEncryptionKeyType)63     public static int toAdSelectionEncryptionKeyType(@EncryptionKeyType int dbEncryptionKeyType) {
64         switch (dbEncryptionKeyType) {
65             case EncryptionKeyType.ENCRYPTION_KEY_TYPE_AUCTION:
66                 return AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.AUCTION;
67             case EncryptionKeyType.ENCRYPTION_KEY_TYPE_JOIN:
68                 return AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.JOIN;
69             case EncryptionKeyType.ENCRYPTION_KEY_TYPE_QUERY:
70                 // Intentional fallthrough
71             case EncryptionKeyType.ENCRYPTION_KEY_TYPE_INVALID:
72                 // Intentional fallthrough
73             default:
74                 return AdSelectionEncryptionKey.AdSelectionEncryptionKeyType.UNASSIGNED;
75         }
76     }
77 }
78