blob: f7d2a1ce645a4a0d0fe470dd886f80d6f6df5f25 [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 Willdenb9d584d2015-01-22 16:35:00 -070020#include <hardware/keymaster_defs.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060021#include <keymaster/authorization_set.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060022#include <keymaster/logger.h>
Shawn Willdend67afae2014-08-19 12:36:27 -060023
Shawn Willdena278f612014-12-23 11:22:21 -070024#include "abstract_factory_registry.h"
25#include "unencrypted_key_blob.h"
26
Shawn Willdend67afae2014-08-19 12:36:27 -060027namespace keymaster {
28
Shawn Willdena278f612014-12-23 11:22:21 -070029class Key;
30
31/**
32 * KeyFactory is a pure interface whose subclasses know how to construct a specific subclass of Key.
33 * There is a one to one correspondence between Key subclasses and KeyFactory subclasses.
34 */
35class KeyFactory {
36 public:
37 virtual ~KeyFactory() {}
38
39 // Required for registry
40 typedef keymaster_algorithm_t KeyType;
41 virtual keymaster_algorithm_t registry_key() const = 0;
42
43 // Factory methods.
Shawn Willden567a4a02014-12-31 12:14:46 -070044 virtual Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) = 0;
Shawn Willdena278f612014-12-23 11:22:21 -070045 virtual Key* ImportKey(const AuthorizationSet& key_description,
46 keymaster_key_format_t key_format, const uint8_t* key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -070047 size_t key_data_length, keymaster_error_t* error) = 0;
48 virtual Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) = 0;
Shawn Willdena278f612014-12-23 11:22:21 -070049
50 // Informational methods.
51 virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) = 0;
52 virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) = 0;
53};
54
55typedef AbstractFactoryRegistry<KeyFactory> KeyFactoryRegistry;
56
Shawn Willdend67afae2014-08-19 12:36:27 -060057class KeyBlob;
58class Operation;
Shawn Willden72014ad2014-09-17 13:04:10 -060059class UnencryptedKeyBlob;
Shawn Willdend67afae2014-08-19 12:36:27 -060060
61class Key {
62 public:
Shawn Willdend67afae2014-08-19 12:36:27 -060063 virtual ~Key() {}
Shawn Willdend67afae2014-08-19 12:36:27 -060064
65 /**
66 * Return a copy of raw key material, in the key's preferred binary format.
67 */
68 virtual keymaster_error_t key_material(UniquePtr<uint8_t[]>*, size_t* size) const = 0;
69
70 /**
71 * Return a copy of raw key material, in the specified format.
72 */
Shawn Willdenf268d742014-08-19 15:36:26 -060073 virtual keymaster_error_t formatted_key_material(keymaster_key_format_t format,
74 UniquePtr<uint8_t[]>* material,
Shawn Willdend67afae2014-08-19 12:36:27 -060075 size_t* size) const = 0;
76
77 const AuthorizationSet& authorizations() const { return authorizations_; }
78
79 protected:
Shawn Willden567a4a02014-12-31 12:14:46 -070080 Key(const KeyBlob& blob);
81 Key(const AuthorizationSet& authorizations) : authorizations_(authorizations) {}
Shawn Willdend67afae2014-08-19 12:36:27 -060082
83 private:
84 AuthorizationSet authorizations_;
85};
86
87} // namespace keymaster
88
89#endif // SYSTEM_KEYMASTER_KEY_H_