1313from logic .pfs import send_new_ephemeral_keys
1414from core .trad_crypto import (
1515 sha3_512 ,
16- encrypt_xchacha20poly1305 ,
17- decrypt_xchacha20poly1305
16+ encrypt_chacha20poly1305
1817)
1918from core .crypto import (
2019 generate_shared_secrets ,
3332 ML_KEM_1024_CT_LEN ,
3433 ML_DSA_87_NAME ,
3534 ML_DSA_87_SIGN_LEN ,
36- CLASSIC_MCELIECE_8_F_NAME ,
37- CLASSIC_MCELIECE_8_F_CT_LEN ,
38- XCHACHA20POLY1305_NONCE_LEN
35+ CLASSIC_MCELIECE_8_NAME ,
36+ CLASSIC_MCELIECE_8_CT_LEN ,
37+ CHACHA20POLY1305_NONCE_LEN
3938
4039)
4140from base64 import b64encode
@@ -57,7 +56,7 @@ def generate_and_send_pads(user_data, user_data_lock, contact_id: str, ui_queue)
5756 auth_token = user_data ["token" ]
5857
5958 contact_kyber_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][ML_KEM_1024_NAME ]
60- contact_mceliece_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][CLASSIC_MCELIECE_8_F_NAME ]
59+ contact_mceliece_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][CLASSIC_MCELIECE_8_NAME ]
6160 our_lt_private_key = user_data ["contacts" ][contact_id ]["lt_sign_keys" ]["our_keys" ]["private_key" ]
6261
6362 our_next_strand_key = user_data ["contacts" ][contact_id ]["our_next_strand_key" ]
@@ -67,22 +66,22 @@ def generate_and_send_pads(user_data, user_data_lock, contact_id: str, ui_queue)
6766 session_headers = user_data ["tmp" ]["session_headers" ]
6867
6968 kyber_ciphertext_blob , kyber_shared_secrets = generate_shared_secrets (contact_kyber_public_key , ML_KEM_1024_NAME )
70- mceliece_ciphertext_blob , mceliece_shared_secrets = generate_shared_secrets (contact_mceliece_public_key , CLASSIC_MCELIECE_8_F_NAME )
69+ mceliece_ciphertext_blob , mceliece_shared_secrets = generate_shared_secrets (contact_mceliece_public_key , CLASSIC_MCELIECE_8_NAME )
7170
72- xchacha_shared_secrets = b''
73- while len (xchacha_shared_secrets ) < OTP_PAD_SIZE :
74- xchacha_shared_secrets += sha3_512 (secrets .token_bytes (64 ))
71+ chacha_shared_secrets = b''
72+ while len (chacha_shared_secrets ) < OTP_PAD_SIZE :
73+ chacha_shared_secrets += sha3_512 (secrets .token_bytes (64 ))
7574
7675
7776 otp_batch_signature = create_signature (ML_DSA_87_NAME , kyber_ciphertext_blob + mceliece_ciphertext_blob , our_lt_private_key )
7877
7978 # Here, the strandkey is actually just added to make messages structure uniform and easier to process in implementations
8079 # once contact receives this, he will save this new random key, then, process the batch, and save the new key derived from the batch.
8180
82- new_strand_nonce = sha3_512 (secrets .token_bytes (XCHACHA20POLY1305_NONCE_LEN ))[:XCHACHA20POLY1305_NONCE_LEN ]
83- _ , ciphertext_blob = encrypt_xchacha20poly1305 (
81+ new_strand_nonce = sha3_512 (secrets .token_bytes (CHACHA20POLY1305_NONCE_LEN ))[:CHACHA20POLY1305_NONCE_LEN ]
82+ _ , ciphertext_blob = encrypt_chacha20poly1305 (
8483 our_next_strand_key ,
85- sha3_512 (secrets .token_bytes (32 ))[:32 ] + new_strand_nonce + MSG_TYPES ["MSG_BATCH" ] + otp_batch_signature + kyber_ciphertext_blob + mceliece_ciphertext_blob + xchacha_shared_secrets ,
84+ sha3_512 (secrets .token_bytes (32 ))[:32 ] + new_strand_nonce + MSG_TYPES ["MSG_BATCH" ] + otp_batch_signature + kyber_ciphertext_blob + mceliece_ciphertext_blob + chacha_shared_secrets ,
8685 nonce = our_next_strand_nonce
8786 )
8887
@@ -101,7 +100,7 @@ def generate_and_send_pads(user_data, user_data_lock, contact_id: str, ui_queue)
101100
102101 # XOR shared secrets together for hybrid encryption
103102 pads , _ = one_time_pad (kyber_shared_secrets , mceliece_shared_secrets )
104- pads , _ = one_time_pad (pads , xchacha_shared_secrets )
103+ pads , _ = one_time_pad (pads , chacha_shared_secrets )
105104
106105 # Derive key from pad + truncate it.
107106 new_strand_key = pads [:32 ]
@@ -138,7 +137,7 @@ def send_message_processor(user_data, user_data_lock, contact_id: str, message:
138137 session_headers = user_data ["tmp" ]["session_headers" ]
139138
140139 contact_kyber_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][ML_KEM_1024_NAME ]
141- contact_mceliece_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][CLASSIC_MCELIECE_8_F_NAME ]
140+ contact_mceliece_public_key = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["contact_public_keys" ][CLASSIC_MCELIECE_8_NAME ]
142141
143142 our_pads = user_data ["contacts" ][contact_id ]["our_pads" ]
144143
@@ -197,7 +196,7 @@ def send_message_processor(user_data, user_data_lock, contact_id: str, message:
197196 # which would break all of our security
198197
199198 new_strand_key = sha3_512 (secrets .token_bytes (32 ))[:32 ]
200- new_strand_nonce = sha3_512 (secrets .token_bytes (XCHACHA20POLY1305_NONCE_LEN ))[:XCHACHA20POLY1305_NONCE_LEN ]
199+ new_strand_nonce = sha3_512 (secrets .token_bytes (CHACHA20POLY1305_NONCE_LEN ))[:CHACHA20POLY1305_NONCE_LEN ]
201200
202201 with user_data_lock :
203202 user_data ["contacts" ][contact_id ]["our_pads" ] = user_data ["contacts" ][contact_id ]["our_pads" ][len (message_encrypted ):]
@@ -210,7 +209,7 @@ def send_message_processor(user_data, user_data_lock, contact_id: str, message:
210209
211210 save_account_data (user_data , user_data_lock )
212211
213- _ , ciphertext_blob = encrypt_xchacha20poly1305 (
212+ _ , ciphertext_blob = encrypt_chacha20poly1305 (
214213 our_next_strand_key ,
215214 new_strand_key + new_strand_nonce + MSG_TYPES ["MSG_NEW" ] + message_encrypted ,
216215 nonce = our_next_strand_nonce
@@ -275,14 +274,14 @@ def messages_data_handler(user_data: dict, user_data_lock, user_data_copied: dic
275274
276275 # /32 because KEM shared_secret is 32 bytes, /64 because sha3_512 output is 64 bytes
277276
278- if len (msgs_plaintext ) != ( (ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_F_CT_LEN ) * (OTP_PAD_SIZE // 32 )) + (64 * (OTP_PAD_SIZE // 64 )) + ML_DSA_87_SIGN_LEN + 1 :
277+ if len (msgs_plaintext ) != ( (ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_CT_LEN ) * (OTP_PAD_SIZE // 32 )) + (64 * (OTP_PAD_SIZE // 64 )) + ML_DSA_87_SIGN_LEN + 1 :
279278 logger .error ("Contact (%s) gave us a otp batch message request with malformed strand plaintext length (%d)" , contact_id , len (msgs_plaintext ))
280279 return
281280
282281 otp_hashchain_signature = msgs_plaintext [1 : ML_DSA_87_SIGN_LEN + 1 ]
283- otp_hashchain_ciphertext = msgs_plaintext [ML_DSA_87_SIGN_LEN + 1 : ML_DSA_87_SIGN_LEN + 1 + ((ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_F_CT_LEN ) * (OTP_PAD_SIZE // 32 ))]
282+ otp_hashchain_ciphertext = msgs_plaintext [ML_DSA_87_SIGN_LEN + 1 : ML_DSA_87_SIGN_LEN + 1 + ((ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_CT_LEN ) * (OTP_PAD_SIZE // 32 ))]
284283
285- xchacha_pads = msgs_plaintext [ML_DSA_87_SIGN_LEN + 1 + ((ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_F_CT_LEN ) * (OTP_PAD_SIZE // 32 )):]
284+ chacha_pads = msgs_plaintext [ML_DSA_87_SIGN_LEN + 1 + ((ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_CT_LEN ) * (OTP_PAD_SIZE // 32 )):]
286285
287286 try :
288287 valid_signature = verify_signature (ML_DSA_87_NAME , otp_hashchain_ciphertext , otp_hashchain_signature , contact_public_key )
@@ -294,7 +293,7 @@ def messages_data_handler(user_data: dict, user_data_lock, user_data_copied: dic
294293 return
295294
296295 our_kyber_key = user_data_copied ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][ML_KEM_1024_NAME ]["private_key" ]
297- our_mceliece_key = user_data_copied ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_F_NAME ]["private_key" ]
296+ our_mceliece_key = user_data_copied ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_NAME ]["private_key" ]
298297
299298 try :
300299 contact_kyber_pads = decrypt_shared_secrets (otp_hashchain_ciphertext [:ML_KEM_1024_CT_LEN * (OTP_PAD_SIZE // 32 )], our_kyber_key , ML_KEM_1024_NAME )
@@ -303,13 +302,13 @@ def messages_data_handler(user_data: dict, user_data_lock, user_data_copied: dic
303302 return
304303
305304 try :
306- contact_mceliece_pads = decrypt_shared_secrets (otp_hashchain_ciphertext [ML_KEM_1024_CT_LEN * (OTP_PAD_SIZE // 32 ):], our_mceliece_key , CLASSIC_MCELIECE_8_F_NAME )
305+ contact_mceliece_pads = decrypt_shared_secrets (otp_hashchain_ciphertext [ML_KEM_1024_CT_LEN * (OTP_PAD_SIZE // 32 ):], our_mceliece_key , CLASSIC_MCELIECE_8_NAME )
307306 except Exception as e :
308307 logger .error ("Failed to decrypt Classic-McEliece8192128's ciphertext from contact (%s), received error: %s" , contact_id , str (e ))
309308 return
310309
311310 contact_pads , _ = one_time_pad (contact_kyber_pads , contact_mceliece_pads )
312- contact_pads , _ = one_time_pad (contact_pads , xchacha_pads )
311+ contact_pads , _ = one_time_pad (contact_pads , chacha_pads )
313312
314313 contact_next_strand_key = contact_pads [:32 ]
315314 contact_pads = contact_pads [32 :]
@@ -320,11 +319,11 @@ def messages_data_handler(user_data: dict, user_data_lock, user_data_copied: dic
320319
321320 user_data ["contacts" ][contact_id ]["contact_next_strand_key" ] = contact_next_strand_key
322321
323- user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_F_NAME ]["rotation_counter" ] += 1
322+ user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_NAME ]["rotation_counter" ] += 1
324323
325324 staged_kyber_private_key = bool (user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["staged_keys" ][ML_KEM_1024_NAME ]["private_key" ])
326325
327- rotation_counter = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_F_NAME ]["rotation_counter" ]
326+ rotation_counter = user_data ["contacts" ][contact_id ]["ephemeral_keys" ]["our_keys" ][CLASSIC_MCELIECE_8_NAME ]["rotation_counter" ]
328327
329328
330329 logger .debug ("Incremented McEliece's rotation_counter by 1 (now is %d) for contact (%s)" , rotation_counter , contact_id )
0 commit comments