#crypto #key #pem #pkcs #rsa

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

4 releases

new 0.2.2 Sep 17, 2021
0.2.1 Sep 14, 2021
0.2.0 Jul 26, 2021
0.1.1 Jul 24, 2021
0.0.0 Feb 16, 2021

#664 in Cryptography

Download history 1/week @ 2021-06-11 1/week @ 2021-07-16 995/week @ 2021-07-23 1825/week @ 2021-07-30 3995/week @ 2021-08-06 7628/week @ 2021-08-13 8903/week @ 2021-08-20 11848/week @ 2021-08-27 11001/week @ 2021-09-03 10002/week @ 2021-09-10

19,704 downloads per month
Used in 17 crates (2 directly)

Apache-2.0 OR MIT

62KB
920 lines

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 to, but not identical with, 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 attempts to implement a strict interpretation of the RFC 7468 rules, implementing all of the MUSTs and SHOULDs while avoiding the MAYs, and targeting the "ABNF (Strict)" subset of the grammar as described in Section 3 Figure 3.

Implementation notes

  • Core PEM implementation is no_std-friendly and requires no heap allocations.
  • Avoids use of copies and temporary buffers.
  • Uses the base64ct crate to decode/encode Base64 in constant-time.
  • PEM parser avoids branching on potentially secret data as much as possible. In the happy path, only 1-byte of secret data is potentially branched upon.

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.

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