blob: 21a9de64dc4ab78a8d113bb0089bc2a6ae8ddad4 [file] [log] [blame]
Shawn Willden4db3fbd2014-08-08 22:13:44 -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_BLOB_H_
18#define SYSTEM_KEYMASTER_KEY_BLOB_H_
19
20#include <cstddef>
21
22#include <stdint.h>
23
24#include <UniquePtr.h>
25
26#include "ae.h"
27#include "authorization_set.h"
28#include "keymaster_defs.h"
29#include "serializable.h"
30
31namespace keymaster {
32
33/**
34 * This class represents a Keymaster key blob, including authorization sets and key material, both
35 * encrypted and unencrypted. It's primary purpose is to serialize and deserialize blob arrays, and
36 * provide access to the data in the blob.
37 */
38class KeyBlob : public Serializable {
39 public:
40 static const size_t NONCE_LENGTH = 12;
41 static const size_t TAG_LENGTH = 128 / 8;
42
43 /**
44 * Create a KeyBlob containing the specified authorization data and key material. The copy of
45 * \p key will be encrypted with key derived from \p master_key, using OCB authenticated
46 * encryption with \p nonce. It is critically important that nonces NEVER be reused. The most
47 * convenient way to accomplish that is to choose them randomly (assuming good randomness, that
48 * means there's a probability of reuse of one in 2^96).
49 *
50 * Note that this interface abuses \p keymaster_key_blob_t a bit. Normally, that struct is used
51 * to contain a full Keymaster blob, i.e. what KeyBlob is designed to create and manage. In
52 * this case we're using it to hold pure key material without any of the additional structure
53 * needed for a true Keymaster key.
54 *
55 * IMPORTANT: After constructing a KeyBlob, call error() to verify that the blob is usable.
56 */
57 KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced,
58 const keymaster_key_blob_t& key, const keymaster_key_blob_t& master_key,
59 uint8_t nonce[NONCE_LENGTH]);
60
61 /**
62 * Create a KeyBlob, reconstituting it from the encrypted material in \p encrypted_key,
63 * decrypted with key derived from \p master_key. The KeyBlob takes ownership of the \p
64 * encrypted_key.key_material.
65 *
66 * Note, again, that \p master_key here is an abuse of \p keymaster_key_blob_t, since it
67 * is just key material, not a full Keymaster blob.
68 *
69 * IMPORTANT: After constructing a KeyBlob, call error() to verify that the blob is usable.
70 */
71 KeyBlob(const keymaster_key_blob_t& encrypted_key, const keymaster_key_blob_t& master_key);
72
73 ~KeyBlob() {
74 memset(nonce_, 0, NONCE_LENGTH);
75 memset(tag_, 0, TAG_LENGTH);
76 if (key_material_.get() != NULL)
77 memset(key_material_.get(), 0, key_material_length_);
78 // Encrypted key material doesn't matter, and AuthorizationSets clear themselves.
79 }
80
81 size_t SerializedSize() const;
82 uint8_t* Serialize(uint8_t* buf, const uint8_t* end) const;
83 bool Deserialize(const uint8_t** buf, const uint8_t* end);
84
85 /**
86 * Decrypt encrypted key. Call this after calling "Deserialize". Until it's called,
87 * key_material() will return a pointer to an uninitialized buffer. Sets error if there is a
88 * problem.
89 */
90 void DecryptKey(const keymaster_key_blob_t& master_key);
91
92 /**
93 * Returns KM_ERROR_OK if all is well, or an appropriate error code if there is a problem. This
94 * error code should be checked after constructing or deserializing/decrypting, and if it does
95 * not return KM_ERROR_OK, then don't call any other methods.
96 */
97 inline keymaster_error_t error() { return error_; }
98
99 inline const uint8_t* nonce() const { return nonce_; }
100 inline const uint8_t* key_material() const { return key_material_.get(); }
101 inline const uint8_t* encrypted_key_material() const { return encrypted_key_material_.get(); }
102 inline size_t key_material_length() const { return key_material_length_; }
103 inline const uint8_t* tag() const { return tag_; }
104
105 inline const AuthorizationSet& enforced() const { return enforced_; }
106 inline const AuthorizationSet& unenforced() const { return unenforced_; }
107
108 private:
109 void EncryptKey(const keymaster_key_blob_t& master_key);
110
111 /**
112 * Create an AES_OCB context initialized with a key derived using \p master_key and the
113 * authorizations.
114 */
115 ae_ctx* InitializeKeyWrappingContext(const keymaster_key_blob_t& master_key,
116 keymaster_error_t* error) const;
117
118 const uint8_t* BuildAuthData(size_t* auth_data_len) const;
119
120 keymaster_error_t error_;
121 uint8_t nonce_[NONCE_LENGTH];
122 UniquePtr<uint8_t[]> key_material_;
123 UniquePtr<uint8_t[]> encrypted_key_material_;
124 size_t key_material_length_;
125 uint8_t tag_[TAG_LENGTH];
126 AuthorizationSet enforced_;
127 AuthorizationSet unenforced_;
128};
129
130} // namespace keymaster
131
132#endif // SYSTEM_KEYMASTER_KEY_BLOB_H_