xref: /aosp_15_r20/system/security/ondevice-signing/KeystoreHmacKey.h (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker  *
4*e1997b9aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker  *
8*e1997b9aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker  *
10*e1997b9aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker  * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker  */
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker #pragma once
18*e1997b9aSAndroid Build Coastguard Worker 
19*e1997b9aSAndroid Build Coastguard Worker #include <optional>
20*e1997b9aSAndroid Build Coastguard Worker 
21*e1997b9aSAndroid Build Coastguard Worker #include <android-base/macros.h>
22*e1997b9aSAndroid Build Coastguard Worker #include <android-base/result.h>
23*e1997b9aSAndroid Build Coastguard Worker 
24*e1997b9aSAndroid Build Coastguard Worker #include <utils/StrongPointer.h>
25*e1997b9aSAndroid Build Coastguard Worker 
26*e1997b9aSAndroid Build Coastguard Worker #include <android/system/keystore2/IKeystoreService.h>
27*e1997b9aSAndroid Build Coastguard Worker 
28*e1997b9aSAndroid Build Coastguard Worker class KeystoreHmacKey {
29*e1997b9aSAndroid Build Coastguard Worker     using IKeystoreService = ::android::system::keystore2::IKeystoreService;
30*e1997b9aSAndroid Build Coastguard Worker     using IKeystoreSecurityLevel = ::android::system::keystore2::IKeystoreSecurityLevel;
31*e1997b9aSAndroid Build Coastguard Worker     using KeyDescriptor = ::android::system::keystore2::KeyDescriptor;
32*e1997b9aSAndroid Build Coastguard Worker 
33*e1997b9aSAndroid Build Coastguard Worker   public:
34*e1997b9aSAndroid Build Coastguard Worker     KeystoreHmacKey(const android::String16& keyAlias, int64_t keyNspace, int keyBootLevel);
35*e1997b9aSAndroid Build Coastguard Worker     android::base::Result<void> initialize(android::sp<IKeystoreService> service,
36*e1997b9aSAndroid Build Coastguard Worker                                            android::sp<IKeystoreSecurityLevel> securityLevel);
37*e1997b9aSAndroid Build Coastguard Worker     android::base::Result<std::string> sign(const std::string& message) const;
38*e1997b9aSAndroid Build Coastguard Worker     android::base::Result<void> verify(const std::string& message,
39*e1997b9aSAndroid Build Coastguard Worker                                        const std::string& signature) const;
40*e1997b9aSAndroid Build Coastguard Worker     android::base::Result<void> deleteKey() const;
41*e1997b9aSAndroid Build Coastguard Worker 
42*e1997b9aSAndroid Build Coastguard Worker   private:
43*e1997b9aSAndroid Build Coastguard Worker     android::base::Result<void> createKey();
44*e1997b9aSAndroid Build Coastguard Worker     KeyDescriptor mDescriptor;
45*e1997b9aSAndroid Build Coastguard Worker     android::sp<IKeystoreService> mService;
46*e1997b9aSAndroid Build Coastguard Worker     android::sp<IKeystoreSecurityLevel> mSecurityLevel;
47*e1997b9aSAndroid Build Coastguard Worker 
48*e1997b9aSAndroid Build Coastguard Worker     int mKeyBootLevel;
49*e1997b9aSAndroid Build Coastguard Worker };
50