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