padding_key.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "storage/common/quota/padding_key.h"
  5. #include <inttypes.h>
  6. #include <cstdint>
  7. #include <vector>
  8. #include "base/no_destructor.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "base/time/time.h"
  11. #include "crypto/hmac.h"
  12. #include "crypto/random.h"
  13. #include "crypto/symmetric_key.h"
  14. #include "net/base/schemeful_site.h"
  15. #include "net/http/http_request_headers.h"
  16. #include "services/network/public/mojom/url_response_head.mojom-shared.h"
  17. using crypto::SymmetricKey;
  18. namespace storage {
  19. namespace {
  20. const SymmetricKey::Algorithm kPaddingKeyAlgorithm = SymmetricKey::AES;
  21. // The range of the padding added to response sizes for opaque resources.
  22. // Increment the CacheStorage padding version if changed.
  23. constexpr uint64_t kPaddingRange = 14431 * 1024;
  24. std::unique_ptr<SymmetricKey>* GetPaddingKeyInternal() {
  25. static base::NoDestructor<std::unique_ptr<SymmetricKey>> s_padding_key([] {
  26. return SymmetricKey::GenerateRandomKey(kPaddingKeyAlgorithm, 128);
  27. }());
  28. return s_padding_key.get();
  29. }
  30. } // namespace
  31. bool ShouldPadResponseType(network::mojom::FetchResponseType type) {
  32. return type == network::mojom::FetchResponseType::kOpaque ||
  33. type == network::mojom::FetchResponseType::kOpaqueRedirect;
  34. }
  35. int64_t ComputeRandomResponsePadding() {
  36. uint64_t raw_random = 0;
  37. crypto::RandBytes(&raw_random, sizeof(uint64_t));
  38. return raw_random % kPaddingRange;
  39. }
  40. int64_t ComputeStableResponsePadding(const url::Origin& origin,
  41. const std::string& response_url,
  42. const base::Time& response_time,
  43. const std::string& request_method,
  44. int64_t side_data_size) {
  45. DCHECK(!response_url.empty());
  46. net::SchemefulSite site(origin);
  47. DCHECK_GT(response_time, base::Time::UnixEpoch());
  48. int64_t microseconds =
  49. (response_time - base::Time::UnixEpoch()).InMicroseconds();
  50. // It should only be possible to have a CORS safelisted method here since
  51. // the spec does not permit other methods for no-cors requests.
  52. DCHECK(request_method == net::HttpRequestHeaders::kGetMethod ||
  53. request_method == net::HttpRequestHeaders::kHeadMethod ||
  54. request_method == net::HttpRequestHeaders::kPostMethod);
  55. std::string key = base::StringPrintf(
  56. "%s-%" PRId64 "-%s-%s-%" PRId64, response_url.c_str(), microseconds,
  57. site.Serialize().c_str(), request_method.c_str(), side_data_size);
  58. crypto::HMAC hmac(crypto::HMAC::SHA256);
  59. CHECK(hmac.Init(GetPaddingKeyInternal()->get()));
  60. uint64_t digest_start = 0;
  61. CHECK(hmac.Sign(key, reinterpret_cast<uint8_t*>(&digest_start),
  62. sizeof(digest_start)));
  63. return digest_start % kPaddingRange;
  64. }
  65. } // namespace storage