Expand description
§RustCrypto: PKCS#1 (RSA)
Pure Rust implementation of Public-Key Cryptography Standards (PKCS) #1: RSA Cryptography Specifications Version 2.2 (RFC 8017).
§About
This crate supports encoding and decoding RSA private and public keys in either PKCS#1 DER (binary) or PEM (text) formats.
PEM encoded RSA private keys begin with:
-----BEGIN RSA PRIVATE KEY-----PEM encoded RSA public keys begin with:
-----BEGIN RSA PUBLIC KEY-----§Minimum Supported Rust Version (MSRV) Policy
MSRV increases are not considered breaking changes and can happen in patch releases.
The crate MSRV accounts for all supported targets and crate feature combinations, excluding explicitly unstable features.
§License
Licensed under either of:
at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
§Usage
§Borrowed DER decoding
RsaPrivateKeyRef and RsaPublicKeyRef borrow their integer data from the input.
Import der::Decode to use from_der:
use pkcs1::{RsaPrivateKeyRef, der::Decode};
let key = RsaPrivateKeyRef::from_der(der)?;Here, der is a byte slice containing a PKCS#1 DER-encoded private key.
RsaPrivateKeyRef::try_from(der) also decodes DER when der has type &[u8].
Both methods are available without enabling any features.
§Owned DER decoding and encoding
Enable the alloc feature to use RsaPrivateKeyOwned and RsaPublicKeyOwned.
These types own their integer data and implement DecodeRsaPrivateKey and
DecodeRsaPublicKey, respectively:
use pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, RsaPrivateKeyOwned};
let key = RsaPrivateKeyOwned::from_pkcs1_der(der)?;
let document = key.to_pkcs1_der()?;
assert_eq!(document.as_bytes(), der);EncodeRsaPrivateKey and EncodeRsaPublicKey support both borrowed and owned keys.
The decoding traits require owned results; use der::Decode for the borrowed types as shown
above. RSA implementations can also implement these traits for their own key types.
§PEM decoding and encoding
Enable the pem feature (which also enables alloc) to use from_pkcs1_pem and
to_pkcs1_pem with the PEM labels shown above:
use pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, LineEnding, RsaPrivateKeyOwned};
let key = RsaPrivateKeyOwned::from_pkcs1_pem(pem)?;
let encoded = key.to_pkcs1_pem(LineEnding::LF)?;
assert_eq!(encoded.as_str(), pem);Here, pem is a string containing a PEM-encoded RSA private key.
For public keys, use RsaPublicKeyOwned with DecodeRsaPublicKey and EncodeRsaPublicKey
(the method names are the same).
Re-exports§
Modules§
- error 🔒
- Error types
- params 🔒
- PKCS#1 RSA parameters.
- private_
key 🔒 - PKCS#1 RSA Private Keys.
- public_
key 🔒 - PKCS#1 RSA Public Keys.
- traits 🔒
- Traits for parsing objects from PKCS#1 encoded documents
- version 🔒
- PKCS#1 version identifier.
Structs§
- Object
Identifier - Object identifier (OID).
- Other
Prime Info - PKCS#1 OtherPrimeInfo as defined in RFC 8017 Appendix 1.2.
- RsaOaep
Params - PKCS#1 RSAES-OAEP parameters as defined in RFC 8017 Appendix 2.1
- RsaPrivate
Key - PKCS#1 RSA Private Keys as defined in RFC 8017 Appendix 1.2.
- RsaPss
Params - PKCS#1 RSASSA-PSS parameters as defined in RFC 8017 Appendix 2.3
- RsaPublic
Key - PKCS#1 RSA Public Keys as defined in RFC 8017 Appendix 1.1.
- UintRef
- Unsigned arbitrary precision ASN.1
INTEGERreference type.
Enums§
- Error
- Error type
- Line
Ending - Line endings: variants of newline characters that can be used with Base64.
- Trailer
Field TrailerFieldas defined in RFC 8017 Appendix 2.3.- Version
- Version identifier for PKCS#1 documents as defined in RFC 8017 Appendix 1.2.
Constants§
- ALGORITHM_
ID AlgorithmIdentifierfor RSA.- ALGORITHM_
OID rsaEncryptionObject Identifier (OID)
Traits§
- Decode
RsaPrivate Key - Decode an owned type from a PKCS#1-encoded
RsaPrivateKey. - Decode
RsaPublic Key - Decode an owned type from a PKCS#1-encoded
RsaPublicKey. - Encode
RsaPrivate Key - Serialize a
RsaPrivateKeyto a PKCS#1 encoded document. - Encode
RsaPublic Key - Serialize a
RsaPublicKeyto a PKCS#1-encoded document.
Type Aliases§
- Other
Prime Infos - Additional RSA prime info in a multi-prime RSA key.
- Result
- Result type
- RsaOaep
Params Owned RsaOaepParamswith allocatingAlgorithmIdentifier<Any>- RsaOaep
Params Ref RsaOaepParamswithAlgorithmIdentifier<AnyRef>- RsaPrivate
KeyOwned - PKCS#1 RSA Private Keys as defined in [RFC 8017 Appendix 1.2].
- RsaPrivate
KeyRef - PKCS#1 RSA Private Keys as defined in [RFC 8017 Appendix 1.2].
- RsaPss
Params Owned - PKCS#1 RSASSA-PSS parameters as defined in [RFC 8017 Appendix 2.3]
- RsaPss
Params Ref - PKCS#1 RSASSA-PSS parameters as defined in [RFC 8017 Appendix 2.3]
- RsaPublic
KeyOwned RsaPublicKeywith allocatingUintINTEGERs.- RsaPublic
KeyRef RsaPublicKeywithUintRefINTEGERs.