blob: a950b422564516d5388c4c261e741a944172d99e [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 Willdenb6837e72015-05-16 09:20:59 -060024#include <keymaster/android_keymaster_utils.h>
Shawn Willdenb9d584d2015-01-22 16:35:00 -070025#include <hardware/keymaster_defs.h>
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060026#include <keymaster/logger.h>
Shawn Willden0a4df7e2014-08-28 16:09:05 -060027
28namespace keymaster {
29
Shawn Willden111edb32015-02-05 22:44:24 -070030class AuthorizationSet;
Shawn Willden63ac0432014-12-29 14:07:08 -070031class Key;
32class Operation;
33
34class OperationFactory {
35 public:
36 virtual ~OperationFactory() {}
37
38 // Required for registry
39 struct KeyType {
40 KeyType(keymaster_algorithm_t alg, keymaster_purpose_t purp)
41 : algorithm(alg), purpose(purp) {}
42
43 keymaster_algorithm_t algorithm;
44 keymaster_purpose_t purpose;
45
46 bool operator==(const KeyType& rhs) const {
47 return algorithm == rhs.algorithm && purpose == rhs.purpose;
48 }
49 };
50 virtual KeyType registry_key() const = 0;
51
52 // Factory methods
Shawn Willden3ed6d062015-04-15 13:39:38 -060053 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
54 keymaster_error_t* error) = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -070055
56 // Informational methods. The returned arrays reference static memory and must not be
57 // deallocated or modified.
58 virtual const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const {
59 *padding_count = 0;
60 return NULL;
61 }
62 virtual const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const {
63 *block_mode_count = 0;
64 return NULL;
65 }
66 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
67 *digest_count = 0;
68 return NULL;
69 }
Shawn Willdend92591d2014-12-30 18:19:10 -070070
71 // Convenience methods
72 bool supported(keymaster_padding_t padding) const;
73 bool supported(keymaster_block_mode_t padding) const;
74 bool supported(keymaster_digest_t padding) const;
Shawn Willden117a0cc2015-06-01 07:05:41 -060075
Shawn Willden294a2db2015-06-17 11:20:56 -060076 bool is_public_key_operation() const;
77
Shawn Willden117a0cc2015-06-01 07:05:41 -060078 bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
79 keymaster_padding_t* padding, keymaster_error_t* error) const;
80 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
81 keymaster_digest_t* digest, keymaster_error_t* error) const;
Shawn Willden63ac0432014-12-29 14:07:08 -070082};
83
Shawn Willden0a4df7e2014-08-28 16:09:05 -060084/**
85 * Abstract base for all cryptographic operations.
86 */
87class Operation {
88 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070089 Operation(keymaster_purpose_t purpose) : purpose_(purpose) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060090 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060091
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060092 keymaster_purpose_t purpose() const { return purpose_; }
93
Shawn Willden111edb32015-02-05 22:44:24 -070094 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
95 AuthorizationSet* output_params) = 0;
Shawn Willdended8e7d2015-06-01 15:29:12 -060096 virtual keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input,
97 AuthorizationSet* output_params, Buffer* output,
98 size_t* input_consumed) = 0;
99 virtual keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& signature,
100 AuthorizationSet* output_params, Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600101 virtual keymaster_error_t Abort() = 0;
102
103 private:
104 const keymaster_purpose_t purpose_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600105};
106
107} // namespace keymaster
108
109#endif // SYSTEM_KEYMASTER_OPERATION_H_