Skip to main content

tor_hscrypto/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45#![allow(clippy::collapsible_if)] // See arti#2342
46#![deny(clippy::unused_async)]
47#![deny(clippy::string_slice)] // See arti#2571
48//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49#![allow(dead_code, unused_variables)]
50
51use rand::RngExt;
52
53mod macros;
54#[cfg(feature = "ope")]
55pub mod ope;
56pub mod ops;
57pub mod pk;
58pub mod pow;
59pub mod time;
60
61use macros::define_bytes;
62
63#[cfg(feature = "memquota-memcost")]
64use {derive_deftly::Deftly, tor_memquota::derive_deftly_template_HasMemoryCost};
65
66define_bytes! {
67/// A value to identify an onion service during a given period. (`N_hs_subcred`)
68///
69/// This is computed from the onion service's public ID and the blinded ID for
70/// the current time period.
71///
72/// Given this piece of information, the original public ID and blinded ID cannot
73/// be re-derived.
74#[derive(Copy, Clone, Debug)]
75pub struct Subcredential([u8; 32]);
76}
77
78/// Counts which revision of an onion service descriptor is which, within a
79/// given time period.
80///
81/// There can be gaps in this numbering. A descriptor with a higher-valued
82/// revision counter supersedes one with a lower revision counter.
83#[derive(
84    Copy,
85    Clone,
86    Debug,
87    Ord,
88    PartialOrd,
89    Eq,
90    PartialEq,
91    derive_more::Deref,
92    derive_more::From,
93    derive_more::Into,
94)]
95pub struct RevisionCounter(u64);
96
97/// Default number of introduction points a service should establish
98///
99/// Default value for `[NUM_INTRO_POINT]`, rend-spec-v3 2.5.4.
100//
101// TODO arguably these aren't "crypto" so should be in some currently non-existent tor-hscommon
102pub const NUM_INTRO_POINT_DEF: usize = 3;
103
104/// Maximum number of introduction points a service should establish and we should tolerate
105///
106/// Maximum value for `[NUM_INTRO_POINT]`, rend-spec-v3 2.5.4.
107pub const NUM_INTRO_POINT_MAX: usize = 20;
108
109/// Length of a `RENDEZVOUS` cookie
110const REND_COOKIE_LEN: usize = 20;
111
112define_bytes! {
113/// An opaque value `RENDEZVOUS_COOKIE` used at a rendezvous point to match clients and services.
114///
115/// See rend-spec-v3 s4.1.
116///
117/// The client includes this value to the rendezvous point in its
118/// `ESTABLISH_RENDEZVOUS` message; the service later provides the same value in its
119/// `RENDEZVOUS1` message.
120#[derive(Copy, Clone, Debug, Eq, PartialEq)]
121#[cfg_attr(
122    feature = "memquota-memcost",
123    derive(Deftly),
124    derive_deftly(HasMemoryCost),
125)]
126pub struct RendCookie([u8; REND_COOKIE_LEN]);
127}
128
129impl rand::distr::Distribution<RendCookie> for rand::distr::StandardUniform {
130    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> RendCookie {
131        RendCookie(rng.random::<[u8; REND_COOKIE_LEN]>().into())
132    }
133}