1 /*
2  * Copyright (C) 2019 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 android.car.encryptionrunner;
18 
19 import android.annotation.IntDef;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 
23 /**
24  * Factory that creates encryption runner.
25  */
26 public class EncryptionRunnerFactory {
27 
EncryptionRunnerFactory()28     private EncryptionRunnerFactory() {
29         // prevent instantiation.
30     }
31 
32     @IntDef({EncryptionRunnerType.UKEY2, EncryptionRunnerType.OOB_UKEY2})
33     public @interface EncryptionRunnerType {
34         /** Use Ukey2 as underlying key exchange. */
35         int UKEY2 = 0;
36         /** Use Ukey2 and an out of band channel as underlying key exchange. */
37         int OOB_UKEY2 = 1;
38     }
39 
40     /**
41      * Creates a new {@link EncryptionRunner} based on {@param type}.
42      */
newRunner(@ncryptionRunnerType int type)43     public static EncryptionRunner newRunner(@EncryptionRunnerType int type) {
44         switch (type) {
45             case EncryptionRunnerType.UKEY2:
46                 return new Ukey2EncryptionRunner();
47             case EncryptionRunnerType.OOB_UKEY2:
48                 return new OobUkey2EncryptionRunner();
49             default:
50                 throw new IllegalArgumentException("Unknown EncryptionRunnerType: " + type);
51         }
52     }
53 
54     /**
55      * Creates a new {@link EncryptionRunner}.
56      *
57      * @deprecated Use {@link #newRunner(int)} instead.
58      */
59     @Deprecated
newRunner()60     public static EncryptionRunner newRunner() {
61         return newRunner(EncryptionRunnerType.UKEY2);
62     }
63 
64     /**
65      * Creates a new {@link EncryptionRunner} one that doesn't actually do encryption but is useful
66      * for testing.
67      */
68     @VisibleForTesting
newDummyRunner()69     public static EncryptionRunner newDummyRunner() {
70         return new DummyEncryptionRunner();
71     }
72 }
73