mundane/public/rsa/
bits.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::fmt::{self, Debug, Display, Formatter};
6
7use util::Sealed;
8
9/// The bit length of an RSA key.
10///
11/// The [`RsaPrivKey`] and [`RsaPubKey`] types take a `B: RsaKeyBits` type
12/// parameter indicating the key's length in bits.
13///
14/// We only support bit lengths of 2048 or greater, as smaller bit lengths are
15/// considered insecure. If 2048 is considered insecure at some point in the
16/// future, then we will remove support for it, which will be a breaking change.
17///
18/// [`RsaPrivKey`]: ::public::rsa::RsaPrivKey
19/// [`RsaPubKey`]: ::public::rsa::RsaPubKey
20pub trait RsaKeyBits: Sized + Copy + Clone + Default + Display + Debug + Sealed {
21    /// The number of bits.
22    const BITS: usize;
23}
24
25/// 2048 bits.
26///
27/// `B2048` indicates a 2048-bit RSA key; it implements [`RsaKeyBits`].
28#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
29pub struct B2048;
30
31/// 3072 bits.
32///
33/// `B3072` indicates a 3072-bit RSA key; it implements [`RsaKeyBits`].
34#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
35pub struct B3072;
36
37/// 4096 bits.
38///
39/// `B4096` indicates a 4096-bit RSA key; it implements [`RsaKeyBits`].
40#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
41pub struct B4096;
42
43/// 6144 bits.
44///
45/// `B6144` indicates a 6144-bit RSA key; it implements [`RsaKeyBits`].
46#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
47pub struct B6144;
48
49/// 8192 bits.
50///
51/// `B8192` indicates a 8192-bit RSA key; it implements [`RsaKeyBits`].
52#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
53pub struct B8192;
54
55macro_rules! impl_bits {
56    ($name:ident, $bits:expr) => {
57        impl RsaKeyBits for $name {
58            const BITS: usize = $bits;
59        }
60
61        impl Display for $name {
62            fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
63                write!(f, "{} bits", $bits)
64            }
65        }
66
67        impl Sealed for $name {}
68    };
69}
70
71impl_bits!(B2048, 2048);
72impl_bits!(B3072, 3072);
73impl_bits!(B4096, 4096);
74impl_bits!(B6144, 6144);
75impl_bits!(B8192, 8192);