blob: 656ba9d9e15fc6450de319211d8757535e02d8e3 [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
20#include <assert.h>
21#include <stdint.h>
22#include <stdlib.h>
23
Shawn Willdenb9d584d2015-01-22 16:35:00 -070024#include <hardware/keymaster_defs.h>
Shawn Willdenada48502015-06-25 06:26:05 -070025#include <keymaster/android_keymaster_utils.h>
26#include <keymaster/authorization_set.h>
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060027#include <keymaster/logger.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060028
29namespace keymaster {
30
Shawn Willden111edb32015-02-05 22:44:24 -070031class AuthorizationSet;
Shawn Willden63ac0432014-12-29 14:07:08 -070032class Key;
33class Operation;
Janis Danisevskisdc877ae2017-05-15 13:57:25 -070034using OperationPtr = UniquePtr<Operation>;
35
Shawn Willden63ac0432014-12-29 14:07:08 -070036class OperationFactory {
37 public:
38 virtual ~OperationFactory() {}
39
40 // Required for registry
41 struct KeyType {
42 KeyType(keymaster_algorithm_t alg, keymaster_purpose_t purp)
43 : algorithm(alg), purpose(purp) {}
44
45 keymaster_algorithm_t algorithm;
46 keymaster_purpose_t purpose;
47
48 bool operator==(const KeyType& rhs) const {
49 return algorithm == rhs.algorithm && purpose == rhs.purpose;
50 }
51 };
52 virtual KeyType registry_key() const = 0;
53
54 // Factory methods
Shawn Willdendeffcb72018-01-07 23:34:58 -070055 virtual OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params,
Matthew Maurera47727e2019-04-04 00:09:39 +000056 keymaster_error_t* error) = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -070057
58 // Informational methods. The returned arrays reference static memory and must not be
59 // deallocated or modified.
60 virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const {
61 *padding_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070062 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070063 }
64 virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const {
65 *block_mode_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070066 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070067 }
68 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
69 *digest_count = 0;
Yi Kong3712b272018-07-30 15:53:23 -070070 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070071 }
Shawn Willdend92591d2014-12-30 18:19:10 -070072
73 // Convenience methods
74 bool supported(keymaster_padding_t padding) const;
75 bool supported(keymaster_block_mode_t padding) const;
76 bool supported(keymaster_digest_t padding) const;
Shawn Willden117a0cc2015-06-01 07:05:41 -060077
Shawn Willden294a2db2015-06-17 11:20:56 -060078 bool is_public_key_operation() const;
79
Shawn Willden117a0cc2015-06-01 07:05:41 -060080 bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
81 keymaster_padding_t* padding, keymaster_error_t* error) const;
82 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
83 keymaster_digest_t* digest, keymaster_error_t* error) const;
Matthew Maurer90174bd2019-05-10 16:31:00 -070084 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
85 keymaster_digest_t* digest, keymaster_error_t* error,
86 bool require_explicit_digest) const;
Shawn Willden63ac0432014-12-29 14:07:08 -070087};
88
Shawn Willden0a4df7e2014-08-28 16:09:05 -060089/**
90 * Abstract base for all cryptographic operations.
91 */
92class Operation {
93 public:
Shawn Willdendeffcb72018-01-07 23:34:58 -070094 explicit Operation(keymaster_purpose_t purpose, AuthorizationSet&& hw_enforced,
95 AuthorizationSet&& sw_enforced)
96 : purpose_(purpose), hw_enforced_(move(hw_enforced)), sw_enforced_(move(sw_enforced)) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060097 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060098
Shawn Willdendeffcb72018-01-07 23:34:58 -070099 Operation(const Operation&) = delete;
100 void operator=(const Operation&) = delete;
101
Shawn Willdenf6ca3a32014-09-11 15:11:32 -0600102 keymaster_purpose_t purpose() const { return purpose_; }
103
Shawn Willdenada48502015-06-25 06:26:05 -0700104 void set_key_id(uint64_t key_id) { key_id_ = key_id; }
105 uint64_t key_id() const { return key_id_; }
Janis Danisevskisf3dc0b82017-05-15 11:19:44 -0700106 virtual keymaster_operation_handle_t operation_handle() const { return operation_handle_; }
Shawn Willdenada48502015-06-25 06:26:05 -0700107
Shawn Willdendeffcb72018-01-07 23:34:58 -0700108 AuthProxy authorizations() const { return AuthProxy(hw_enforced_, sw_enforced_); }
Shawn Willdenada48502015-06-25 06:26:05 -0700109
Shawn Willden111edb32015-02-05 22:44:24 -0700110 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
111 AuthorizationSet* output_params) = 0;
Shawn Willdended8e7d2015-06-01 15:29:12 -0600112 virtual keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input,
113 AuthorizationSet* output_params, Buffer* output,
114 size_t* input_consumed) = 0;
Shawn Willdencb647fe2016-01-27 12:59:13 -0700115 virtual keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& input,
116 const Buffer& signature, AuthorizationSet* output_params,
117 Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600118 virtual keymaster_error_t Abort() = 0;
119
Shawn Willdendeffcb72018-01-07 23:34:58 -0700120 protected:
Shawn Willdencb647fe2016-01-27 12:59:13 -0700121 // Helper function for implementing Finish() methods that need to call Update() to process
122 // input, but don't expect any output.
123 keymaster_error_t UpdateForFinish(const AuthorizationSet& input_params, const Buffer& input);
Janis Danisevskisf3dc0b82017-05-15 11:19:44 -0700124 keymaster_operation_handle_t operation_handle_;
Shawn Willdencb647fe2016-01-27 12:59:13 -0700125
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600126 private:
127 const keymaster_purpose_t purpose_;
Shawn Willdendeffcb72018-01-07 23:34:58 -0700128 AuthorizationSet hw_enforced_;
129 AuthorizationSet sw_enforced_;
Shawn Willdenada48502015-06-25 06:26:05 -0700130 uint64_t key_id_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600131};
132
133} // namespace keymaster
134
135#endif // SYSTEM_KEYMASTER_OPERATION_H_