forked from Mbed-TLS/mbedtls
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Psa thread safety doc update #1
Merged
gilles-peskine-arm
merged 1 commit into
gilles-peskine-arm:psa-thread_safety-doc
from
AndrzejKurek:psa-thread_safety-doc
Nov 8, 2021
Merged
Psa thread safety doc update #1
gilles-peskine-arm
merged 1 commit into
gilles-peskine-arm:psa-thread_safety-doc
from
AndrzejKurek:psa-thread_safety-doc
Nov 8, 2021
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gilles-peskine-arm
approved these changes
Nov 8, 2021
gilles-peskine-arm
added a commit
that referenced
this pull request
Mar 10, 2022
The PSA Crypto API uses 0 as the initial counter value, but the test vector in RFC 7539 uses 1. So the unit tests here include an extra leading block. The expected data for this leading block was calculated with Cryptodome. #!/usr/bin/env python3 import re from Cryptodome.Cipher import ChaCha20 key = bytes.fromhex('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f') nonce = bytes.fromhex('000000000000004a00000000') encrypt = lambda pt: ChaCha20.new(key=key, nonce=nonce).encrypt(pt) # Cryptodome uses counter=0, like PSA Crypto. Prepend a 64-byte input block #0 # so that the plaintext from RFC 7539 starts exactly at block #1. header = b'The RFC 7539 test vector uses counter=1, but PSA uses counter=0.' assert(len(header) == 64) sunscreen = b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." plaintext = header + sunscreen zeros = b'\x00' * len(plaintext) keystream = encrypt(zeros) ciphertext = encrypt(plaintext) print('RFC 7539 §2.4.2') print('Keystream:') print(re.sub(r'(..)', r'\1:', keystream[64:].hex())) print('Ciphertext Subscreen:') print(re.sub(r'(..)', r'\1 ', ciphertext[64:].hex())) print('') print(f"""\ PSA symmetric decrypt: ChaCha20, RFC7539 keystream depends_on:PSA_WANT_ALG_STREAM_CIPHER:PSA_WANT_KEY_TYPE_CHACHA20 # Keystream from RFC 7539 §2.4.2, with an extra 64-byte output block prepended # because the test vector starts at counter=1 but our API starts at counter=0. cipher_decrypt:PSA_ALG_STREAM_CIPHER:PSA_KEY_TYPE_CHACHA20:"{key.hex()}":"{nonce.hex()}":"{zeros.hex()}":"{keystream.hex()}" PSA symmetric decrypt: ChaCha20, RFC7539 sunscreen depends_on:PSA_WANT_ALG_STREAM_CIPHER:PSA_WANT_KEY_TYPE_CHACHA20 # Test vector from RFC 7539 §2.4.2, with an extra 64-byte block prepended # because the test vector starts at counter=1 but our API starts at counter=0. cipher_decrypt:PSA_ALG_STREAM_CIPHER:PSA_KEY_TYPE_CHACHA20:"{key.hex()}":"{nonce.hex()}":"{ciphertext.hex()}":"{plaintext.hex()}" """) Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
gilles-peskine-arm
added a commit
that referenced
this pull request
Mar 10, 2022
The PSA Crypto API uses 0 as the initial counter value, but the test vector in RFC 7539 uses 1. So the unit tests here include an extra leading block. The expected data for this leading block was calculated with Cryptodome. #!/usr/bin/env python3 import re from Cryptodome.Cipher import ChaCha20 key = bytes.fromhex('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f') nonce = bytes.fromhex('000000000000004a00000000') encrypt = lambda pt: ChaCha20.new(key=key, nonce=nonce).encrypt(pt) # Cryptodome uses counter=0, like PSA Crypto. Prepend a 64-byte input block #0 # so that the plaintext from RFC 7539 starts exactly at block #1. header = b'The RFC 7539 test vector uses counter=1, but PSA uses counter=0.' assert(len(header) == 64) sunscreen = b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." plaintext = header + sunscreen zeros = b'\x00' * len(plaintext) keystream = encrypt(zeros) ciphertext = encrypt(plaintext) print('RFC 7539 §2.4.2') print('Keystream:') print(re.sub(r'(..)', r'\1:', keystream[64:].hex())) print('Ciphertext Subscreen:') print(re.sub(r'(..)', r'\1 ', ciphertext[64:].hex())) print('') print(f"""\ PSA symmetric decrypt: ChaCha20, RFC7539 keystream depends_on:PSA_WANT_ALG_STREAM_CIPHER:PSA_WANT_KEY_TYPE_CHACHA20 # Keystream from RFC 7539 §2.4.2, with an extra 64-byte output block prepended # because the test vector starts at counter=1 but our API starts at counter=0. cipher_decrypt:PSA_ALG_STREAM_CIPHER:PSA_KEY_TYPE_CHACHA20:"{key.hex()}":"{nonce.hex()}":"{zeros.hex()}":"{keystream.hex()}" PSA symmetric decrypt: ChaCha20, RFC7539 sunscreen depends_on:PSA_WANT_ALG_STREAM_CIPHER:PSA_WANT_KEY_TYPE_CHACHA20 # Test vector from RFC 7539 §2.4.2, with an extra 64-byte block prepended # because the test vector starts at counter=1 but our API starts at counter=0. cipher_decrypt:PSA_ALG_STREAM_CIPHER:PSA_KEY_TYPE_CHACHA20:"{key.hex()}":"{nonce.hex()}":"{ciphertext.hex()}":"{plaintext.hex()}" """) Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Updated some of the TODO's, before adding personal considerations