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 | |
| 24 | #include <keymaster/google_keymaster_utils.h> |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 25 | #include <hardware/keymaster_defs.h> |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 26 | #include <keymaster/logger.h> |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 27 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame^] | 28 | #include "abstract_factory_registry.h" |
| 29 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 30 | namespace keymaster { |
| 31 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 32 | class AuthorizationSet; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame^] | 33 | class Key; |
| 34 | class Operation; |
| 35 | |
| 36 | class 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 |
| 55 | virtual Operation* CreateOperation(const Key& key, const Logger& logger, |
| 56 | keymaster_error_t* error) = 0; |
| 57 | |
| 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; |
| 62 | return NULL; |
| 63 | } |
| 64 | virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const { |
| 65 | *block_mode_count = 0; |
| 66 | return NULL; |
| 67 | } |
| 68 | virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const { |
| 69 | *digest_count = 0; |
| 70 | return NULL; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | typedef AbstractFactoryRegistry<OperationFactory> OperationFactoryRegistry; |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 75 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 76 | /** |
| 77 | * Abstract base for all cryptographic operations. |
| 78 | */ |
| 79 | class Operation { |
| 80 | public: |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 81 | Operation(keymaster_purpose_t purpose, const Logger& logger) |
| 82 | : purpose_(purpose), logger_(logger) {} |
| 83 | virtual ~Operation() {} |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 84 | |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 85 | keymaster_purpose_t purpose() const { return purpose_; } |
| 86 | |
| 87 | const Logger& logger() { return logger_; } |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 88 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 89 | virtual keymaster_error_t Begin(const AuthorizationSet& input_params, |
| 90 | AuthorizationSet* output_params) = 0; |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 91 | virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, |
| 92 | Buffer* output, size_t* input_consumed) = 0; |
| 93 | virtual keymaster_error_t Finish(const AuthorizationSet& /* additional_params */, |
| 94 | const Buffer& signature, Buffer* output) = 0; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 95 | virtual keymaster_error_t Abort() = 0; |
| 96 | |
| 97 | private: |
| 98 | const keymaster_purpose_t purpose_; |
Shawn Willden | f6ca3a3 | 2014-09-11 15:11:32 -0600 | [diff] [blame] | 99 | const Logger& logger_; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace keymaster |
| 103 | |
| 104 | #endif // SYSTEM_KEYMASTER_OPERATION_H_ |