blob: 1ae45e6333614ec8e3a20624145aa740ebfb1301 [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>
23#include <crypto_scrypt.h>
24}
25
26#include <UniquePtr.h>
27#include <gatekeeper/gatekeeper.h>
28#include <iostream>
29#include <unordered_map>
30
31namespace gatekeeper {
32
33
34class SoftGateKeeper : public GateKeeper {
35public:
36 static const uint32_t SIGNATURE_LENGTH_BYTES = 32;
37
38 // scrypt params
39 static const uint64_t N = 16384;
40 static const uint32_t r = 8;
41 static const uint32_t p = 1;
42
43 static const int MAX_UINT_32_CHARS = 11;
44
45 SoftGateKeeper() {
46 key_.reset(new uint8_t[SIGNATURE_LENGTH_BYTES]);
47 memset(key_.get(), 0, SIGNATURE_LENGTH_BYTES);
48 }
49
50 virtual ~SoftGateKeeper() {
51 }
52
53 virtual bool GetAuthTokenKey(const uint8_t **auth_token_key,
54 uint32_t *length) const {
55 if (auth_token_key == NULL || length == NULL) return false;
56 *auth_token_key = const_cast<const uint8_t *>(key_.get());
57 *length = SIGNATURE_LENGTH_BYTES;
58 return true;
59 }
60
61 virtual void GetPasswordKey(const uint8_t **password_key, uint32_t *length) {
62 if (password_key == NULL || length == NULL) return;
63 *password_key = const_cast<const uint8_t *>(key_.get());
64 *length = SIGNATURE_LENGTH_BYTES;
65 }
66
67 virtual void ComputePasswordSignature(uint8_t *signature, uint32_t signature_length,
68 const uint8_t *, uint32_t, const uint8_t *password,
69 uint32_t password_length, salt_t salt) const {
70 if (signature == NULL) return;
71 crypto_scrypt(password, password_length, reinterpret_cast<uint8_t *>(&salt),
72 sizeof(salt), N, r, p, signature, signature_length);
73 }
74
75 virtual void GetRandom(void *random, uint32_t requested_length) const {
76 if (random == NULL) return;
77 RAND_pseudo_bytes((uint8_t *) random, requested_length);
78 }
79
80 virtual void ComputeSignature(uint8_t *signature, uint32_t signature_length,
81 const uint8_t *, uint32_t, const uint8_t *, const uint32_t) const {
82 if (signature == NULL) return;
83 memset(signature, 0, signature_length);
84 }
85
86 virtual uint64_t GetMillisecondsSinceBoot() const {
87 struct timespec time;
88 int res = clock_gettime(CLOCK_BOOTTIME, &time);
89 if (res < 0) return 0;
90 return (time.tv_sec * 1000) + (time.tv_nsec / 1000 / 1000);
91 }
92
93 virtual bool IsHardwareBacked() const {
94 return false;
95 }
96
97 virtual bool GetFailureRecord(uint32_t uid, secure_id_t user_id, failure_record_t *record) {
98 failure_record_t *stored = &failure_map_[uid];
99 if (user_id != stored->secure_user_id) {
100 stored->secure_user_id = user_id;
101 stored->last_checked_timestamp = 0;
102 stored->failure_counter = 0;
103 }
104 memcpy(record, stored, sizeof(*record));
105 return true;
106 }
107
108 virtual void ClearFailureRecord(uint32_t uid, secure_id_t user_id) {
109 failure_record_t *stored = &failure_map_[uid];
110 stored->secure_user_id = user_id;
111 stored->last_checked_timestamp = 0;
112 stored->failure_counter = 0;
113 }
114
115 virtual bool WriteFailureRecord(uint32_t uid, failure_record_t *record) {
116 failure_map_[uid] = *record;
117 return true;
118 }
119
120private:
121 UniquePtr<uint8_t> key_;
122 std::unordered_map<uint32_t, failure_record_t> failure_map_;
123};
124}
125
126#endif // SOFT_GATEKEEPER_H_
127