tink_core/
lib.rs

1// Copyright 2020 The Tink-Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17//! Core crate for Tink.
18
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![deny(broken_intra_doc_links)]
21
22pub mod cryptofmt;
23pub mod keyset;
24pub mod primitiveset;
25pub mod registry;
26pub mod subtle;
27pub mod utils;
28pub use utils::TinkError;
29
30/// The [upstream Tink](https://github.com/google/tink) version that this Rust
31/// port is based on.
32pub const UPSTREAM_VERSION: &str = "1.6.0";
33
34/// Type alias for `u32` values being used as key identifiers.
35pub type KeyId = u32;
36
37// Traits for primitives.
38mod aead;
39pub use aead::*;
40mod deterministic_aead;
41pub use deterministic_aead::*;
42mod hybrid_decrypt;
43pub use hybrid_decrypt::*;
44mod hybrid_encrypt;
45pub use hybrid_encrypt::*;
46mod mac;
47pub use mac::*;
48mod prf;
49pub use prf::*;
50mod signer;
51pub use signer::*;
52mod streamingaead;
53pub use streamingaead::*;
54mod verifier;
55pub use verifier::*;
56
57/// The primitives available in Tink.
58pub enum Primitive {
59    Aead(Box<dyn Aead>),
60    DeterministicAead(Box<dyn DeterministicAead>),
61    HybridDecrypt(Box<dyn HybridDecrypt>),
62    HybridEncrypt(Box<dyn HybridEncrypt>),
63    Mac(Box<dyn Mac>),
64    Prf(Box<dyn Prf>),
65    Signer(Box<dyn Signer>),
66    StreamingAead(Box<dyn StreamingAead>),
67    Verifier(Box<dyn Verifier>),
68}
69
70/// Manual implementation of the [`Clone`] trait, which makes use of the trait bounds
71/// on the individual primitive types; specifically that they provide a `box_clone()`
72/// method.
73impl Clone for Primitive {
74    fn clone(&self) -> Self {
75        match self {
76            Primitive::Aead(p) => Primitive::Aead(p.box_clone()),
77            Primitive::DeterministicAead(p) => Primitive::DeterministicAead(p.box_clone()),
78            Primitive::HybridDecrypt(p) => Primitive::HybridDecrypt(p.box_clone()),
79            Primitive::HybridEncrypt(p) => Primitive::HybridEncrypt(p.box_clone()),
80            Primitive::Mac(p) => Primitive::Mac(p.box_clone()),
81            Primitive::Prf(p) => Primitive::Prf(p.box_clone()),
82            Primitive::Signer(p) => Primitive::Signer(p.box_clone()),
83            Primitive::StreamingAead(p) => Primitive::StreamingAead(p.box_clone()),
84            Primitive::Verifier(p) => Primitive::Verifier(p.box_clone()),
85        }
86    }
87}
88
89// Conversions from the [`Primitive`] `enum` wrapper to specific primitive types.  Will panic if the
90// wrong type is passed in.
91
92impl From<Primitive> for Box<dyn Aead> {
93    fn from(p: Primitive) -> Box<dyn Aead> {
94        match p {
95            Primitive::Aead(p) => p,
96            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
97        }
98    }
99}
100
101impl From<Primitive> for Box<dyn DeterministicAead> {
102    fn from(p: Primitive) -> Box<dyn DeterministicAead> {
103        match p {
104            Primitive::DeterministicAead(p) => p,
105            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
106        }
107    }
108}
109
110impl From<Primitive> for Box<dyn HybridDecrypt> {
111    fn from(p: Primitive) -> Box<dyn HybridDecrypt> {
112        match p {
113            Primitive::HybridDecrypt(p) => p,
114            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
115        }
116    }
117}
118
119impl From<Primitive> for Box<dyn HybridEncrypt> {
120    fn from(p: Primitive) -> Box<dyn HybridEncrypt> {
121        match p {
122            Primitive::HybridEncrypt(p) => p,
123            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
124        }
125    }
126}
127
128impl From<Primitive> for Box<dyn Mac> {
129    fn from(p: Primitive) -> Box<dyn Mac> {
130        match p {
131            Primitive::Mac(p) => p,
132            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
133        }
134    }
135}
136
137impl From<Primitive> for Box<dyn Prf> {
138    fn from(p: Primitive) -> Box<dyn Prf> {
139        match p {
140            Primitive::Prf(p) => p,
141            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
142        }
143    }
144}
145
146impl From<Primitive> for Box<dyn Signer> {
147    fn from(p: Primitive) -> Box<dyn Signer> {
148        match p {
149            Primitive::Signer(p) => p,
150            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
151        }
152    }
153}
154
155impl From<Primitive> for Box<dyn StreamingAead> {
156    fn from(p: Primitive) -> Box<dyn StreamingAead> {
157        match p {
158            Primitive::StreamingAead(p) => p,
159            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
160        }
161    }
162}
163
164impl From<Primitive> for Box<dyn Verifier> {
165    fn from(p: Primitive) -> Box<dyn Verifier> {
166        match p {
167            Primitive::Verifier(p) => p,
168            _ => panic!("attempt to convert wrong primitive type"), // safe: precondition
169        }
170    }
171}