summaryrefslogtreecommitdiff
path: root/e5/e5837f539515fef588ac0837d97cc82bd3a941
blob: c6e488fe10307ba0e6e7aa64176f039bed9ae607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
Return-Path: <dev@jonasschnelli.ch>
Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org
	[172.17.192.35])
	by mail.linuxfoundation.org (Postfix) with ESMTPS id 8D37D1D90
	for <bitcoin-dev@lists.linuxfoundation.org>;
	Fri, 22 Mar 2019 21:11:09 +0000 (UTC)
X-Greylist: delayed 00:06:12 by SQLgrey-1.7.6
Received: from bitcoin.jonasschnelli.ch (bitcoinsrv.jonasschnelli.ch
	[138.201.55.219])
	by smtp1.linuxfoundation.org (Postfix) with ESMTP id CA43919B
	for <bitcoin-dev@lists.linuxfoundation.org>;
	Fri, 22 Mar 2019 21:11:05 +0000 (UTC)
Received: by bitcoin.jonasschnelli.ch (Postfix, from userid 1002)
	id E13A415E4FD4; Fri, 22 Mar 2019 22:04:51 +0100 (CET)
X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on
	smtp1.linux-foundation.org
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,HTML_MESSAGE,
	MIME_QP_LONG_LINE autolearn=ham version=3.3.1
Received: from [192.168.0.3] (cable-static-236-151.teleport.ch
	[213.188.236.151])
	by bitcoin.jonasschnelli.ch (Postfix) with ESMTPSA id 8381A15E4721
	for <bitcoin-dev@lists.linuxfoundation.org>;
	Fri, 22 Mar 2019 22:04:50 +0100 (CET)
From: Jonas Schnelli <dev@jonasschnelli.ch>
Content-Type: multipart/signed;
	boundary="Apple-Mail=_D4FB7183-E1A8-4BFC-AF55-2484312B4577";
	protocol="application/pgp-signature"; micalg=pgp-sha256
Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\))
Message-Id: <26A4BEC6-403C-4534-8A2D-57C71D1736FB@jonasschnelli.ch>
Date: Fri, 22 Mar 2019 22:04:46 +0100
To: Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
X-Mailer: Apple Mail (2.3445.102.3)
X-Virus-Scanned: clamav-milter 0.100.2 at bitcoinsrv.jonasschnelli.ch
X-Virus-Status: Clean
X-Mailman-Approved-At: Fri, 22 Mar 2019 23:16:11 +0000
Subject: [bitcoin-dev] New BIP - v2 peer-to-peer message transport protocol
	(former BIP151)
X-BeenThere: bitcoin-dev@lists.linuxfoundation.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: Bitcoin Protocol Discussion <bitcoin-dev.lists.linuxfoundation.org>
List-Unsubscribe: <https://lists.linuxfoundation.org/mailman/options/bitcoin-dev>,
	<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=unsubscribe>
List-Archive: <http://lists.linuxfoundation.org/pipermail/bitcoin-dev/>
List-Post: <mailto:bitcoin-dev@lists.linuxfoundation.org>
List-Help: <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=help>
List-Subscribe: <https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev>,
	<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=subscribe>
X-List-Received-Date: Fri, 22 Mar 2019 21:11:09 -0000


--Apple-Mail=_D4FB7183-E1A8-4BFC-AF55-2484312B4577
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_8E38DED6-E4A9-4675-B6EB-ADCB2DFFEBD1"


--Apple-Mail=_8E38DED6-E4A9-4675-B6EB-ADCB2DFFEBD1
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hi

The overhauled version of the former BIP151 has fundamental differences =
and deserves (requires?) a new BIP.
Calling it =E2=80=9Ev2 peer-to-peer message transport protocol=E2=80=9C =
is more accurate since it is no longer only about encryption.

The formatted draft proposal can be found here: =
https://gist.github.com/jonasschnelli/c530ea8421b8d0e80c51486325587c52

Significant changes compared to the current available BIP151
* A optimised AEAD construct is now proposed (ChaCha20Poly1305@Bitcoin), =
reducing the required ChaCha20 rounds (compared to the openSSH version).
* introduce NODE_P2P_V2
* 32bytes-per-side =E2=80=9Epseudorandom" key exchange
* the multi message envelope has been removed
* the length of a packet uses now a 3-byte integer with 23 available =
bits
* introduction of short-command-ID (ex.: uint8_t 13 =3D=3D INV, etc.) =
which result in
 some v2 messages require less bandwidth then v1
* the key derivation and what communication direction uses what key is =
now more
 specific

First benchmarks of the used primitives
https://github.com/bitcoin/bitcoin/pull/15519#issuecomment-469705289 =
<https://github.com/bitcoin/bitcoin/pull/15519#issuecomment-469705289>

Benchmark of the AEAD compared to the HASH (double SHA256)
(Indicates that v2 messages may be more performant):
https://github.com/bitcoin/bitcoin/pull/15649#issuecomment-475782376 =
<https://github.com/bitcoin/bitcoin/pull/15649#issuecomment-475782376>


Proposal:

<pre>
  BIP: ???
  Layer: Peer Services
  Title: Version 2 Peer-to-Peer Message Transport Protocol
  Author: Jonas Schnelli <dev@jonasschnelli.ch>
  Status: Draft
  Type: Standards Track
  Created: 2019-03-08
  License: PD
</pre>

=3D=3D Abstract =3D=3D

This BIP describes a new Bitcoin peer to peer transport protocol with
opportunistic encryption.

=3D=3D Motivation =3D=3D

The current peer-to-peer protocol is partially inefficient and in =
plaintext.

With the current unencrypted message transport, BGP hijack, block delay =
attacks
and message tempering are inexpensive and can be executed in a covert =
way
(undetectable MITM)<ref>[https://btc-hijack.ethz.ch/files/btc_hijack.pdf
Hijacking Bitcoin: Routing Attacks on Cryptocurrencies - M. Apostolaki, =
A.
Zohar, L.Vanbever]</ref>.

Adding opportunistic encryption introduces a high risk for attackers of =
being
detected. Peer operators can compare encryption session IDs or use other =
form
of authentication schemes <ref
=
name=3D"bip150">[https://github.com/bitcoin/bips/blob/master/bip-0150.medi=
awiki
BIP150]</ref> to identify an attack.

Each current version 1 Bitcoin peer-to-peer message uses a double-SHA256
checksum truncated to 4 bytes. Roughly the same amount of computation =
power
would be required for encrypting and authenticating a peer-to-peer =
message with
ChaCha20 & Poly1305.

Additionally, this BIP describes a way how data manipulation (blocking =
or
tempering commands by an intercepting TCP/IP node) would be identifiable =
by the
communicating peers.

Encrypting traffic between peers is already possible with VPN, tor, =
stunnel,
curveCP or any other encryption mechanism on a deeper OSI level, =
however, most
of those solutions require significant knowhow in how to setup such a =
secure
channel and are therefore not widely deployed.

=3D=3D Specification =3D=3D

<blockquote>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", =
"SHOULD",
"SHOULD NOT", "RECOMMENDED",  "MAY", and "OPTIONAL" in this document are =
to be
interpreted as described in RFC =
2119<ref>[https://tools.ietf.org/html/rfc2119
RFC 2119]</ref>.
</blockquote>

A peer that supports the message transport protocol as defined in this =
proposal
MUST accept encryption requests from all peers.

Both communication direction share the same shared-secret but have =
different
symmetric cipher keys.

The encryption handshake MUST happen before sending any other messages =
to the
responding peer.

If the responding peer closes the connection after sending the handshake
request, the initiating peer MAY try to connect again with the v1 =
peer-to-peer
transport protocol. Such reconnects allow an attacker to "downgrade" the
encryption to plaintext communication and thus, accepting v1 connections =
MUST
not be done when the Bitcoin peer-to-peer network uses almost only v2
communication.


=3D=3D=3D NODE_P2P_V2 =3D=3D=3D

Peers supporting the transport protocol after this proposal MUST signal
<code>NODE_P2P_V2</code>
<pre>
NODE_P2P_V2 =3D (1 << 11)
</pre>

A peer usually learns an address along with the expected service flags =
which
MAY be used to filter possible outbound peers.

A peer signaling <code>NODE_P2P_V2</code> MUST accept encrypted =
communication
specified in this proposal.

Peers MAY only make outbound connections to peers supporting
<code>NODE_P2P_V2</code>.

=3D=3D=3D Handshake =3D=3D=3D

<pre>
 =
--------------------------------------------------------------------------=
--------------
 | Initiator                             Responder                       =
               |
 |                                                                       =
               |
 | x, X         :=3D SECP256k1_KEYGEN()                                  =
                 |
 | CLIENT_HDATA :=3D X                                                   =
                 |
 |                                                                       =
               |
 |               --- CLIENT_HDATA --->                                   =
               |
 |                                                                       =
               |
 |                                       y, Y           :=3D =
SECP256k1_KEYGEN()           |
 |                                       ECDH_KEY       :=3D =
SECP256k1_ECDH(X,y)          |
 |                                       SERVER_HDATA   :=3D Y           =
                 |
 |                                                                       =
               |
 |               <-- SERVER_HDATA ----                                   =
               |
 |                                                                       =
               |
 | ECDH_KEY     :=3D SECP256k1_ECDH(x,Y)                                 =
                 |
 =
--------------------------------------------------------------------------=
--------------
</pre>

To request encrypted communication (only possible if yet no other =
messages have
been sent or received), the initiating peer generates an EC secp256k1 =
ephemeral
key and sends the corresponding 32-byte public key to the responding =
peer and
waits for the remote 32-byte public key from the counterparty.

ODD secp256k1 public keys MUST be used (public keys starting with 0x02). =
If the
public key from the generated ephemeral key is an EVEN public key =
(starting
with 0x03), negating the key and recalculating its public key SHOULD be =
done.
Only using ODD public makes it more complex to identify the handshake =
based on
analyzing the traffic.

The handshake request and response message are raw 32byte payloads =
containing
no header, length or checksum (the pure 32byte payload) and MUST be sent =
before
anything else.

Public keys starting with the 4-byte network magic are forbidden and =
MUST lead
to locally re-generate an ephemeral-key.

Pseudocode for the ephemeral-key generation
<pre>
do {
    ecdh_key.MakeNewKey();
    if (ecdh_key.GetPubKey()[0] =3D=3D 3) {
        ecdh_key.Negate();
    }
} while (m_ecdh_key.GetPubKey()[0..3] =3D=3D NETWORK_MAGIC);
</pre>

Once a peer has received the public key from its counterparty, the =
shared
secret MUST be calculated by using secp256k1 ECDH.

Private keys will never be transmitted. The shared secret can only be
calculated if an attacker knows at least one private key and the =
counterparties
public key. This key-exchange is based on the discrete log problem and =
thus not
sufficiently strong against known forms of possible quantum computer
algorithms. Adding an additional quantum resistant key exchange like =
NewHope is
possible but out of scope for this proposal.

After a successful handshake, the messages format MUST use the "v2 =
messages
structure". Non-encrypted v1 messages from the initiating peer MUST lead =
to an
immediate connection termination.

After a successful handshake, both peers MUST cleanse the =
ephemeral-session-key
from memory and/or persistence storage.

A peer not supporting this proposal will not perform the described =
handshake
and thus send a v1 version message.
Peers supporting this BIP MAY optionally allow unencrypted v1 =
communication by
detecting a v1 version message by the initial 11-byte sequence of =
<code>4byte
net magic || "version"</code>.

=3D=3D=3D Symmetric Encryption Cipher Keys =3D=3D=3D

Once the ECDH secret (<code>ECDH_KEY</code>) is calculated on each side, =
the
symmetric encryption cipher keys MUST be derived with HKDF
<ref>[https://tools.ietf.org/html/rfc5869 HKDF (RFC 5869)]</ref> after =
the
following specification:

1. HKDF extraction
<code>PRK =3D HKDF_EXTRACT(hash=3DSHA256, =
salt=3D"BitcoinSharedSecret||INITIATOR_32BYTES_PUBKEY||RESPONDER_32BYTES_P=
UBKEY", ikm=3DECDH_KEY)</code>.

2. Derive Key_1_A (K_1 communication direction A)
<code>K1A =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_1_A",=
 L=3D32)</code>

2. Derive Key_2_A (K_2 communication direction A)
<code>K1B =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_2_A",=
 L=3D32)</code>

3. Derive Key_1_B (K_1 communication direction B)
<code>K2 =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_1_B", =
L=3D32)</code>

3. Derive Key_2_B (K_2 communication direction B)
<code>K2 =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_2_B", =
L=3D32)</code>

=3D=3D=3D Session ID =3D=3D=3D

Both parties MUST also calculate the 256bit session-id using <code>SID =3D=

HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinSessionID", =
L=3D32)</code>. The
session-id can be used for authenticating the encryption-session =
(identity
check).

The session-id MUST be presented to the user on request.

=3D=3D=3D ChaCha20-Poly1305@Bitcoin Cipher Suite =3D=3D=3D

=3D=3D=3D=3D Background =3D=3D=3D=3D

ChaCha20 is a stream cipher designed by Daniel Bernstein and described =
in
<ref>[http://cr.yp.to/chacha/chacha-20080128.pdf ChaCha20]</ref>. It =
operates
by permuting 128 fixed bits, 128 or 256 bits of key, a 64 bit nonce and =
a 64
bit counter into 64 bytes of output. This output is used as a keystream, =
with
any unused bytes simply discarded.

Poly1305 <ref>[http://cr.yp.to/mac/poly1305-20050329.pdf =
Poly1305]</ref>, also
by Daniel Bernstein, is a one-time Carter-Wegman MAC that computes a 128 =
bit
integrity tag given a message and a single-use 256 bit secret key.

The chacha20-poly1305@bitcoin combines these two primitives into an
authenticated encryption mode. The construction used is based on that =
proposed
for TLS by Adam Langley in
<ref>[http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-03 =
"ChaCha20
and Poly1305 based Cipher Suites for TLS", Adam Langley]</ref>, but =
differs in
the layout of data passed to the MAC and in the addition of encryption =
of the
packet lengths.

=3D=3D=3D=3D Detailed Construction =3D=3D=3D=3D

The chacha20-poly1305@bitcoin cipher requires two 256 bits of key =
material as
output from the key exchange. Each key (K_1 and K_2) are used by two =
separate
instances of chacha20.

The instance keyed by K_1 is a stream cipher that is used only to =
encrypt the 3
byte packet length field and has its own sequence number. The second =
instance,
keyed by K_2, is used in conjunction with poly1305 to build an AEAD
(Authenticated Encryption with Associated Data) that is used to encrypt =
and
authenticate the entire packet.

Two separate cipher instances are used here so as to keep the packet =
lengths
confidential but not create an oracle for the packet payload cipher by
decrypting and using the packet length prior to checking the MAC. By =
using an
independently-keyed cipher instance to encrypt the length, an active =
attacker
seeking to exploit the packet input handling as a decryption oracle can =
learn
nothing about the payload contents or its MAC (assuming key derivation,
ChaCha20 and Poly1305 are secure).

The AEAD is constructed as follows: for each packet, generate a Poly1305 =
key by
taking the first 256 bits of ChaCha20 stream output generated using K_2, =
an IV
consisting of the packet sequence number encoded as an LE uint64 and a =
ChaCha20
block counter of zero. The K_2 ChaCha20 block counter is then set to the
little-endian encoding of 1 (i.e. {1, 0, 0, 0, 0, 0, 0, 0}) and this =
instance
is used for encryption of the packet payload.

=3D=3D=3D=3D Packet Handling =3D=3D=3D=3D

When receiving a packet, the length must be decrypted first. When 3 =
bytes of
ciphertext length have been received, they may be decrypted.

A ChaCha20 round always calculates 64bytes which is sufficient to crypt =
21
times a 3 bytes length field (21*3 =3D 63). The length field sequence =
number can
thus be used 21 times (keystream caching).

The length field must be enc-/decrypted with the ChaCha20 keystream =
keyed with
K_1 defined by block counter 0, the length field sequence number in =
little
endian and a keystream position from 0 to 60.

Pseudo code example:
<pre>
// init
sequence_nr_payload =3D 0; //payload sequence number
sequence_nr_length_field =3D 0; //length field sequence number (will be =
reused)
aad_length_field_pos =3D 0; //position in the length field cipher =
instance keystream chunk

...

// actual encryption
if cache_length_field_sequence_number !=3D sequence_nr_length_field {
  cache_keystream_64_bytes =3D ChaCha20(key=3DK_1, =
iv=3Dlittle_endian(sequence_nr_length_field), counter=3D0);
  cache_length_field_sequence_number =3D sequence_nr_length_field
}
packet_length =3D =
XOR_TO_LE(cache_length_field_sequence_number[aad_length_field_pos - =
aad_length_field_pos+3], ciphertext[0-3])

sequence_nr_payload++;
aad_length_field_pos +=3D 3; //skip 3 bytes in keystream
if (aad_length_field_pos + 3 > 64) { //if we are outside of the 64byte =
keystream...
  aad_length_field_pos =3D 0; // reset at position 0
  sequence_nr_length_field++; // increase length field sequence number
}
</pre>

Once the entire packet has been received, the MAC MUST be checked before
decryption. A per-packet Poly1305 key is generated as described above =
and the
MAC tag calculated using Poly1305 with this key over the ciphertext of =
the
packet length and the payload together. The calculated MAC is then =
compared in
constant time with the one appended to the packet and the packet =
decrypted
using ChaCha20 as described above (with K_2, the packet sequence number =
as
nonce and a starting block counter of 1).

Detection of an invalid MAC MUST lead to immediate connection =
termination.

To send a packet, first encode the 3 byte length and encrypt it using =
K_1 as
described above. Encrypt the packet payload (using K_2) and append it to =
the
encrypted length. Finally, calculate a MAC tag and append it.

The initiating peer MUST use <code>K_1_A, K_2_A</code> to encrypt =
messages on
the send channel, <code>K_1_B, K_2_B</code> MUST be used to decrypt =
messages on
the receive channel.

The responding peer MUST use <code>K_1_A, K_2_A</code> to decrypt =
messages on
the receive channel, <code>K_1_B, K_2_B</code> MUST be used to encrypt =
messages
on the send channel.

Optimized implementations of ChaCha20-Poly1305@bitcoin are relatively =
fast in
general, therefore it is very likely that encrypted messages require not =
more
CPU cycles per bytes then the current unencrypted p2p message format
(ChaCha20/Poly1305 versus double SHA256).

The initial packet sequence numbers are 0.

K_2 ChaCha20 cipher instance (payload) must never reuse a {key, nonce} =
for
encryption nor may it be used to encrypt more than 2^70 bytes under the =
same
{key, nonce}.

K_1 ChaCha20 cipher instance (length field/AAD) must never reuse a {key, =
nonce,
position-in-keystream} for encryption nor may it be used to encrypt more =
than
2^70 bytes under the same {key, nonce}.

We use message sequence numbers for both communication directions.

<pre>
 =
--------------------------------------------------------------------------=
----------------
 | Initiator                          Responder                          =
                 |
 |                                                                       =
                 |
 | AEAD() =3D ChaCha20Poly1305Bitcoin()                                  =
                   |
 | MSG_A_CIPH =3D AEAD(k=3DK_1_A, K_2_A, payload_nonce=3D0, aad_nonce=3D0,=
 aad_pos=3D0, msg)        |
 |                                                                       =
                 |
 |                         --- MSG_CIPH --->                             =
                 |
 |                                                                       =
                 |
 |                                    msg   :=3D AEAD(k=3DK_1_A,K_2_A, =
n=3D0, ..., MSG_A_CIPH)  |
 |                                                                       =
                 |
 =
--------------------------------------------------------------------------=
----------------
</pre>

=3D=3D=3D=3D Test Vectors =3D=3D=3D=3D

<pre>
message   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00
k1 (DATA) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00
k2 (AAD)  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00

AAD keystream
76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 bd d2 19 b8 a0 8d ed 1a =
a8 36 ef cc 8b 77 0d c7 da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 =
6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86

ciphertext
76 b8 e0 9f 07 e7 be 55 51 38 7a 98 ba 97 7c 73 2d 08 0d cb 0f 29 a0 48 =
e3 65 69 12 c6 53 3e 32

MAC
d2 fc 11 82 9c 1b 6c 1d f1 f5 51 cd 61 31 ff 08
</pre>

<pre>
message   01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00
k1 (DATA) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00
k2 (AAD)  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00

AAD keystream
76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 bd d2 19 b8 a0 8d ed 1a =
a8 36 ef cc 8b 77 0d c7 da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 =
6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86

ciphertext
77 b8 e0 9f 07 e7 be 55 51 38 7a 98 ba 97 7c 73 2d 08 0d cb 0f 29 a0 48 =
e3 65 69 12 c6 53 3e 32

MAC
ba f0 c8 5b 6d ff 86 02 b0 6c f5 2a 6a ef c6 2e
</pre>

<pre>
message
ff 00 00 f1 95 e6 69 82 10 5f fb 64 0b b7 75 7f 57 9d a3 16 02 fc 93 ec =
01 ac 56 f8 5a c3 c1 34 a4 54 7b 73 3b 46 41 30 42 c9 44 00 49 17 69 05 =
d3 be 59 ea 1c 53 f1 59 16 15 5c 2b e8 24 1a 38 00 8b 9a 26 bc 35 94 1e =
24 44 17 7c 8a de 66 89 de 95 26 49 86 d9 58 89 fb 60 e8 46 29 c9 bd 9a =
5a cb 1c c1 18 be 56 3e b9 b3 a4 a4 72 f8 2e 09 a7 e7 78 49 2b 56 2e f7 =
13 0e 88 df e0 31 c7 9d b9 d4 f7 c7 a8 99 15 1b 9a 47 50 32 b6 3f c3 85 =
24 5f e0 54 e3 dd 5a 97 a5 f5 76 fe 06 40 25 d3 ce 04 2c 56 6a b2 c5 07 =
b1 38 db 85 3e 3d 69 59 66 09 96 54 6c c9 c4 a6 ea fd c7 77 c0 40 d7 0e =
af 46 f7 6d ad 39 79 e5 c5 36 0c 33 17 16 6a 1c 89 4c 94 a3 71 87 6a 94 =
df 76 28 fe 4e aa f2 cc b2 7d 5a aa e0 ad 7a d0 f9 d4 b6 ad 3b 54 09 87 =
46 d4 52 4d 38 40 7a 6d eb 3a b7 8f ab 78 c9

k1 (DATA) 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 =
15 16 17 18 19 1a 1b 1c 1d 1e 1f
k2 (AAD)  ff 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 =
15 16 17 18 19 1a 1b 1c 1d 1e 1f

AAD keystream
c6 40 c1 71 1e 3e e9 04 ac 35 c5 7a b9 79 1c 8a 1c 40 86 03 a9 0b 77 a8 =
3b 54 f6 c8 44 cb 4b 06 d9 4e 7f c6 c8 00 e1 65 ac d6 61 47 e8 0e c4 5a =
56 7f 6c e6 6d 05 ec 0c ae 67 9d ce eb 89 00 17

ciphertext
39 40 c1 e9 2d a4 58 2f f6 f9 2a 77 6a eb 14 d0 14 d3 84 ee b3 0f 66 0d =
ac f7 0a 14 a2 3f d3 1e 91 21 27 01 33 4e 2c e1 ac f5 19 9d c8 4f 4d 61 =
dd be 65 71 bc a5 af 87 4b 4c 92 26 c2 6e 65 09 95 d1 57 64 4e 18 48 b9 =
6e d6 c2 10 2d 54 89 a0 50 e7 1d 29 a5 a6 6e ce 11 de 5f b5 c9 55 8d 54 =
da 28 fe 45 b0 bc 4d b4 e5 b8 80 30 bf c4 a3 52 b4 b7 06 8e cc f6 56 ba =
e7 ad 6a 35 61 53 15 fc 7c 49 d4 20 03 88 d5 ec a6 7c 2e 82 2e 06 93 36 =
c6 9b 40 db 67 e0 f3 c8 12 09 c5 0f 32 16 a4 b8 9f b3 ae 1b 98 4b 78 51 =
a2 ec 6f 68 ab 12 b1 01 ab 12 0e 1e a7 31 3b b9 3b 5a 0f 71 18 5c 7f ea =
01 7d db 92 76 98 61 c2 9d ba 4f bc 43 22 80 d5 df f2 1b 36 d1 c4 c7 90 =
12 8b 22 69 99 50 bb 18 bf 74 c4 48 cd fe 54 7d 8e d4 f6 57 d8 00 5f dc =
0c d7 a0 50 c2 d4 60 50 a4 4c 43 76 35 58 58

MAC
98 1f be 8b 18 42 88 27 6e 7a 93 ea bc 89 9c 4a
</pre>


=3D=3D=3D v2 Messages Structure =3D=3D=3D

{|class=3D"wikitable"
! Field Size !! Description !! Data type !! Comments
|-
| 3 || length & flag || 23 + 1 bits || Encrypted length of ciphertext =
payload (not counting the MAC tag) in number of bytes (only 2^23 is =
usable, most significant bit is the rekey-flag)
|-
| 1-13 || encrypted command || variable || ASCII command (or one byte =
short command ID)
|-
| ? || encrypted payload || ? || The actual data
|-
| 16 || MAC tag || ? || 128bit MAC-tag
|}

Encrypted messages do not have the 4byte network magic.

The maximum message size is 2^23 (8=E2=80=99388=E2=80=99608) bytes. =
Future communication MAY
exceed this limit and thus MUST be split into different messages.

Decrypting and processing the message before the authentication succeeds =
(MAC
verified) MUST not be done.

The 4byte sha256 checksum is no longer required because the AEAD (MAC).

Both peers MUST keep track of the message sequence number (uint32) of =
sent and
received messages for building a 64-bit symmetric cipher IV.

The command field MUST start with a byte that defines the length of the =
ASCII
command string up to 12 chars (1 to 12) or a short command ID (see =
below).

=3D=3D=3D=3D Short Command ID =3D=3D=3D=3D

To save valuable bandwidth, the v2 message format supports message =
command
short IDs for message types with high frequency. The ID/string mapping =
is a
peer to peer arrangement and MAY be negotiated between the initiating =
and
responding peer. A peer conforming to this proposal MUST support short =
IDs
based on the table below and SHOULD use short command IDs for outgoing =
messages.

{|class=3D"wikitable"
! Number !! Command
|-
| 13 || INV
|-
| 14 || HEADERS
|-
| 15 || PING
|-
| 16 || PONG
|-
|}

=3D=3D=3D=3D Length comparisons between v1 and v2 messages =3D=3D=3D=3D

<pre>
v1 in: 4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+36(Payload) =3D=3D =
60
v2 inv: 3(MessageSize&Flag)+1(Command)+36(Payload)+16(MAC) =3D=3D 56
(93.33%)
</pre>

<pre>
v1 ping: 4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+8(Payload) =3D=3D=
 32
v2 pong: 3(MessageSize&Flag)+1(Command)+8(Payload)+16(MAC) =3D=3D 28
(87.5%)
</pre>

<pre>
v1 block: =
4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+1=E2=80=99048=E2=80=99576(=
Payload) =3D 1=E2=80=99048=E2=80=99600
v2 block: 3(MessageSize&Flag)+6(CommandStr)+8(Payload)+16(MAC) =3D=3D 28 =
=3D 1=E2=80=99048=E2=80=99601
(100.000095%)
</pre>

=3D=3D=3D Re-Keying =3D=3D=3D

Re-keying can be signaled by setting the most significant bit in the =
length
field before encryption. A peer signaling a rekey MUST use the next key =
for
encryption messages AFTER the message where the signaling has been done.

A peer identifying a rekey by checking the most significant bit in the =
envelope
length must use the next key for decrypt messages AFTER the message =
where the
signaling has been detected.

The next symmetric cipher key MUST be calculated by =
<code>SHA256(SHA256(session
ID || old_symmetric_cipher_key))</code> and the packet sequence number =
of the
according encryption direction must be set to 0.

Re-Keying interval is a peer policy with a minimum timespan of 10 =
seconds.

The Re-Keying must be done after every 1GB of data sent (recommended by =
RFC4253
SSH Transport) or if the last rekey was more than an hour ago.

Peers calculate the counterparty limits and MUST disconnect immediately =
if a
violation of the limits has been detected.


=3D=3D=3D Risks =3D=3D=3D

The encryption does not include an authentication scheme. This BIP does =
not
cover a proposal to avoid MITM attacks during the encryption =
initialization.
However, peers MUST show the session-id to the user on request which =
allows to
identify a MITM by a manual verification on a secure channel.

Optional authentication schemes may be covered by other proposals <ref
=
name=3D"bip150">[https://github.com/bitcoin/bips/blob/master/bip-0150.medi=
awiki
BIP150]</ref>.

An attacker could delay or halt v2 protocol enforcement by providing a
reasonable amount of peers not supporting the v2 protocol.

=3D=3D Compatibility =3D=3D

This proposal is backward compatible (as long as not enforced). =
Non-supporting
peers can still use unencrypted communications.

=3D=3D Reference implementation =3D=3D
* Complete Bitcoin Core implementation: =
https://github.com/bitcoin/bitcoin/pull/14032
* Reference implementation of the AEAD in C: =
https://github.com/jonasschnelli/chacha20poly1305

=3D=3D References =3D=3D

<references/>

=3D=3D Acknowledgements =3D=3D
* Pieter Wuille and Gregory Maxwell for most of the ideas in this BIP.
* Tim Ruffing for the review and the hint for the enhancement of the =
symmetric
key derivation


=3D=3D Copyright =3D=3D
This work is placed in the public domain.


--Apple-Mail=_8E38DED6-E4A9-4675-B6EB-ADCB2DFFEBD1
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><div =
dir=3D"auto" style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
line-break: after-white-space;" class=3D"">Hi<br class=3D""><br =
class=3D"">The overhauled version of the former BIP151 has fundamental =
differences and deserves (requires?) a new BIP.</div><div dir=3D"auto" =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; line-break: =
after-white-space;" class=3D"">Calling it =E2=80=9Ev2 peer-to-peer =
message transport protocol=E2=80=9C is more accurate since it is no =
longer only about encryption.</div><div dir=3D"auto" style=3D"word-wrap: =
break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" =
class=3D""><br class=3D""></div><div dir=3D"auto" style=3D"word-wrap: =
break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" =
class=3D"">The formatted draft proposal can be found here:&nbsp;<a =
href=3D"https://gist.github.com/jonasschnelli/c530ea8421b8d0e80c5148632558=
7c52" =
class=3D"">https://gist.github.com/jonasschnelli/c530ea8421b8d0e80c5148632=
5587c52</a><br class=3D""><br class=3D"">Significant changes compared to =
the current available BIP151<br class=3D"">* A optimised AEAD construct =
is now proposed (ChaCha20Poly1305@Bitcoin), reducing the required =
ChaCha20 rounds (compared to the openSSH version).<br class=3D"">* =
introduce NODE_P2P_V2<br class=3D"">* 32bytes-per-side =E2=80=9Epseudorand=
om" key exchange<br class=3D"">* the multi message envelope has been =
removed<br class=3D"">* the length of a packet uses now a 3-byte integer =
with 23 available bits<br class=3D"">* introduction of short-command-ID =
(ex.: uint8_t 13 =3D=3D INV, etc.) which result in<br =
class=3D"">&nbsp;some v2 messages require less bandwidth then v1<br =
class=3D"">*&nbsp;the key derivation and what communication direction =
uses what key is now more<br class=3D"">&nbsp;specific<div class=3D""><br =
class=3D""></div><div class=3D"">First benchmarks of the used =
primitives</div><div class=3D""><a =
href=3D"https://github.com/bitcoin/bitcoin/pull/15519#issuecomment-4697052=
89" =
class=3D"">https://github.com/bitcoin/bitcoin/pull/15519#issuecomment-4697=
05289</a></div><div class=3D""><br class=3D""></div><div =
class=3D"">Benchmark of the AEAD compared to the HASH (double =
SHA256)</div><div class=3D"">(Indicates that v2 messages may be more =
performant):</div><div class=3D""><a =
href=3D"https://github.com/bitcoin/bitcoin/pull/15649#issuecomment-4757823=
76" =
class=3D"">https://github.com/bitcoin/bitcoin/pull/15649#issuecomment-4757=
82376</a></div><div class=3D""><br class=3D""></div><div class=3D""><br =
class=3D""></div><div class=3D"">Proposal:</div><div class=3D""><br =
class=3D""></div><div class=3D""><div class=3D"">&lt;pre&gt;</div><div =
class=3D"">&nbsp; BIP: ???</div><div class=3D"">&nbsp; Layer: Peer =
Services</div><div class=3D"">&nbsp; Title: Version 2 Peer-to-Peer =
Message Transport Protocol</div><div class=3D"">&nbsp; Author: Jonas =
Schnelli &lt;dev@jonasschnelli.ch&gt;</div><div class=3D"">&nbsp; =
Status: Draft</div><div class=3D"">&nbsp; Type: Standards =
Track</div><div class=3D"">&nbsp; Created: 2019-03-08</div><div =
class=3D"">&nbsp; License: PD</div><div class=3D"">&lt;/pre&gt;</div><div =
class=3D""><br class=3D""></div><div class=3D"">=3D=3D Abstract =
=3D=3D</div><div class=3D""><br class=3D""></div><div class=3D"">This =
BIP describes a new Bitcoin peer to peer transport protocol =
with&nbsp;</div><div class=3D"">opportunistic encryption.</div><div =
class=3D""><br class=3D""></div><div class=3D"">=3D=3D Motivation =
=3D=3D</div><div class=3D""><br class=3D""></div><div class=3D"">The =
current peer-to-peer protocol is partially inefficient and in =
plaintext.</div><div class=3D""><br class=3D""></div><div class=3D"">With =
the current unencrypted message transport, BGP hijack, block delay =
attacks&nbsp;</div><div class=3D"">and message tempering are inexpensive =
and can be executed in a covert way&nbsp;</div><div =
class=3D"">(undetectable =
MITM)&lt;ref&gt;[https://btc-hijack.ethz.ch/files/btc_hijack.pdf&nbsp;</di=
v><div class=3D"">Hijacking Bitcoin: Routing Attacks on Cryptocurrencies =
- M. Apostolaki, A.&nbsp;</div><div class=3D"">Zohar, =
L.Vanbever]&lt;/ref&gt;.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Adding opportunistic encryption introduces a high risk for =
attackers of being&nbsp;</div><div class=3D"">detected. Peer operators =
can compare encryption session IDs or use other form&nbsp;</div><div =
class=3D"">of authentication schemes &lt;ref&nbsp;</div><div =
class=3D"">name=3D"bip150"&gt;[https://github.com/bitcoin/bips/blob/master=
/bip-0150.mediawiki&nbsp;</div><div class=3D"">BIP150]&lt;/ref&gt; to =
identify an attack.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Each current version 1 Bitcoin peer-to-peer message uses a =
double-SHA256&nbsp;</div><div class=3D"">checksum truncated to 4 bytes. =
Roughly the same amount of computation power&nbsp;</div><div =
class=3D"">would be required for encrypting and authenticating a =
peer-to-peer message with&nbsp;</div><div class=3D"">ChaCha20 &amp; =
Poly1305.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Additionally, this BIP describes a way how data manipulation =
(blocking or&nbsp;</div><div class=3D"">tempering commands by an =
intercepting TCP/IP node) would be identifiable by the&nbsp;</div><div =
class=3D"">communicating peers.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Encrypting traffic between peers is =
already possible with VPN, tor, stunnel,&nbsp;</div><div =
class=3D"">curveCP or any other encryption mechanism on a deeper OSI =
level, however, most&nbsp;</div><div class=3D"">of those solutions =
require significant knowhow in how to setup such a =
secure&nbsp;</div><div class=3D"">channel and are therefore not widely =
deployed.</div><div class=3D""><br class=3D""></div><div class=3D"">=3D=3D=
 Specification =3D=3D</div><div class=3D""><br class=3D""></div><div =
class=3D"">&lt;blockquote&gt;</div><div class=3D"">The key words "MUST", =
"MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",</div><div =
class=3D"">"SHOULD NOT", "RECOMMENDED", &nbsp;"MAY", and "OPTIONAL" in =
this document are to be</div><div class=3D"">interpreted as described in =
RFC 2119&lt;ref&gt;[https://tools.ietf.org/html/rfc2119&nbsp;</div><div =
class=3D"">RFC 2119]&lt;/ref&gt;.</div><div =
class=3D"">&lt;/blockquote&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">A peer that supports the message =
transport protocol as defined in this proposal&nbsp;</div><div =
class=3D"">MUST accept encryption requests from all peers.</div><div =
class=3D""><br class=3D""></div><div class=3D"">Both communication =
direction share the same shared-secret but have =
different&nbsp;</div><div class=3D"">symmetric cipher keys.</div><div =
class=3D""><br class=3D""></div><div class=3D"">The encryption handshake =
MUST happen before sending any other messages to the&nbsp;</div><div =
class=3D"">responding peer.</div><div class=3D""><br class=3D""></div><div=
 class=3D"">If the responding peer closes the connection after sending =
the handshake&nbsp;</div><div class=3D"">request, the initiating peer =
MAY try to connect again with the v1 peer-to-peer&nbsp;</div><div =
class=3D"">transport protocol. Such reconnects allow an attacker to =
"downgrade" the&nbsp;</div><div class=3D"">encryption to plaintext =
communication and thus, accepting v1 connections MUST&nbsp;</div><div =
class=3D"">not be done when the Bitcoin peer-to-peer network uses almost =
only v2&nbsp;</div><div class=3D"">communication.</div><div class=3D""><br=
 class=3D""></div><div class=3D""><br class=3D""></div><div class=3D"">=3D=
=3D=3D NODE_P2P_V2 =3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">Peers supporting the transport protocol =
after this proposal MUST signal&nbsp;</div><div =
class=3D"">&lt;code&gt;NODE_P2P_V2&lt;/code&gt;</div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">NODE_P2P_V2 =3D (1 &lt;&lt; =
11)</div><div class=3D"">&lt;/pre&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">A peer usually learns an address along =
with the expected service flags which&nbsp;</div><div class=3D"">MAY be =
used to filter possible outbound peers.</div><div class=3D""><br =
class=3D""></div><div class=3D"">A peer signaling =
&lt;code&gt;NODE_P2P_V2&lt;/code&gt; MUST accept encrypted =
communication&nbsp;</div><div class=3D"">specified in this =
proposal.</div><div class=3D""><br class=3D""></div><div class=3D"">Peers =
MAY only make outbound connections to peers supporting&nbsp;</div><div =
class=3D"">&lt;code&gt;NODE_P2P_V2&lt;/code&gt;.</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D=3D Handshake =3D=3D=3D</div><div =
class=3D""><br class=3D""></div><div class=3D"">&lt;pre&gt;</div><div =
class=3D"">&nbsp;---------------------------------------------------------=
-------------------------------</div><div class=3D"">&nbsp;| Initiator =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; Responder &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| x, X &nbsp; &nbsp; &nbsp; &nbsp; :=3D=
 SECP256k1_KEYGEN() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |</div><div =
class=3D"">&nbsp;| CLIENT_HDATA :=3D X &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --- =
CLIENT_HDATA ---&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div class=3D"">&nbsp;| =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y, Y =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :=3D SECP256k1_KEYGEN() &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; |</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ECDH_KEY &nbsp; &nbsp; &nbsp; =
:=3D SECP256k1_ECDH(X,y) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; SERVER_HDATA &nbsp; :=3D Y &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&lt;-- SERVER_HDATA ---- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;| ECDH_KEY &nbsp; &nbsp; :=3D SECP256k1_ECDH(x,Y) =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div =
class=3D"">&nbsp;---------------------------------------------------------=
-------------------------------</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">To request encrypted communication (only possible if yet no =
other messages have&nbsp;</div><div class=3D"">been sent or received), =
the initiating peer generates an EC secp256k1 ephemeral&nbsp;</div><div =
class=3D"">key and sends the corresponding 32-byte public key to the =
responding peer and&nbsp;</div><div class=3D"">waits for the remote =
32-byte public key from the counterparty.</div><div class=3D""><br =
class=3D""></div><div class=3D"">ODD secp256k1 public keys MUST be used =
(public keys starting with 0x02). If the&nbsp;</div><div class=3D"">public=
 key from the generated ephemeral key is an EVEN public key =
(starting&nbsp;</div><div class=3D"">with 0x03), negating the key and =
recalculating its public key SHOULD be done.</div><div class=3D"">Only =
using ODD public makes it more complex to identify the handshake based =
on&nbsp;</div><div class=3D"">analyzing the traffic.</div><div =
class=3D""><br class=3D""></div><div class=3D"">The handshake request =
and response message are raw 32byte payloads containing&nbsp;</div><div =
class=3D"">no header, length or checksum (the pure 32byte payload) and =
MUST be sent before&nbsp;</div><div class=3D"">anything else.</div><div =
class=3D""><br class=3D""></div><div class=3D"">Public keys starting =
with the 4-byte network magic are forbidden and MUST =
lead&nbsp;</div><div class=3D"">to locally re-generate an =
ephemeral-key.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Pseudocode for the ephemeral-key generation</div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">do {</div><div =
class=3D"">&nbsp; &nbsp; ecdh_key.MakeNewKey();</div><div =
class=3D"">&nbsp; &nbsp; if (ecdh_key.GetPubKey()[0] =3D=3D 3) =
{</div><div class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; =
ecdh_key.Negate();</div><div class=3D"">&nbsp; &nbsp; }</div><div =
class=3D"">} while (m_ecdh_key.GetPubKey()[0..3] =3D=3D =
NETWORK_MAGIC);</div><div class=3D"">&lt;/pre&gt;</div><div class=3D""><br=
 class=3D""></div><div class=3D"">Once a peer has received the public =
key from its counterparty, the shared&nbsp;</div><div class=3D"">secret =
MUST be calculated by using secp256k1 ECDH.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Private keys will never be transmitted. =
The shared secret can only be&nbsp;</div><div class=3D"">calculated if =
an attacker knows at least one private key and the =
counterparties&nbsp;</div><div class=3D"">public key. This key-exchange =
is based on the discrete log problem and thus not&nbsp;</div><div =
class=3D"">sufficiently strong against known forms of possible quantum =
computer&nbsp;</div><div class=3D"">algorithms. Adding an additional =
quantum resistant key exchange like NewHope is&nbsp;</div><div =
class=3D"">possible but out of scope for this proposal.</div><div =
class=3D""><br class=3D""></div><div class=3D"">After a successful =
handshake, the messages format MUST use the "v2 messages&nbsp;</div><div =
class=3D"">structure". Non-encrypted v1 messages from the initiating =
peer MUST lead to an&nbsp;</div><div class=3D"">immediate connection =
termination.</div><div class=3D""><br class=3D""></div><div =
class=3D"">After a successful handshake, both peers MUST cleanse the =
ephemeral-session-key&nbsp;</div><div class=3D"">from memory and/or =
persistence storage.</div><div class=3D""><br class=3D""></div><div =
class=3D"">A peer not supporting this proposal will not perform the =
described handshake&nbsp;</div><div class=3D"">and thus send a v1 =
version message.</div><div class=3D"">Peers supporting this BIP MAY =
optionally allow unencrypted v1 communication by&nbsp;</div><div =
class=3D"">detecting a v1 version message by the initial 11-byte =
sequence of &lt;code&gt;4byte&nbsp;</div><div class=3D"">net magic || =
"version"&lt;/code&gt;.</div><div class=3D""><br class=3D""></div><div =
class=3D"">=3D=3D=3D Symmetric Encryption Cipher Keys =3D=3D=3D</div><div =
class=3D""><br class=3D""></div><div class=3D"">Once the ECDH secret =
(&lt;code&gt;ECDH_KEY&lt;/code&gt;) is calculated on each side, =
the&nbsp;</div><div class=3D"">symmetric encryption cipher keys MUST be =
derived with HKDF&nbsp;</div><div =
class=3D"">&lt;ref&gt;[https://tools.ietf.org/html/rfc5869 HKDF (RFC =
5869)]&lt;/ref&gt; after the&nbsp;</div><div class=3D"">following =
specification:</div><div class=3D""><br class=3D""></div><div =
class=3D"">1. HKDF extraction</div><div class=3D"">&lt;code&gt;PRK =3D =
HKDF_EXTRACT(hash=3DSHA256, =
salt=3D"BitcoinSharedSecret||INITIATOR_32BYTES_PUBKEY||RESPONDER_32BYTES_P=
UBKEY", ikm=3DECDH_KEY)&lt;/code&gt;.</div><div class=3D""><br =
class=3D""></div><div class=3D"">2. Derive Key_1_A (K_1 communication =
direction A)</div><div class=3D"">&lt;code&gt;K1A =3D =
HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_1_A", =
L=3D32)&lt;/code&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">2. Derive Key_2_A (K_2 communication direction A)</div><div =
class=3D"">&lt;code&gt;K1B =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, =
info=3D"BitcoinK_2_A", L=3D32)&lt;/code&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">3. Derive Key_1_B (K_1 communication =
direction B)</div><div class=3D"">&lt;code&gt;K2 =3D =
HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, info=3D"BitcoinK_1_B", =
L=3D32)&lt;/code&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">3. Derive Key_2_B (K_2 communication direction B)</div><div =
class=3D"">&lt;code&gt;K2 =3D HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, =
info=3D"BitcoinK_2_B", L=3D32)&lt;/code&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D=3D Session ID =3D=3D=3D</div><div =
class=3D""><br class=3D""></div><div class=3D"">Both parties MUST also =
calculate the 256bit session-id using &lt;code&gt;SID =3D&nbsp;</div><div =
class=3D"">HKDF_EXPAND(prk=3DPRK, hash=3DSHA256, =
info=3D"BitcoinSessionID", L=3D32)&lt;/code&gt;. The&nbsp;</div><div =
class=3D"">session-id can be used for authenticating the =
encryption-session (identity&nbsp;</div><div class=3D"">check).</div><div =
class=3D""><br class=3D""></div><div class=3D"">The session-id MUST be =
presented to the user on request.</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D=3D ChaCha20-Poly1305@Bitcoin =
Cipher Suite =3D=3D=3D</div><div class=3D""><br class=3D""></div><div =
class=3D"">=3D=3D=3D=3D Background =3D=3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">ChaCha20 is a stream cipher designed by =
Daniel Bernstein and described in&nbsp;</div><div =
class=3D"">&lt;ref&gt;[http://cr.yp.to/chacha/chacha-20080128.pdf =
ChaCha20]&lt;/ref&gt;. It operates&nbsp;</div><div class=3D"">by =
permuting 128 fixed bits, 128 or 256 bits of key, a 64 bit nonce and a =
64&nbsp;</div><div class=3D"">bit counter into 64 bytes of output. This =
output is used as a keystream, with&nbsp;</div><div class=3D"">any =
unused bytes simply discarded.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Poly1305 =
&lt;ref&gt;[http://cr.yp.to/mac/poly1305-20050329.pdf =
Poly1305]&lt;/ref&gt;, also&nbsp;</div><div class=3D"">by Daniel =
Bernstein, is a one-time Carter-Wegman MAC that computes a 128 =
bit&nbsp;</div><div class=3D"">integrity tag given a message and a =
single-use 256 bit secret key.</div><div class=3D""><br =
class=3D""></div><div class=3D"">The chacha20-poly1305@bitcoin combines =
these two primitives into an&nbsp;</div><div class=3D"">authenticated =
encryption mode. The construction used is based on that =
proposed&nbsp;</div><div class=3D"">for TLS by Adam Langley =
in&nbsp;</div><div =
class=3D"">&lt;ref&gt;[http://tools.ietf.org/html/draft-agl-tls-chacha20po=
ly1305-03 "ChaCha20&nbsp;</div><div class=3D"">and Poly1305 based Cipher =
Suites for TLS", Adam Langley]&lt;/ref&gt;, but differs =
in&nbsp;</div><div class=3D"">the layout of data passed to the MAC and =
in the addition of encryption of the&nbsp;</div><div class=3D"">packet =
lengths.</div><div class=3D""><br class=3D""></div><div class=3D"">=3D=3D=3D=
=3D Detailed Construction =3D=3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">The chacha20-poly1305@bitcoin cipher =
requires two 256 bits of key material as&nbsp;</div><div class=3D"">output=
 from the key exchange. Each key (K_1 and K_2) are used by two =
separate&nbsp;</div><div class=3D"">instances of chacha20.</div><div =
class=3D""><br class=3D""></div><div class=3D"">The instance keyed by =
K_1 is a stream cipher that is used only to encrypt the =
3&nbsp;</div><div class=3D"">byte packet length field and has its own =
sequence number. The second instance,&nbsp;</div><div class=3D"">keyed =
by K_2, is used in conjunction with poly1305 to build an =
AEAD&nbsp;</div><div class=3D"">(Authenticated Encryption with =
Associated Data) that is used to encrypt and&nbsp;</div><div =
class=3D"">authenticate the entire packet.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Two separate cipher instances are used =
here so as to keep the packet lengths&nbsp;</div><div =
class=3D"">confidential but not create an oracle for the packet payload =
cipher by&nbsp;</div><div class=3D"">decrypting and using the packet =
length prior to checking the MAC. By using an&nbsp;</div><div =
class=3D"">independently-keyed cipher instance to encrypt the length, an =
active attacker&nbsp;</div><div class=3D"">seeking to exploit the packet =
input handling as a decryption oracle can learn&nbsp;</div><div =
class=3D"">nothing about the payload contents or its MAC (assuming key =
derivation,&nbsp;</div><div class=3D"">ChaCha20 and Poly1305 are =
secure).</div><div class=3D""><br class=3D""></div><div class=3D"">The =
AEAD is constructed as follows: for each packet, generate a Poly1305 key =
by&nbsp;</div><div class=3D"">taking the first 256 bits of ChaCha20 =
stream output generated using K_2, an IV&nbsp;</div><div =
class=3D"">consisting of the packet sequence number encoded as an LE =
uint64 and a ChaCha20&nbsp;</div><div class=3D"">block counter of zero. =
The K_2 ChaCha20 block counter is then set to the&nbsp;</div><div =
class=3D"">little-endian encoding of 1 (i.e. {1, 0, 0, 0, 0, 0, 0, 0}) =
and this instance&nbsp;</div><div class=3D"">is used for encryption of =
the packet payload.</div><div class=3D""><br class=3D""></div><div =
class=3D"">=3D=3D=3D=3D Packet Handling =3D=3D=3D=3D</div><div =
class=3D""><br class=3D""></div><div class=3D"">When receiving a packet, =
the length must be decrypted first. When 3 bytes of&nbsp;</div><div =
class=3D"">ciphertext length have been received, they may be =
decrypted.</div><div class=3D""><br class=3D""></div><div class=3D"">A =
ChaCha20 round always calculates 64bytes which is sufficient to crypt =
21&nbsp;</div><div class=3D"">times a 3 bytes length field (21*3 =3D =
63). The length field sequence number can&nbsp;</div><div class=3D"">thus =
be used 21 times (keystream caching).</div><div class=3D""><br =
class=3D""></div><div class=3D"">The length field must be enc-/decrypted =
with the ChaCha20 keystream keyed with&nbsp;</div><div class=3D"">K_1 =
defined by block counter 0, the length field sequence number in =
little&nbsp;</div><div class=3D"">endian and a keystream position from 0 =
to 60.</div><div class=3D""><br class=3D""></div><div class=3D"">Pseudo =
code example:</div><div class=3D"">&lt;pre&gt;</div><div class=3D"">// =
init</div><div class=3D"">sequence_nr_payload =3D 0; //payload sequence =
number</div><div class=3D"">sequence_nr_length_field =3D 0; //length =
field sequence number (will be reused)</div><div =
class=3D"">aad_length_field_pos =3D 0; //position in the length field =
cipher instance keystream chunk</div><div class=3D""><br =
class=3D""></div><div class=3D"">...</div><div class=3D""><br =
class=3D""></div><div class=3D"">// actual encryption</div><div =
class=3D"">if cache_length_field_sequence_number !=3D =
sequence_nr_length_field {</div><div class=3D"">&nbsp; =
cache_keystream_64_bytes =3D ChaCha20(key=3DK_1, =
iv=3Dlittle_endian(sequence_nr_length_field), counter=3D0);</div><div =
class=3D"">&nbsp; cache_length_field_sequence_number =3D =
sequence_nr_length_field</div><div class=3D"">}</div><div =
class=3D"">packet_length =3D =
XOR_TO_LE(cache_length_field_sequence_number[aad_length_field_pos - =
aad_length_field_pos+3], ciphertext[0-3])</div><div class=3D""><br =
class=3D""></div><div class=3D"">sequence_nr_payload++;</div><div =
class=3D"">aad_length_field_pos +=3D 3; //skip 3 bytes in =
keystream</div><div class=3D"">if (aad_length_field_pos + 3 &gt; 64) { =
//if we are outside of the 64byte keystream...</div><div class=3D"">&nbsp;=
 aad_length_field_pos =3D 0; // reset at position 0</div><div =
class=3D"">&nbsp; sequence_nr_length_field++; // increase length field =
sequence number</div><div class=3D"">}</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">Once the entire packet has been received, the MAC MUST be =
checked before&nbsp;</div><div class=3D"">decryption. A per-packet =
Poly1305 key is generated as described above and the&nbsp;</div><div =
class=3D"">MAC tag calculated using Poly1305 with this key over the =
ciphertext of the&nbsp;</div><div class=3D"">packet length and the =
payload together. The calculated MAC is then compared in&nbsp;</div><div =
class=3D"">constant time with the one appended to the packet and the =
packet decrypted&nbsp;</div><div class=3D"">using ChaCha20 as described =
above (with K_2, the packet sequence number as&nbsp;</div><div =
class=3D"">nonce and a starting block counter of 1).</div><div =
class=3D""><br class=3D""></div><div class=3D"">Detection of an invalid =
MAC MUST lead to immediate connection termination.</div><div =
class=3D""><br class=3D""></div><div class=3D"">To send a packet, first =
encode the 3 byte length and encrypt it using K_1 as&nbsp;</div><div =
class=3D"">described above. Encrypt the packet payload (using K_2) and =
append it to the&nbsp;</div><div class=3D"">encrypted length. Finally, =
calculate a MAC tag and append it.</div><div class=3D""><br =
class=3D""></div><div class=3D"">The initiating peer MUST use =
&lt;code&gt;K_1_A, K_2_A&lt;/code&gt; to encrypt messages =
on&nbsp;</div><div class=3D"">the send channel, &lt;code&gt;K_1_B, =
K_2_B&lt;/code&gt; MUST be used to decrypt messages on&nbsp;</div><div =
class=3D"">the receive channel.</div><div class=3D""><br =
class=3D""></div><div class=3D"">The responding peer MUST use =
&lt;code&gt;K_1_A, K_2_A&lt;/code&gt; to decrypt messages =
on&nbsp;</div><div class=3D"">the receive channel, &lt;code&gt;K_1_B, =
K_2_B&lt;/code&gt; MUST be used to encrypt messages&nbsp;</div><div =
class=3D"">on the send channel.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Optimized implementations of =
ChaCha20-Poly1305@bitcoin are relatively fast in&nbsp;</div><div =
class=3D"">general, therefore it is very likely that encrypted messages =
require not more&nbsp;</div><div class=3D"">CPU cycles per bytes then =
the current unencrypted p2p message format&nbsp;</div><div =
class=3D"">(ChaCha20/Poly1305 versus double SHA256).</div><div =
class=3D""><br class=3D""></div><div class=3D"">The initial packet =
sequence numbers are 0.</div><div class=3D""><br class=3D""></div><div =
class=3D"">K_2 ChaCha20 cipher instance (payload) must never reuse a =
{key, nonce} for&nbsp;</div><div class=3D"">encryption nor may it be =
used to encrypt more than 2^70 bytes under the same&nbsp;</div><div =
class=3D"">{key, nonce}.</div><div class=3D""><br class=3D""></div><div =
class=3D"">K_1 ChaCha20 cipher instance (length field/AAD) must never =
reuse a {key, nonce,&nbsp;</div><div class=3D"">position-in-keystream} =
for encryption nor may it be used to encrypt more than&nbsp;</div><div =
class=3D"">2^70 bytes under the same {key, nonce}.</div><div =
class=3D""><br class=3D""></div><div class=3D"">We use message sequence =
numbers for both communication directions.</div><div class=3D""><br =
class=3D""></div><div class=3D"">&lt;pre&gt;</div><div =
class=3D"">&nbsp;---------------------------------------------------------=
---------------------------------</div><div class=3D"">&nbsp;| Initiator =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp;Responder &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |</div><div class=3D"">&nbsp;| &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp;|</div><div class=3D"">&nbsp;| AEAD() =3D =
ChaCha20Poly1305Bitcoin() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
|</div><div class=3D"">&nbsp;| MSG_A_CIPH =3D AEAD(k=3DK_1_A, K_2_A, =
payload_nonce=3D0, aad_nonce=3D0, aad_pos=3D0, msg) &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --- MSG_CIPH ---&gt; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp;msg &nbsp; :=3D AEAD(k=3DK_1_A,K_2_A, n=3D0, ..., =
MSG_A_CIPH) &nbsp;|</div><div class=3D"">&nbsp;| &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;|</div><div =
class=3D"">&nbsp;---------------------------------------------------------=
---------------------------------</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">=3D=3D=3D=3D Test Vectors =3D=3D=3D=3D</div><div class=3D""><br=
 class=3D""></div><div class=3D"">&lt;pre&gt;</div><div class=3D"">message=
 &nbsp; 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00</div><div class=3D"">k1 (DATA) 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00</div><div class=3D"">k2 (AAD) &nbsp;00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00</div><div class=3D""><br class=3D""></div><div class=3D"">AAD =
keystream</div><div class=3D"">76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 =
bd 28 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 da 41 59 7c 51 57 =
48 8d 77 24 e0 3f b8 d8 4a 37 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee =
65 86</div><div class=3D""><br class=3D""></div><div =
class=3D"">ciphertext</div><div class=3D"">76 b8 e0 9f 07 e7 be 55 51 38 =
7a 98 ba 97 7c 73 2d 08 0d cb 0f 29 a0 48 e3 65 69 12 c6 53 3e =
32</div><div class=3D""><br class=3D""></div><div class=3D"">MAC</div><div=
 class=3D"">d2 fc 11 82 9c 1b 6c 1d f1 f5 51 cd 61 31 ff 08</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">message &nbsp; 01 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00</div><div class=3D"">k1 (DATA) 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =
00</div><div class=3D"">k2 (AAD) &nbsp;00 00 00 00 00 00 00 00 00 00 00 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</div><div =
class=3D""><br class=3D""></div><div class=3D"">AAD keystream</div><div =
class=3D"">76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 bd d2 19 b8 =
a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 da 41 59 7c 51 57 48 8d 77 24 e0 3f =
b8 d8 4a 37 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86</div><div =
class=3D""><br class=3D""></div><div class=3D"">ciphertext</div><div =
class=3D"">77 b8 e0 9f 07 e7 be 55 51 38 7a 98 ba 97 7c 73 2d 08 0d cb =
0f 29 a0 48 e3 65 69 12 c6 53 3e 32</div><div class=3D""><br =
class=3D""></div><div class=3D"">MAC</div><div class=3D"">ba f0 c8 5b 6d =
ff 86 02 b0 6c f5 2a 6a ef c6 2e</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">message</div><div =
class=3D"">ff 00 00 f1 95 e6 69 82 10 5f fb 64 0b b7 75 7f 57 9d a3 16 =
02 fc 93 ec 01 ac 56 f8 5a c3 c1 34 a4 54 7b 73 3b 46 41 30 42 c9 44 00 =
49 17 69 05 d3 be 59 ea 1c 53 f1 59 16 15 5c 2b e8 24 1a 38 00 8b 9a 26 =
bc 35 94 1e 24 44 17 7c 8a de 66 89 de 95 26 49 86 d9 58 89 fb 60 e8 46 =
29 c9 bd 9a 5a cb 1c c1 18 be 56 3e b9 b3 a4 a4 72 f8 2e 09 a7 e7 78 49 =
2b 56 2e f7 13 0e 88 df e0 31 c7 9d b9 d4 f7 c7 a8 99 15 1b 9a 47 50 32 =
b6 3f c3 85 24 5f e0 54 e3 dd 5a 97 a5 f5 76 fe 06 40 25 d3 ce 04 2c 56 =
6a b2 c5 07 b1 38 db 85 3e 3d 69 59 66 09 96 54 6c c9 c4 a6 ea fd c7 77 =
c0 40 d7 0e af 46 f7 6d ad 39 79 e5 c5 36 0c 33 17 16 6a 1c 89 4c 94 a3 =
71 87 6a 94 df 76 28 fe 4e aa f2 cc b2 7d 5a aa e0 ad 7a d0 f9 d4 b6 ad =
3b 54 09 87 46 d4 52 4d 38 40 7a 6d eb 3a b7 8f ab 78 c9</div><div =
class=3D""><br class=3D""></div><div class=3D"">k1 (DATA) 00 01 02 03 04 =
05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c =
1d 1e 1f</div><div class=3D"">k2 (AAD) &nbsp;ff 01 02 03 04 05 06 07 08 =
09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e =
1f</div><div class=3D""><br class=3D""></div><div class=3D"">AAD =
keystream</div><div class=3D"">c6 40 c1 71 1e 3e e9 04 ac 35 c5 7a b9 79 =
1c 8a 1c 40 86 03 a9 0b 77 a8 3b 54 f6 c8 44 cb 4b 06 d9 4e 7f c6 c8 00 =
e1 65 ac d6 61 47 e8 0e c4 5a 56 7f 6c e6 6d 05 ec 0c ae 67 9d ce eb 89 =
00 17</div><div class=3D""><br class=3D""></div><div =
class=3D"">ciphertext</div><div class=3D"">39 40 c1 e9 2d a4 58 2f f6 f9 =
2a 77 6a eb 14 d0 14 d3 84 ee b3 0f 66 0d ac f7 0a 14 a2 3f d3 1e 91 21 =
27 01 33 4e 2c e1 ac f5 19 9d c8 4f 4d 61 dd be 65 71 bc a5 af 87 4b 4c =
92 26 c2 6e 65 09 95 d1 57 64 4e 18 48 b9 6e d6 c2 10 2d 54 89 a0 50 e7 =
1d 29 a5 a6 6e ce 11 de 5f b5 c9 55 8d 54 da 28 fe 45 b0 bc 4d b4 e5 b8 =
80 30 bf c4 a3 52 b4 b7 06 8e cc f6 56 ba e7 ad 6a 35 61 53 15 fc 7c 49 =
d4 20 03 88 d5 ec a6 7c 2e 82 2e 06 93 36 c6 9b 40 db 67 e0 f3 c8 12 09 =
c5 0f 32 16 a4 b8 9f b3 ae 1b 98 4b 78 51 a2 ec 6f 68 ab 12 b1 01 ab 12 =
0e 1e a7 31 3b b9 3b 5a 0f 71 18 5c 7f ea 01 7d db 92 76 98 61 c2 9d ba =
4f bc 43 22 80 d5 df f2 1b 36 d1 c4 c7 90 12 8b 22 69 99 50 bb 18 bf 74 =
c4 48 cd fe 54 7d 8e d4 f6 57 d8 00 5f dc 0c d7 a0 50 c2 d4 60 50 a4 4c =
43 76 35 58 58&nbsp;</div><div class=3D""><br class=3D""></div><div =
class=3D"">MAC</div><div class=3D"">98 1f be 8b 18 42 88 27 6e 7a 93 ea =
bc 89 9c 4a</div><div class=3D"">&lt;/pre&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D""><br class=3D""></div><div class=3D"">=3D=3D=
=3D v2 Messages Structure =3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">{|class=3D"wikitable"</div><div =
class=3D"">! Field Size !! Description !! Data type !! =
Comments</div><div class=3D"">|-</div><div class=3D"">| 3 || length =
&amp; flag || 23 + 1 bits || Encrypted length of ciphertext payload (not =
counting the MAC tag) in number of bytes (only 2^23 is usable, most =
significant bit is the rekey-flag)</div><div class=3D"">|-</div><div =
class=3D"">| 1-13 || encrypted command || variable || ASCII command (or =
one byte short command ID)</div><div class=3D"">|-</div><div class=3D"">| =
? || encrypted payload || ? || The actual data</div><div =
class=3D"">|-</div><div class=3D"">| 16 || MAC tag || ? || 128bit =
MAC-tag</div><div class=3D"">|}</div><div class=3D""><br =
class=3D""></div><div class=3D"">Encrypted messages do not have the =
4byte network magic.</div><div class=3D""><br class=3D""></div><div =
class=3D"">The maximum message size is 2^23 (8=E2=80=99388=E2=80=99608) =
bytes. Future communication MAY&nbsp;</div><div class=3D"">exceed this =
limit and thus MUST be split into different messages.</div><div =
class=3D""><br class=3D""></div><div class=3D"">Decrypting and =
processing the message before the authentication succeeds =
(MAC&nbsp;</div><div class=3D"">verified) MUST not be done.</div><div =
class=3D""><br class=3D""></div><div class=3D"">The 4byte sha256 =
checksum is no longer required because the AEAD (MAC).</div><div =
class=3D""><br class=3D""></div><div class=3D"">Both peers MUST keep =
track of the message sequence number (uint32) of sent =
and&nbsp;</div><div class=3D"">received messages for building a 64-bit =
symmetric cipher IV.</div><div class=3D""><br class=3D""></div><div =
class=3D"">The command field MUST start with a byte that defines the =
length of the ASCII&nbsp;</div><div class=3D"">command string up to 12 =
chars (1 to 12) or a short command ID (see below).</div><div =
class=3D""><br class=3D""></div><div class=3D"">=3D=3D=3D=3D Short =
Command ID =3D=3D=3D=3D</div><div class=3D""><br class=3D""></div><div =
class=3D"">To save valuable bandwidth, the v2 message format supports =
message command&nbsp;</div><div class=3D"">short IDs for message types =
with high frequency. The ID/string mapping is a&nbsp;</div><div =
class=3D"">peer to peer arrangement and MAY be negotiated between the =
initiating and&nbsp;</div><div class=3D"">responding peer. A peer =
conforming to this proposal MUST support short IDs&nbsp;</div><div =
class=3D"">based on the table below and SHOULD use short command IDs for =
outgoing messages.</div><div class=3D""><br class=3D""></div><div =
class=3D"">{|class=3D"wikitable"</div><div class=3D"">! Number !! =
Command</div><div class=3D"">|-</div><div class=3D"">| 13 || =
INV</div><div class=3D"">|-</div><div class=3D"">| 14 || =
HEADERS</div><div class=3D"">|-</div><div class=3D"">| 15 || =
PING</div><div class=3D"">|-</div><div class=3D"">| 16 || PONG</div><div =
class=3D"">|-</div><div class=3D"">|}</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D=3D=3D Length comparisons between =
v1 and v2 messages =3D=3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">&lt;pre&gt;</div><div class=3D"">v1 in: =
4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+36(Payload) =3D=3D =
60</div><div class=3D"">v2 inv: =
3(MessageSize&amp;Flag)+1(Command)+36(Payload)+16(MAC) =3D=3D =
56</div><div class=3D"">(93.33%)</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">v1 ping: =
4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+8(Payload) =3D=3D =
32</div><div class=3D"">v2 pong: =
3(MessageSize&amp;Flag)+1(Command)+8(Payload)+16(MAC) =3D=3D =
28</div><div class=3D"">(87.5%)</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">&lt;pre&gt;</div><div class=3D"">v1 block: =
4(Magic)+12(Command)+4(MessageSize)+4(Checksum)+1=E2=80=99048=E2=80=99576(=
Payload) =3D 1=E2=80=99048=E2=80=99600</div><div class=3D"">v2 block: =
3(MessageSize&amp;Flag)+6(CommandStr)+8(Payload)+16(MAC) =3D=3D 28 =3D =
1=E2=80=99048=E2=80=99601</div><div class=3D"">(100.000095%)</div><div =
class=3D"">&lt;/pre&gt;</div><div class=3D""><br class=3D""></div><div =
class=3D"">=3D=3D=3D Re-Keying =3D=3D=3D</div><div class=3D""><br =
class=3D""></div><div class=3D"">Re-keying can be signaled by setting =
the most significant bit in the length&nbsp;</div><div class=3D"">field =
before encryption. A peer signaling a rekey MUST use the next key =
for&nbsp;</div><div class=3D"">encryption messages AFTER the message =
where the signaling has been done.</div><div class=3D""><br =
class=3D""></div><div class=3D"">A peer identifying a rekey by checking =
the most significant bit in the envelope&nbsp;</div><div class=3D"">length=
 must use the next key for decrypt messages AFTER the message where =
the&nbsp;</div><div class=3D"">signaling has been detected.</div><div =
class=3D""><br class=3D""></div><div class=3D"">The next symmetric =
cipher key MUST be calculated by =
&lt;code&gt;SHA256(SHA256(session&nbsp;</div><div class=3D"">ID || =
old_symmetric_cipher_key))&lt;/code&gt; and the packet sequence number =
of the&nbsp;</div><div class=3D"">according encryption direction must be =
set to 0.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Re-Keying interval is a peer policy with a minimum timespan =
of 10 seconds.</div><div class=3D""><br class=3D""></div><div =
class=3D"">The Re-Keying must be done after every 1GB of data sent =
(recommended by RFC4253&nbsp;</div><div class=3D"">SSH Transport) or if =
the last rekey was more than an hour ago.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Peers calculate the counterparty limits =
and MUST disconnect immediately if a&nbsp;</div><div class=3D"">violation =
of the limits has been detected.</div><div class=3D""><br =
class=3D""></div><div class=3D""><br class=3D""></div><div class=3D"">=3D=3D=
=3D Risks =3D=3D=3D</div><div class=3D""><br class=3D""></div><div =
class=3D"">The encryption does not include an authentication scheme. =
This BIP does not&nbsp;</div><div class=3D"">cover a proposal to avoid =
MITM attacks during the encryption initialization.&nbsp;</div><div =
class=3D"">However, peers MUST show the session-id to the user on =
request which allows to&nbsp;</div><div class=3D"">identify a MITM by a =
manual verification on a secure channel.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Optional authentication schemes may be =
covered by other proposals &lt;ref&nbsp;</div><div =
class=3D"">name=3D"bip150"&gt;[https://github.com/bitcoin/bips/blob/master=
/bip-0150.mediawiki&nbsp;</div><div =
class=3D"">BIP150]&lt;/ref&gt;.</div><div class=3D""><br =
class=3D""></div><div class=3D"">An attacker could delay or halt v2 =
protocol enforcement by providing a&nbsp;</div><div class=3D"">reasonable =
amount of peers not supporting the v2 protocol.</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D Compatibility =3D=3D</div><div =
class=3D""><br class=3D""></div><div class=3D"">This proposal is =
backward compatible (as long as not enforced). =
Non-supporting&nbsp;</div><div class=3D"">peers can still use =
unencrypted communications.</div><div class=3D""><br class=3D""></div><div=
 class=3D"">=3D=3D Reference implementation =3D=3D</div><div class=3D"">* =
Complete Bitcoin Core implementation: =
https://github.com/bitcoin/bitcoin/pull/14032</div><div class=3D"">* =
Reference implementation of the AEAD in C: =
https://github.com/jonasschnelli/chacha20poly1305</div><div class=3D""><br=
 class=3D""></div><div class=3D"">=3D=3D References =3D=3D</div><div =
class=3D""><br class=3D""></div><div =
class=3D"">&lt;references/&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D Acknowledgements =3D=3D</div><div =
class=3D"">* Pieter Wuille and Gregory Maxwell for most of the ideas in =
this BIP.</div><div class=3D"">* Tim Ruffing for the review and the hint =
for the enhancement of the symmetric&nbsp;</div><div class=3D"">key =
derivation</div><div class=3D""><br class=3D""></div><div class=3D""><br =
class=3D""></div><div class=3D"">=3D=3D Copyright =3D=3D</div><div =
class=3D"">This work is placed in the public domain.</div><div =
class=3D""><br class=3D""></div></div></div></body></html>=

--Apple-Mail=_8E38DED6-E4A9-4675-B6EB-ADCB2DFFEBD1--

--Apple-Mail=_D4FB7183-E1A8-4BFC-AF55-2484312B4577
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEyhopCNzi8TB0xizeHrd2uwPHki0FAlyVTe4ACgkQHrd2uwPH
ki3+5g/+PzezmCawRZNOVKMjYl3QXIgufxBkT6zcWKbPXFozyPTKgw4X3RvyATc4
HCjWZTpT+nM+RJnMTmkKUiF3R2LrU+n8UFLsy2Aij+TN2ugHjRaNzfn36c9q5Gi9
a5el4ltBBRnZSyxm4G9fOoGZ1ZEBDF9A5+tWmPlHfR4W5j2avc6EabIeiju+qWq9
5zbw2hevihnZXIq5TJ8IxFADgJTDJtUb47kkFI0YN8Mq0vJ/b1FQy3QwOjwkGuRE
EaGVHENKSlQos70nnFpkWPlfaHYV94Qup1BL3LYpQCxle8I56Km9ZSySWTRRBto4
b0QhGGEIuImYtWa0fHz9JScpMhBx711FDEpaKdIuYIc+DpablESAqnCYxxJgmDkH
KLkNXw2zl/MUpyVqZgP/B4ca6iH2jKLxp01uhWzWKsFbXb7Ljdr8wYYlh63KhoXJ
GzK8dZaJBIhtdj9pD9DTzxqjc8z6dvFb5/HrpePmap6vkKweF3YhvVBIqeHymyWL
P3aO8QbGTaZ9Y42BjDsu0psLVyXOtseR96Oy4/GD+lcSIs4fyaYReWcfvKpreoxa
pAfnc6Xx37FEnIqFx+gXBz+nBA6Vs4s4746GNIG1KIwdLX88GB6l3uDIzJiSTZVR
07hcLOOwLeAwj0zVyfyJhsIWM8XEdDPCgLtgZC6XkYC4d8m9zHA=
=MJLN
-----END PGP SIGNATURE-----

--Apple-Mail=_D4FB7183-E1A8-4BFC-AF55-2484312B4577--