1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Set of libraries for privacy-preserving networking apps
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2023 by
//     Dr. Maxim Orlovsky <orlovsky@cyphernet.org>
//
// Copyright 2022-2023 Cyphernet DAO, Switzerland
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(docsrs, feature(doc_auto_cfg))]

//! Implementation-independent abstractions for main cryptographic algorithms
//! used for end-to-end encryption and authorization.

#[macro_use]
extern crate amplify;

mod digest;
#[cfg(feature = "x25519")]
pub mod x25519;
#[cfg(feature = "ed25519")]
pub mod ed25519;

pub mod display;

use std::fmt::Debug;

pub use digest::*;

use crate::display::{Encoding, MultiDisplay};

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From, Default)]
#[display("invalid secret key")]
#[non_exhaustive]
pub struct EcSkInvalid {}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From, Default)]
#[display("invalid public key")]
#[non_exhaustive]
pub struct EcPkInvalid {}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From, Default)]
#[display("invalid signature data")]
#[non_exhaustive]
pub struct EcSigInvalid {}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
#[non_exhaustive]
pub enum EcdhError {
    /// public key provided for the ECDH is a weak key
    WeakPk,

    #[display(inner)]
    #[from]
    InvalidPk(EcPkInvalid),

    #[display(inner)]
    #[from]
    InvalidSk(EcSkInvalid),
}

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum EcSerError {
    #[display(inner)]
    #[from]
    Io(amplify::IoError),

    /// public key has invalid length {0}
    InvalidKeyLength(usize),

    #[display(inner)]
    #[from]
    InvalidKey(EcPkInvalid),

    /// error parsing encoding: {0}
    DataEncoding(String),
}

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum EcVerifyError {
    /// public key provided for the ECDH is weak key
    WeakPk,

    #[display(inner)]
    #[from]
    InvalidPk(EcPkInvalid),

    #[display(inner)]
    #[from]
    InvalidSignature(EcSigInvalid),

    /// the provided signature does not matches public key or is not valid for
    /// the given message
    SignatureMismatch,
}

/// Elliptic-curve based public key type which can be used in ECDH or signature schemes.
///
/// # Safety
///
/// The type provides no guarantees on the key validity upon deserialization.
pub trait EcPk: Clone + Eq + Send + Debug + MultiDisplay<Encoding> {
    const COMPRESSED_LEN: usize;
    const CURVE_NAME: &'static str;

    // TODO: When generic_const_exprs arrive switch to Self::COMPRESSED_LEN arrays
    type Compressed: Copy + Sized + Send + AsRef<[u8]>;

    fn base_point() -> Self;

    fn to_pk_compressed(&self) -> Self::Compressed;
    fn from_pk_compressed(pk: Self::Compressed) -> Result<Self, EcPkInvalid>;
    fn from_pk_compressed_slice(slice: &[u8]) -> Result<Self, EcPkInvalid>;
}

/// Elliptic-curve based private key type.
///
/// # Safety
///
/// The type provides no guarantees on the key validity upon deserialization.
pub trait EcSk: Clone + Eq + Send {
    type Pk: EcPk;

    fn generate_keypair() -> (Self, Self::Pk)
    where Self: Sized;
    fn to_pk(&self) -> Result<Self::Pk, EcSkInvalid>;
}

/// Marker trait for elliptic-curve based signatures
pub trait EcSig: Clone + Eq + Sized + Send + AsRef<[u8]> + Debug + MultiDisplay<Encoding> {
    const COMPRESSED_LEN: usize;

    type Pk: EcPk;
    // TODO: When generic_const_exprs arrive switch to Self::COMPRESSED_LEN arrays
    type Compressed: Copy + Sized + Send + AsRef<[u8]>;

    fn to_sig_compressed(&self) -> Self::Compressed;
    fn from_sig_compressed(sig: Self::Compressed) -> Result<Self, EcSigInvalid>;
    fn from_sig_compressed_slice(slice: &[u8]) -> Result<Self, EcSigInvalid>;

    fn verify(&self, pk: &Self::Pk, msg: impl AsRef<[u8]>) -> Result<(), EcVerifyError>;
}

/// Elliptic-curve based public key type which can be used for ECDH.
///
/// # Safety
///
/// The type provides no guarantees on the key validity upon deserialization.
pub trait Ecdh: EcSk {
    type SharedSecret: Copy + Eq + Sized + Send + AsRef<[u8]>;

    fn ecdh(&self, pk: &Self::Pk) -> Result<Self::SharedSecret, EcdhError>;
}

/// Signature scheme trait
pub trait EcSign: EcSk {
    type Sig: EcSig<Pk = Self::Pk>;

    fn cert(&self) -> Result<Cert<Self::Sig>, EcSkInvalid> {
        let pk = self.to_pk()?;
        let sig = self.sign(pk.to_pk_compressed());
        Ok(Cert { pk, sig })
    }
    fn sign(&self, msg: impl AsRef<[u8]>) -> Self::Sig;
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct CertFormat {
    pub enc: Encoding,
    pub sep: &'static str,
}

impl CertFormat {
    pub fn new(sep: &'static str, enc: Encoding) -> Self { CertFormat { enc, sep } }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Cert<S: EcSig> {
    pub pk: S::Pk,
    pub sig: S,
}

impl<S: EcSig> MultiDisplay<CertFormat> for Cert<S>
where
    S: MultiDisplay<Encoding>,
    S::Pk: MultiDisplay<Encoding>,
{
    type Display = String;
    fn display_fmt(&self, f: &CertFormat) -> Self::Display {
        format!("{}{}{}", self.pk.display_fmt(&f.enc), f.sep, self.sig.display_fmt(&f.enc))
    }
}

impl<S: EcSig> Cert<S> {
    pub fn verify(&self) -> Result<(), EcVerifyError> {
        self.sig.verify(&self.pk, self.pk.to_pk_compressed())
    }
}

#[cfg(feature = "ec25519")]
mod ec22519_err_convert {
    use ec25519::Error;

    use super::*;

    impl From<Error> for EcPkInvalid {
        fn from(err: Error) -> Self {
            match err {
                Error::InvalidPublicKey => EcPkInvalid {},

                Error::WeakPublicKey
                | Error::InvalidSecretKey
                | Error::SignatureMismatch
                | Error::InvalidSignature
                | Error::InvalidSeed
                | Error::InvalidBlind
                | Error::InvalidNoise
                | Error::ParseError
                | Error::NonCanonical => {
                    unreachable!("ECDH in ed25519-compact crate should not generate this errors")
                }
            }
        }
    }

    impl From<Error> for EcSkInvalid {
        fn from(err: Error) -> Self {
            match err {
                Error::InvalidSecretKey => EcSkInvalid {},

                Error::WeakPublicKey
                | Error::InvalidPublicKey
                | Error::SignatureMismatch
                | Error::InvalidSignature
                | Error::InvalidSeed
                | Error::InvalidBlind
                | Error::InvalidNoise
                | Error::ParseError
                | Error::NonCanonical => {
                    unreachable!("ECDH in ed25519-compact crate should not generate this errors")
                }
            }
        }
    }

    impl From<Error> for EcSerError {
        fn from(err: Error) -> Self { EcSerError::DataEncoding(err.to_string()) }
    }

    impl From<Error> for EcdhError {
        fn from(err: Error) -> Self {
            match err {
                Error::WeakPublicKey => EcdhError::WeakPk,
                Error::InvalidPublicKey => EcdhError::InvalidPk(EcPkInvalid {}),
                Error::InvalidSecretKey => EcdhError::InvalidSk(EcSkInvalid {}),

                Error::SignatureMismatch
                | Error::InvalidSignature
                | Error::InvalidSeed
                | Error::InvalidBlind
                | Error::InvalidNoise
                | Error::ParseError
                | Error::NonCanonical => {
                    unreachable!("ECDH in ed25519-compact crate should not generate this errors")
                }
            }
        }
    }

    impl From<Error> for EcVerifyError {
        fn from(err: Error) -> Self {
            match err {
                Error::WeakPublicKey => EcVerifyError::WeakPk,
                Error::InvalidPublicKey => EcVerifyError::InvalidPk(EcPkInvalid {}),
                Error::SignatureMismatch => EcVerifyError::SignatureMismatch,
                Error::InvalidSignature => EcVerifyError::InvalidSignature(EcSigInvalid {}),

                Error::InvalidSecretKey
                | Error::InvalidSeed
                | Error::InvalidBlind
                | Error::InvalidNoise
                | Error::ParseError
                | Error::NonCanonical => {
                    unreachable!("ECDH in ed25519-compact crate should not generate this errors")
                }
            }
        }
    }
}

#[cfg(feature = "multibase")]
impl From<multibase::Error> for EcSerError {
    fn from(err: multibase::Error) -> Self { EcSerError::DataEncoding(err.to_string()) }
}