blob: 06f9ea5ac1e7ca24030a5348b65cd9312454682b [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
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
27#define VALUE_SIZE 32768
28
29/* Here is the file format. There are two parts in blob.value, the secret and
30 * the description. The secret is stored in ciphertext, and its original size
31 * can be found in blob.length. The description is stored after the secret in
32 * plaintext, and its size is specified in blob.info. The total size of the two
33 * parts must be no more than VALUE_SIZE bytes. The first field is the version,
34 * the second is the blob's type, and the third byte is flags. Fields other
35 * than blob.info, blob.length, and blob.value are modified by encryptBlob()
36 * and decryptBlob(). Thus they should not be accessed from outside. */
37
38/* ** Note to future implementors of encryption: **
39 * Currently this is the construction:
40 * metadata || Enc(MD5(data) || data)
41 *
42 * This should be the construction used for encrypting if re-implementing:
43 *
44 * Derive independent keys for encryption and MAC:
45 * Kenc = AES_encrypt(masterKey, "Encrypt")
46 * Kmac = AES_encrypt(masterKey, "MAC")
47 *
48 * Store this:
49 * metadata || AES_CTR_encrypt(Kenc, rand_IV, data) ||
50 * HMAC(Kmac, metadata || Enc(data))
51 */
52struct __attribute__((packed)) blob {
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.
61 int32_t length; // in network byte order when encrypted
62 uint8_t value[VALUE_SIZE + AES_BLOCK_SIZE];
63};
64
65static const uint8_t CURRENT_BLOB_VERSION = 2;
66
67typedef enum {
68 TYPE_ANY = 0, // meta type that matches anything
69 TYPE_GENERIC = 1,
70 TYPE_MASTER_KEY = 2,
71 TYPE_KEY_PAIR = 3,
72 TYPE_KEYMASTER_10 = 4,
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040073 TYPE_KEY_CHARACTERISTICS = 5,
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070074} BlobType;
75
76class Entropy;
77
78class Blob {
79 public:
80 Blob(const uint8_t* value, size_t valueLength, const uint8_t* info, uint8_t infoLength,
81 BlobType type);
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070082 explicit Blob(blob b);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070083
84 Blob();
85
86 const uint8_t* getValue() const { return mBlob.value; }
87
88 int32_t getLength() const { return mBlob.length; }
89
90 const uint8_t* getInfo() const { return mBlob.value + mBlob.length; }
91 uint8_t getInfoLength() const { return mBlob.info; }
92
93 uint8_t getVersion() const { return mBlob.version; }
94
95 bool isEncrypted() const;
96 void setEncrypted(bool encrypted);
97
Shawn Willdend5a24e62017-02-28 13:53:24 -070098 bool isSuperEncrypted() const;
99 void setSuperEncrypted(bool superEncrypted);
100
Rubin Xu67899de2017-04-21 19:15:13 +0100101 bool isCriticalToDeviceEncryption() const;
102 void setCriticalToDeviceEncryption(bool critical);
103
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700104 bool isFallback() const { return mBlob.flags & KEYSTORE_FLAG_FALLBACK; }
105 void setFallback(bool fallback);
106
107 void setVersion(uint8_t version) { mBlob.version = version; }
108 BlobType getType() const { return BlobType(mBlob.type); }
109 void setType(BlobType type) { mBlob.type = uint8_t(type); }
110
111 ResponseCode writeBlob(const char* filename, AES_KEY* aes_key, State state, Entropy* entropy);
112 ResponseCode readBlob(const char* filename, AES_KEY* aes_key, State state);
113
114 private:
115 struct blob mBlob;
116};
117
118#endif // KEYSTORE_BLOB_H_