4646from core .trad_crypto import (
4747 derive_key_argon2id ,
4848 sha3_512 ,
49- hkdf ,
50- encrypt_chacha20poly1305 ,
51- decrypt_chacha20poly1305
49+ encrypt_xchacha20poly1305 ,
50+ decrypt_xchacha20poly1305
5251)
5352from base64 import b64encode , b64decode
5453from core .constants import (
5554 SMP_NONCE_LENGTH ,
5655 SMP_PROOF_LENGTH ,
5756 SMP_QUESTION_MAX_LEN ,
5857 SMP_ANSWER_OUTPUT_LEN ,
58+ ARGON2_SALT_LEN ,
5959 ML_KEM_1024_NAME ,
6060 ML_KEM_1024_CT_LEN ,
6161 ML_DSA_87_PK_LEN ,
62- CHACHA20POLY1305_NONCE_LEN
62+ XCHACHA20POLY1305_NONCE_LEN
6363)
6464import hashlib
6565import secrets
@@ -134,13 +134,9 @@ def smp_step_2(user_data: dict, user_data_lock, contact_id: str, message: dict,
134134 our_nonce = secrets .token_bytes (SMP_NONCE_LENGTH )
135135
136136 key_ciphertext , chacha_key = encap_shared_secret (contact_kem_public_key , ML_KEM_1024_NAME )
137- chacha_key = hkdf (
138- chacha_key ,
139- salt = contact_id .encode ("utf-8" ) + our_id .encode ("utf-8" ),
140- info = b"Coldwire SMP encryption ChaCha20 key"
141- )
137+ chacha_key = sha3_512 (chacha_key )[:32 ]
142138
143- ciphertext_nonce , ciphertext_blob = encrypt_chacha20poly1305 (
139+ ciphertext_nonce , ciphertext_blob = encrypt_xchacha20poly1305 (
144140 chacha_key ,
145141 signing_public_key + our_nonce ,
146142 counter = 2
@@ -187,19 +183,14 @@ def smp_step_3(user_data: dict, user_data_lock: threading.Lock, contact_id: str,
187183 ciphertext_blob = b64decode (message ["ciphertext_blob" ], validate = True )
188184 key_ciphertext = ciphertext_blob [:ML_KEM_1024_CT_LEN ]
189185
190- print (len (our_kem_private_key ))
191186 chacha_key = decap_shared_secret (key_ciphertext , our_kem_private_key , ML_KEM_1024_NAME )
192187
193- chacha_key = hkdf (
194- chacha_key ,
195- salt = our_id .encode ("utf-8" ) + contact_id .encode ("utf-8" ),
196- info = b"Coldwire SMP encryption ChaCha20 key"
197- )
188+ chacha_key = sha3_512 (chacha_key )[:32 ]
198189
199- smp_plaintext = decrypt_chacha20poly1305 (
190+ smp_plaintext = decrypt_xchacha20poly1305 (
200191 chacha_key ,
201- ciphertext_blob [ML_KEM_1024_CT_LEN : ML_KEM_1024_CT_LEN + CHACHA20POLY1305_NONCE_LEN ],
202- ciphertext_blob [ML_KEM_1024_CT_LEN + CHACHA20POLY1305_NONCE_LEN :]
192+ ciphertext_blob [ML_KEM_1024_CT_LEN : ML_KEM_1024_CT_LEN + XCHACHA20POLY1305_NONCE_LEN ],
193+ ciphertext_blob [ML_KEM_1024_CT_LEN + XCHACHA20POLY1305_NONCE_LEN :]
203194 )
204195
205196 contact_signing_public_key = smp_plaintext [:ML_DSA_87_PK_LEN ]
@@ -212,7 +203,7 @@ def smp_step_3(user_data: dict, user_data_lock: threading.Lock, contact_id: str,
212203 contact_key_fingerprint = sha3_512 (contact_signing_public_key )
213204
214205 # Derieve a high-entropy secret key from the low-entropy answer
215- argon2id_salt = sha3_512 (contact_nonce + our_nonce )
206+ argon2id_salt = sha3_512 (contact_nonce + our_nonce )[: ARGON2_SALT_LEN ]
216207 answer_secret , _ = derive_key_argon2id (answer .encode ("utf-8" ), salt = argon2id_salt , output_length = SMP_ANSWER_OUTPUT_LEN )
217208
218209 # Compute our proof
@@ -221,7 +212,7 @@ def smp_step_3(user_data: dict, user_data_lock: threading.Lock, contact_id: str,
221212
222213 logger .debug ("Our proof of contact (%s) public-key fingerprint: %s" , contact_id , our_proof )
223214
224- ciphertext_nonce , ciphertext_blob = encrypt_chacha20poly1305 (
215+ ciphertext_nonce , ciphertext_blob = encrypt_xchacha20poly1305 (
225216 chacha_key ,
226217 signing_public_key + our_nonce + our_proof + question .encode ("utf-8" ),
227218 counter = 3
@@ -259,7 +250,7 @@ def smp_step_4_request_answer(user_data, user_data_lock, contact_id, message, ui
259250 tmp_key = b64decode (user_data ["contacts" ][contact_id ]["lt_sign_key_smp" ]["tmp_key" ])
260251
261252 ciphertext_blob = b64decode (message ["ciphertext_blob" ], validate = True )
262- smp_plaintext = decrypt_chacha20poly1305 (tmp_key , ciphertext_blob [:CHACHA20POLY1305_NONCE_LEN ], ciphertext_blob [CHACHA20POLY1305_NONCE_LEN :])
253+ smp_plaintext = decrypt_xchacha20poly1305 (tmp_key , ciphertext_blob [:XCHACHA20POLY1305_NONCE_LEN ], ciphertext_blob [XCHACHA20POLY1305_NONCE_LEN :])
263254
264255 contact_signing_public_key = smp_plaintext [:ML_DSA_87_PK_LEN ]
265256 contact_nonce = b64encode (smp_plaintext [ML_DSA_87_PK_LEN : SMP_NONCE_LENGTH + ML_DSA_87_PK_LEN ]).decode ()
@@ -306,7 +297,7 @@ def smp_step_4_answer_provided(user_data, user_data_lock, contact_id, answer, ui
306297 our_key_fingerprint = sha3_512 (our_signing_public_key )
307298
308299 # Derieve a high-entropy secret key from the low-entropy answer
309- argon2id_salt = sha3_512 (our_nonce + contact_nonce )
300+ argon2id_salt = sha3_512 (our_nonce + contact_nonce )[: ARGON2_SALT_LEN ]
310301 answer_secret , _ = derive_key_argon2id (answer .encode ("utf-8" ), salt = argon2id_salt , output_length = SMP_ANSWER_OUTPUT_LEN )
311302
312303 # Compute our proof
@@ -330,7 +321,7 @@ def smp_step_4_answer_provided(user_data, user_data_lock, contact_id, answer, ui
330321 our_proof = contact_nonce + our_nonce + contact_key_fingerprint
331322 our_proof = hmac .new (answer_secret , our_proof , hashlib .sha3_512 ).digest ()
332323
333- ciphertext_nonce , ciphertext_blob = encrypt_chacha20poly1305 (
324+ ciphertext_nonce , ciphertext_blob = encrypt_xchacha20poly1305 (
334325 tmp_key ,
335326 our_proof ,
336327 counter = 4
@@ -377,15 +368,15 @@ def smp_step_5(user_data, user_data_lock, contact_id, message, ui_queue) -> None
377368 our_key_fingerprint = sha3_512 (our_signing_public_key + our_kem_public_key )
378369
379370 # Derieve a high-entropy secret key from the low-entropy answer
380- argon2id_salt = sha3_512 (contact_nonce + our_nonce )
371+ argon2id_salt = sha3_512 (contact_nonce + our_nonce )[: ARGON2_SALT_LEN ]
381372 answer_secret , _ = derive_key_argon2id (answer .encode ("utf-8" ), salt = argon2id_salt , output_length = SMP_ANSWER_OUTPUT_LEN )
382373
383374 # Compute the proof
384375 our_proof = our_nonce + contact_nonce + our_key_fingerprint
385376 our_proof = hmac .new (answer_secret , our_proof , hashlib .sha3_512 ).digest ()
386377
387378 ciphertext_blob = b64decode (message ["ciphertext_blob" ], validate = True )
388- contact_proof = decrypt_chacha20poly1305 (tmp_key , ciphertext_blob [:CHACHA20POLY1305_NONCE_LEN ], ciphertext_blob [CHACHA20POLY1305_NONCE_LEN :])
379+ contact_proof = decrypt_xchacha20poly1305 (tmp_key , ciphertext_blob [:XCHACHA20POLY1305_NONCE_LEN ], ciphertext_blob [XCHACHA20POLY1305_NONCE_LEN :])
389380
390381
391382 logger .debug ("SMP Proof sent to us: %s" , contact_proof )
0 commit comments