blob: 75fe11dc19ad130e0d391740722f70e54c6aaedc [file] [log] [blame]
Andres Moralesae242922015-05-18 09:26:19 -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>
Andres Moralesc7ab1e82015-06-22 08:24:45 -070023#include <openssl/sha.h>
24
Andres Moralesae242922015-05-18 09:26:19 -070025#include <crypto_scrypt.h>
26}
27
28#include <UniquePtr.h>
29#include <gatekeeper/gatekeeper.h>
30#include <iostream>
31#include <unordered_map>
32
33namespace gatekeeper {
34
Andres Moralesc7ab1e82015-06-22 08:24:45 -070035struct fast_hash_t {
36 uint64_t salt;
37 uint8_t digest[SHA256_DIGEST_LENGTH];
38};
Andres Moralesae242922015-05-18 09:26:19 -070039
40class SoftGateKeeper : public GateKeeper {
41public:
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
51 SoftGateKeeper() {
52 key_.reset(new uint8_t[SIGNATURE_LENGTH_BYTES]);
53 memset(key_.get(), 0, SIGNATURE_LENGTH_BYTES);
54 }
55
56 virtual ~SoftGateKeeper() {
57 }
58
59 virtual bool GetAuthTokenKey(const uint8_t **auth_token_key,
60 uint32_t *length) const {
61 if (auth_token_key == NULL || length == NULL) return false;
Andres Moralese1f827f2015-06-01 09:59:05 -070062 uint8_t *auth_token_key_copy = new uint8_t[SIGNATURE_LENGTH_BYTES];
63 memcpy(auth_token_key_copy, key_.get(), SIGNATURE_LENGTH_BYTES);
64
65 *auth_token_key = auth_token_key_copy;
Andres Moralesae242922015-05-18 09:26:19 -070066 *length = SIGNATURE_LENGTH_BYTES;
67 return true;
68 }
69
70 virtual void GetPasswordKey(const uint8_t **password_key, uint32_t *length) {
71 if (password_key == NULL || length == NULL) return;
Andres Moralese1f827f2015-06-01 09:59:05 -070072 uint8_t *password_key_copy = new uint8_t[SIGNATURE_LENGTH_BYTES];
73 memcpy(password_key_copy, key_.get(), SIGNATURE_LENGTH_BYTES);
74
75 *password_key = password_key_copy;
Andres Moralesae242922015-05-18 09:26:19 -070076 *length = SIGNATURE_LENGTH_BYTES;
77 }
78
79 virtual void ComputePasswordSignature(uint8_t *signature, uint32_t signature_length,
80 const uint8_t *, uint32_t, const uint8_t *password,
81 uint32_t password_length, salt_t salt) const {
82 if (signature == NULL) return;
83 crypto_scrypt(password, password_length, reinterpret_cast<uint8_t *>(&salt),
84 sizeof(salt), N, r, p, signature, signature_length);
85 }
86
87 virtual void GetRandom(void *random, uint32_t requested_length) const {
88 if (random == NULL) return;
89 RAND_pseudo_bytes((uint8_t *) random, requested_length);
90 }
91
92 virtual void ComputeSignature(uint8_t *signature, uint32_t signature_length,
93 const uint8_t *, uint32_t, const uint8_t *, const uint32_t) const {
94 if (signature == NULL) return;
95 memset(signature, 0, signature_length);
96 }
97
98 virtual uint64_t GetMillisecondsSinceBoot() const {
99 struct timespec time;
100 int res = clock_gettime(CLOCK_BOOTTIME, &time);
101 if (res < 0) return 0;
102 return (time.tv_sec * 1000) + (time.tv_nsec / 1000 / 1000);
103 }
104
105 virtual bool IsHardwareBacked() const {
106 return false;
107 }
108
Andres Moralese1f827f2015-06-01 09:59:05 -0700109 virtual bool GetFailureRecord(uint32_t uid, secure_id_t user_id, failure_record_t *record,
110 bool /* secure */) {
Andres Moralesae242922015-05-18 09:26:19 -0700111 failure_record_t *stored = &failure_map_[uid];
112 if (user_id != stored->secure_user_id) {
113 stored->secure_user_id = user_id;
114 stored->last_checked_timestamp = 0;
115 stored->failure_counter = 0;
116 }
117 memcpy(record, stored, sizeof(*record));
118 return true;
119 }
120
Andres Moralese1f827f2015-06-01 09:59:05 -0700121 virtual bool ClearFailureRecord(uint32_t uid, secure_id_t user_id, bool /* secure */) {
Andres Moralesae242922015-05-18 09:26:19 -0700122 failure_record_t *stored = &failure_map_[uid];
123 stored->secure_user_id = user_id;
124 stored->last_checked_timestamp = 0;
125 stored->failure_counter = 0;
Andres Moralese1f827f2015-06-01 09:59:05 -0700126 return true;
Andres Moralesae242922015-05-18 09:26:19 -0700127 }
128
Andres Moralese1f827f2015-06-01 09:59:05 -0700129 virtual bool WriteFailureRecord(uint32_t uid, failure_record_t *record, bool /* secure */) {
Andres Moralesae242922015-05-18 09:26:19 -0700130 failure_map_[uid] = *record;
131 return true;
132 }
133
Andres Moralesc7ab1e82015-06-22 08:24:45 -0700134 fast_hash_t ComputeFastHash(const SizedBuffer &password, uint64_t salt) {
135 fast_hash_t fast_hash;
136 size_t digest_size = password.length + sizeof(salt);
137 std::unique_ptr<uint8_t[]> digest(new uint8_t[digest_size]);
138 memcpy(digest.get(), &salt, sizeof(salt));
139 memcpy(digest.get() + sizeof(salt), password.buffer.get(), password.length);
140
141 SHA256(digest.get(), digest_size, (uint8_t *) &fast_hash.digest);
142
143 fast_hash.salt = salt;
144 return fast_hash;
145 }
146
147 bool VerifyFast(const fast_hash_t &fast_hash, const SizedBuffer &password) {
148 fast_hash_t computed = ComputeFastHash(password, fast_hash.salt);
149 return memcmp(computed.digest, fast_hash.digest, SHA256_DIGEST_LENGTH) == 0;
150 }
151
152 bool DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
153 FastHashMap::const_iterator it = fast_hash_map_.find(expected_handle->user_id);
Andres Morales9ea9a062015-06-23 11:27:09 -0700154 if (it != fast_hash_map_.end() && VerifyFast(it->second, password)) {
155 return true;
Andres Moralesc7ab1e82015-06-22 08:24:45 -0700156 } else {
157 if (GateKeeper::DoVerify(expected_handle, password)) {
158 uint64_t salt;
159 GetRandom(&salt, sizeof(salt));
160 fast_hash_map_[expected_handle->user_id] = ComputeFastHash(password, salt);
161 return true;
162 }
163 }
164
165 return false;
166 }
167
Andres Moralesae242922015-05-18 09:26:19 -0700168private:
Andres Moralesc7ab1e82015-06-22 08:24:45 -0700169
170 typedef std::unordered_map<uint32_t, failure_record_t> FailureRecordMap;
171 typedef std::unordered_map<uint64_t, fast_hash_t> FastHashMap;
172
Andres Moralese1f827f2015-06-01 09:59:05 -0700173 UniquePtr<uint8_t[]> key_;
Andres Moralesc7ab1e82015-06-22 08:24:45 -0700174 FailureRecordMap failure_map_;
175 FastHashMap fast_hash_map_;
Andres Moralesae242922015-05-18 09:26:19 -0700176};
177}
178
179#endif // SOFT_GATEKEEPER_H_
180