/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/src/ec/keys.rs
Line | Count | Source |
1 | | use super::{Curve, ELEM_MAX_BYTES, SEED_MAX_BYTES}; |
2 | | use crate::{cpu, error, rand}; |
3 | | |
4 | | pub struct KeyPair { |
5 | | seed: Seed, |
6 | | public_key: PublicKey, |
7 | | } |
8 | | |
9 | | impl KeyPair { |
10 | 0 | pub(super) fn derive( |
11 | 0 | seed: Seed, |
12 | 0 | cpu_features: cpu::Features, |
13 | 0 | ) -> Result<Self, error::Unspecified> { |
14 | 0 | let public_key = seed.compute_public_key(cpu_features)?; |
15 | 0 | Ok(Self { seed, public_key }) |
16 | 0 | } |
17 | | |
18 | 0 | pub fn public_key(&self) -> &PublicKey { |
19 | 0 | &self.public_key |
20 | 0 | } |
21 | 0 | pub fn split(self) -> (Seed, PublicKey) { |
22 | 0 | (self.seed, self.public_key) |
23 | 0 | } |
24 | | } |
25 | | |
26 | | pub struct Seed { |
27 | | bytes: [u8; SEED_MAX_BYTES], |
28 | | curve: &'static Curve, |
29 | | } |
30 | | |
31 | | impl Seed { |
32 | 0 | pub(crate) fn generate( |
33 | 0 | curve: &'static Curve, |
34 | 0 | rng: &dyn rand::SecureRandom, |
35 | 0 | cpu: cpu::Features, |
36 | 0 | ) -> Result<Self, error::Unspecified> { |
37 | 0 | let mut r = Self { |
38 | 0 | bytes: [0u8; SEED_MAX_BYTES], |
39 | 0 | curve, |
40 | 0 | }; |
41 | 0 | (curve.generate_private_key)(rng, &mut r.bytes[..curve.elem_scalar_seed_len], cpu)?; |
42 | 0 | Ok(r) |
43 | 0 | } |
44 | | |
45 | 0 | pub(crate) fn from_bytes( |
46 | 0 | curve: &'static Curve, |
47 | 0 | bytes: untrusted::Input, |
48 | 0 | cpu: cpu::Features, |
49 | 0 | ) -> Result<Self, error::Unspecified> { |
50 | 0 | let bytes = bytes.as_slice_less_safe(); |
51 | 0 | if curve.elem_scalar_seed_len != bytes.len() { |
52 | 0 | return Err(error::Unspecified); |
53 | 0 | } |
54 | 0 | (curve.check_private_key_bytes)(bytes, cpu)?; |
55 | 0 | let mut r = Self { |
56 | 0 | bytes: [0; SEED_MAX_BYTES], |
57 | 0 | curve, |
58 | 0 | }; |
59 | 0 | r.bytes[..curve.elem_scalar_seed_len].copy_from_slice(bytes); |
60 | 0 | Ok(r) |
61 | 0 | } |
62 | | |
63 | 0 | pub fn bytes_less_safe(&self) -> &[u8] { |
64 | 0 | &self.bytes[..self.curve.elem_scalar_seed_len] |
65 | 0 | } |
66 | | |
67 | 0 | pub(crate) fn compute_public_key( |
68 | 0 | &self, |
69 | 0 | cpu_features: cpu::Features, |
70 | 0 | ) -> Result<PublicKey, error::Unspecified> { |
71 | 0 | let mut public_key = PublicKey { |
72 | 0 | bytes: [0u8; PUBLIC_KEY_MAX_LEN], |
73 | 0 | len: self.curve.public_key_len, |
74 | 0 | }; |
75 | 0 | (self.curve.public_from_private)( |
76 | 0 | &mut public_key.bytes[..public_key.len], |
77 | 0 | self, |
78 | 0 | cpu_features, |
79 | 0 | )?; |
80 | 0 | Ok(public_key) |
81 | 0 | } |
82 | | } |
83 | | |
84 | | #[derive(Copy, Clone)] |
85 | | pub struct PublicKey { |
86 | | bytes: [u8; PUBLIC_KEY_MAX_LEN], |
87 | | len: usize, |
88 | | } |
89 | | |
90 | | impl AsRef<[u8]> for PublicKey { |
91 | 0 | fn as_ref(&self) -> &[u8] { |
92 | 0 | &self.bytes[..self.len] |
93 | 0 | } |
94 | | } |
95 | | |
96 | | /// The maximum length, in bytes, of an encoded public key. |
97 | | pub const PUBLIC_KEY_MAX_LEN: usize = 1 + (2 * ELEM_MAX_BYTES); |