Skip to main content

pkcs1/private_key/
other_prime_info.rs

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