pkcs1/private_key/
other_prime_info.rs

1//! PKCS#1 OtherPrimeInfo support.
2
3use der::{
4    asn1::UintRef, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Writer,
5};
6
7/// PKCS#1 OtherPrimeInfo as defined in [RFC 8017 Appendix 1.2].
8///
9/// ASN.1 structure containing an additional prime in a multi-prime RSA key.
10///
11/// ```text
12/// OtherPrimeInfo ::= SEQUENCE {
13///     prime             INTEGER,  -- ri
14///     exponent          INTEGER,  -- di
15///     coefficient       INTEGER   -- ti
16/// }
17/// ```
18///
19/// [RFC 8017 Appendix 1.2]: https://datatracker.ietf.org/doc/html/rfc8017#appendix-A.1.2
20#[derive(Clone)]
21pub struct OtherPrimeInfo<'a> {
22    /// Prime factor `r_i` of `n`, where `i` >= 3.
23    pub prime: UintRef<'a>,
24
25    /// Exponent: `d_i = d mod (r_i - 1)`.
26    pub exponent: UintRef<'a>,
27
28    /// CRT coefficient: `t_i = (r_1 * r_2 * ... * r_(i-1))^(-1) mod r_i`.
29    pub coefficient: UintRef<'a>,
30}
31
32impl<'a> DecodeValue<'a> for OtherPrimeInfo<'a> {
33    fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
34        reader.read_nested(header.length, |reader| {
35            Ok(Self {
36                prime: reader.decode()?,
37                exponent: reader.decode()?,
38                coefficient: reader.decode()?,
39            })
40        })
41    }
42}
43
44impl EncodeValue for OtherPrimeInfo<'_> {
45    fn value_len(&self) -> der::Result<Length> {
46        self.prime.encoded_len()? + self.exponent.encoded_len()? + self.coefficient.encoded_len()?
47    }
48
49    fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
50        self.prime.encode(writer)?;
51        self.exponent.encode(writer)?;
52        self.coefficient.encode(writer)?;
53        Ok(())
54    }
55}
56
57impl<'a> Sequence<'a> for OtherPrimeInfo<'a> {}