• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2017 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 use crate::{error, rand};
16 
17 pub use self::keys::{KeyPair, PublicKey, Seed};
18 
19 pub struct Curve {
20     pub public_key_len: usize,
21     pub elem_scalar_seed_len: usize,
22 
23     pub id: CurveID,
24 
25     // Precondition: `bytes` is the correct length.
26     check_private_key_bytes: fn(bytes: &[u8]) -> Result<(), error::Unspecified>,
27 
28     generate_private_key:
29         fn(rng: &dyn rand::SecureRandom, &mut [u8]) -> Result<(), error::Unspecified>,
30 
31     public_from_private:
32         fn(public_out: &mut [u8], private_key: &Seed) -> Result<(), error::Unspecified>,
33 }
34 
35 derive_debug_via_id!(Curve);
36 
37 #[derive(Clone, Copy, Debug, PartialEq)]
38 pub enum CurveID {
39     #[cfg(not(target_arch = "wasm32"))]
40     Curve25519,
41     P256,
42     P384,
43 }
44 
45 const ELEM_MAX_BITS: usize = 384;
46 pub const ELEM_MAX_BYTES: usize = (ELEM_MAX_BITS + 7) / 8;
47 
48 pub const SCALAR_MAX_BYTES: usize = ELEM_MAX_BYTES;
49 const SEED_MAX_BYTES: usize = ELEM_MAX_BYTES;
50 
51 /// The maximum length of a PKCS#8 documents generated by *ring* for ECC keys.
52 ///
53 /// This is NOT the maximum length of a PKCS#8 document that can be consumed by
54 /// `pkcs8::unwrap_key()`.
55 ///
56 /// `40` is the length of the P-384 template. It is actually one byte shorter
57 /// than the P-256 template, but the private key and the public key are much
58 /// longer.
59 pub const PKCS8_DOCUMENT_MAX_LEN: usize = 40 + SCALAR_MAX_BYTES + keys::PUBLIC_KEY_MAX_LEN;
60 
61 pub mod curve25519;
62 mod keys;
63 pub mod suite_b;
64