#private-key #pem #key #pkcs #rsa #crypto

no-std pem-rfc7468

PEM Encoding (RFC 7468) for PKIX, PKCS, and CMS Structures, implementing a strict subset of the original Privacy-Enhanced Mail encoding intended specifically for use with cryptographic keys, certificates, and other messages. Provides a no_std-friendly, constant-time implementation suitable for use with cryptographic private keys

18 releases ()

1.0.0-rc.2 Oct 15, 2024
1.0.0-rc.1 Jul 28, 2024
1.0.0-pre.0 Jan 7, 2024
0.7.0 Feb 26, 2023
0.0.0 Feb 16, 2021

#1521 in Cryptography

Download history 616027/week @ 2024-08-06 641025/week @ 2024-08-13 671546/week @ 2024-08-20 624082/week @ 2024-08-27 681046/week @ 2024-09-03 664817/week @ 2024-09-10 640374/week @ 2024-09-17 709515/week @ 2024-09-24 722536/week @ 2024-10-01 726164/week @ 2024-10-08 815746/week @ 2024-10-15 868573/week @ 2024-10-22 837146/week @ 2024-10-29 825677/week @ 2024-11-05 857146/week @ 2024-11-12 910725/week @ 2024-11-19

3,596,913 downloads per month
Used in 754 crates (25 directly)

Apache-2.0 OR MIT

115KB
2K SLoC

RustCrypto: PEM Encoding (RFC 7468)

crate Docs Build Status Apache2/MIT licensed Rust Version Project Chat

Pure Rust implementation of PEM Encoding (RFC 7468) for PKIX, PKCS, and CMS Structures, a strict subset of the original Privacy-Enhanced Mail encoding intended specifically for use with cryptographic keys, certificates, and other messages.

Provides a no_std-friendly, constant-time implementation suitable for use with cryptographic private keys.

Documentation

About

Many cryptography-related document formats, such as certificates (PKIX), private and public keys/keypairs (PKCS), and other cryptographic messages (CMS) provide an ASCII encoding which can be traced back to Privacy-Enhanced Mail (PEM) as defined RFC 1421, which look like the following:

-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIBftnHPp22SewYmmEoMcX8VwI4IHwaqd+9LFPj/15eqF
-----END PRIVATE KEY-----

However, all of these formats actually implement a text-based encoding that is similar but not identical to the legacy PEM encoding as described in RFC 1421.

For this reason, RFC 7468 was created to describe a stricter form of "PEM encoding" for use in these applications which codifies the previously de facto rules that most implementations operate by, and makes recommendations to promote interoperability.

This crate provides a strict interpretation of the RFC 7468 rules, implementing MUSTs and SHOULDs while avoiding the MAYs, targeting the "ABNF (Strict)" subset of the grammar as described in RFC 7468 Section 3 Figure 3 (p6).

Implementation notes

  • no_std-friendly core implementation which requires no heap allocations and avoids copies and temporary buffers.
  • Optional alloc-dependent convenience features and buffered decoder/encoder.
  • Uses the base64ct crate to decode/encode Base64 in constant-time.
  • PEM parser avoids branching on potentially secret data as much as possible.

The paper Util::Lookup: Exploiting key decoding in cryptographic libraries demonstrates how the leakage from non-constant-time PEM parsers can be used to practically extract RSA private keys from SGX enclaves.

Minimum Supported Rust Version

This crate requires Rust 1.81 at a minimum.

We may change the MSRV in the future, but it will be accompanied by a minor version bump.

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.

Dependencies