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