@@ -285,12 +285,14 @@ impl IHandshakeState for InitiatorAwaitingActTwoState {
285
285
hash,
286
286
) ?;
287
287
288
+ let mut act_three = EMPTY_ACT_THREE ;
289
+
288
290
// start serializing act three
289
291
// 1. c = encryptWithAD(temp_k2, 1, h, s.pub.serializeCompressed())
290
- let tagged_encrypted_pubkey = chacha:: encrypt ( & temporary_key, 1 , & hash, & initiator_static_public_key. serialize ( ) ) ;
292
+ chacha:: encrypt ( & temporary_key, 1 , & hash, & initiator_static_public_key. serialize ( ) , & mut act_three [ 1 .. 50 ] ) ;
291
293
292
294
// 2. h = SHA-256(h || c)
293
- let hash = concat_then_sha256 ! ( hash, tagged_encrypted_pubkey ) ;
295
+ let hash = concat_then_sha256 ! ( hash, act_three [ 1 .. 50 ] ) ;
294
296
295
297
// 3. se = ECDH(s.priv, re)
296
298
let ecdh = ecdh ( & initiator_static_private_key, & responder_ephemeral_public_key) ;
@@ -299,7 +301,7 @@ impl IHandshakeState for InitiatorAwaitingActTwoState {
299
301
let ( chaining_key, temporary_key) = hkdf:: derive ( & chaining_key, & ecdh) ;
300
302
301
303
// 5. t = encryptWithAD(temp_k3, 0, h, zero)
302
- let authentication_tag = chacha:: encrypt ( & temporary_key, 0 , & hash, & [ 0 ; 0 ] ) ;
304
+ chacha:: encrypt ( & temporary_key, 0 , & hash, & [ 0 ; 0 ] , & mut act_three [ 50 .. ] ) ;
303
305
304
306
// 6. sk, rk = HKDF(ck, zero)
305
307
let ( sending_key, receiving_key) = hkdf:: derive ( & chaining_key, & [ 0 ; 0 ] ) ;
@@ -308,11 +310,6 @@ impl IHandshakeState for InitiatorAwaitingActTwoState {
308
310
// - done by Conduit
309
311
let conduit = Conduit :: new ( sending_key, receiving_key, chaining_key) ;
310
312
311
- // Send m = 0 || c || t over the network buffer
312
- let mut act_three = EMPTY_ACT_THREE ;
313
- act_three[ 1 ..50 ] . copy_from_slice ( & tagged_encrypted_pubkey) ;
314
- act_three[ 50 ..] . copy_from_slice ( & authentication_tag) ;
315
-
316
313
Ok ( (
317
314
Some ( Act :: Three ( act_three) ) ,
318
315
HandshakeState :: Complete ( Some ( ( conduit, responder_static_public_key) ) )
@@ -352,7 +349,7 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
352
349
// 2. Parse the read message (m) into v, c, and t
353
350
let version = act_three_bytes[ 0 ] ;
354
351
let tagged_encrypted_pubkey = & act_three_bytes[ 1 ..50 ] ;
355
- let chacha_tag = & act_three_bytes[ 50 ..66 ] ;
352
+ let chacha_tag = & act_three_bytes[ 50 ..] ;
356
353
357
354
// 3. If v is an unrecognized handshake version, then the responder MUST abort the connection attempt.
358
355
if version != 0 {
@@ -361,8 +358,9 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
361
358
}
362
359
363
360
// 4. rs = decryptWithAD(temp_k2, 1, h, c)
364
- let remote_pubkey_vec = chacha:: decrypt ( & temporary_key, 1 , & hash, & tagged_encrypted_pubkey) ?;
365
- let initiator_pubkey = if let Ok ( public_key) = PublicKey :: from_slice ( remote_pubkey_vec. as_slice ( ) ) {
361
+ let mut remote_pubkey = [ 0 ; 33 ] ;
362
+ chacha:: decrypt ( & temporary_key, 1 , & hash, & tagged_encrypted_pubkey, & mut remote_pubkey) ?;
363
+ let initiator_pubkey = if let Ok ( public_key) = PublicKey :: from_slice ( & remote_pubkey) {
366
364
public_key
367
365
} else {
368
366
return Err ( "invalid remote public key" . to_string ( ) ) ;
@@ -378,7 +376,7 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
378
376
let ( chaining_key, temporary_key) = hkdf:: derive ( & chaining_key, & ecdh) ;
379
377
380
378
// 8. p = decryptWithAD(temp_k3, 0, h, t)
381
- let _tag_check = chacha:: decrypt ( & temporary_key, 0 , & hash, & chacha_tag) ?;
379
+ chacha:: decrypt ( & temporary_key, 0 , & hash, & chacha_tag, & mut [ 0 ; 0 ] ) ?;
382
380
383
381
// 9. rk, sk = HKDF(ck, zero)
384
382
let ( receiving_key, sending_key) = hkdf:: derive ( & chaining_key, & [ 0 ; 0 ] ) ;
@@ -434,15 +432,13 @@ fn calculate_act_message(local_private_ephemeral_key: &SecretKey, local_public_e
434
432
435
433
// 5. ACT1: c = encryptWithAD(temp_k1, 0, h, zero)
436
434
// 5. ACT2: c = encryptWithAD(temp_k2, 0, h, zero)
437
- let tagged_ciphertext = chacha:: encrypt ( & temporary_key, 0 , & hash, & [ 0 ; 0 ] ) ;
435
+ chacha:: encrypt ( & temporary_key, 0 , & hash, & [ 0 ; 0 ] , & mut act_out [ 34 .. ] ) ;
438
436
439
437
// 6. h = SHA-256(h || c)
440
- let hash = concat_then_sha256 ! ( hash, tagged_ciphertext ) ;
438
+ let hash = concat_then_sha256 ! ( hash, & act_out [ 34 .. ] ) ;
441
439
442
440
// Send m = 0 || e.pub.serializeCompressed() || c
443
-
444
441
act_out[ 1 ..34 ] . copy_from_slice ( & serialized_local_public_key) ;
445
- act_out[ 34 ..50 ] . copy_from_slice ( & tagged_ciphertext) ;
446
442
447
443
( hash, chaining_key, temporary_key)
448
444
}
@@ -455,7 +451,7 @@ fn process_act_message(act_bytes: &[u8], local_private_key: &SecretKey, chaining
455
451
// 2.Parse the read message (m) into v, re, and c
456
452
let version = act_bytes[ 0 ] ;
457
453
let ephemeral_public_key_bytes = & act_bytes[ 1 ..34 ] ;
458
- let chacha_tag = & act_bytes[ 34 ..50 ] ;
454
+ let chacha_tag = & act_bytes[ 34 ..] ;
459
455
460
456
let ephemeral_public_key = if let Ok ( public_key) = PublicKey :: from_slice ( & ephemeral_public_key_bytes) {
461
457
public_key
@@ -481,7 +477,7 @@ fn process_act_message(act_bytes: &[u8], local_private_key: &SecretKey, chaining
481
477
let ( chaining_key, temporary_key) = hkdf:: derive ( & chaining_key, & ecdh) ;
482
478
483
479
// 7. p = decryptWithAD(temp_k1, 0, h, c)
484
- let _tag_check = chacha:: decrypt ( & temporary_key, 0 , & hash, & chacha_tag) ?;
480
+ chacha:: decrypt ( & temporary_key, 0 , & hash, & chacha_tag, & mut [ 0 ; 0 ] ) ?;
485
481
486
482
// 8. h = SHA-256(h || c)
487
483
let hash = concat_then_sha256 ! ( hash, chacha_tag) ;
0 commit comments