Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 1 | /* |
| 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 Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 24 | #include <hardware/keymaster_defs.h> |
Shawn Willden | ada4850 | 2015-06-25 06:26:05 -0700 | [diff] [blame] | 25 | #include <keymaster/android_keymaster_utils.h> |
| 26 | #include <keymaster/authorization_set.h> |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 27 | #include <keymaster/logger.h> |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 28 | |
| 29 | namespace keymaster { |
| 30 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 31 | class AuthorizationSet; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 32 | class Key; |
| 33 | class Operation; |
| 34 | |
| 35 | class OperationFactory { |
| 36 | public: |
| 37 | virtual ~OperationFactory() {} |
| 38 | |
| 39 | // Required for registry |
| 40 | struct KeyType { |
| 41 | KeyType(keymaster_algorithm_t alg, keymaster_purpose_t purp) |
| 42 | : algorithm(alg), purpose(purp) {} |
| 43 | |
| 44 | keymaster_algorithm_t algorithm; |
| 45 | keymaster_purpose_t purpose; |
| 46 | |
| 47 | bool operator==(const KeyType& rhs) const { |
| 48 | return algorithm == rhs.algorithm && purpose == rhs.purpose; |
| 49 | } |
| 50 | }; |
| 51 | virtual KeyType registry_key() const = 0; |
| 52 | |
| 53 | // Factory methods |
Shawn Willden | 3ed6d06 | 2015-04-15 13:39:38 -0600 | [diff] [blame] | 54 | virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params, |
| 55 | keymaster_error_t* error) = 0; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 56 | |
| 57 | // Informational methods. The returned arrays reference static memory and must not be |
| 58 | // deallocated or modified. |
| 59 | virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const { |
| 60 | *padding_count = 0; |
| 61 | return NULL; |
| 62 | } |
| 63 | virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const { |
| 64 | *block_mode_count = 0; |
| 65 | return NULL; |
| 66 | } |
| 67 | virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const { |
| 68 | *digest_count = 0; |
| 69 | return NULL; |
| 70 | } |
Shawn Willden | d92591d | 2014-12-30 18:19:10 -0700 | [diff] [blame] | 71 | |
| 72 | // Convenience methods |
| 73 | bool supported(keymaster_padding_t padding) const; |
| 74 | bool supported(keymaster_block_mode_t padding) const; |
| 75 | bool supported(keymaster_digest_t padding) const; |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 76 | |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 77 | bool is_public_key_operation() const; |
| 78 | |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 79 | bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key, |
| 80 | keymaster_padding_t* padding, keymaster_error_t* error) const; |
| 81 | bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key, |
| 82 | keymaster_digest_t* digest, keymaster_error_t* error) const; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 85 | /** |
| 86 | * Abstract base for all cryptographic operations. |
| 87 | */ |
| 88 | class Operation { |
| 89 | public: |
Chih-Hung Hsieh | 5d5e42b | 2016-07-12 11:53:56 -0700 | [diff] [blame^] | 90 | explicit Operation(keymaster_purpose_t purpose) : purpose_(purpose) {} |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 91 | virtual ~Operation() {} |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 92 | |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 93 | keymaster_purpose_t purpose() const { return purpose_; } |
| 94 | |
Shawn Willden | ada4850 | 2015-06-25 06:26:05 -0700 | [diff] [blame] | 95 | void set_key_id(uint64_t key_id) { key_id_ = key_id; } |
| 96 | uint64_t key_id() const { return key_id_; } |
| 97 | |
| 98 | void SetAuthorizations(const AuthorizationSet& auths) { |
| 99 | key_auths_.Reinitialize(auths.data(), auths.size()); |
| 100 | } |
| 101 | const AuthorizationSet authorizations() { return key_auths_; } |
| 102 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 103 | virtual keymaster_error_t Begin(const AuthorizationSet& input_params, |
| 104 | AuthorizationSet* output_params) = 0; |
Shawn Willden | ded8e7d | 2015-06-01 15:29:12 -0600 | [diff] [blame] | 105 | virtual keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input, |
| 106 | AuthorizationSet* output_params, Buffer* output, |
| 107 | size_t* input_consumed) = 0; |
Shawn Willden | cb647fe | 2016-01-27 12:59:13 -0700 | [diff] [blame] | 108 | virtual keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& input, |
| 109 | const Buffer& signature, AuthorizationSet* output_params, |
| 110 | Buffer* output) = 0; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 111 | virtual keymaster_error_t Abort() = 0; |
| 112 | |
Shawn Willden | cb647fe | 2016-01-27 12:59:13 -0700 | [diff] [blame] | 113 | protected: |
| 114 | // Helper function for implementing Finish() methods that need to call Update() to process |
| 115 | // input, but don't expect any output. |
| 116 | keymaster_error_t UpdateForFinish(const AuthorizationSet& input_params, const Buffer& input); |
| 117 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 118 | private: |
| 119 | const keymaster_purpose_t purpose_; |
Shawn Willden | ada4850 | 2015-06-25 06:26:05 -0700 | [diff] [blame] | 120 | AuthorizationSet key_auths_; |
| 121 | uint64_t key_id_; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace keymaster |
| 125 | |
| 126 | #endif // SYSTEM_KEYMASTER_OPERATION_H_ |