blob: c0dc14ef4d5fbbbf94b0d89e7917a3703c123838 [file] [log] [blame]
Shawn Willdend67afae2014-08-19 12:36:27 -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_KEY_H_
18#define SYSTEM_KEYMASTER_KEY_H_
19
Shawn Willden06298102015-05-25 23:12:48 -060020#include <UniquePtr.h>
21
Shawn Willdenb9d584d2015-01-22 16:35:00 -070022#include <hardware/keymaster_defs.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060023#include <keymaster/authorization_set.h>
Shawn Willdena278f612014-12-23 11:22:21 -070024
Shawn Willdend67afae2014-08-19 12:36:27 -060025namespace keymaster {
26
Shawn Willdena278f612014-12-23 11:22:21 -070027class Key;
Shawn Willden0cb69422015-05-26 08:31:37 -060028class KeymasterContext;
Shawn Willden06298102015-05-25 23:12:48 -060029class OperationFactory;
30struct KeymasterKeyBlob;
Shawn Willdena278f612014-12-23 11:22:21 -070031
32/**
Shawn Willden0cb69422015-05-26 08:31:37 -060033 * KeyFactory is a abstraction whose subclasses know how to construct a specific subclass of Key.
Shawn Willdena278f612014-12-23 11:22:21 -070034 * There is a one to one correspondence between Key subclasses and KeyFactory subclasses.
35 */
36class KeyFactory {
37 public:
Shawn Willden0cb69422015-05-26 08:31:37 -060038 KeyFactory(const KeymasterContext* context) : context_(context) {}
Shawn Willdena278f612014-12-23 11:22:21 -070039 virtual ~KeyFactory() {}
40
Shawn Willdena278f612014-12-23 11:22:21 -070041 // Factory methods.
Shawn Willden0cb69422015-05-26 08:31:37 -060042 virtual keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
43 KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060044 AuthorizationSet* sw_enforced) const = 0;
Shawn Willden0cb69422015-05-26 08:31:37 -060045
46 virtual keymaster_error_t ImportKey(const AuthorizationSet& key_description,
47 keymaster_key_format_t input_key_material_format,
48 const KeymasterKeyBlob& input_key_material,
49 KeymasterKeyBlob* output_key_blob,
50 AuthorizationSet* hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060051 AuthorizationSet* sw_enforced) const = 0;
Shawn Willden0cb69422015-05-26 08:31:37 -060052
53 virtual keymaster_error_t LoadKey(const KeymasterKeyBlob& key_material,
54 const AuthorizationSet& hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060055 const AuthorizationSet& sw_enforced,
56 UniquePtr<Key>* key) const = 0;
57
58 virtual OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const = 0;
Shawn Willdena278f612014-12-23 11:22:21 -070059
60 // Informational methods.
Shawn Willden06298102015-05-25 23:12:48 -060061 virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) const = 0;
62 virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) const = 0;
Shawn Willden0cb69422015-05-26 08:31:37 -060063
64 protected:
65 const KeymasterContext* context_;
Shawn Willdena278f612014-12-23 11:22:21 -070066};
67
Shawn Willdend67afae2014-08-19 12:36:27 -060068class KeyBlob;
69class Operation;
Shawn Willden72014ad2014-09-17 13:04:10 -060070class UnencryptedKeyBlob;
Shawn Willdend67afae2014-08-19 12:36:27 -060071
72class Key {
73 public:
Shawn Willdend67afae2014-08-19 12:36:27 -060074 virtual ~Key() {}
Shawn Willdend67afae2014-08-19 12:36:27 -060075
76 /**
77 * Return a copy of raw key material, in the key's preferred binary format.
78 */
79 virtual keymaster_error_t key_material(UniquePtr<uint8_t[]>*, size_t* size) const = 0;
80
81 /**
82 * Return a copy of raw key material, in the specified format.
83 */
Shawn Willdenf268d742014-08-19 15:36:26 -060084 virtual keymaster_error_t formatted_key_material(keymaster_key_format_t format,
85 UniquePtr<uint8_t[]>* material,
Shawn Willdend67afae2014-08-19 12:36:27 -060086 size_t* size) const = 0;
87
88 const AuthorizationSet& authorizations() const { return authorizations_; }
89
90 protected:
Shawn Willden0cb69422015-05-26 08:31:37 -060091 Key(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced,
92 keymaster_error_t* error);
Shawn Willdend67afae2014-08-19 12:36:27 -060093
94 private:
95 AuthorizationSet authorizations_;
96};
97
98} // namespace keymaster
99
100#endif // SYSTEM_KEYMASTER_KEY_H_