blob: 24878caa4f3ac5c9f6b7184963c41d7226d7e646 [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 Willden567a4a02014-12-31 12:14:46 -070055 virtual Operation* CreateOperation(const Key& key, 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 Willden63ac0432014-12-29 14:07:08 -070076};
77
78typedef AbstractFactoryRegistry<OperationFactory> OperationFactoryRegistry;
Shawn Willden111edb32015-02-05 22:44:24 -070079
Shawn Willden0a4df7e2014-08-28 16:09:05 -060080/**
81 * Abstract base for all cryptographic operations.
82 */
83class Operation {
84 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070085 Operation(keymaster_purpose_t purpose) : purpose_(purpose) {}
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060086 virtual ~Operation() {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060087
Shawn Willdenf6ca3a32014-09-11 15:11:32 -060088 keymaster_purpose_t purpose() const { return purpose_; }
89
Shawn Willden111edb32015-02-05 22:44:24 -070090 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
91 AuthorizationSet* output_params) = 0;
Shawn Willden6bfbff02015-02-06 19:48:24 -070092 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
93 Buffer* output, size_t* input_consumed) = 0;
Shawn Willden23d4a742015-03-19 15:33:21 -060094 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
Shawn Willden6bfbff02015-02-06 19:48:24 -070095 const Buffer& signature, Buffer* output) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060096 virtual keymaster_error_t Abort() = 0;
97
98 private:
99 const keymaster_purpose_t purpose_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600100};
101
102} // namespace keymaster
103
104#endif // SYSTEM_KEYMASTER_OPERATION_H_