blob: 3276d1ebbd45e761f98165aaa8c85c1589f9d43a [file] [log] [blame]
Janis Danisevskisf9f366e2019-06-04 17:01:25 -07001/*
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
18#ifndef SOFT_GATEKEEPER_H_
19#define SOFT_GATEKEEPER_H_
20
21extern "C" {
22#include <openssl/rand.h>
23#include <openssl/sha.h>
24
25#include <crypto_scrypt.h>
26}
27
28#include <android-base/memory.h>
29#include <gatekeeper/gatekeeper.h>
30
31#include <iostream>
32#include <memory>
33#include <unordered_map>
34
35namespace gatekeeper {
36
37struct fast_hash_t {
38 uint64_t salt;
39 uint8_t digest[SHA256_DIGEST_LENGTH];
40};
41
42class SoftGateKeeper : public GateKeeper {
43 public:
44 static const uint32_t SIGNATURE_LENGTH_BYTES = 32;
45
46 // scrypt params
47 static const uint64_t N = 16384;
48 static const uint32_t r = 8;
49 static const uint32_t p = 1;
50
51 static const int MAX_UINT_32_CHARS = 11;
52
53 SoftGateKeeper() {
54 key_.reset(new uint8_t[SIGNATURE_LENGTH_BYTES]);
55 memset(key_.get(), 0, SIGNATURE_LENGTH_BYTES);
56 }
57
58 virtual ~SoftGateKeeper() {}
59
60 virtual bool GetAuthTokenKey(const uint8_t** auth_token_key, uint32_t* length) const {
61 if (auth_token_key == NULL || length == NULL) return false;
62 *auth_token_key = key_.get();
63 *length = SIGNATURE_LENGTH_BYTES;
64 return true;
65 }
66
67 virtual void GetPasswordKey(const uint8_t** password_key, uint32_t* length) {
68 if (password_key == NULL || length == NULL) return;
69 *password_key = key_.get();
70 *length = SIGNATURE_LENGTH_BYTES;
71 }
72
73 virtual void ComputePasswordSignature(uint8_t* signature, uint32_t signature_length,
74 const uint8_t*, uint32_t, const uint8_t* password,
75 uint32_t password_length, salt_t salt) const {
76 if (signature == NULL) return;
77 crypto_scrypt(password, password_length, reinterpret_cast<uint8_t*>(&salt), sizeof(salt), N,
78 r, p, signature, signature_length);
79 }
80
81 virtual void GetRandom(void* random, uint32_t requested_length) const {
82 if (random == NULL) return;
83 RAND_pseudo_bytes((uint8_t*)random, requested_length);
84 }
85
86 virtual void ComputeSignature(uint8_t* signature, uint32_t signature_length, const uint8_t*,
87 uint32_t, const uint8_t*, const uint32_t) const {
88 if (signature == NULL) return;
89 memset(signature, 0, signature_length);
90 }
91
92 virtual uint64_t GetMillisecondsSinceBoot() const {
93 struct timespec time;
94 int res = clock_gettime(CLOCK_BOOTTIME, &time);
95 if (res < 0) return 0;
96 return (time.tv_sec * 1000) + (time.tv_nsec / 1000 / 1000);
97 }
98
99 virtual bool IsHardwareBacked() const { return false; }
100
101 virtual bool GetFailureRecord(uint32_t uid, secure_id_t user_id, failure_record_t* record,
102 bool /* secure */) {
103 failure_record_t* stored = &failure_map_[uid];
104 if (user_id != stored->secure_user_id) {
105 stored->secure_user_id = user_id;
106 stored->last_checked_timestamp = 0;
107 stored->failure_counter = 0;
108 }
109 memcpy(record, stored, sizeof(*record));
110 return true;
111 }
112
113 virtual bool ClearFailureRecord(uint32_t uid, secure_id_t user_id, bool /* secure */) {
114 failure_record_t* stored = &failure_map_[uid];
115 stored->secure_user_id = user_id;
116 stored->last_checked_timestamp = 0;
117 stored->failure_counter = 0;
118 return true;
119 }
120
121 virtual bool WriteFailureRecord(uint32_t uid, failure_record_t* record, bool /* secure */) {
122 failure_map_[uid] = *record;
123 return true;
124 }
125
126 fast_hash_t ComputeFastHash(const SizedBuffer& password, uint64_t salt) {
127 fast_hash_t fast_hash;
128 size_t digest_size = password.size() + sizeof(salt);
129 std::unique_ptr<uint8_t[]> digest(new uint8_t[digest_size]);
130 memcpy(digest.get(), &salt, sizeof(salt));
131 memcpy(digest.get() + sizeof(salt), password.Data<uint8_t>(), password.size());
132
133 SHA256(digest.get(), digest_size, (uint8_t*)&fast_hash.digest);
134
135 fast_hash.salt = salt;
136 return fast_hash;
137 }
138
139 bool VerifyFast(const fast_hash_t& fast_hash, const SizedBuffer& password) {
140 fast_hash_t computed = ComputeFastHash(password, fast_hash.salt);
141 return memcmp(computed.digest, fast_hash.digest, SHA256_DIGEST_LENGTH) == 0;
142 }
143
144 bool DoVerify(const password_handle_t* expected_handle, const SizedBuffer& password) {
145 uint64_t user_id = android::base::get_unaligned<secure_id_t>(&expected_handle->user_id);
146 FastHashMap::const_iterator it = fast_hash_map_.find(user_id);
147 if (it != fast_hash_map_.end() && VerifyFast(it->second, password)) {
148 return true;
149 } else {
150 if (GateKeeper::DoVerify(expected_handle, password)) {
151 uint64_t salt;
152 GetRandom(&salt, sizeof(salt));
153 fast_hash_map_[user_id] = ComputeFastHash(password, salt);
154 return true;
155 }
156 }
157
158 return false;
159 }
160
161 private:
162 typedef std::unordered_map<uint32_t, failure_record_t> FailureRecordMap;
163 typedef std::unordered_map<uint64_t, fast_hash_t> FastHashMap;
164
165 std::unique_ptr<uint8_t[]> key_;
166 FailureRecordMap failure_map_;
167 FastHashMap fast_hash_map_;
168};
169} // namespace gatekeeper
170
171#endif // SOFT_GATEKEEPER_H_