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 | Curve25519, |
40 | P256, |
41 | P384, |
42 | } |
43 | |
44 | const ELEM_MAX_BITS: usize = 384; |
45 | pub const ELEM_MAX_BYTES: usize = (ELEM_MAX_BITS + 7) / 8; |
46 | |
47 | pub const SCALAR_MAX_BYTES: usize = ELEM_MAX_BYTES; |
48 | const SEED_MAX_BYTES: usize = ELEM_MAX_BYTES; |
49 | |
50 | /// The maximum length of a PKCS#8 documents generated by *ring* for ECC keys. |
51 | /// |
52 | /// This is NOT the maximum length of a PKCS#8 document that can be consumed by |
53 | /// `pkcs8::unwrap_key()`. |
54 | /// |
55 | /// `40` is the length of the P-384 template. It is actually one byte shorter |
56 | /// than the P-256 template, but the private key and the public key are much |
57 | /// longer. |
58 | pub const PKCS8_DOCUMENT_MAX_LEN: usize = 40 + SCALAR_MAX_BYTES + keys::PUBLIC_KEY_MAX_LEN; |
59 | |
60 | pub mod curve25519; |
61 | mod keys; |
62 | pub mod suite_b; |
63 | |