Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 1 | /* |
| 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_UNENCRYPTED_KEY_BLOB_H_ |
| 18 | #define SYSTEM_KEYMASTER_UNENCRYPTED_KEY_BLOB_H_ |
| 19 | |
| 20 | #include <keymaster/key_blob.h> |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | /** |
| 25 | * Extends KeyBlob to provide access the the unencrypted key material as well as the encrypted form. |
| 26 | */ |
| 27 | class UnencryptedKeyBlob : public KeyBlob { |
| 28 | public: |
| 29 | /** |
| 30 | * Create a UnencryptedKeyBlob containing the specified authorization data and key material. \p |
| 31 | * key will be encrypted with a key derived from \p master_key, using OCB authenticated |
| 32 | * encryption with \p nonce. It is critically important that nonces NEVER be reused. The most |
| 33 | * convenient way to accomplish that is to choose them randomly. |
| 34 | * |
| 35 | * IMPORTANT: After constructing a UnencryptedKeyBlob, call error() to verify that the blob is |
| 36 | * usable. |
| 37 | */ |
| 38 | UnencryptedKeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced, |
| 39 | const AuthorizationSet& hidden, const uint8_t* unencrypted_key, |
| 40 | size_t unencrypted_key_length, const uint8_t* master_key, |
| 41 | size_t master_key_length, const uint8_t nonce[NONCE_LENGTH]); |
| 42 | |
| 43 | /** |
| 44 | * Create a UnencryptedKeyBlob, extracting the enforced and unenforced sets and decrypting the |
| 45 | * key. The KeyBlob does *not* take ownership of key_blob. |
| 46 | * |
| 47 | * IMPORTANT: After constructing a UnencryptedKeyBlob, call error() to verify that the blob is |
| 48 | * usable. |
| 49 | */ |
| 50 | UnencryptedKeyBlob(const keymaster_key_blob_t& key_blob, const AuthorizationSet& hidden, |
| 51 | const uint8_t* master_key, size_t master_key_length); |
| 52 | |
| 53 | inline const uint8_t* unencrypted_key_material() const { |
| 54 | return unencrypted_key_material_.get(); |
| 55 | } |
| 56 | inline size_t unencrypted_key_material_length() const { |
| 57 | return unencrypted_key_material_length_; |
| 58 | } |
| 59 | inline const AuthorizationSet& hidden() const { return hidden_; } |
| 60 | |
| 61 | private: |
| 62 | void DecryptKey(const uint8_t* master_key, size_t master_key_length); |
| 63 | void EncryptKey(const uint8_t* master_key, size_t master_key_length, const uint8_t* nonce); |
| 64 | |
| 65 | /** |
| 66 | * Create an AES_OCB context initialized with a key derived using \p master_key and the |
| 67 | * authorizations. |
| 68 | */ |
| 69 | class AeCtx; |
| 70 | AeCtx* InitializeKeyWrappingContext(const uint8_t* master_key, size_t master_key_length); |
| 71 | |
| 72 | const uint8_t* BuildDerivationData(size_t* derivation_data_len) const; |
| 73 | |
| 74 | UniquePtr<uint8_t[]> unencrypted_key_material_; |
| 75 | size_t unencrypted_key_material_length_; |
| 76 | AuthorizationSet hidden_; |
| 77 | }; |
| 78 | |
| 79 | } // namespace keymaster |
| 80 | |
| 81 | #endif // SYSTEM_KEYMASTER_UNENCRYPTED_KEY_BLOB_H_ |