| 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::{cpu, 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], cpu: cpu::Features) -> Result<(), error::Unspecified>, |
| 27 | |
| 28 | generate_private_key: fn( |
| 29 | rng: &dyn rand::SecureRandom, |
| 30 | &mut [u8], |
| 31 | cpu: cpu::Features, |
| 32 | ) -> Result<(), error::Unspecified>, |
| 33 | |
| 34 | public_from_private: fn( |
| 35 | public_out: &mut [u8], |
| 36 | private_key: &Seed, |
| 37 | cpu: cpu::Features, |
| 38 | ) -> Result<(), error::Unspecified>, |
| 39 | } |
| 40 | |
| 41 | derive_debug_via_id!(Curve); |
| 42 | |
| 43 | #[derive (Clone, Copy, Debug, PartialEq)] |
| 44 | pub enum CurveID { |
| 45 | Curve25519, |
| 46 | P256, |
| 47 | P384, |
| 48 | } |
| 49 | |
| 50 | const ELEM_MAX_BITS: usize = 384; |
| 51 | pub const ELEM_MAX_BYTES: usize = (ELEM_MAX_BITS + 7) / 8; |
| 52 | |
| 53 | pub const SCALAR_MAX_BYTES: usize = ELEM_MAX_BYTES; |
| 54 | const SEED_MAX_BYTES: usize = ELEM_MAX_BYTES; |
| 55 | |
| 56 | /// The maximum length of a PKCS#8 documents generated by *ring* for ECC keys. |
| 57 | /// |
| 58 | /// This is NOT the maximum length of a PKCS#8 document that can be consumed by |
| 59 | /// `pkcs8::unwrap_key()`. |
| 60 | /// |
| 61 | /// `40` is the length of the P-384 template. It is actually one byte shorter |
| 62 | /// than the P-256 template, but the private key and the public key are much |
| 63 | /// longer. |
| 64 | pub const PKCS8_DOCUMENT_MAX_LEN: usize = 40 + SCALAR_MAX_BYTES + keys::PUBLIC_KEY_MAX_LEN; |
| 65 | |
| 66 | pub mod curve25519; |
| 67 | mod keys; |
| 68 | pub mod suite_b; |
| 69 | |