ring/
ec.rs

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 AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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
15use crate::{cpu, error, rand};
16
17pub use self::keys::{KeyPair, PublicKey, Seed};
18
19pub 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
41derive_debug_via_id!(Curve);
42
43#[derive(Clone, Copy, Debug, PartialEq)]
44pub enum CurveID {
45    Curve25519,
46    P256,
47    P384,
48}
49
50const ELEM_MAX_BITS: usize = 384;
51pub const ELEM_MAX_BYTES: usize = (ELEM_MAX_BITS + 7) / 8;
52
53pub const SCALAR_MAX_BYTES: usize = ELEM_MAX_BYTES;
54const 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.
64pub const PKCS8_DOCUMENT_MAX_LEN: usize = 40 + SCALAR_MAX_BYTES + keys::PUBLIC_KEY_MAX_LEN;
65
66pub mod curve25519;
67mod keys;
68pub mod suite_b;