blob: 74948fa8cb13d4b3d8dc70e8072075d2ace850a0 [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;
34
35class 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 Willden3ed6d062015-04-15 13:39:38 -060054 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
55 keymaster_error_t* error) = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -070056
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 Willdend92591d2014-12-30 18:19:10 -070071
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 Willden117a0cc2015-06-01 07:05:41 -060076
Shawn Willden294a2db2015-06-17 11:20:56 -060077 bool is_public_key_operation() const;
78
Shawn Willden117a0cc2015-06-01 07:05:41 -060079 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 Willden63ac0432014-12-29 14:07:08 -070083};
84
Shawn Willden0a4df7e2014-08-28 16:09:05 -060085/**
86 * Abstract base for all cryptographic operations.
87 */
88class Operation {
89 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070090 Operation(keymaster_purpose_t purpose) : purpose_(purpose) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060091 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060092
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060093 keymaster_purpose_t purpose() const { return purpose_; }
94
Shawn Willdenada48502015-06-25 06:26:05 -070095 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 Willden111edb32015-02-05 22:44:24 -0700103 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
104 AuthorizationSet* output_params) = 0;
Shawn Willdended8e7d2015-06-01 15:29:12 -0600105 virtual keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input,
106 AuthorizationSet* output_params, Buffer* output,
107 size_t* input_consumed) = 0;
108 virtual keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& signature,
109 AuthorizationSet* output_params, Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600110 virtual keymaster_error_t Abort() = 0;
111
112 private:
113 const keymaster_purpose_t purpose_;
Shawn Willdenada48502015-06-25 06:26:05 -0700114 AuthorizationSet key_auths_;
115 uint64_t key_id_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600116};
117
118} // namespace keymaster
119
120#endif // SYSTEM_KEYMASTER_OPERATION_H_