Skip to content

Add support for message-based encryption and decryption (PKCS#11 3.0) #255

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

Merged
merged 8 commits into from
Apr 15, 2025

Conversation

Jakuje
Copy link
Contributor

@Jakuje Jakuje commented Apr 4, 2025

Toward this change, I had to go through some hops.

  • Update the headers to PKCS#11 3.1
  • Fix the function list initialization to be able to use the new PKCS#11 3.0 functions
  • Added the new API functions
  • Added tests (work kryoptic)

Jakuje added 5 commits April 2, 2025 11:20
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
This is follow-up with the recommendations from parallaxsecond#248 and this will
actually allow use to call the new functions from the crate.

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
@Jakuje Jakuje force-pushed the message-encrypt branch from 40a9485 to 79604e3 Compare April 4, 2025 09:16
@Jakuje Jakuje marked this pull request as ready for review April 4, 2025 09:16
@Jakuje
Copy link
Contributor Author

Jakuje commented Apr 4, 2025

The clippy failure is something in the code that I did not touch so I assume rust/clippy was updated somewhere and new warnings are showing up. On the first sight, its not clear to me how to correctly fix it.

Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Thank you!!

I think the clippy warning is only about aligning lists with two spaces instead of more

@Jakuje
Copy link
Contributor Author

Jakuje commented Apr 6, 2025

I think the clippy warning is only about aligning lists with two spaces instead of more

Yeah, thats how much I got from the message, but its the continuation of the list item so I am not sure how it will render in the docs when I will remove the indent for the list. But I can try. It will break at worst :)

@Jakuje Jakuje force-pushed the message-encrypt branch 2 times, most recently from 991a958 to 66c25e8 Compare April 7, 2025 07:45
@Jakuje
Copy link
Contributor Author

Jakuje commented Apr 7, 2025

I think the clippy warning is only about aligning lists with two spaces instead of more

Yeah, thats how much I got from the message, but its the continuation of the list item so I am not sure how it will render in the docs when I will remove the indent for the list. But I can try. It will break at worst :)

Turned out it just needed one less space than I would expect in the RSA case (and few less than in the other). Fixed in the last commit.

hug-dev
hug-dev previously approved these changes Apr 8, 2025
@hug-dev hug-dev requested a review from wiktor-k April 11, 2025 12:04
wiktor-k
wiktor-k previously approved these changes Apr 14, 2025
Copy link
Collaborator

@wiktor-k wiktor-k left a comment

Choose a reason for hiding this comment

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

LGTM 👍 thanks! A couple of smaller things :)

@@ -805,6 +805,9 @@ pub enum Mechanism<'a> {
AesKeyWrapPad,
/// AES-GCM mechanism
AesGcm(aead::GcmParams<'a>),
/// AES-GCM mechanism with message based API and parameters
/// TODO should we reuse the AesGcm and use Option<> to select parameter?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the TODO should use normal comment (//) not to be rendered as docs 🤔


/// True if the mechanism can be used to decrypt encrypted messages
///
/// See [`Session::decrypt`](crate::session::Session::decrypt_message)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The below is important to catch stuff like here (decrypt vs decrypt_message).

self.flags.contains(MechanismInfoFlags::MESSAGE_DECRYPT)
}

/// True if the mechanism can be used with encrypt/decrypt_bessage_begin API.
Copy link
Collaborator

Choose a reason for hiding this comment

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

message typo

.into_result(Function::DecryptMessage)?;
}

data.resize(data_len.try_into()?, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this call necessary if the vec is already created with correct size in the first place? 🤔

Copy link
Contributor Author

@Jakuje Jakuje Apr 14, 2025

Choose a reason for hiding this comment

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

I used the same approach as in the Session::decrypt() as tried and tested :)

I agree that this might not be necessary for the PKCS#11 modules that return the right value on the first try, but the PKCS#11 specs says that the value does not have to be exact and could be larger so shrinking the vector is probably a good thing to do here:

  1. If pBuf is NULL_PTR, then all that the function does is return (in *pulBufLen) a number of bytes which would suffice to hold the cryptographic output produced from the input to the function.� This number may somewhat exceed the precise number of bytes needed, but should not exceed it by a large amount.� CKR_OK is returned by the function.

From 5.2 Conventions for functions returning output in a variable-length buffer

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great, thanks for digging this bit up!

But if the value returned can be bigger than the vector we pass in wouldn't the PKCS#11 call overwrite memory right after the Vec's buffer? And then the resize would still overwrite that PKCS#11-filled memory with zeros (since Vec implementation would "think" it has been uninitialized).

I guess this is a rare condition since the Vec would probably have spare capacity but thinking about all these edge cases gets me 😬

I understand the feel about adjusting the code around and then the reviewer being unhappy about not your code though 😅 Maybe we can track that in a separate issue and close this conversation here? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it should work only the other way round -- the size query can return larger value, but the operation itself will return the right value so no buffer should overrun in this code, unless I miss something.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ack. Then the Vec::truncate would be more appropriate, IMO. (I'm not saying you should adjust it, just for completeness for future work 🫠 )

Jakuje added 3 commits April 14, 2025 11:19
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
@Jakuje Jakuje dismissed stale reviews from wiktor-k and hug-dev via 614c946 April 14, 2025 09:23
@wiktor-k wiktor-k enabled auto-merge April 14, 2025 09:54
@wiktor-k wiktor-k merged commit 3e64198 into parallaxsecond:main Apr 15, 2025
8 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.

None yet

3 participants