blob: 564359501022110e83ebac41624c1b9bc6b1123d [file] [log] [blame]
Shawn Willden0a4df7e2014-08-28 16:09:05 -06001/*
2 * Copyright 2014 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#ifndef SYSTEM_KEYMASTER_OPERATION_H_
18#define SYSTEM_KEYMASTER_OPERATION_H_
19
T.J. Mercieraed61b12022-07-26 23:20:07 +000020#include <utility>
21
Shawn Willden0a4df7e2014-08-28 16:09:05 -060022#include <assert.h>
23#include <stdint.h>
24#include <stdlib.h>
25
Shawn Willdenb9d584d2015-01-22 16:35:00 -070026#include <hardware/keymaster_defs.h>
Shawn Willdenada48502015-06-25 06:26:05 -070027#include <keymaster/android_keymaster_utils.h>
28#include <keymaster/authorization_set.h>
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060029#include <keymaster/logger.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060030
31namespace keymaster {
32
Shawn Willden111edb32015-02-05 22:44:24 -070033class AuthorizationSet;
Shawn Willden63ac0432014-12-29 14:07:08 -070034class Key;
35class Operation;
Janis Danisevskisdc877ae2017-05-15 13:57:25 -070036using OperationPtr = UniquePtr<Operation>;
37
Shawn Willden63ac0432014-12-29 14:07:08 -070038class OperationFactory {
39 public:
40 virtual ~OperationFactory() {}
41
42 // Required for registry
43 struct KeyType {
44 KeyType(keymaster_algorithm_t alg, keymaster_purpose_t purp)
45 : algorithm(alg), purpose(purp) {}
46
47 keymaster_algorithm_t algorithm;
48 keymaster_purpose_t purpose;
49
50 bool operator==(const KeyType& rhs) const {
51 return algorithm == rhs.algorithm && purpose == rhs.purpose;
52 }
53 };
54 virtual KeyType registry_key() const = 0;
55
56 // Factory methods
Shawn Willdendeffcb72018-01-07 23:34:58 -070057 virtual OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params,
Matthew Maurera47727e2019-04-04 00:09:39 +000058 keymaster_error_t* error) = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -070059
60 // Informational methods. The returned arrays reference static memory and must not be
61 // deallocated or modified.
62 virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const {
63 *padding_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070064 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070065 }
66 virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const {
67 *block_mode_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070068 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070069 }
70 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
71 *digest_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070072 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070073 }
Shawn Willdend92591d2014-12-30 18:19:10 -070074
75 // Convenience methods
76 bool supported(keymaster_padding_t padding) const;
77 bool supported(keymaster_block_mode_t padding) const;
78 bool supported(keymaster_digest_t padding) const;
Shawn Willden117a0cc2015-06-01 07:05:41 -060079
Shawn Willden294a2db2015-06-17 11:20:56 -060080 bool is_public_key_operation() const;
81
Shawn Willden117a0cc2015-06-01 07:05:41 -060082 bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
83 keymaster_padding_t* padding, keymaster_error_t* error) const;
84 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
85 keymaster_digest_t* digest, keymaster_error_t* error) const;
Matthew Maurer90174bd2019-05-10 16:31:00 -070086 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
87 keymaster_digest_t* digest, keymaster_error_t* error,
88 bool require_explicit_digest) const;
Shawn Willden63ac0432014-12-29 14:07:08 -070089};
90
Shawn Willden0a4df7e2014-08-28 16:09:05 -060091/**
92 * Abstract base for all cryptographic operations.
93 */
94class Operation {
95 public:
Shawn Willdendeffcb72018-01-07 23:34:58 -070096 explicit Operation(keymaster_purpose_t purpose, AuthorizationSet&& hw_enforced,
97 AuthorizationSet&& sw_enforced)
T.J. Mercieraed61b12022-07-26 23:20:07 +000098 : purpose_(purpose), hw_enforced_(std::move(hw_enforced)),
99 sw_enforced_(std::move(sw_enforced)) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -0600100 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600101
Shawn Willdendeffcb72018-01-07 23:34:58 -0700102 Operation(const Operation&) = delete;
103 void operator=(const Operation&) = delete;
104
Shawn Willdenf6ca3a32014-09-11 15:11:32 -0600105 keymaster_purpose_t purpose() const { return purpose_; }
106
Shawn Willdenada48502015-06-25 06:26:05 -0700107 void set_key_id(uint64_t key_id) { key_id_ = key_id; }
108 uint64_t key_id() const { return key_id_; }
Shawn Willden2865c252021-07-31 15:34:12 -0600109 void set_secure_deletion_slot(uint32_t secure_deletion_slot) {
110 secure_deletion_slot_ = secure_deletion_slot;
111 }
112 uint32_t secure_deletion_slot() const { return secure_deletion_slot_; }
Janis Danisevskisf3dc0b82017-05-15 11:19:44 -0700113 virtual keymaster_operation_handle_t operation_handle() const { return operation_handle_; }
Shawn Willdenada48502015-06-25 06:26:05 -0700114
Shawn Willdendeffcb72018-01-07 23:34:58 -0700115 AuthProxy authorizations() const { return AuthProxy(hw_enforced_, sw_enforced_); }
Qi Wud8b79212021-02-08 01:18:52 +0800116 AuthorizationSet hw_enforced() const { return hw_enforced_; }
117 AuthorizationSet sw_enforced() const { return sw_enforced_; }
Shawn Willdenada48502015-06-25 06:26:05 -0700118
David Zeuthen1755a6c2021-03-15 12:33:01 -0400119 // Creates and initializes |confirmation_verifier_buffer_| that can be retrieved with
120 // get_confirmation_verifier_buffer().
121 //
122 // Returns false on allocation failure.
123 bool create_confirmation_verifier_buffer() {
124 if (!confirmation_verifier_buffer_) {
125 Buffer* buffer = new (std::nothrow)
126 Buffer(kConfirmationTokenMessageTag, kConfirmationTokenMessageTagSize);
127 if (buffer == nullptr) {
128 return false;
129 }
130 confirmation_verifier_buffer_.reset(buffer);
131 }
132 return true;
133 }
134
135 // If a Buffer for ConfirmationUI verification was created with
136 // create_confirmation_verifier_buffer(), returns it. If not, returns |nullptr|.
137 Buffer* get_confirmation_verifier_buffer() { return confirmation_verifier_buffer_.get(); }
138
Shawn Willden111edb32015-02-05 22:44:24 -0700139 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
140 AuthorizationSet* output_params) = 0;
Shawn Willdended8e7d2015-06-01 15:29:12 -0600141 virtual keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input,
142 AuthorizationSet* output_params, Buffer* output,
143 size_t* input_consumed) = 0;
Shawn Willdencb647fe2016-01-27 12:59:13 -0700144 virtual keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& input,
145 const Buffer& signature, AuthorizationSet* output_params,
146 Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600147 virtual keymaster_error_t Abort() = 0;
148
Shawn Willdendeffcb72018-01-07 23:34:58 -0700149 protected:
Shawn Willdencb647fe2016-01-27 12:59:13 -0700150 // Helper function for implementing Finish() methods that need to call Update() to process
151 // input, but don't expect any output.
152 keymaster_error_t UpdateForFinish(const AuthorizationSet& input_params, const Buffer& input);
Janis Danisevskisf3dc0b82017-05-15 11:19:44 -0700153 keymaster_operation_handle_t operation_handle_;
Shawn Willdencb647fe2016-01-27 12:59:13 -0700154
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600155 private:
156 const keymaster_purpose_t purpose_;
Shawn Willdendeffcb72018-01-07 23:34:58 -0700157 AuthorizationSet hw_enforced_;
158 AuthorizationSet sw_enforced_;
Shawn Willdenada48502015-06-25 06:26:05 -0700159 uint64_t key_id_;
Shawn Willden2865c252021-07-31 15:34:12 -0600160 uint32_t secure_deletion_slot_ = 0;
David Zeuthen1755a6c2021-03-15 12:33:01 -0400161 UniquePtr<Buffer> confirmation_verifier_buffer_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600162};
163
164} // namespace keymaster
165
166#endif // SYSTEM_KEYMASTER_OPERATION_H_