cryptography

repository·main·Indexed 27 days ago

https://github.com/pyca/cryptography

A Python package providing high-level cryptographic recipes and low-level interfaces to common algorithms, including symmetric ciphers, message digests, and key derivation functions. It includes the Cobblestone streaming symmetric encryption implementation (Cobblestone-128 and Cobblestone-256) and maintains a strict API stability and deprecation policy.

Tokens
96.5K
Snippets
157
Records
553
Agent score
93%

What's inside cryptography

  1. Understand the cryptography API stability policy

    main

    The cryptography library maintains a strong API stability policy for all documented APIs and behaviors.

    What is guaranteed:

    • Public APIs will not be removed or renamed without providing a compatibility alias.
    • The behavior of existing APIs will not change.

    What is NOT guaranteed:

    • Internal object details like dir(obj) or obj.__dict__ may change when new features are added.
    • Objects are not guaranteed to be pickleable; pickled objects from one version may not be loadable in future versions.
    • Sub-classing is not supported unless explicitly documented; behavior regarding sub-classes is not guaranteed to be stable.
    • Development versions are not covered by this policy.

    Security Exception: To resolve security issues or harden the library against attacks, the stability policy may be violated.

  2. Understand the transition from OpenSSL to Rust in pyca/cryptography

    main

    To improve performance and memory safety, pyca/cryptography is actively migrating its functionality from OpenSSL to Rust.

    Key migration benefits include:

    • Performance: Moving X.509 certificate parsing to Rust resulted in a 10x performance improvement over OpenSSL 3. Moving public key parsing to Rust improved end-to-end X.509 path validation by 60%.
    • Memory Safety: By using Rust for parsing and X.509 operations, the library avoids several OpenSSL-related CVEs and leverages Rust's memory safety guarantees.

    Currently, the library uses a hybrid approach: pure-Rust for parsing and X.509 operations, and OpenSSL for providing core cryptographic algorithms.

  3. Understand supported versions and OpenSSL updates

    main

    Supported Versions

    cryptography provides security support for the main branch and the most recent release.

    OpenSSL Updates

    cryptography statically links OpenSSL in binary distributions for Windows, macOS, and Linux. To avoid shipping insecure software, cryptography will release a new version whenever OpenSSL releases a security or bug fix. It is strongly recommended to upgrade to the latest version as soon as a new release is announced.

  4. Understand the Recipes vs. Hazmat layers

    main

    The cryptography library is organized into two distinct layers:

    1. Recipes Layer: High-level, safe cryptographic recipes (e.g., fernet, x509) that require little to no configuration. These are recommended for most use cases to avoid common cryptographic errors.
    2. Hazmat (Hazardous Materials) Layer: Low-level cryptographic primitives located in the cryptography.hazmat package. These require in-depth knowledge of cryptographic concepts and can be dangerous if used incorrectly. Documentation for these primitives will always include a warning admonition.
  5. Use Message Authentication Codes (MAC) for data integrity

    main

    The cryptography library provides several Message Authentication Code (MAC) algorithms to ensure data integrity and authenticity.

    Recommendation: It is strongly recommended to use HMAC for most use cases unless you have a specific requirement for another algorithm.

    Supported MAC algorithms include:

    • hmac (Recommended)
    • cmac
    • poly1305
  6. Understand future changes to OpenSSL support in pyca/cryptography

    main

    The pyca/cryptography project is moving away from a strict requirement for OpenSSL implementations due to ongoing concerns regarding performance, complexity, and memory safety. Developers should be aware of the following upcoming shifts in the library's direction:

    • New Functionality Requirements: New APIs may be introduced that are exclusive to OpenSSL forks like LibreSSL, BoringSSL, or AWS-LC. For example, upcoming support for ML-KEM and ML-DSA may only be available via these forks and not through standard OpenSSL.
    • Binary Wheel Linking: The project is investigating moving from statically linking OpenSSL in binary wheels to linking against one of the OpenSSL forks.
    • Potential Deprecation: If a successful transition to an OpenSSL fork is achieved, the project may consider dropping support for OpenSSL entirely.
    • Alternative Libraries: The project is actively tracking non-OpenSSL derived libraries (such as Graviola) as long-term potential alternatives.
  7. Understand FIPS 186-2 ECDSA test vectors

    main

    The cryptography_vectors package includes example test files for FIPS 186-2 ECDSA validation. These files are structured as follows:

    • .rsp files: These are response files formatted specifically for CAVS (Cryptographic Algorithm Validation Service) validation.
    • SigGen.txt: This file contains values for ECDSA signature generation. To calculate the signature components r and s according to the X9.62 standard, you will need the additional values provided in this file:
      • d: The private key.
      • k: The random value used in calculating the signature (r, s) (refer to ANS X9.62).
  8. Understand CCM CAVS example file formats

    main

    The cryptography_vectors package provides two types of CCM (Counter with CBC-MAC) example files for testing:

    1. Response files (.rsp): These contain properly formatted CAVS (Cryptographic Algorithm Validation Suite) response files.
    2. DVPT text files (DVPT{128/192/256}.txt): These contain the same values as the corresponding .rsp files but include additional diagnostic information. For failed test cases, the reason for failure is provided in parentheses following the result (e.g., Result = Fail (2 - CT changed)). Note that this extra information is not part of a standard CAVS response format.
  9. Verify RSA OAEP SHA2 test vectors using Java and Bouncy Castle

    main

    To verify the RSA OAEP SHA2 test vectors against another implementation, you can use a Java program that utilizes the Bouncy Castle library.

    Prerequisites

    1. Java SDK: Ensure the Java SDK is installed (e.g., jdk-8u77-macosx-x64.dmg or later).
    2. Bouncy Castle JAR: Download the latest Bouncy Castle JAR (e.g., bcprov-jdk15on-154.jar).

    Compilation

    Compile the VerifyRSAOAEPSHA2.java program by setting the -classpath to include the Bouncy Castle JAR and the current directory:

    javac -classpath ~/Downloads/bcprov-jdk15on-154.jar:./ VerifyRSAOAEPSHA2.java

    Execution

    Run the compiled program by providing the path to the SHA-2 vectors in the classpath:

    java -classpath ~/Downloads/bcprov-jdk15on-154.jar:./ VerifyRSAOAEPSHA2
    $ javac -classpath ~/Downloads/bcprov-jdk15on-154.jar:./ VerifyRSAOAEPSHA2.java
    $ java -classpath ~/Downloads/bcprov-jdk15on-154.jar:./ VerifyRSAOAEPSHA2
  10. C binding style guide

    main

    When writing C bindings, follow these style requirements:

    • Parameters: Do not name parameters in function signatures (e.g., long f(long);), unless they are inside a struct.
    • No Arguments: Always include void if a function takes no arguments (e.g., long f(void);).
    • Line Wrapping: Wrap lines at 80 characters.
    • Spacing: Include a space after commas between parameters.
    • Comments: Use C-style /* */ comments; do not use C++-style //.
    • Constants: Do not use #define for values. Instead, assign them to appropriate types using static const (e.g., static const int SOME_VALUE;).
  11. Manage Rust requirements in Docker deployments

    main

    A Rust toolchain is only required during the build phase of cryptography. It is not required to use the library at runtime.

    For Docker deployments, use a multi-stage Dockerfile. Install the Rust toolchain in the build stage to compile the package, then copy only the necessary artifacts to the final runtime image. This keeps the runtime image lightweight and secure by excluding the Rust compiler and build tools.

  12. Use Key Derivation Functions (KDFs) for cryptographic operations

    main

    Key derivation functions (KDFs) derive bytes suitable for cryptographic operations from passwords or other data sources using a pseudo-random function (PRF).

    Common use cases include:

    • Cryptographic key derivation: Deriving a key suitable for use as input to an encryption algorithm (often called key stretching). Examples include using PBKDF2HMAC or HKDF.
    • Password storage: Using computationally intensive algorithms that are demanding on both computational and memory resources to make brute-force attacks difficult.