blob: 4156ed31176df27a454f0519e5cb1e803baa9579 [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
24#include <keymaster/google_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
Shawn Willden63ac0432014-12-29 14:07:08 -070028#include "abstract_factory_registry.h"
29
Shawn Willden0a4df7e2014-08-28 16:09:05 -060030namespace keymaster {
31
Shawn Willden111edb32015-02-05 22:44:24 -070032class AuthorizationSet;
Shawn Willden63ac0432014-12-29 14:07:08 -070033class Key;
34class Operation;
35
36class 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 Willden3ed6d062015-04-15 13:39:38 -060055 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
56 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;
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 }
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 Willden63ac0432014-12-29 14:07:08 -070077};
78
79typedef AbstractFactoryRegistry<OperationFactory> OperationFactoryRegistry;
Shawn Willden111edb32015-02-05 22:44:24 -070080
Shawn Willden0a4df7e2014-08-28 16:09:05 -060081/**
82 * Abstract base for all cryptographic operations.
83 */
84class Operation {
85 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070086 Operation(keymaster_purpose_t purpose) : purpose_(purpose) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060087 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060088
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060089 keymaster_purpose_t purpose() const { return purpose_; }
90
Shawn Willden111edb32015-02-05 22:44:24 -070091 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
92 AuthorizationSet* output_params) = 0;
Shawn Willden6bfbff02015-02-06 19:48:24 -070093 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
94 Buffer* output, size_t* input_consumed) = 0;
Shawn Willden23d4a742015-03-19 15:33:21 -060095 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
Shawn Willden6bfbff02015-02-06 19:48:24 -070096 const Buffer& signature, Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060097 virtual keymaster_error_t Abort() = 0;
98
99 private:
100 const keymaster_purpose_t purpose_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600101};
102
103} // namespace keymaster
104
105#endif // SYSTEM_KEYMASTER_OPERATION_H_