1 /*
2  * Copyright 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 #include <keymaster/key_blob_utils/integrity_assured_key_blob.h>
18 
19 #include <assert.h>
20 
21 #include <openssl/hmac.h>
22 #include <openssl/mem.h>
23 
24 #include <keymaster/android_keymaster_utils.h>
25 #include <keymaster/authorization_set.h>
26 #include <keymaster/km_openssl/openssl_err.h>
27 
28 namespace keymaster {
29 
30 static const uint8_t BLOB_VERSION = 0;
31 static const size_t HMAC_SIZE = 8;
32 static const char HMAC_KEY[] = "IntegrityAssuredBlob0";
33 
min(size_t a,size_t b)34 inline size_t min(size_t a, size_t b) {
35     if (a < b) return a;
36     return b;
37 }
38 
39 class HmacCleanup {
40   public:
HmacCleanup(HMAC_CTX * ctx)41     explicit HmacCleanup(HMAC_CTX* ctx) : ctx_(ctx) {}
~HmacCleanup()42     ~HmacCleanup() { HMAC_CTX_cleanup(ctx_); }
43 
44   private:
45     HMAC_CTX* ctx_;
46 };
47 
ComputeHmac(const uint8_t * serialized_data,size_t serialized_data_size,const AuthorizationSet & hidden,uint8_t hmac[HMAC_SIZE])48 static keymaster_error_t ComputeHmac(const uint8_t* serialized_data, size_t serialized_data_size,
49                                      const AuthorizationSet& hidden, uint8_t hmac[HMAC_SIZE]) {
50     size_t hidden_bytes_size = hidden.SerializedSize();
51     UniquePtr<uint8_t[]> hidden_bytes(new (std::nothrow) uint8_t[hidden_bytes_size]);
52     if (!hidden_bytes.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
53     hidden.Serialize(hidden_bytes.get(), hidden_bytes.get() + hidden_bytes_size);
54 
55     HMAC_CTX ctx;
56     HMAC_CTX_init(&ctx);
57     const EVP_MD* md = EVP_sha256();
58     if (!HMAC_Init_ex(&ctx, HMAC_KEY, sizeof(HMAC_KEY), md, nullptr /* engine */))
59         return TranslateLastOpenSslError();
60     HmacCleanup cleanup(&ctx);
61 
62     uint8_t tmp[EVP_MAX_MD_SIZE];
63     unsigned tmp_len;
64     if (!HMAC_Update(&ctx, serialized_data, serialized_data_size) ||
65         !HMAC_Update(&ctx, hidden_bytes.get(), hidden_bytes_size) ||  //
66         !HMAC_Final(&ctx, tmp, &tmp_len))
67         return TranslateLastOpenSslError();
68 
69     assert(tmp_len >= HMAC_SIZE);
70     memcpy(hmac, tmp, min(HMAC_SIZE, tmp_len));
71 
72     return KM_ERROR_OK;
73 }
74 
SerializeIntegrityAssuredBlob(const KeymasterKeyBlob & key_material,const AuthorizationSet & hidden,const AuthorizationSet & hw_enforced,const AuthorizationSet & sw_enforced,KeymasterKeyBlob * key_blob)75 keymaster_error_t SerializeIntegrityAssuredBlob(const KeymasterKeyBlob& key_material,
76                                                 const AuthorizationSet& hidden,
77                                                 const AuthorizationSet& hw_enforced,
78                                                 const AuthorizationSet& sw_enforced,
79                                                 KeymasterKeyBlob* key_blob) {
80     size_t size = 1 /* version */ +                //
81                   key_material.SerializedSize() +  //
82                   hw_enforced.SerializedSize() +   //
83                   sw_enforced.SerializedSize() +   //
84                   HMAC_SIZE;
85 
86     if (!key_blob->Reset(size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
87 
88     uint8_t* p = key_blob->writable_data();
89     *p++ = BLOB_VERSION;
90     p = key_material.Serialize(p, key_blob->end());
91     p = hw_enforced.Serialize(p, key_blob->end());
92     p = sw_enforced.Serialize(p, key_blob->end());
93 
94     return ComputeHmac(key_blob->key_material, p - key_blob->key_material, hidden, p);
95 }
96 
DeserializeIntegrityAssuredBlob(const KeymasterKeyBlob & key_blob,const AuthorizationSet & hidden,KeymasterKeyBlob * key_material,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced)97 keymaster_error_t DeserializeIntegrityAssuredBlob(const KeymasterKeyBlob& key_blob,
98                                                   const AuthorizationSet& hidden,
99                                                   KeymasterKeyBlob* key_material,
100                                                   AuthorizationSet* hw_enforced,
101                                                   AuthorizationSet* sw_enforced) {
102     const uint8_t* p = key_blob.begin();
103     const uint8_t* end = key_blob.end();
104 
105     if (p > end || p + HMAC_SIZE > end) return KM_ERROR_INVALID_KEY_BLOB;
106 
107     uint8_t computed_hmac[HMAC_SIZE];
108     keymaster_error_t error = ComputeHmac(key_blob.begin(), key_blob.key_material_size - HMAC_SIZE,
109                                           hidden, computed_hmac);
110     if (error != KM_ERROR_OK) return error;
111 
112     if (CRYPTO_memcmp(key_blob.end() - HMAC_SIZE, computed_hmac, HMAC_SIZE) != 0)
113         return KM_ERROR_INVALID_KEY_BLOB;
114 
115     return DeserializeIntegrityAssuredBlob_NoHmacCheck(key_blob, key_material, hw_enforced,
116                                                        sw_enforced);
117 }
118 
DeserializeIntegrityAssuredBlob_NoHmacCheck(const KeymasterKeyBlob & key_blob,KeymasterKeyBlob * key_material,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced)119 keymaster_error_t DeserializeIntegrityAssuredBlob_NoHmacCheck(const KeymasterKeyBlob& key_blob,
120                                                               KeymasterKeyBlob* key_material,
121                                                               AuthorizationSet* hw_enforced,
122                                                               AuthorizationSet* sw_enforced) {
123     const uint8_t* p = key_blob.begin();
124     const uint8_t* end = key_blob.end() - HMAC_SIZE;
125 
126     if (p > end) return KM_ERROR_INVALID_KEY_BLOB;
127 
128     if (*p != BLOB_VERSION) return KM_ERROR_INVALID_KEY_BLOB;
129     ++p;
130 
131     if (!key_material->Deserialize(&p, end) ||  //
132         !hw_enforced->Deserialize(&p, end) ||   //
133         !sw_enforced->Deserialize(&p, end))
134         return KM_ERROR_INVALID_KEY_BLOB;
135 
136     return KM_ERROR_OK;
137 }
138 
139 }  // namespace keymaster
140