SoftGateKeeper.h 5.7 KB

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