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

Minidriver function CardRSADecrypt returns incorrect length when unpadding SC_ALGORITHM_RSA_RAW/CARD_PADDING_PKCS1 #3086

Closed
jozsefd opened this issue Mar 22, 2024 · 2 comments

Comments

@jozsefd
Copy link
Contributor

jozsefd commented Mar 22, 2024

Problem Description

Minidriver function CardRSADecrypt returns incorrect length when unpadding SC_ALGORITHM_RSA_RAW data as CARD_PADDING_PKCS1 using the function sc_pkcs1_strip_02_padding_constant_time(). The function sc_pkcs1_strip_02_padding_constant_time() sets the last output parameter to modulus_length / 8 - SC_PKCS1_PADDING_MIN_SIZE, not the actual length of the unpadded data, on the other hand, the function returns the correct length.

Proposed Resolution

// pInfo->cbData = (DWORD) temp;
pInfo->cbData = (DWORD) r;

Steps to reproduce

This isssue can be reproduced only after #3084 and #3085 has been fixed. It can be reproduced with a windows application calling API CryptDecrypt() with a card using raw RSA (like nqApplet).
The following commit contains all 3 fixes: 723209f

Logs

The expected length of the decrypted data after unpadding was 128, but got 373:
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] sec.c:50:sc_decipher: returning with: 384
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] card.c:523:sc_unlock: called
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] pkcs15-sec.c:169:use_key: returning with: 384
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] sc_pkcs15_decipher: DECRYPT-INFO dwVersion=2
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] sc_pkcs15_decipher: stripping PKCS1 padding
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] padding.c:159:sc_pkcs1_strip_02_padding_constant_time: called
P:20068; T:21236 2024-03-22 08:30:24.185 [cardmod] decrypted data(373):

@jozsefd
Copy link
Contributor Author

jozsefd commented Mar 22, 2024

#3077 will fix this problem.

@jozsefd
Copy link
Contributor Author

jozsefd commented Mar 22, 2024

I have tested #3077 with various cards, it fixes this issue.

@jozsefd jozsefd closed this as completed Mar 22, 2024
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

No branches or pull requests

1 participant