summaryrefslogtreecommitdiff
path: root/lib/ssl/test/cryptcookie.erl
blob: 19a26651acf23b4062fabb3f4b0b514e7ba33a7f (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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2022-2023. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%% -------------------------------------------------------------------------
%%
%% Module for an encrypted Erlang stream based only on
%% the cookie as a shared secret
%%
-module(cryptcookie).
-feature(maybe_expr, enable).

-export([supported/0, start_keypair_server/0, init/1, init/2,
         encrypt_and_send_chunk/4, recv_and_decrypt_chunk/2]).

%% For kTLS integration
-export([ktls_info/1, ktls_info/0]).
-include_lib("ssl/src/ssl_cipher.hrl").
-include_lib("ssl/src/ssl_internal.hrl").
-include_lib("ssl/src/ssl_record.hrl").


-define(PROTOCOL, (?MODULE)).

%% -------------------------------------------------------------------------
%% The curve choice greatly affects setup time,
%% we really want an Edwards curve but that would
%% require a very new openssl version.
%%
%% Twisted brainpool curves (*t1) are faster than
%% non-twisted (*r1), 256 is much faster than 384,
%% and so on...
%%
%%% -define(CURVE, brainpoolP384t1).
%%% -define(CURVE, brainpoolP256t1).
-define(CURVE, secp256r1).     % Portability
-define(CIPHER, aes_256_gcm).  % kTLS compatible
-define(HMAC, sha384).

%% kTLS integration
-define(TLS_VERSION, ?TLS_1_3).
-define(CIPHER_SUITE, (?TLS_AES_256_GCM_SHA384)).


supported() ->
    maybe
        true ?= crypto_supports(curves,  ?CURVE),
        true ?= crypto_supports(ciphers, ?CIPHER),
        true ?= crypto_supports(macs,    hmac),
        true ?= crypto_supports(hashs,   ?HMAC),
        ok
    end.

crypto_supports(Tag, Item) ->
    lists:member(Item, crypto:supports(Tag))
        orelse "Crypto does not support "
        ++ atom_to_list(Tag) ++ ": " ++ atom_to_list(Item).

%% -------------------------------------------------------------------------

-define(PACKET_SIZE, (1 bsl 16)).  % 2 byte size header
%% Plenty of room for AEAD tag and chunk type
-define(CHUNK_SIZE, (?PACKET_SIZE - 256)).

-record(params,
        {hmac_algorithm = ?HMAC,
         aead_cipher = ?CIPHER,
         iv,
         key,
         tag_len = 16,
         rekey_count = 256 * 1024,
         rekey_time = 2 * 3600, % (seconds): 2 hours
         rekey_timestamp,
         rekey_key
        }).

params() ->
    #{ iv_length := IVLen, key_length := KeyLen } =
        crypto:cipher_info(?CIPHER),
    #params{ iv = IVLen, key = KeyLen, rekey_timestamp = timestamp() }.


-record(keypair,
        {type = ecdh,
         params = ?CURVE,
         public,
         private,
         life_count = 256, % Number of connection setups
         life_time = 3600, % 1 hour
         life_timestamp
        }).

%% -------------------------------------------------------------------------
%% Keep the node's public/private key pair in the process state
%% of a key pair server linked to the net_kernel process.
%% Create the key pair the first time it is needed
%% so crypto gets time to start first.
%%

start_keypair_server() ->
    Parent = self(),
    Ref = make_ref(),
    _ =
        spawn_link(
          fun () ->
                  try register(?MODULE, self()) of
                      true ->
                          Parent ! Ref,
                          keypair_server()
                  catch error : badarg ->
                          %% Already started - simply exit
                          %% and let the other run
                          Parent ! Ref,
                          ok
                  end
          end),
    receive Ref ->
            ?PROTOCOL
    end.

keypair_server() ->
    keypair_server(undefined, 1).
%%
keypair_server(KeyPair) ->
    keypair_server(KeyPair, KeyPair#keypair.life_count).
%%
keypair_server(_KeyPair, 0) ->
    keypair_server();
keypair_server(KeyPair, Count) ->
    receive
        {RefAlias, get_keypair} when is_reference(RefAlias) ->
            case KeyPair of
                undefined ->
                    KeyPair_1 = generate_keypair(),
                    RefAlias ! {RefAlias, KeyPair_1},
                    keypair_server(KeyPair_1);
                #keypair{} ->
                    RefAlias ! {RefAlias, KeyPair},
                    keypair_server(KeyPair, Count - 1)
            end;
        {RefAlias, get_new_keypair} ->
            KeyPair_1 = generate_keypair(),
            RefAlias ! {RefAlias, KeyPair_1},
            keypair_server(KeyPair_1)
    end.

call_keypair_server(Request) ->
    Pid = whereis(?MODULE),
    RefAlias = erlang:monitor(process, Pid, [{alias, reply_demonitor}]),
    Pid ! {RefAlias, Request},
    receive
        {RefAlias, Reply} ->
            Reply;
        {'DOWN', RefAlias, process, Pid, Reason} ->
            error({keypair_server, Reason})
    end.

generate_keypair() ->
    #keypair{ type = Type, params = Params } = #keypair{},
    {Public, Private} = crypto:generate_key(Type, Params),
    #keypair{
       public = Public, private = Private,
       life_timestamp = timestamp() }.


get_keypair() ->
    call_keypair_server(?FUNCTION_NAME).

get_new_keypair() ->
    call_keypair_server(?FUNCTION_NAME).

compute_shared_secret(
  #keypair{
     type = PublicKeyType,
     params = PublicKeyParams,
     private = PrivKey }, PubKey) ->
    %%
    crypto:compute_key(PublicKeyType, PubKey, PrivKey, PublicKeyParams).

%% -------------------------------------------------------------------------

-define(DATA_CHUNK, 2).
-define(TICK_CHUNK, 3).
-define(REKEY_CHUNK, 4).

%% -------------------------------------------------------------------------
%% Crypto strategy
%% -------
%% The crypto strategy is as simple as possible to get an encrypted
%% connection as benchmark reference.  It is geared around AEAD
%% ciphers in particular AES-GCM.
%%
%% The init message and the start message must fit in the TCP buffers
%% since both sides start with sending the init message, waits
%% for the other end's init message, sends the start message
%% and waits for the other end's start message.  So if the send
%% blocks we have a deadlock.
%%
%% The init + start sequence tries to implement Password Encrypted
%% Key Exchange using a node public/private key pair and the
%% shared secret (the Cookie) to create session encryption keys
%% that can not be re-created if the shared secret is compromized,
%% which should create forward secrecy.  You need both nodes'
%% key pairs and the shared secret to decrypt the traffic
%% between the nodes.
%%
%% All exchanged messages uses {packet, 2} i.e 16 bit size header.
%%
%% The init message contains a random number and encrypted: the public key
%% and two random numbers.  The encryption is done with Key and IV hashed
%% from the unencrypted random number and the shared secret.
%%
%% The other node's public key is used with the own node's private
%% key to create a shared key that is hashed with one of the encrypted
%% random numbers from each side to create Key and IV for the session.
%%
%% The start message contains the two encrypted random numbers
%% this time encrypted with the session keys for verification
%% by the other side, plus the rekey count.  The rekey count
%% is just there to get an early check for if the other side's
%% maximum rekey count is acceptable, it is just an embryo
%% of some better check.  Any side may rekey earlier but if the
%% rekey count is exceeded the connection fails.  Rekey is also
%% triggered by a timer.
%%
%% Subsequent encrypted messages has the sequence number and the length
%% of the message as AAD data, and an incrementing IV.  These messages
%% has got a message type that differentiates data from ticks and rekeys.
%% Ticks have a random size in an attempt to make them less obvious to spot.
%%
%% Rekeying is done by the sender that creates a new key pair and
%% a new shared secret from the other end's public key and with
%% this and the current key and iv hashes a new key and iv.
%% The new public key is sent to the other end that uses it
%% and its old private key to create the same new shared
%% secret and from that a new key and iv.
%% So the receiver keeps its private key, and the sender keeps
%% the receivers public key for the connection's life time.
%% While the sender generates a new key pair at every rekey,
%% which changes the shared secret at every rekey.
%%
%% The only reaction to errors is to crash noisily (?) which will bring
%% down the connection and hopefully produce something useful
%% in the local log, but all the other end sees is a closed connection.
%% -------------------------------------------------------------------------


%% -------------------------------------------------------------------------
%% Initialize encryption on Stream; initial handshake
%%
%% init(Stream, Secret) ->
%%     {NewStream, ChunkSize, [RecvSeq|RecvParams], [SendSeq|SendParams]}.

init(Stream) ->
    Secret = atom_to_binary(auth:get_cookie(), latin1),
    init(Stream, Secret).

init(Stream = {_, OutStream, _}, Secret) ->
    #keypair{ public = PubKey } = KeyPair = get_keypair(),
    Params = params(),
    {R2, R3, Msg} = init_msg(Params, Secret, PubKey),
    OutStream_1 = init_send_block(OutStream, Msg, iolist_size(Msg)),
    Stream_1 = setelement(2, Stream, OutStream_1),
    init_recv(Stream_1, Params, Secret, KeyPair, R2, R3).

init_recv(
  {InStream, OutStream, ControllingProcessFun},
  Params = #params{ iv = IVLen },
  Secret, KeyPair, R2, R3) ->
    %%
    [InitMsg | InStream_1] = init_recv_block(InStream),
    IVSaltLen = IVLen - 6,
    try
        case init_msg(Params, Secret, KeyPair, R2, R3, InitMsg) of
            {SendParams =
                 #params{iv = <<IV2ASalt:IVSaltLen/binary, IV2ANo:48>> },
             RecvParams,
             SendStartMsg} ->
                OutStream_1 =
                    init_send_block(
                      OutStream, SendStartMsg, iolist_size(SendStartMsg)),
                [RecvStartMsg | InStream_2] = init_recv_block(InStream_1),
                RecvParams_1 =
                    #params{
                       iv = <<IV2BSalt:IVSaltLen/binary, IV2BNo:48>> } =
                    start_msg(RecvParams, R2, R3, RecvStartMsg),
                Stream_2 = {InStream_2, OutStream_1, ControllingProcessFun},
                RecvSeqParams =
                    [0 | RecvParams_1#params{ iv = {IV2BSalt, IV2BNo} }],
                SendSeqParams =
                    [0 | SendParams#params{ iv = {IV2ASalt, IV2ANo} }],
                CipherState = {RecvSeqParams, SendSeqParams},
                {Stream_2, ?CHUNK_SIZE, CipherState}
        end
    catch
        Class : Reason : Stacktrace when Class =:= error ->
            error_report(
              [init_recv_exception,
               {class, Class},
               {reason, Reason},
               {stacktrace, Stacktrace}]),
            _ = trace({Reason, Stacktrace}),
            exit(connection_closed)
    end.

init_msg(
  #params{
     hmac_algorithm = HmacAlgo,
     aead_cipher = AeadCipher,
     key = KeyLen,
     iv = IVLen,
     tag_len = TagLen }, Secret, PubKeyA) ->
    %%
    RLen = KeyLen + IVLen,
    <<R1A:RLen/binary, R2A:RLen/binary, R3A:RLen/binary>> =
        crypto:strong_rand_bytes(3 * RLen),
    {Key1A, IV1A} = hmac_key_iv(HmacAlgo, R1A, Secret, KeyLen, IVLen),
    Plaintext = [R2A, R3A, PubKeyA],
    MsgLen = byte_size(R1A) + TagLen + iolist_size(Plaintext),
    AAD = [<<MsgLen:32>>, R1A],
    {Ciphertext, Tag} =
        crypto:crypto_one_time_aead(
          AeadCipher, Key1A, IV1A, Plaintext, AAD, TagLen, true),
    Msg = [R1A, Tag, Ciphertext],
    {R2A, R3A, Msg}.
%%
init_msg(
  #params{
     hmac_algorithm = HmacAlgo,
     aead_cipher = AeadCipher,
     key = KeyLen,
     iv = IVLen,
     tag_len = TagLen,
     rekey_count = RekeyCount } = Params,
  Secret, KeyPair, R2A, R3A, Msg) ->
    %%
    RLen = KeyLen + IVLen,
    case Msg of
        <<R1B:RLen/binary, Tag:TagLen/binary, Ciphertext/binary>> ->
            {Key1B, IV1B} = hmac_key_iv(HmacAlgo, R1B, Secret, KeyLen, IVLen),
            MsgLen = byte_size(Msg),
            AAD = [<<MsgLen:32>>, R1B],
            case
                crypto:crypto_one_time_aead(
                  AeadCipher, Key1B, IV1B, Ciphertext, AAD, Tag, false)
            of
                <<R2B:RLen/binary, R3B:RLen/binary, PubKeyB/binary>> ->
                    SharedSecret = compute_shared_secret(KeyPair, PubKeyB),
                    %%
                    {Key2A, IV2A} =
                        hmac_key_iv(
                          HmacAlgo, SharedSecret, [R2A, R3B], KeyLen, IVLen),
                    SendParams =
                        Params#params{
                          rekey_key = PubKeyB,
                          key = Key2A, iv = IV2A },
                    %%
                    StartCleartext = [R2B, R3B, <<RekeyCount:32>>],
                    StartMsgLen = TagLen + iolist_size(StartCleartext),
                    StartAAD = <<StartMsgLen:32>>,
                    {StartCiphertext, StartTag} =
                        crypto:crypto_one_time_aead(
                          AeadCipher, Key2A, IV2A,
                          StartCleartext, StartAAD, TagLen, true),
                    StartMsg = [StartTag, StartCiphertext],
                    %%
                    {Key2B, IV2B} =
                        hmac_key_iv(
                          HmacAlgo, SharedSecret, [R2B, R3A], KeyLen, IVLen),
                    RecvParams =
                        Params#params{
                          rekey_key = KeyPair,
                          key = Key2B, iv = IV2B },
                    %%
                    {SendParams, RecvParams, StartMsg}
            end
    end.

start_msg(
  #params{
     aead_cipher = AeadCipher,
     key = Key2B,
     iv = IV2B,
     tag_len = TagLen,
     rekey_count = RekeyCountA } = RecvParams, R2A, R3A, Msg) ->
    %%
    case Msg of
        <<Tag:TagLen/binary, Ciphertext/binary>> ->
            KeyLen = byte_size(Key2B),
            IVLen = byte_size(IV2B),
            RLen = KeyLen + IVLen,
            MsgLen = byte_size(Msg),
            AAD = <<MsgLen:32>>,
            case
                crypto:crypto_one_time_aead(
                  AeadCipher, Key2B, IV2B, Ciphertext, AAD, Tag, false)
            of
                <<R2A:RLen/binary, R3A:RLen/binary, RekeyCountB:32>>
                  when RekeyCountA =< (RekeyCountB bsl 2),
                       RekeyCountB =< (RekeyCountA bsl 2) ->
                    RecvParams#params{ rekey_count = RekeyCountB }
            end
    end.

hmac_key_iv(HmacAlgo, MacKey, Data, KeyLen, IVLen) ->
    <<Key:KeyLen/binary, IV:IVLen/binary>> =
        crypto:macN(hmac, HmacAlgo, MacKey, Data, KeyLen + IVLen),
    {Key, IV}.


init_send_block(OutStream, Chunk, Size) ->
    OutStream_1 = send_block(OutStream, Chunk, Size),
    if
        hd(OutStream_1) =:= closed ->
            error_report(
              [?FUNCTION_NAME,
               {reason, closed}]),
            _ = trace({?FUNCTION_NAME, closed}),
            exit(connection_closed);
        true ->
            OutStream_1
    end.

init_recv_block(InStream) ->
    Result = [Data | _] = recv_block(InStream),
    if
        Data =:= closed ->
            error_report(
              [?FUNCTION_NAME,
               {reason, closed}]),
            _ = trace({?FUNCTION_NAME, closed}),
            exit(connection_closed);
        true ->
            Result
    end.


%% -------------------------------------------------------------------------
encrypt_and_send_chunk(
  OutStream,
  [Seq |
   Params =
       #params{ rekey_count = RekeyCount,
                rekey_time = RekeyTime,
                rekey_timestamp = RekeyTimestamp }],
   Chunk, Size) ->
    %%
    Timestamp = timestamp(),
    if
        RekeyCount =< Seq;
        RekeyTimestamp + RekeyTime =< Timestamp ->
            {OutStream_1, Params_1} =
                encrypt_and_send_rekey_chunk(
                  OutStream, Seq, Params, Timestamp),
            if
                hd(OutStream_1) =:= closed ->
                    {OutStream_1, [0 | Params_1]};
                true ->
                    {encrypt_and_send_chunk(
                       OutStream_1, 0, Params_1, Chunk, Size),
                     [1 | Params_1]}
            end;
        true ->
            {encrypt_and_send_chunk(OutStream, Seq, Params, Chunk, Size),
             [Seq + 1 | Params]}
    end.

encrypt_and_send_chunk(OutStream, Seq, Params, Chunk, 0) -> % Tick
    <<>> = Chunk, % ASSERT
    %% A ticks are sent as a somewhat random size block
    %% to make it less obvious to spot
    <<S:8>> = crypto:strong_rand_bytes(1),
    TickSize = 8 + (S band 63),
    TickData = crypto:strong_rand_bytes(TickSize),
    encrypt_and_send_block(
      OutStream, Seq, Params, [?TICK_CHUNK, TickData], 1 + TickSize);
encrypt_and_send_chunk(OutStream, Seq, Params, Chunk, Size) ->
    encrypt_and_send_block(
      OutStream, Seq, Params, [?DATA_CHUNK, Chunk], 1 + Size).

encrypt_and_send_rekey_chunk(
  OutStream, Seq,
  Params =
      #params{
         rekey_key = PubKeyB,
         key = Key,
         iv = {IVSalt, IVNo},
         hmac_algorithm = HmacAlgo },
  Timestamp) ->
    %%
    KeyLen = byte_size(Key),
    IVSaltLen = byte_size(IVSalt),
    #keypair{ public = PubKeyA } = KeyPair = get_new_keypair(),
    SharedSecret = compute_shared_secret(KeyPair, PubKeyB),
    IV = <<(IVNo + Seq):48>>,
    {Key_1, <<IVSalt_1:IVSaltLen/binary, IVNo_1:48>>} =
        hmac_key_iv(
          HmacAlgo, SharedSecret, [Key, IVSalt, IV],
          KeyLen, IVSaltLen + 6),
    %%
    {encrypt_and_send_block(
       OutStream, Seq, Params,
       [?REKEY_CHUNK, PubKeyA], 1 + byte_size(PubKeyA)),
     Params#params{
       key = Key_1, iv = {IVSalt_1, IVNo_1},
       rekey_timestamp = Timestamp }}.

encrypt_and_send_block(OutStream, Seq, Params, Block, Size) ->
    {EncryptedBlock, EncryptedSize} =
        encrypt_block(Seq, Params, Block, Size),
    send_block(OutStream, EncryptedBlock, EncryptedSize).

encrypt_block(
  Seq,
  #params{
     aead_cipher = AeadCipher,
     iv = {IVSalt, IVNo}, key = Key, tag_len = TagLen },
  Block, Size) ->
    %%
    EncryptedSize = Size + TagLen,
    AAD = <<Seq:32, EncryptedSize:32>>,
    IVBin = <<IVSalt/binary, (IVNo + Seq):48>>,
    {Ciphertext, CipherTag} =
        crypto:crypto_one_time_aead(
          AeadCipher, Key, IVBin, Block, AAD, TagLen, true),
    EncryptedBlock = [Ciphertext, CipherTag],
    {EncryptedBlock, EncryptedSize}.

%% Send packet=2
%%
send_block(OutStream, Block, Size) ->
    Msg =
        if
            is_binary(Block) -> [<<Size:16>>, Block];
            is_list(Block)   -> [<<Size:16>> | Block]
        end,
    (hd(OutStream))(OutStream, Msg).


%% -------------------------------------------------------------------------

recv_and_decrypt_chunk(InStream, SeqParams = [Seq | Params]) ->
    case recv_block(InStream) of
        Result = [closed | _] ->
            {Result, SeqParams};
        [Ciphertext | InStream_1] ->
            case decrypt_block(Seq, Params, Ciphertext) of
                <<?DATA_CHUNK, DataChunk/binary>> ->
                    {[DataChunk | InStream_1], [Seq + 1 | Params]};
                <<?TICK_CHUNK, _/binary>> ->
                    {[<<>> | InStream_1], [Seq + 1 | Params]};
                <<?REKEY_CHUNK, RekeyChunk>> ->
                    case decrypt_rekey(Params, RekeyChunk) of
                        Params_1 = #params{} ->
                            recv_and_decrypt_chunk(
                              InStream_1, [0 | Params_1]);
                        Problem when is_atom(Problem) ->
                            error_report(
                              [?FUNCTION_NAME, {reason, Problem}]),
                            {[closed | InStream_1], [Seq + 1 | Params]}
                    end;
                <<_UnknownChunk/binary>> ->
                    error_report([?FUNCTION_NAME, {reason, unknown_chunk}]),
                    {[closed | InStream_1], [Seq + 1 | Params]};
                Problem when is_atom(Problem) ->
                    error_report([?FUNCTION_NAME, {reason, Problem}]),
                    {[closed | InStream_1], [Seq + 1 | Params]}
            end
    end.

%% Non-optimized receive packet=2
%%
recv_block(InStream) ->
    Result_1 = [Data | InStream_1] = (hd(InStream))(InStream, 2),
    if
        Data =:= closed ->
            Result_1;
        is_binary(Data) ->
            recv_block(InStream_1, Data);
        is_list(Data) ->
            recv_block(InStream_1, iolist_to_binary(Data))
    end.
%%
recv_block(InStream, <<0:16>>) ->
    [<<>> | InStream];
recv_block(InStream, <<Size:16>>) ->
    case (hd(InStream))(InStream, Size) of
        [Data | InStream_1] when is_list(Data) ->
            [iolist_to_binary(Data) | InStream_1];
        Result = [Data | _]
          when is_binary(Data);
               Data =:= closed ->
            Result
    end.

decrypt_block(
  Seq, #params{ rekey_count = RekeyCount }, _Ciphertext)
  when RekeyCount =:= Seq ->
    %% This was one chunk too many without rekeying
    rekey_overdue;
decrypt_block(
  Seq,
  #params{
     aead_cipher = AeadCipher,
     iv = {IVSalt, IVNo}, key = Key, tag_len = TagLen },
  Ciphertext) ->
    %%
    CiphertextSize = byte_size(Ciphertext),
    if
        CiphertextSize < TagLen ->
            decrypt_short_block;
        true ->
            AAD = <<Seq:32, CiphertextSize:32>>,
            IV = <<IVSalt/binary, (IVNo + Seq):48>>,
            Size = CiphertextSize - TagLen,
            <<EncryptedBlock:Size/binary, CipherTag:TagLen/binary>> =
                Ciphertext,
            case
                crypto:crypto_one_time_aead(
                  AeadCipher, Key, IV, EncryptedBlock, AAD, CipherTag,
                  false)
            of
                Block when is_binary(Block) ->
                    Block;
                error ->
                    decrypt_error
            end
    end.

decrypt_rekey(
  Params =
      #params{
         iv = IV,
         key = Key,
         rekey_key = #keypair{public = PubKeyA} = KeyPair,
         hmac_algorithm = HmacAlgorithm},
  RekeyChunk) ->
    %%
    PubKeyLen = byte_size(PubKeyA),
    case RekeyChunk of
        <<PubKeyB:PubKeyLen/binary>> ->
            SharedSecret = compute_shared_secret(KeyPair, PubKeyB),
            KeyLen = byte_size(Key),
            IVLen = byte_size(IV),
            IVSaltLen = IVLen - 6,
            {Key_1, <<IVSalt_1:IVSaltLen/binary, IVNo_1:48>>} =
                hmac_key_iv(
                  HmacAlgorithm, SharedSecret, [Key, IV], KeyLen, IVLen),
            Params#params{
              iv = {IVSalt_1, IVNo_1},
              key = Key_1 };
        _ ->
            decrypt_bad_rekey_chunk
    end.


%% -------------------------------------------------------------------------
ktls_info(
  {[RecvSeq |
    #params{
       iv = {RecvSalt, RecvIV},
       key = RecvKey,
       aead_cipher = ?CIPHER } = _RecvParams],
   [SendSeq |
    #params{
       iv = {SendSalt, SendIV},
       key = SendKey,
       aead_cipher = ?CIPHER } = _SendParams]}) ->
    %%
    RecvState =
        #cipher_state{
           key = <<RecvKey/bytes>>,
           iv = <<RecvSalt/bytes, (RecvIV + RecvSeq):48>> },
    SendState =
        #cipher_state{
           key = <<SendKey/bytes>>,
           iv = <<SendSalt/bytes, (SendIV + SendSeq):48>> },
    #{ tls_version => ?TLS_VERSION,
       cipher_suite => ?CIPHER_SUITE,
       read_state => RecvState,
       read_seq => RecvSeq,
       write_state => SendState,
       write_seq => SendSeq }.

%% Dummy cipher parameters to use when checking if kTLS is supported.
%% Completely random to avoid accidentally creating an unsafe connection.
%%
ktls_info() ->
    #params{ iv = IVLen, key = KeyLen } = params(),
    <<RecvSeq:48, RecvKey:KeyLen/bytes, RecvIV:IVLen/bytes>> =
        crypto:strong_rand_bytes(6 + KeyLen + IVLen),
    <<SendSeq:48, SendKey:KeyLen/bytes, SendIV:IVLen/bytes>> =
        crypto:strong_rand_bytes(6 + KeyLen + IVLen),
    RecvState = #cipher_state{ key = RecvKey, iv = RecvIV },
    SendState = #cipher_state{ key = SendKey, iv = SendIV },
    #{ tls_version => ?TLS_VERSION,
       cipher_suite => ?CIPHER_SUITE,
       read_state => RecvState,
       read_seq => RecvSeq,
       write_state => SendState,
       write_seq => SendSeq }.


%% -------------------------------------------------------------------------
-ifdef(undefined).
-define(RECORD_TO_MAP(Name),
        record_to_map(Record) when element(1, (Record)) =:= (Name) ->
               record_to_map(record_info(fields, Name), Record, 2, #{})).
?RECORD_TO_MAP(params).
%%
record_to_map([Field | Fields], Record, Index, Map) ->
    record_to_map(
      Fields, Record, Index + 1,
      Map#{ Field => element(Index, Record) });
record_to_map([], _Record, _Index, Map) ->
    Map.
-endif.

timestamp() ->
    erlang:monotonic_time(second).


error_report(Report) ->
    error_logger:error_report(Report).

-ifdef(undefined).
info_report(Report) ->
    error_logger:info_report(Report).
-endif.

%% Trace point
trace(Term) -> Term.