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