Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/browsing_topics/util.h" |
| 6 | |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Elly | 4b270c4 | 2024-11-26 20:09:04 | [diff] [blame] | 9 | #include "base/numerics/byte_conversions.h" |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 10 | #include "base/rand_util.h" |
Elly | 15dee24d | 2025-05-10 01:10:23 | [diff] [blame] | 11 | #include "crypto/hash.h" |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 12 | #include "crypto/hmac.h" |
Yao Xiao | c95d315 | 2022-03-18 03:43:22 | [diff] [blame] | 13 | #include "third_party/blink/public/common/features.h" |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 14 | |
| 15 | namespace browsing_topics { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Note that updating the use case prefixes below will change the pre-existing |
| 20 | // per-user stickiness. Some of the derived data may already have been persisted |
| 21 | // elsewhere. Be sure you are aware of the implications before updating those |
| 22 | // strings. Note also that the version here is just about the hash method, and |
| 23 | // is distinctive from the broader configuration version of the Topics API. |
| 24 | const char kRandomOrTopTopicDecisionPrefix[] = |
| 25 | "TopicsV1_RandomOrTopTopicDecision|"; |
| 26 | const char kRandomTopicIndexDecisionPrefix[] = |
| 27 | "TopicsV1_RandomTopicIndexDecision|"; |
| 28 | const char kTopTopicIndexDecisionPrefix[] = "TopicsV1_TopTopicIndexDecision|"; |
Yao Xiao | 7d28788 | 2024-09-28 16:22:31 | [diff] [blame] | 29 | const char kEpochIntroductionTimeDecisionPrefix[] = |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 30 | "TopicsV1_EpochSwitchTimeDecision|"; |
Yao Xiao | 7d28788 | 2024-09-28 16:22:31 | [diff] [blame] | 31 | const char kEpochPhaseOutTimeDecisionPrefix[] = |
| 32 | "TopicsV1_EpochPhaseOutTimeDecision|"; |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 33 | const char kContextDomainStoragePrefix[] = "TopicsV1_ContextDomainStorage|"; |
Yao Xiao | c95d315 | 2022-03-18 03:43:22 | [diff] [blame] | 34 | const char kMainFrameHostStoragePrefix[] = "TopicsV1_MainFrameHostStorage|"; |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 35 | |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 36 | uint64_t HmacHash(ReadOnlyHmacKey hmac_key, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 37 | const std::string& use_case_prefix, |
| 38 | const std::string& data) { |
Elly | 4b270c4 | 2024-11-26 20:09:04 | [diff] [blame] | 39 | auto hash = crypto::hmac::SignSha256( |
| 40 | hmac_key, base::as_byte_span(use_case_prefix + data)); |
| 41 | return base::U64FromNativeEndian(base::span(hash).first<8u>()); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 42 | } |
| 43 | |
Peter Kasting | 4f54ba6 | 2022-10-29 01:45:01 | [diff] [blame] | 44 | bool g_hmac_key_overridden = false; |
danakj | 95305d27 | 2024-05-09 20:38:44 | [diff] [blame] | 45 | |
Peter Kasting | 4f54ba6 | 2022-10-29 01:45:01 | [diff] [blame] | 46 | browsing_topics::HmacKey& GetHmacKeyOverrideForTesting() { |
| 47 | static browsing_topics::HmacKey key; |
| 48 | return key; |
| 49 | } |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 50 | |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 51 | std::string GetEpochId(base::Time epoch_calculation_time) { |
| 52 | int64_t time_microseconds = |
| 53 | epoch_calculation_time.ToDeltaSinceWindowsEpoch().InMicroseconds(); |
| 54 | |
| 55 | return std::string(reinterpret_cast<const char*>(&time_microseconds), |
| 56 | sizeof(time_microseconds)); |
| 57 | } |
| 58 | |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 59 | } // namespace |
| 60 | |
Yao Xiao | a7cada47 | 2024-04-11 20:44:19 | [diff] [blame] | 61 | bool DoesCalculationFailDueToHanging(CalculatorResultStatus status) { |
| 62 | return status == CalculatorResultStatus::kHangingAfterApiUsageRequested || |
| 63 | status == CalculatorResultStatus::kHangingAfterHistoryRequested || |
| 64 | status == CalculatorResultStatus::kHangingAfterModelRequested || |
| 65 | status == CalculatorResultStatus::kHangingAfterAnnotationRequested; |
| 66 | } |
| 67 | |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 68 | HmacKey GenerateRandomHmacKey() { |
Peter Kasting | 4f54ba6 | 2022-10-29 01:45:01 | [diff] [blame] | 69 | if (g_hmac_key_overridden) |
| 70 | return GetHmacKeyOverrideForTesting(); |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 71 | |
| 72 | HmacKey result = {}; |
danakj | 95305d27 | 2024-05-09 20:38:44 | [diff] [blame] | 73 | base::RandBytes(result); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | uint64_t HashTopDomainForRandomOrTopTopicDecision( |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 79 | ReadOnlyHmacKey hmac_key, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 80 | base::Time epoch_calculation_time, |
| 81 | const std::string& top_domain) { |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 82 | return HmacHash(hmac_key, kRandomOrTopTopicDecisionPrefix, |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 83 | GetEpochId(epoch_calculation_time) + top_domain); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | uint64_t HashTopDomainForRandomTopicIndexDecision( |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 87 | ReadOnlyHmacKey hmac_key, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 88 | base::Time epoch_calculation_time, |
| 89 | const std::string& top_domain) { |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 90 | return HmacHash(hmac_key, kRandomTopicIndexDecisionPrefix, |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 91 | GetEpochId(epoch_calculation_time) + top_domain); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | uint64_t HashTopDomainForTopTopicIndexDecision( |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 95 | ReadOnlyHmacKey hmac_key, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 96 | base::Time epoch_calculation_time, |
| 97 | const std::string& top_domain) { |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 98 | return HmacHash(hmac_key, kTopTopicIndexDecisionPrefix, |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 99 | GetEpochId(epoch_calculation_time) + top_domain); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 100 | } |
| 101 | |
Yao Xiao | 7d28788 | 2024-09-28 16:22:31 | [diff] [blame] | 102 | uint64_t HashTopDomainForEpochIntroductionTimeDecision( |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 103 | ReadOnlyHmacKey hmac_key, |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 104 | base::Time epoch_calculation_time, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 105 | const std::string& top_domain) { |
Yao Xiao | 7d28788 | 2024-09-28 16:22:31 | [diff] [blame] | 106 | return HmacHash(hmac_key, kEpochIntroductionTimeDecisionPrefix, |
| 107 | GetEpochId(epoch_calculation_time) + top_domain); |
| 108 | } |
| 109 | |
| 110 | uint64_t HashTopDomainForEpochPhaseOutTimeDecision( |
| 111 | ReadOnlyHmacKey hmac_key, |
| 112 | base::Time epoch_calculation_time, |
| 113 | const std::string& top_domain) { |
| 114 | return HmacHash(hmac_key, kEpochPhaseOutTimeDecisionPrefix, |
Yao Xiao | 63cc1fc | 2024-09-25 17:21:51 | [diff] [blame] | 115 | GetEpochId(epoch_calculation_time) + top_domain); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 116 | } |
| 117 | |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 118 | HashedDomain HashContextDomainForStorage(ReadOnlyHmacKey hmac_key, |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 119 | const std::string& context_domain) { |
| 120 | return HashedDomain( |
| 121 | HmacHash(hmac_key, kContextDomainStoragePrefix, context_domain)); |
| 122 | } |
| 123 | |
Yao Xiao | c95d315 | 2022-03-18 03:43:22 | [diff] [blame] | 124 | HashedHost HashMainFrameHostForStorage(const std::string& main_frame_host) { |
Elly | 15dee24d | 2025-05-10 01:10:23 | [diff] [blame] | 125 | auto hash = |
| 126 | crypto::hash::Sha256(kMainFrameHostStoragePrefix + main_frame_host); |
| 127 | int64_t result = base::I64FromNativeEndian(base::span(hash).first<8u>()); |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 128 | return HashedHost(result); |
| 129 | } |
| 130 | |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 131 | void OverrideHmacKeyForTesting(ReadOnlyHmacKey hmac_key) { |
Peter Kasting | 4f54ba6 | 2022-10-29 01:45:01 | [diff] [blame] | 132 | g_hmac_key_overridden = true; |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 133 | std::ranges::copy(hmac_key, GetHmacKeyOverrideForTesting().begin()); |
Yao Xiao | 0cb3fc1 | 2022-03-16 00:30:38 | [diff] [blame] | 134 | } |
| 135 | |
Yao Xiao | dc070d1 | 2022-03-08 21:19:13 | [diff] [blame] | 136 | } // namespace browsing_topics |