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