Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CKR_GENERAL_ERROR with CKR_ENCRYPTED_DATA_INVALID or CKR_ENCRYPTED_DATA_LEN_RANGE upon decryption failure #690

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

hansonchar
Copy link
Contributor

@hansonchar hansonchar commented Dec 3, 2022

See #689

Summary by CodeRabbit

  • New Features

    • Enhanced key generation capabilities for various cryptographic algorithms (AES, DES, RSA, DSA, EC, EDDSA, GOST)
    • Added support for key wrapping and unwrapping using symmetric and asymmetric mechanisms
    • Introduced key derivation methods for DH, ECDH, and EDDSA
    • Implemented mechanism validation and fork detection
  • Improvements

    • Extended cryptographic functionality for more robust key management
    • Added parameter checking for advanced cryptographic operations

@hansonchar
Copy link
Contributor Author

I see the CI/CD workflow involved downloading of some files but the downloading failed. Any idea how to unblock?

@jschlyter jschlyter requested a review from bjosv December 14, 2024 16:14
@jschlyter jschlyter marked this pull request as draft December 14, 2024 16:15
@jschlyter
Copy link
Contributor

Please rebase so Github actions are included.

@hansonchar
Copy link
Contributor Author

Rebased

Copy link
Contributor

@bjosv bjosv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks correct to me.

@jschlyter jschlyter marked this pull request as ready for review December 17, 2024 10:58
@jschlyter jschlyter requested a review from a team as a code owner December 17, 2024 10:58
@jschlyter
Copy link
Contributor

@coderabbitai review

Copy link

coderabbitai bot commented Jan 29, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Jan 29, 2025

Walkthrough

The changes in the SoftHSM library involve significant enhancements to cryptographic key management and generation capabilities. The modifications expand the library's functionality by adding new methods for symmetric and asymmetric key wrapping, unwrapping, and deriving keys using various cryptographic mechanisms. The implementation now supports a broader range of key types including AES, DES, RSA, DSA, EC, and GOST, with improved mechanisms for key generation, parameter validation, and mechanism permission checks.

Changes

File Change Summary
src/lib/SoftHSM.cpp - Added multiple key wrapping/unwrapping methods for symmetric and asymmetric keys
- Implemented key derivation methods for DH, ECDH, EDDSA
- Added key generation methods for various key types (AES, DES, RSA, DSA, EC, etc.)
- Introduced mechanism parameter validation and permission checking functions
- Added fork detection method

Sequence Diagram

sequenceDiagram
    participant Client
    participant SoftHSM
    participant KeyManager
    
    Client->>SoftHSM: Generate Key
    SoftHSM->>KeyManager: Validate Mechanism
    KeyManager-->>SoftHSM: Mechanism Permitted
    SoftHSM->>SoftHSM: Generate Key Pair
    SoftHSM-->>Client: Return Key Handles
Loading

Poem

🐰 In the realm of crypto's dance,
SoftHSM leaps with newfound chance,
Keys wrap and unwrap with grace,
Mechanisms find their rightful place,
A rabbit's cryptographic romance! 🔐

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/lib/SoftHSM.cpp (2)

3312-3320: Improve error handling for decryption failures

The code now returns CKR_ENCRYPTED_DATA_INVALID instead of a generic CKR_GENERAL_ERROR when decryption fails. This is a more specific and appropriate error code for decryption failures.

Consider adding debug logging to help diagnose decryption failures:

 if (!cipher->decryptUpdate(encryptedData,data))
 {
    session->resetOp();
+   DEBUG_MSG("Decryption update failed");
    return CKR_ENCRYPTED_DATA_INVALID;
 }

3381-3389: Improve error handling for asymmetric decryption failures

Similar to the symmetric case, the code now returns CKR_ENCRYPTED_DATA_INVALID instead of CKR_GENERAL_ERROR for asymmetric decryption failures.

Consider adding debug logging here as well:

 if (!asymCrypto->decrypt(privateKey,encryptedData,data,mechanism))
 {
    session->resetOp();
+   DEBUG_MSG("Asymmetric decryption failed");
    return CKR_ENCRYPTED_DATA_INVALID;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4be49e3 and e287379.

📒 Files selected for processing (1)
  • src/lib/SoftHSM.cpp (5 hunks)
🔇 Additional comments (3)
src/lib/SoftHSM.cpp (3)

3478-3493: Improve error handling for multi-part decryption failures

The code now returns more specific error codes for multi-part decryption failures:

  • CKR_ENCRYPTED_DATA_INVALID for decryption failures
  • CKR_ENCRYPTED_DATA_LEN_RANGE for output buffer size mismatches

This is a good improvement in error handling specificity.


Line range hint 3581-3592: Improve error handling for final decryption failures

Similar improvements in error handling for the final decryption operation:

  • CKR_ENCRYPTED_DATA_INVALID for decryption failures
  • CKR_ENCRYPTED_DATA_LEN_RANGE for output buffer size mismatches

This is consistent with the other error handling improvements.


Line range hint 1-5000: Verify error code consistency across the codebase

The changes introduce more specific error codes for decryption failures. We should verify that these error codes are used consistently across the entire codebase.

Consider documenting the error handling strategy for cryptographic operations in a central location to ensure consistency across the codebase. This should include:

  1. When to use specific error codes vs generic ones
  2. What debug information should be logged for each type of error
  3. Guidelines for error recovery and cleanup
✅ Verification successful

Error codes are used consistently for decryption failures

The codebase demonstrates consistent and well-defined usage of error codes:

  • CKR_ENCRYPTED_DATA_LEN_RANGE is correctly used for all size/length related issues
  • CKR_ENCRYPTED_DATA_INVALID is properly used for actual decryption failures
  • Error handling includes appropriate cleanup and logging
  • Test suite validates error code usage for both single and multi-part operations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for consistent use of CKR_ENCRYPTED_DATA_INVALID and CKR_ENCRYPTED_DATA_LEN_RANGE
rg "CKR_ENCRYPTED_DATA_(INVALID|LEN_RANGE)" -A 2 -B 2

Length of output: 5333

@jschlyter jschlyter merged commit 557b2fd into softhsm:develop Jan 29, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failure in decrypting invalid ciphertext should not return CKR_GENERAL_ERROR
3 participants