1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <optional>
20 
21 #include <android-base/macros.h>
22 #include <android-base/result.h>
23 
24 #include <utils/StrongPointer.h>
25 
26 #include <android/system/keystore2/IKeystoreService.h>
27 
28 class KeystoreHmacKey {
29     using IKeystoreService = ::android::system::keystore2::IKeystoreService;
30     using IKeystoreSecurityLevel = ::android::system::keystore2::IKeystoreSecurityLevel;
31     using KeyDescriptor = ::android::system::keystore2::KeyDescriptor;
32 
33   public:
34     KeystoreHmacKey();
35     android::base::Result<void> initialize(android::sp<IKeystoreService> service,
36                                            android::sp<IKeystoreSecurityLevel> securityLevel);
37     android::base::Result<std::string> sign(const std::string& message) const;
38     android::base::Result<void> verify(const std::string& message,
39                                        const std::string& signature) const;
40 
41   private:
42     android::base::Result<void> createKey();
43     KeyDescriptor mDescriptor;
44     android::sp<IKeystoreService> mService;
45     android::sp<IKeystoreSecurityLevel> mSecurityLevel;
46 };
47