Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 KEYSTORE_BLOB_H_ |
| 18 | #define KEYSTORE_BLOB_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <openssl/aes.h> |
| 23 | #include <openssl/md5.h> |
| 24 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 25 | #include <keystore/keymaster_types.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 26 | #include <keystore/keystore.h> |
Branden Archer | a7a29fa | 2019-01-10 09:24:14 -0800 | [diff] [blame] | 27 | #include <vector> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 28 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 29 | constexpr size_t kValueSize = 32768; |
| 30 | constexpr size_t kAesKeySize = 128 / 8; |
| 31 | constexpr size_t kGcmTagLength = 128 / 8; |
| 32 | constexpr size_t kGcmIvLength = 96 / 8; |
Branden Archer | b7ff91b | 2019-01-10 15:01:49 -0800 | [diff] [blame^] | 33 | constexpr size_t kAes128KeySizeBytes = 128 / 8; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 34 | |
| 35 | /* Here is the file format. There are two parts in blob.value, the secret and |
| 36 | * the description. The secret is stored in ciphertext, and its original size |
| 37 | * can be found in blob.length. The description is stored after the secret in |
| 38 | * plaintext, and its size is specified in blob.info. The total size of the two |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 39 | * parts must be no more than kValueSize bytes. The first field is the version, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 40 | * the second is the blob's type, and the third byte is flags. Fields other |
| 41 | * than blob.info, blob.length, and blob.value are modified by encryptBlob() |
| 42 | * and decryptBlob(). Thus they should not be accessed from outside. */ |
| 43 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 44 | struct __attribute__((packed)) blobv3 { |
| 45 | uint8_t version; |
| 46 | uint8_t type; |
| 47 | uint8_t flags; |
| 48 | uint8_t info; |
| 49 | uint8_t initialization_vector[AES_BLOCK_SIZE]; // Only 96 bits is used, rest is zeroed. |
| 50 | uint8_t aead_tag[kGcmTagLength]; |
| 51 | int32_t length; // in network byte order, only for backward compatibility |
| 52 | uint8_t value[kValueSize + AES_BLOCK_SIZE]; |
| 53 | }; |
| 54 | |
| 55 | struct __attribute__((packed)) blobv2 { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 56 | uint8_t version; |
| 57 | uint8_t type; |
| 58 | uint8_t flags; |
| 59 | uint8_t info; |
| 60 | uint8_t vector[AES_BLOCK_SIZE]; |
| 61 | uint8_t encrypted[0]; // Marks offset to encrypted data. |
| 62 | uint8_t digest[MD5_DIGEST_LENGTH]; |
| 63 | uint8_t digested[0]; // Marks offset to digested data. |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 64 | int32_t length; // in network byte order |
| 65 | uint8_t value[kValueSize + AES_BLOCK_SIZE]; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 68 | static_assert(sizeof(blobv3) == sizeof(blobv2) && |
| 69 | offsetof(blobv3, initialization_vector) == offsetof(blobv2, vector) && |
| 70 | offsetof(blobv3, aead_tag) == offsetof(blobv2, digest) && |
| 71 | offsetof(blobv3, aead_tag) == offsetof(blobv2, encrypted) && |
| 72 | offsetof(blobv3, length) == offsetof(blobv2, length) && |
| 73 | offsetof(blobv3, value) == offsetof(blobv2, value), |
| 74 | "Oops. Blob layout changed."); |
| 75 | |
| 76 | static const uint8_t CURRENT_BLOB_VERSION = 3; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 77 | |
| 78 | typedef enum { |
| 79 | TYPE_ANY = 0, // meta type that matches anything |
| 80 | TYPE_GENERIC = 1, |
| 81 | TYPE_MASTER_KEY = 2, |
| 82 | TYPE_KEY_PAIR = 3, |
| 83 | TYPE_KEYMASTER_10 = 4, |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 84 | TYPE_KEY_CHARACTERISTICS = 5, |
Branden Archer | b7ff91b | 2019-01-10 15:01:49 -0800 | [diff] [blame^] | 85 | TYPE_MASTER_KEY_AES256 = 7, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 86 | } BlobType; |
| 87 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 88 | class Blob { |
| 89 | public: |
| 90 | Blob(const uint8_t* value, size_t valueLength, const uint8_t* info, uint8_t infoLength, |
| 91 | BlobType type); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 92 | explicit Blob(blobv3 b); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 93 | Blob(); |
| 94 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 95 | ~Blob() { mBlob = {}; } |
| 96 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 97 | const uint8_t* getValue() const { return mBlob.value; } |
| 98 | |
| 99 | int32_t getLength() const { return mBlob.length; } |
| 100 | |
| 101 | const uint8_t* getInfo() const { return mBlob.value + mBlob.length; } |
| 102 | uint8_t getInfoLength() const { return mBlob.info; } |
| 103 | |
| 104 | uint8_t getVersion() const { return mBlob.version; } |
| 105 | |
| 106 | bool isEncrypted() const; |
| 107 | void setEncrypted(bool encrypted); |
| 108 | |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 109 | bool isSuperEncrypted() const; |
| 110 | void setSuperEncrypted(bool superEncrypted); |
| 111 | |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 112 | bool isCriticalToDeviceEncryption() const; |
| 113 | void setCriticalToDeviceEncryption(bool critical); |
| 114 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 115 | bool isFallback() const { return mBlob.flags & KEYSTORE_FLAG_FALLBACK; } |
| 116 | void setFallback(bool fallback); |
| 117 | |
| 118 | void setVersion(uint8_t version) { mBlob.version = version; } |
| 119 | BlobType getType() const { return BlobType(mBlob.type); } |
| 120 | void setType(BlobType type) { mBlob.type = uint8_t(type); } |
| 121 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 122 | keystore::SecurityLevel getSecurityLevel() const; |
| 123 | void setSecurityLevel(keystore::SecurityLevel); |
| 124 | |
Branden Archer | a7a29fa | 2019-01-10 09:24:14 -0800 | [diff] [blame] | 125 | ResponseCode writeBlob(const std::string& filename, const std::vector<uint8_t>& aes_key, |
| 126 | State state); |
| 127 | ResponseCode readBlob(const std::string& filename, const std::vector<uint8_t>& aes_key, |
| 128 | State state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 131 | blobv3 mBlob; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #endif // KEYSTORE_BLOB_H_ |