/
index.xml
2983 lines (2900 loc) · 379 KB
/
index.xml
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
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Dr Franziskus Kiefer</title>
<link>https://www.franziskuskiefer.de/</link>
<description>Recent content on Dr Franziskus Kiefer</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<lastBuildDate>Mon, 02 May 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://www.franziskuskiefer.de/index.xml" rel="self" type="application/rss+xml" /><item>
<title>What is High Assurance Cryptography?</title>
<link>https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/</link>
<pubDate>Mon, 02 May 2022 00:00:00 +0000</pubDate>
<guid>https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/</guid>
<description><img src="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/header.jpg" alt="Featured image of post What is High Assurance Cryptography?" /><p>With my company <a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> we build high assurance cryptography.
But what does this actually mean?</p>
<p>Before focusing on cryptography it is interesting to look at high assurance
software in general.
How is high assurance software different from other software?</p>
<p>High assurance software is usually seen as being more trustworthy than other
software.
This is especially interesting in high-risk/high-stakes environments such as
financial institutions or governments.
There are different ways to achieve better guarantees for software.
Today the most commonly used technique to increase trust into software is using
certifications like <a class="link" href="https://en.wikipedia.org/wiki/Common_Criteria" target="_blank" rel="noopener"
>common criteria</a>
or <a class="link" href="https://en.wikipedia.org/wiki/FIPS_140-2" target="_blank" rel="noopener"
>FIPS</a>.
While these certifications offer a certain level of additional guarantees, only
the highest levels require some form of formal verification of the production
source code.
As such certification usually reaches only up to a certain level of high assurance.</p>
<p>Instead, using formal methods to increase trust in software offers real tangible
guarantees on a software artifact.
But in order to get actual guarantees we have to define the properties that are
guaranteed and put it into perspective by defining different assurance levels.
Before doing this we look at the different techniques used in high assurance
software engineering.</p>
<h2 id="techniques">Techniques</h2>
<p>There are a number of different techniques used in (high assurance) software engineering.
Some are simply good engineering practice while others go beyond what is done for most software.
The following picture gives a high-level overview of the different techniques.
(The list is of course not exhaustive and some grouping might be arbitrary.)</p>
<p><figure style="flex-grow: 200; flex-basis: 480px">
<a href="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview1.png" data-size="1440x720"><img src="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview1.png"
srcset="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview1_hu3cede752948e24ef3b92606ea47496ec_569899_480x0_resize_box_3.png 480w, https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview1_hu3cede752948e24ef3b92606ea47496ec_569899_1024x0_resize_box_3.png 1024w"
width="1440"
height="720"
loading="lazy"
alt="High assurance tools - an overview">
</a>
<figcaption>High assurance tools - an overview</figcaption>
</figure></p>
<p>On the left side we have safe programming languages and engineering processes
that are the bedrock of high assurance software engineering.
Processes ensure that the development and maintenance process is safe while
using a safe programming language such as Rust that gives memory safety
guarantees provides a safety baseline for the software.</p>
<p>The engineering infrastructure in the center is used to enforce policies and link
everything together.
It is the glue that implements engineering processes and ensures safe operations
of the code at all times.
On the right hand side we have engineering practices divided into dynamic and
static tools, or testing and formal methods.
There are a lot of different testing techniques from fuzzing to known answer
tests that ensure that the code is operating correctly and safely for a certain
set of inputs.
With static analysis and formal methods we can go a step further and ensure that
the code is correct and safe for all inputs.</p>
<h2 id="assurance-levels">Assurance Levels</h2>
<p>Speaking about “high assurance” is so vague that it can be considered meaningless.
When speaking about high assurance software it is paramount to define a set of
properties the software guarantees.
To this end we define a set of assurance levels a software artifact can achieve
and a set of techniques used to get there.
The following picture sorts the previously defined techniques according to the
level of assurance they provide and the complexity required to use them.</p>
<p><figure style="flex-grow: 192; flex-basis: 462px">
<a href="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview2.png" data-size="2018x1047"><img src="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview2.png"
srcset="https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview2_hu6645ebbbb45afaa59acb0d3491403de9_661764_480x0_resize_box_3.png 480w, https://www.franziskuskiefer.de/p/what-is-high-assurance-cryptography/overview2_hu6645ebbbb45afaa59acb0d3491403de9_661764_1024x0_resize_box_3.png 1024w"
width="2018"
height="1047"
loading="lazy"
alt="High assurance - an overview">
</a>
<figcaption>High assurance - an overview</figcaption>
</figure></p>
<h3 id="ca-1">CA-1</h3>
<p>The first level of high assurance software is what I’d flippantly call “well
written software”.
At this level no complex tools are required. Instead good engineering principles
are applied.</p>
<p>Note that this level must not be used for cryptographic software.
<strong>Cryptography requires at least CA-2.</strong></p>
<p>The programming language and how it is used is the first important point.
A programming language like Rust that is memory safe and has a lot of additional
features that allow to ensure that the code is “safe” is paramount.
It is well known that memory safety is the root cause for most security issues
with about 70% (see what <a class="link" href="https://www.chromium.org/Home/chromium-security/memory-safety/" target="_blank" rel="noopener"
>Google</a>, <a class="link" href="https://msrnd-cdn-stor.azureedge.net/bluehat/bluehatil/2019/assets/doc/Trends%2C%20Challenges%2C%20and%20Strategic%20Shifts%20in%20the%20Software%20Vulnerability%20Mitigation%20Landscape.pdf" target="_blank" rel="noopener"
>Microsoft</a>, or the <a class="link" href="https://www.memorysafety.org/docs/memory-safety/" target="_blank" rel="noopener"
>ISRG</a> say about this).
They are only possible because languages like C and C++ are not memory safe.</p>
<p>The code requires a sufficient amount of tests. It is difficult and arbitrary to
define an exact number for test coverage.
But I think it is safe to say that a test coverage of less than 70% is not
acceptable.
Defining test coverage, quantitatively and qualitatively, is part of the
engineering process.</p>
<p>Because testing against itself is not sufficient, test vectors with known answer
tests must be used.
If the implementation does not have a generally accepted specification or
(de-facto) reference implementation that can be used to get test vectors, a
reference implementation (specification) must be written to produce test vectors.</p>
<p>Engineering processes must be implemented that ensure that the code can be safely
maintained in the long-run.
An engineering infrastructure must be in place to enforce processes and help the engineering process.</p>
<p><strong>Properties:</strong></p>
<ul>
<li>Memory safety</li>
<li>Adequate test coverage</li>
<li>Known answer tests</li>
<li>Engineering best practices
<ul>
<li>Review guidelines</li>
<li>Continuous integration</li>
<li>Documentation</li>
<li>(Security) bug reporting</li>
<li>Release management</li>
</ul>
</li>
</ul>
<h3 id="ca-2">CA-2</h3>
<p>The second level includes everything from the first level but adds properties
specific to cryptographic code and more advanced testing methods.</p>
<p>In cryptographic code it is important that no decisions are made based on secret
information.
It is therefore necessary to avoid any branching on secret data or memory access
based on secret data.
There is research tooling out there that tries to ensure this and a lot of good
practices to avoid it. But there’s no comprehensive way of doing this right now.
With Cryspen we develop a set of tools to ensure secret independent computation that we use and maintain.</p>
<p>On this level more advanced testing such as fuzzing and property based testing
is required as well to make sure that the code is not only safe in well defined
states but can handle any input.</p>
<p><strong>Properties:</strong></p>
<ul>
<li>Secret independent computation
<ul>
<li>No secret-dependent branching</li>
<li>No secret-dependent memory access</li>
</ul>
</li>
<li>Advanced testing
<ul>
<li>Fuzzing</li>
<li>Sanitizer builds</li>
<li>Property based testing</li>
</ul>
</li>
</ul>
<h4 id="ca-2-1">CA-2+</h4>
<p>The secret independent computation properties in CA-2 can be shown on different
levels.
Often this is done only on the programming language level rather than the machine
code.
If the secret independent computation is ensured on the machine code level, we
call this CA-2+.
Higher levels are augmented with the + in the same way if the secret independence
is given after compilation.</p>
<p><strong>Properties:</strong></p>
<ul>
<li>Secret independent computation ensured on the machine code level</li>
</ul>
<h3 id="ca-3a">CA-3a</h3>
<p>The third assurance level includes the first two but requires formal methods.
This is what we will always aim for. But because of the complexity and real world
constraints it is not always feasible to achieve this level for all code.</p>
<p>Cryptographic primitives are one building block used to build cryptographic
protocols.
Because they require highly efficient implementations but usually have a rather
succinct mathematical definition, functional and semantic correctness are the
properties we are interested in.
In particular, the efficient implementation of a cryptographic primitive must be
shown equivalent to a self-evidently correct specification.
Additionally semantic properties such as “decryption is the inverse of encryption”
can be shown.
The exact properties proven must be clearly stated for every artifact.</p>
<p>Data structures and other building blocks are needed to build cryptographic
protocols in addition to the primitives.
They must be similarly shown to be correct and safe to use by using functional
and semantic correctness proofs.</p>
<p>On this level the cryptographic protocols themselves are written in a succinct
way such that they can be inspected by hand and compared to a general
specification if available.</p>
<p><strong>Properties:</strong></p>
<ul>
<li>Functional and semantic security proofs</li>
</ul>
<h3 id="ca-3b">CA-3b</h3>
<p>The third level can be extended to require formal proofs on the protocol layer.
While cryptographic protocols can often be written in a way that they are
self-evidently correct and efficient, this is not enough if the protocol is not
standardized or is too complex to inspect manually.</p>
<p>Security models and properties are defined for a protocol and they are proven on
the implementation of the cryptographic protocols using formal methods.
These properties are very specific to each protocol and can range from the
correctness of a state machine to the security against a certain type of attacker.</p>
<p><strong>Properties:</strong></p>
<ul>
<li>Security proofs</li>
</ul>
<h2 id="high-assurance">High-Assurance?</h2>
<p><a class="link" href="https://highassurance.rs" target="_blank" rel="noopener"
>https://highassurance.rs</a> has some great documentation on writing good Rust code.
However, it also has a good example of what I <em>don’t</em> consider high assurance software
and exemplifies why it is so important to specify all claims precisely instead of
simply claiming high assurance.
<em>(Note that this is just an example to show why it is so important to exactly specify what assurances are given. This is not supposed to bash the <a class="link" href="https://highassurance.rs" target="_blank" rel="noopener"
>https://highassurance.rs</a> folks. I think it&rsquo;s a great effort.)</em></p>
<p>Let’s take a look at chapters <a class="link" href="https://highassurance.rs/chp2/dynamic_assurance_1.html" target="_blank" rel="noopener"
>2.4-2.6</a> that describe a high assurance
implementation of the RC4 stream cipher. At the end of the section the authors
state the following:</p>
<blockquote>
<p>You&rsquo;ve now built your first piece of high assurance software (sans the RC4 algorithm itself). Your RC4 library is:</p>
<ul>
<li>Fully memory-safe, hence <code>#![forbid(unsafe_code)]</code></li>
<li>Stand-alone and capable for running almost anywhere, hence <code>#![no_std]</code></li>
<li>Functionally validated, using official IETF test vectors</li>
</ul>
</blockquote>
<p>While these points are all on the list above they are what I would consider good
engineering principles (known answer tests with test vectors, and memory safety).
In particular, they lay the foundation for high assurance software but don’t
constitute high assurance in itself. As is, the code itself would be CA-1 but
additional mechanisms in the form of engineering processes and infrastructure
are needed for full CA-1 compliance.
(Similar additional mechanisms are mentioned in <a class="link" href="https://highassurance.rs/chp2/_index.html" target="_blank" rel="noopener"
>Chapter 2</a>.)</p>
<p>However, CA-1 is not sufficient for cryptographic primitives as stated above.
To reach CA-2 the code needs to be rewritten though because <strong>it has secret
dependent memory access</strong>.
After doing this property based testing, fuzzing, and techniques ensuring secret
independent computation have to be added.</p>
<p>Because the code can be seen as a spec one can argue that it reaches CA-3a now
as well.
CA-3b is not applicable.
However it is not very efficient but rather a specification.
In order to use the algorithm in a real application one might want to implement
an efficient version that would then require an equivalence proof with the
specification.</p>
<p><em>RC4 itself is of course not secure and must not be used! This is just taking the example from <a class="link" href="https://highassurance.rs" target="_blank" rel="noopener"
>https://highassurance.rs</a>.</em></p>
<h2 id="cryspen-high-assurance-cryptography">Cryspen High Assurance Cryptography</h2>
<p>At Cryspen we consider CA-1 regular software.
Cryptographic code at Cryspen must always be CA-2 or higher.</p>
<p>We are working on different high assurance cryptographic primitives and
protocols right now.</p>
<h3 id="hacl">HACL</h3>
<p>The <a class="link" href="https://github.com/cryspen/hacl-packages" target="_blank" rel="noopener"
>HACL packages</a> wrap the HACL* research artifacts and constitute a high
assurance cryptographic library.
The library is CA-3a because it is proven to be memory safe, has correctness
proofs with respect to a specification and ensures secret independent computation
on a programming language level.</p>
<h3 id="hpke">HPKE</h3>
<p>The <a class="link" href="https://github.com/cryspen/hpke-spec/" target="_blank" rel="noopener"
>HPKE implementation</a> is a specification of the <a class="link" href="https://datatracker.ietf.org/doc/rfc9180/" target="_blank" rel="noopener"
>HPKE RFC</a> and as such is CA-3a
because it is self-evidently correct as a specification and uses formally
verified cryptography with CA-3a.
In a next step the <a class="link" href="https://github.com/cryspen/hpke-spec/" target="_blank" rel="noopener"
>HPKE implementation</a> will be connected to the <a class="link" href="https://www.benjaminlipp.de/p/hpke-cryptographic-standard/#pre" target="_blank" rel="noopener"
>Cryptoverif</a>
models to prove security properties and thus reach the highest assurance
level CA-3b.</p>
<h3 id="tls-13">TLS 1.3</h3>
<p>In an <a class="link" href="https://www.assure.ngi.eu/" target="_blank" rel="noopener"
>NGI Assure</a> project we develop the first formally verified, production
ready TLS 1.3 implementation.</p>
<hr>
<p><a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> offers high assurance cryptographic implementations.
<a class="link" href="mailto:franziskus@cryspen.com" >Get in touch for more information.</a></p>
</description>
</item>
<item>
<title>An Executable HPKE Specification</title>
<link>https://www.franziskuskiefer.de/p/an-executable-hpke-specification/</link>
<pubDate>Thu, 24 Feb 2022 00:00:00 +0000</pubDate>
<guid>https://www.franziskuskiefer.de/p/an-executable-hpke-specification/</guid>
<description><img src="https://www.franziskuskiefer.de/p/an-executable-hpke-specification/header.png" alt="Featured image of post An Executable HPKE Specification" /><p><a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>, published as <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>RFC 9180</a>, describes a scheme for hybrid public key encryption.</p>
<blockquote>
<p>📚 Please go and read our <a class="link" href="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/" >TL;DR on HPKE</a> if you nee more background on HPKE.</p>
</blockquote>
<p>In this I describe the first executable HPKE specification using <a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a>.
It is not only an executable specification of <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>, it is also an annotated version
of the RFC that can be read instead of (or in addition to) the RFC.
While the <a class="link" href="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/" >TL;DR on HPKE</a> was intended for consumers or potential users of HPKE,
this blog post is aimed at implementators that want to implement HPKE or understand
it better.</p>
<p>It is a showcase for Cryspen&rsquo;s technology stack.
In a follow up blog post we will describe how to connect the <a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> specification
to efficient cryptographic primitives and formal proofs.</p>
<p><em>This blog post focuses on the Base and Auth mode with DHKEM to demonstrate the capabilities
of hacspec.
For the full specification please read the full <a class="link" href="https://tech.cryspen.com/hpke-spec" target="_blank" rel="noopener"
>documentation</a>, or look at the
<a class="link" href="https://github.com/cryspen/hpke-spec" target="_blank" rel="noopener"
>Github repository</a> for the HPKE hacspec source code.</em></p>
<p>Recall that HPKE provides a variant of public-key encryption of arbitrary-sized plaintexts for a recipient public key.
It works for any combination of an asymmetric key encapsulation mechanism (KEM), key derivation function (KDF), and authenticated encryption with additional data (AEAD) encryption function.</p>
<p>In the following I&rsquo;ll first show the high-level API of HPKE works before giving
details on the core functions within HPKE.
All code examples are in hacspec.</p>
<blockquote>
<p>💡 Go ahead and <a class="link" href="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/#try-it-out-now" >run the hacspec HPKE in the browser</a>.</p>
</blockquote>
<h2 id="encrypting-to-a-public-key">Encrypting to a Public Key</h2>
<p>This is the most basic functionality HPKE offers; encrypting a payload to a public key.
So how does this look on the outside?</p>
<p>The process consists of two steps.
<em>First</em> a random <code>shared_secret</code> is generated that can be used for
symmetric encryption with an AEAD, and an encapsulation that can be used by the
receiver in combination with their private key to compute the same <code>shared_secret</code>.
This function is denoted <code>SetupBaseS</code> below (because this is setting up the sender
in the HPKE base mode).
Note that the setup function expands the <code>shared_secret</code> to a key schedule that
is used by the AEAD.
More details in the <a class="link" href="#setup" >Setup</a> section.
<em>Then</em> the <code>shared_secret</code> is used to encrypt the payload with an AEAD.
This function is denoted <code>AeadSeal</code> below.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">enc</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">nonce</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">))</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">SetupBaseS</span><span class="p">(</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">pkR</span><span class="p">,</span><span class="w"> </span><span class="n">info</span><span class="p">,</span><span class="w"> </span><span class="n">randomness</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="kd">let</span><span class="w"> </span><span class="n">cipher_text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AeadSeal</span><span class="p">(</span><span class="n">aead_id</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">nonce</span><span class="p">,</span><span class="w"> </span><span class="n">aad</span><span class="p">,</span><span class="w"> </span><span class="n">payload</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>The receiver gets <code>cipher_text</code> and <code>enc</code> that it can use to retrieve the <code>payload</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">nonce</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">SetupBaseR</span><span class="p">(</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">enc</span><span class="p">,</span><span class="w"> </span><span class="n">skR</span><span class="p">,</span><span class="w"> </span><span class="n">info</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="kd">let</span><span class="w"> </span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AeadOpen</span><span class="p">(</span><span class="n">aead</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">nonce</span><span class="p">,</span><span class="w"> </span><span class="n">aad</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>In the remainder of this blog post we&rsquo;ll show how <code>SetupBaseS</code> is defined.
For a description of the receiver please check out the <a class="link" href="https://tech.cryspen.com/hpke-spec" target="_blank" rel="noopener"
>documentation</a>.
We will not define <code>AeadSeal</code> and <code>AeadOpen</code> here as they follow the definition
of <a class="link" href="https://www.rfc-editor.org/info/rfc5116" target="_blank" rel="noopener"
>RFC 5116</a>.</p>
<details>
<summary>💡 Background on hacspec Syntax</summary>
In case you are not familiar with hacspec (Rust) syntax, here are some short explainers
to understand the hacspec code.
<p><strong>The Question mark <code>?</code></strong></p>
<p>The question mark <code>?</code> at the end of most lines in the hacspec code is the
way Rust performs error propagation.
If the function that is called before the <code>?</code> does not return an error result,
the program continues as expected.
But if the function returns an error, the function stops and returns with the error
instead.</p>
<p><strong>The Result Type</strong></p>
<p>hacspec (and Rust) uses a <code>Result</code> type such as <code>Result&lt;OkType, ErrorType&gt;</code> to return errors.
In hacspec result types are often wrapped into type aliases.
For example the <code>SenderContextResult</code> type in the code snippet for <code>SetupBaseS</code>
below is a type alias for <code>Result&lt;(Encapsulation, KeySchedule), Error&gt;</code>.
If the function is successful and we reach line 10, the function returns success,
which is written as <code>SenderContextResult::Ok(...)</code>.</p>
</details>
<h3 id="the-auth-mode">The Auth Mode</h3>
<p>In the Auth mode HPKE requires additional input to the <code>Setup</code> functions.
The sender needs to provide their private key <code>skS</code> to authenticate themselves.
The receiver uses the sender&rsquo;s public key <code>pkS</code> in addition to authenticate the sender.
The two functions are defined as follows.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="n">SetupAuthS</span><span class="p">(</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">pkR</span>:<span class="p">,</span><span class="w"> </span><span class="n">info</span><span class="p">,</span><span class="w"> </span><span class="n">skS</span><span class="p">,</span><span class="w"> </span><span class="n">randomness</span><span class="p">);</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="n">SetupAuthR</span><span class="p">(</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">enc</span><span class="p">,</span><span class="w"> </span><span class="n">skR</span><span class="p">,</span><span class="w"> </span><span class="n">info</span><span class="p">,</span><span class="w"> </span><span class="n">pkS</span><span class="p">);</span><span class="w">
</span></span></span></code></pre></div><h3 id="setup">Setup</h3>
<p>In order to set up the KEM and key schedule the sender uses the following <code>SetupBaseS</code>
function.
Recall that the <code>BaseS</code> refers to the HPKE base mode and sender.</p>
<p>The function takes the receiver&rsquo;s public key <code>pkR</code> and context information <code>info</code>
(a sequence of bytes to bind the setup to a specific context).
In addition we need to pass in the <code>configuration</code> that contains the mode as well
as the algorithm identifiers for the KEM.
Because hacspec can&rsquo;t draw its own <code>randomness</code>, as explained <a class="link" href="#randomness" >below</a>,
it is passed in as well.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">SetupBaseS</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">configuration</span>: <span class="nc">HPKEConfig</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">pkR</span>: <span class="kp">&amp;</span><span class="nc">HpkePublicKey</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">info</span>: <span class="kp">&amp;</span><span class="nc">Info</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">randomness</span>: <span class="nc">Randomness</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">SenderContextResult</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">shared_secret</span><span class="p">,</span><span class="w"> </span><span class="n">enc</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Encap</span><span class="p">(</span><span class="n">kem</span><span class="p">(</span><span class="n">configuration</span><span class="p">),</span><span class="w"> </span><span class="n">pkR</span><span class="p">,</span><span class="w"> </span><span class="n">randomness</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">key_schedule</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KeySchedule</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">config</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">shared_secret</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">info</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">default_psk</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">default_psk_id</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">SenderContextResult</span>::<span class="nb">Ok</span><span class="p">((</span><span class="n">enc</span><span class="p">,</span><span class="w"> </span><span class="n">key_schedule</span><span class="p">))</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">}</span><span class="w">
</span></span></span></code></pre></div><p>For comparison you can find the RFC pseudocode definition of <code>SetupBaseS</code> below
(which is not well defined as is because it is missing the algorithm identifiers).
The main difference between the two functions is the explicit configuration and
randomness required in hacspec.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">def SetupBaseS(pkR, info):
</span></span><span class="line"><span class="cl"> shared_secret, enc = Encap(pkR)
</span></span><span class="line"><span class="cl"> return enc, KeyScheduleS(mode_base, shared_secret, info,
</span></span><span class="line"><span class="cl"> default_psk, default_psk_id)
</span></span></code></pre></div><p>The setup function calls the two functions <a class="link" href="#encap" ><code>Encap</code></a> and <a class="link" href="#keyschedule" ><code>KeySchedule</code></a>.</p>
<h3 id="encap">Encap</h3>
<p><em>(Reminder: For demonstration purposes we use the DHKEM defined in the RFC.)</em></p>
<p>The <code>Encap</code> function takes the receiver&rsquo;s public key <code>pkR</code> and generates a <code>shared_secret</code> as well
as an <code>encapsulation</code>.</p>
<p>It is necessary to pass in the algorithm identifier to know
which KEM to use and the randomness to generate a new ephemeral key pair for the
KEM.
See the <a class="link" href="#implementation-considerations" >discussion section below</a> on the necessity of the API changes.
Because the function can fail it returns a result instead of simply the computed
values as described in the RFC pseudocode.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">def Encap(pkR):
</span></span><span class="line"><span class="cl"> skE, pkE = GenerateKeyPair()
</span></span><span class="line"><span class="cl"> dh = DH(skE, pkR)
</span></span><span class="line"><span class="cl"> enc = SerializePublicKey(pkE)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> pkRm = SerializePublicKey(pkR)
</span></span><span class="line"><span class="cl"> kem_context = concat(enc, pkRm)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> shared_secret = ExtractAndExpand(dh, kem_context)
</span></span><span class="line"><span class="cl"> return shared_secret, enc
</span></span></code></pre></div><p>All these changes make it much clearer what can happen within the function and
in particular which error states might occur.</p>
<p>The <code>Encap</code> function generates a fresh DH key pair and computes the DH between the
receivers public key and the ephemeral private key $\text{dh}=\text{skE}*\text{pkR}$.
The <code>shared_secret</code> is then computed as the output of a key derivation function (HKDF)
on input of the <code>dh</code> value and the context that binds the key derivation to the
parameters and public values.
The encapsulation <code>enc</code> is the serialized public key <code>pkE</code> generated in the first
step.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">Encap</span><span class="p">(</span><span class="n">pkR</span>: <span class="kp">&amp;</span><span class="nc">PublicKey</span><span class="p">,</span><span class="w"> </span><span class="n">alg</span>: <span class="nc">KEM</span><span class="p">,</span><span class="w"> </span><span class="n">rand</span>: <span class="nc">Randomness</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">EncapResult</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">skE</span><span class="p">,</span><span class="w"> </span><span class="n">pkE</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GenerateKeyPair</span><span class="p">(</span><span class="n">alg</span><span class="p">,</span><span class="w"> </span><span class="n">rand</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">dh</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">DH</span><span class="p">(</span><span class="n">alg</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">skE</span><span class="p">,</span><span class="w"> </span><span class="n">pkR</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">encapsulation</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">SerializePublicKey</span><span class="p">(</span><span class="n">alg</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">pkE</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">pkRm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">SerializePublicKey</span><span class="p">(</span><span class="n">alg</span><span class="p">,</span><span class="w"> </span><span class="n">pkR</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">kem_context</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">enc</span><span class="p">.</span><span class="n">concat</span><span class="p">(</span><span class="o">&amp;</span><span class="n">pkRm</span><span class="p">);</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">shared_secret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ExtractAndExpand</span><span class="p">(</span><span class="n">alg</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">alg</span><span class="p">),</span><span class="w"> </span><span class="n">dh</span><span class="p">,</span><span class="w"> </span><span class="n">kem_context</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">EncapResult</span>::<span class="nb">Ok</span><span class="p">((</span><span class="n">shared_secret</span><span class="p">,</span><span class="w"> </span><span class="n">encapsulation</span><span class="p">))</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">}</span><span class="w">
</span></span></span></code></pre></div><h3 id="keyschedule">KeySchedule</h3>
<p>In order to use the <code>shared_secret</code> with an AEAD and allow exporting additional
key material, the following <code>KeySchedule</code> derives the <code>key</code> and <code>base_nonce</code> for the
AEAD and an <code>exporter_secret</code> to export other keys.
The key schedule is essentially a series of HKDF calls to extract different keys
from the shared secret.</p>
<p>The main difference to the RFC here is again that it is necessary to pass in algorithm
identifiers and the <code>suite_id</code> to <code>LabeledExtract</code> and <code>LabeledExpand</code>.
The <code>suite_id</code> binds the KDF extract and expand functions to the specific context
and is implicit in the RFC.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">KeySchedule</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">config</span>: <span class="nc">HPKEConfig</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">shared_secret</span>: <span class="kp">&amp;</span><span class="nc">SharedSecret</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">info</span>: <span class="kp">&amp;</span><span class="nc">Info</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">psk</span>: <span class="kp">&amp;</span><span class="nc">Psk</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">psk_id</span>: <span class="kp">&amp;</span><span class="nc">PskId</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">ContextResult</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">VerifyPSKInputs</span><span class="p">(</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">psk</span><span class="p">,</span><span class="w"> </span><span class="n">psk_id</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">HPKEConfig</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span><span class="w"> </span><span class="n">_kem</span><span class="p">,</span><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w"> </span><span class="n">aead</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">config</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">psk_id_hash</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExtract</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">empty_bytes</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">psk_id_hash_label</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">psk_id</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">info_hash</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExtract</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">empty_bytes</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">info_hash_label</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">info</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">key_schedule_context</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hpke_mode_label</span><span class="p">(</span><span class="n">mode</span><span class="p">)</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">.</span><span class="n">concat_owned</span><span class="p">(</span><span class="n">psk_id_hash</span><span class="p">)</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">.</span><span class="n">concat_owned</span><span class="p">(</span><span class="n">info_hash</span><span class="p">);</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">secret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExtract</span><span class="p">(</span><span class="n">kdf</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w"> </span><span class="n">shared_secret</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">secret_label</span><span class="p">(),</span><span class="w"> </span><span class="n">psk</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExpand</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">secret</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">key_label</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">key_schedule_context</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Nk</span><span class="p">(</span><span class="n">aead</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">base_nonce</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExpand</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">secret</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">base_nonce_label</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">key_schedule_context</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Nn</span><span class="p">(</span><span class="n">aead</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">exporter_secret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LabeledExpand</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">kdf</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">suite_id</span><span class="p">(</span><span class="n">config</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">secret</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">exp_label</span><span class="p">(),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">&amp;</span><span class="n">key_schedule_context</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Nh</span><span class="p">(</span><span class="n">kdf</span><span class="p">),</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">}</span><span class="w">
</span></span></span></code></pre></div><p>For comparison you can find the RFC pseudocode definition of <code>KeySchedule</code> below.
Note that the significantly longer hacspec definition above is not in fact longer
but has longer lines that are wrapped.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">def KeySchedule&lt;ROLE&gt;(mode, shared_secret, info, psk, psk_id):
</span></span><span class="line"><span class="cl"> VerifyPSKInputs(mode, psk, psk_id)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> psk_id_hash = LabeledExtract(&#34;&#34;, &#34;psk_id_hash&#34;, psk_id)
</span></span><span class="line"><span class="cl"> info_hash = LabeledExtract(&#34;&#34;, &#34;info_hash&#34;, info)
</span></span><span class="line"><span class="cl"> key_schedule_context = concat(mode, psk_id_hash, info_hash)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> secret = LabeledExtract(shared_secret, &#34;secret&#34;, psk)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> key = LabeledExpand(secret, &#34;key&#34;, key_schedule_context, Nk)
</span></span><span class="line"><span class="cl"> base_nonce = LabeledExpand(secret, &#34;base_nonce&#34;, key_schedule_context, Nn)
</span></span><span class="line"><span class="cl"> exporter_secret = LabeledExpand(secret, &#34;exp&#34;, key_schedule_context, Nh)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> return Context&lt;ROLE&gt;(key, base_nonce, 0, exporter_secret)
</span></span></code></pre></div><p>This is all that is needed to implement <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>.
All code examples here are taken directly from the Cryspen HPKE reference implementation.
You can find the full code in the <a class="link" href="https://github.com/cryspen/hpke-spec" target="_blank" rel="noopener"
>Github repository</a> as well as the <a class="link" href="https://tech.cryspen.com/hpke-spec" target="_blank" rel="noopener"
>documentation</a>.</p>
<h2 id="implementation-considerations">Implementation Considerations</h2>
<p>When defining HPKE in hacspec, or most other programming languages, there are a
number of considerations that impact the way the code looks.</p>
<p>The hacspec code is as close to the RFC pseudocode as possible.
But some changes are needed.</p>
<h3 id="randomness">Randomness</h3>
<p>hacspec does not allow to draw randomness.
It is therefore necessary to pass in randomness every time it is needed.</p>
<p>This approach is pretty close to the way this would be implemented in native Rust
where a random-number generator is passed in and used to generate randomness.
For simplicity hacspec expects the randomness to be drawn on the outside instead
of doing it within the specification.</p>
<p>Note that it is possible to pre-determine the amount of randomness needed by HPKE
calls because randomness is only needed when setting up the sender.
At this point the KEM mechanisms and hence the required randomness is known.</p>
<h3 id="configuration-parameters">Configuration Parameters</h3>
<p>The <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> RFC makes most of the configuration implicit to the functions rather than
passing the algorithm identifiers around.
Because the hacspec implementation has to know which algorithm to pick, this is
of course not possible here.</p>
<p>HPKE hacspec functions take either an <a class="link" href="https://tech.cryspen.com/hpke-spec/hpke/struct.HPKEConfig.html" target="_blank" rel="noopener"
><code>HPKEConfig</code></a> object with all algorithms
in it or the specific algorithm identifier needed for the operation.</p>
<h3 id="naming">Naming</h3>
<p>The <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> RFC uses, in some cases, names that are impossible to use in hacspec
because they are keywords or contain illegal characters.
Further does hacspec not support member functions as defined for the <code>Context</code>.</p>
<p>We therefore replace <code>.</code> in member function calls such as <code>Context.Export</code> with an underscore,
i.e. write <code>Context_Export</code>.
Keywords such as <code>open</code> are replaced with a semantically equivalent version, i.e.
<code>HpkeOpen</code> in this example.</p>
<h3 id="secret-bytes">Secret bytes</h3>
<p>hacspec has the notion of secret integers that can&rsquo;t be used for certain operations
and should enforce secret-independent computation time.</p>
<p>For simplicity the hacspec HPKE implementation uses secret bytes everywhere even
if not necessary, e.g. for cipher texts.</p>
<h3 id="errors">Errors</h3>
<p>While the RFC defines a set of errors it does not always define which errors
are raised.
For example, it leaves open whether implementations convert errors from the
Diffie-Hellman operations into KEM errors (<code>EncapError</code>/<code>DecapError</code>) or not.</p>
<p>With the specific implementation in hacspec here the errors are clearly defined.</p>
<h2 id="about-hacspec">About hacspec</h2>
<p><a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> is a specification language for cryptographic mechanisms, and more, embedded in <a class="link" href="https://www.rust-lang.org/" target="_blank" rel="noopener"
>Rust</a>.
It is a language for writing succinct, executable, formal specifications for cryptographic components.
Syntactically, hacspec is a purely functional subset of Rust that aims to be readable by developers, cryptographers, and verification experts.
An application developer can use hacspec to specify and prototype cryptographic components in Rust, and then either replace this specification with a verified implementation before deployment
or use the hacspec code directly.</p>
<p>We used <a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> here to write an executable, succinct, specification of <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>
that&rsquo;s embedding the full RFC into its <a class="link" href="https://tech.cryspen.com/hpke-spec" target="_blank" rel="noopener"
>documentation</a></p>
<p><a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> is at the heart of a novel, modular verification framework for Rust
applications developed by <a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> in cooperation with the <a class="link" href="https://team.inria.fr/prosecco/" target="_blank" rel="noopener"
>Prosecco</a> team.</p>
<h2 id="summary">Summary</h2>
<p>Even though <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> is a relatively simple scheme it requires care when implementing.
This blog post gives an overview of how <a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> can be used to achieve an executable
version of the <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> RFC that can be used as implementation on its own or as
specification and reference implementation when implementing HPKE.</p>
<p>My company <a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> offers support for using <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> as well as high assurance implementations
of HPKE and other protocols.
<a class="link" href="mailto:franziskus@cryspen.com" >Get in touch for more information.</a></p>
<hr>
<ul>
<li><i class="fab fa-github"></i> <a class="link" href="https://github.com/cryspen/hpke-spec" target="_blank" rel="noopener"
>Github repository</a></li>
<li><i class="fa fa-book" aria-hidden="true"></i> <a class="link" href="https://tech.cryspen.com/hpke-spec" target="_blank" rel="noopener"
>Documentation</a></li>
<li><i class="fa fa-file" aria-hidden="true"></i> <a class="link" href="https://www.rfc-editor.org/rfc/rfc9180.html" target="_blank" rel="noopener"
>RFC</a></li>
</ul>
</description>
</item>
<item>
<title>TL;DR - Hybrid Public Key Encryption</title>
<link>https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/</link>
<pubDate>Thu, 24 Feb 2022 00:00:00 +0000</pubDate>
<guid>https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/</guid>
<description><img src="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/hybrid.jpg" alt="Featured image of post TL;DR - Hybrid Public Key Encryption" /><p><a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> is a <a class="link" href="https://datatracker.ietf.org/rg/cfrg/about/" target="_blank" rel="noopener"
>CFRG</a> in <a class="link" href="https://www.rfc-editor.org/rfc/rfc9180.html" target="_blank" rel="noopener"
>RFC 9180</a> that describes a scheme for hybrid public key encryption.
It is co-authored by my <a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> co-founder <a class="link" href="https://bhargavan.info/index.html" target="_blank" rel="noopener"
>Karthikeyan Bhargavan</a> and one of
his PhD students <a class="link" href="https://www.benjaminlipp.de" target="_blank" rel="noopener"
>Benjamin Lipp</a> as part of his research at <a class="link" href="https://team.inria.fr/prosecco" target="_blank" rel="noopener"
>Inria</a>.</p>
<p>This blog post will give a brief overview of the specification and describes some use cases.</p>
<p>If you want to learn more about the security proofs behind HPKE and the RFC process,
Benjamin wrote an <a class="link" href="https://www.benjaminlipp.de/p/hpke-cryptographic-standard/" target="_blank" rel="noopener"
>excellent blog post</a> about it.</p>
<p>Hybrid Public Key Encryption, or short HPKE, is a cyrptographic mechanism that
allows encrypting payload to a public key.
It is called &ldquo;hybrid&rdquo; because the payload is encrypted with a symmetric scheme.
The symmetric key is then encrypted to the receivers public key.
The <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> standard defines a number of natural extensions to the basic setting
that allow the sender to authenticate themselves.</p>
<h2 id="hybrid-crypto-systems">Hybrid Crypto Systems</h2>
<p>Hybrid public key encryption has been used in different ways since the early 1990s in protocols such as <a class="link" href="https://en.wikipedia.org/wiki/Pretty_Good_Privacy" target="_blank" rel="noopener"
>PGP</a> or <a class="link" href="https://en.wikipedia.org/wiki/Cryptographic_Message_Syntax" target="_blank" rel="noopener"
>SMIME</a>.
While these two protocols are for a very specific use case a more general version of hybrid encryption is described in <a class="link" href="https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme" target="_blank" rel="noopener"
>ECIES</a>.
<a class="link" href="https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme" target="_blank" rel="noopener"
>ECIES</a> is part of many systems nowadays.
<a class="link" href="https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/storing_keys_in_the_secure_enclave" target="_blank" rel="noopener"
>Storing keys in the secure enclave on an iOS</a> device for example uses <a class="link" href="https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme" target="_blank" rel="noopener"
>ECIES</a>.
For a deeper explanation and history of hybrid crypto systems I recommend reading
<a class="link" href="https://blog.cloudflare.com/hybrid-public-key-encryption/" target="_blank" rel="noopener"
>Christopher Wood&rsquo;s blog post</a>.</p>
<p>However, there&rsquo;s no general description of hybrid public key encryption with modern primitives.
The <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> standard solves this issue.
Even before the RFC is finalised it is used in specifications for <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-tls-esni/" target="_blank" rel="noopener"
>ECH</a>, <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-mls-protocol/" target="_blank" rel="noopener"
>MLS</a>, <a class="link" href="https://datatracker.ietf.org/doc/draft-pauly-dprive-oblivious-doh/" target="_blank" rel="noopener"
>ODOH</a>, and <a class="link" href="https://datatracker.ietf.org/doc/draft-gpew-priv-ppm/" target="_blank" rel="noopener"
>PPM</a>.
This shows the high demand for <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>.</p>
<p>HPKE has four distinct modes of operation: Base, Auth, PSK, AuthPSK.
In this blog post we only describe the two most commonly used modes Base and Auth.</p>
<p><strong>HPKE Modes</strong></p>
<p>The <em>Base</em> mode is the most common use case for HPKE where payload is encrypted
to a public key.
All other modes are authenticated in different ways.</p>
<ul>
<li><em>Auth</em> uses the sender&rsquo;s private key for authentication</li>
<li><em>PSK</em> uses a pre-shared, high-entropy, key for authentication</li>
<li><em>AuthPSK</em> uses the sender&rsquo;s private key as well as a pre-shared, high-entropy, key for authentication</li>
</ul>
<h3 id="encrypting-to-a-public-key">Encrypting to a public key</h3>
<p>The following figure depicts the general flow of encrypting to a public key.
This is the Base mode in HPKE.
This is the most basic application of hybrid crypto systems.
Alex knows the public key from Sasha and wants to send some <code>Data</code> to them.
Shasha&rsquo;s public key is used to encrypt a shared secret, which is used to encrypt
the <code>Data</code>.
The encrypted shared secret as well as the encrypted data is sent to Sasha, who
can retrieve the shared secret with their corresponding private key.</p>
<p><figure style="flex-grow: 99; flex-basis: 238px">
<a href="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/hybrid-encryption.png" data-size="1229x1237"><img src="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/hybrid-encryption.png"
srcset="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/hybrid-encryption_hu0ec71786f79143a99e1764e382ce63cf_316986_480x0_resize_box_3.png 480w, https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/hybrid-encryption_hu0ec71786f79143a99e1764e382ce63cf_316986_1024x0_resize_box_3.png 1024w"
width="1229"
height="1237"
loading="lazy"
>
</a>
</figure></p>
<h3 id="encrypting-to-a-public-key--authenticating-with-an-asymmetric-key">Encrypting to a public key &amp; Authenticating with an asymmetric key</h3>
<p>Sometimes it is useful or necessary to authenticate the sender of the data.
This is depicted in the figure below and represents the HPKE Auth mode.
The authentication is achieved by mixing in the sender&rsquo;s private key such that
the receiver will only retrieve the correct shared secret if the public key
they use for the sender corresponds to the used private key.</p>
<p><figure style="flex-grow: 118; flex-basis: 285px">
<a href="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/authenticated-hybrid-encryption.png" data-size="1470x1237"><img src="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/authenticated-hybrid-encryption.png"
srcset="https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/authenticated-hybrid-encryption_hu2a8758be36ffa4ce890f3eb2a453dd08_355212_480x0_resize_box_3.png 480w, https://www.franziskuskiefer.de/p/tldr-hybrid-public-key-encryption/authenticated-hybrid-encryption_hu2a8758be36ffa4ce890f3eb2a453dd08_355212_1024x0_resize_box_3.png 1024w"
width="1470"
height="1237"
loading="lazy"
>
</a>
</figure></p>
<h2 id="hpke">HPKE</h2>
<p><a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> is essentially standardising how to use <a class="link" href="https://en.wikipedia.org/wiki/Key_encapsulation" target="_blank" rel="noopener"
>Key Encapsulation Mechanisms (KEM)</a>
for hybrid encryption.
The sender in <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> uses a KEM to generate the shared secret as well as the
encapsulation.
The shared secret is then used in an <a class="link" href="https://en.wikipedia.org/wiki/Authenticated_encryption#Authenticated_encryption_with_associated_data_%28AEAD%29" target="_blank" rel="noopener"
>AEAD</a> (after running it through a key schedule)
in order to encrypt a payload.</p>
<p>All HPKE use cases so far only take advantage of the single-shot APIs from HPKE.
In order to encrypt a payload to a public key the sender needs to provide the
receiver&rsquo;s public key <code>pkR</code>, some information <code>info</code> and additional data <code>aad</code> to bind the encryption
to a certain context, as well as the payload <code>pt</code>.
HPKE returns the cipher text <code>ct</code> as well as the encapsulation <code>enc</code> that are both sent to
the receiver.</p>
<pre tabindex="0"><code>enc, ct &lt;- Seal(pkR, info, aad, pt)
</code></pre><p>When using the Auth mode the sender&rsquo;s private key <code>sk</code> is needed in addition.</p>
<p>The receiver takes the encapsulation <code>enc</code> and cipher text <code>ct</code> together with their
private key to retrieve the payload.</p>
<pre tabindex="0"><code>pt &lt;- Open(enc, skR, info, aad, ct)
</code></pre><p>When using the Auth mode the sender&rsquo;s public key <code>pkS</code> is needed in addition.</p>
<h3 id="multiple-encryptions">Multiple Encryptions</h3>
<p>HPKE allows multiple encryptions with the same shared secret.
This is favourable if multiple messages are sent from the sender to the receiver.
To this end HPKE generates a context that allows encrypting (and decrypting) multiple
messages.</p>
<pre tabindex="0"><code>enc, ContextS &lt;- SetupS(pkR, info)
ContextR &lt;- SetupR(enc, skR, info)
</code></pre><p>Note that HPKE goes a step further than the <a class="link" href="https://datatracker.ietf.org/doc/html/rfc5116" target="_blank" rel="noopener"
>AEAD RFC</a> and simplifies the API.
The consumer only needs to provide the payload and (potentially empty) additional
data.
HPKE takes care of providing unique nonces to the AEAD and fails if the maximum
number of encryptions with the context have been performed &mdash; in particular if
the nonce would overflow.</p>
<pre tabindex="0"><code>ct &lt;- ContextS.Seal(aad, pt)
pt &lt;- ContextR.Open(aad, ct)
</code></pre><h3 id="exporting-secrets">Exporting Secrets</h3>
<p>In some scenarios applications need to establish additional shared secrets.
This can be achieved with the HPKE exporter interface.
The API is similar to the <code>Seal</code> and <code>Open</code> functions above but don&rsquo;t require
a payload or additional data.
Instead an <code>exporter_context</code> and the length of the exported secret <code>L</code> have to
be provided.</p>
<pre tabindex="0"><code>enc, exported_secret &lt;- SendExport(pkR, info, exporter_context, L)
exported_secret &lt;- ReceiveExport(enc, skR, info, exporter_context, L)
</code></pre><h3 id="use-cases">Use Cases</h3>
<p>Instead of inventing new use cases for HPKE we describe how HPKE is used in <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-mls-protocol/" target="_blank" rel="noopener"
>MLS</a>
and <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-tls-esni/" target="_blank" rel="noopener"
>ECH</a> as they reflect common uses of hybrid public key encryption.</p>
<h4 id="hpke-in-mls">HPKE in MLS</h4>
<p><a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-mls-protocol/" target="_blank" rel="noopener"
>MLS</a> (Message Layer Security) is an IETF draft that standardises a new way of efficiently encrypting messages
between participants in groups.
It aims to solve the problem of end-to-end encryption in instant messaging.
<a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> is a core building block.
In order to encrypt a message to a specific entity in the protocol, <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-mls-protocol/" target="_blank" rel="noopener"
>MLS</a> uses <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>
to encrypt the message to the specific public key.</p>
<h4 id="hpke-in-ech">HPKE in ECH</h4>
<p><a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-tls-esni/" target="_blank" rel="noopener"
>ECH</a> (Encrypted Client Hello) is a mechanism in <a class="link" href="https://tools.ietf.org/html/rfc8446" target="_blank" rel="noopener"
>TLS</a> (Transport Layer Security)
for encrypting a ClientHello message under a server public key.
This description from the <a class="link" href="https://datatracker.ietf.org/doc/draft-ietf-tls-esni/" target="_blank" rel="noopener"
>ECH</a> draft corresponds directly to the Base mode of <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a>
as described above.
This allows <a class="link" href="https://tools.ietf.org/html/rfc8446" target="_blank" rel="noopener"
>TLS</a> connections to become more private because they don&rsquo;t leak
information about the connection, in particular the exact server the client wants
to connect to.</p>
<h2 id="demo">Demo</h2>
<p>In order to better understand the message flow and working of HPKE we put together
an interactive demo below to demonstrate how HPKE works.</p>
<ol>
<li>First generate a key pair for the receiver.</li>
<li>Then populate the info, additional data, and payload fields on the sender side.</li>
<li>When clicking the &ldquo;HPKE Seal&rdquo; button on the sender the following happens</li>
</ol>
<ul>
<li>The sender retrieves the public key from the receiver that has been generated in the first step.</li>
<li>The sender uses HPKE to encrypt the payload together with the info and additional data to the receiver&rsquo;s public key.</li>
<li>The result is written into the Encoded Shared Secret and Ciphertext fields.</li>
</ul>
<ol start="4">
<li>When clicking &ldquo;HPKE Open&rdquo; the receiver uses the private key to retrieve the shared secret and decrypt the ciphertext.
The &ldquo;Info&rdquo; and &ldquo;Additional Data&rdquo; are the same as entered on the sender&rsquo;s side.</li>
</ol>
<h3 id="try-it-out-now">Try it out now!</h3>
<iframe width="100%" height="800px" src="./hpke_demo/index.html"></iframe>
<p>The demo is written in <a class="link" href="https://hacspec.org" target="_blank" rel="noopener"
>hacspec</a> (a subset of Rust) with a <a class="link" href="https://webassembly.org/" target="_blank" rel="noopener"
>WASM</a> frontend.
Please stay tuned for a follow-up blog post diving into the details of this implementation.</p>
<h2 id="summary">Summary</h2>
<p><a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> is a simple, but very powerful new tool that allows to efficiently solve
key distribution problems (see the MLS use case) as well as increase privacy
in existing protocols (see the ECH use case).</p>
<p>My company <a class="link" href="https://www.cryspen.com" target="_blank" rel="noopener"
>Cryspen</a> offers support for using <a class="link" href="https://datatracker.ietf.org/doc/draft-irtf-cfrg-hpke/" target="_blank" rel="noopener"
>HPKE</a> as well as high assurance implementations
of HPKE and other protocols.
<a class="link" href="mailto:franziskus@cryspen.com" >Get in touch for more information.</a></p>
</description>
</item>
<item>
<title>Cryspen ERC PoC Grant</title>
<link>https://www.franziskuskiefer.de/p/cryspen-erc-poc-grant/</link>
<pubDate>Thu, 10 Feb 2022 00:00:00 +0000</pubDate>
<guid>https://www.franziskuskiefer.de/p/cryspen-erc-poc-grant/</guid>
<description><img src="https://www.franziskuskiefer.de/p/cryspen-erc-poc-grant/erc.jpg" alt="Featured image of post Cryspen ERC PoC Grant" /><p>My co-founder <a class="link" href="https://bhargavan.info/" target="_blank" rel="noopener"
>Karthik</a> got awarded an <a class="link" href="https://erc.europa.eu/funding/proof-concept" target="_blank" rel="noopener"
>ERC Proof of Concept grant</a> for
commercialising the know-how and landmark research results from his Inria
research group <a class="link" href="https://team.inria.fr/prosecco/" target="_blank" rel="noopener"
>PROSECCO</a> through Cryspen.</p>
<h2 id="announcement">Announcement</h2>
<p>Cryptographic mechanisms are crucial to the security of our digital lives but their design and implementation remains notoriously difficult and error-prone. With the help of two ERC Grants, Karthikeyan Bhargavan and the Prosecco team at Inria have developed state-of-the-art formal verification techniques that can be applied to real-world cryptographic software. In particular, they used these techniques to help design and analyze the TLS 1.3 protocol standard, as well to build the HACL* verified cryptographic library, code from which has been incorporated in mainstream software projects including Mozilla Firefox, the Linux Kernel, the Tezos Blockchain, the WireGuard VPN, and the ElectionGuard voting system.</p>
<p>The goal of this new ERC Proof of Concept grant is to build upon these these landmark research results and to commercialise the associated know-how through a new company called Cryspen. Cryspen will build a verified cryptographic software stack and an associated verification toolchain that is well-documented and easy to use for security developers. Cryspen will offer consulting and support contracts for this stack as well as software contracts for developing new cryptographic applications, ranging from secure messaging system to privacy-preserving machine learning.</p>
<p>Cryspen is co-founded by Franziskus Kiefer (CEO), Karthikeyan Bhargavan, and Jonathan Protzenko. For more information, email <a class="link" href="mailto:info@cryspen.com" >info@cryspen.com</a></p>
</description>
</item>
<item>
<title>(HACL*) AEAD Benchmarks</title>
<link>https://www.franziskuskiefer.de/p/hacl-aead-benchmarks/</link>
<pubDate>Fri, 12 Nov 2021 00:00:00 +0000</pubDate>
<guid>https://www.franziskuskiefer.de/p/hacl-aead-benchmarks/</guid>
<description><img src="https://www.franziskuskiefer.de/p/hacl-aead-benchmarks/AeadBenchHero.png" alt="Featured image of post (HACL*) AEAD Benchmarks" /><blockquote>
<p>This is in response to the <a class="link" href="https://kerkour.com/rust-symmetric-encryption-aead-benchmark/" target="_blank" rel="noopener"
>blog post by Sylvain Kerkour</a> benchmarking ring and
Rust Crypto AEADs.
I was curious how HACL* stacks up to these two with these parameters.</p>
</blockquote>
<p>I&rsquo;m maintaining the <a class="link" href="https://crates.io/crates/evercrypt" target="_blank" rel="noopener"
>Evercrypt crate</a>, a wrapper
around the formally verified crypto library <a class="link" href="https://github.com/project-everest/hacl-star" target="_blank" rel="noopener"
>HACL*</a>.
HACL* is a customizable, fast, formally verified crypto library written in F* and extracted to C.</p>
<ul>
<li><a class="link" href="https://crates.io/crates/chacha20poly1305" target="_blank" rel="noopener"
>RustCrypto’s ChaCha20-Poly1305</a></li>
<li><a class="link" href="https://crates.io/crates/aes-gcm" target="_blank" rel="noopener"
>RustCrypto’s AES-256-GCM</a></li>
<li><a class="link" href="https://crates.io/crates/ring" target="_blank" rel="noopener"
>ring’s ChaCha20-Poly1305</a></li>
<li><a class="link" href="https://crates.io/crates/ring" target="_blank" rel="noopener"
>ring’s AES-256-GCM</a></li>
</ul>
<h2 id="results">Results</h2>
<p>I&rsquo;m listing all results here for comparison as I&rsquo;m (obviously) running the benchmarks on a different machine.</p>
<table>
<thead>
<tr>
<th></th>
<th>100B</th>
<th>1kB</th>
<th>100kB</th>
<th>1MB</th>
<th>10MB</th>
<th>100MB</th>
<th>1GB</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="link" href="https://crates.io/crates/chacha20poly1305" target="_blank" rel="noopener"
>RustCrypto’s ChaCha20-Poly1305</a> v0.8.2</td>
<td>1.6232 us (58.753 MiB/s)</td>
<td>2.6941 us (353.98 MiB/s)</td>
<td>120.10 us (794.10 MiB/s)</td>
<td>1.1921 ms (800.02 MiB/s)</td>
<td>12.015 ms (793.75 MiB/s)</td>
<td>119.87 ms (795.58 MiB/s)</td>
<td>1.1947 s (798.27 MiB/s)</td>
</tr>
<tr>
<td><a class="link" href="https://crates.io/crates/aes-gcm" target="_blank" rel="noopener"
>RustCrypto’s AES-256-GCM</a> v0.9.4</td>
<td>448.97 ns (212.42 MiB/s)</td>
<td>1.5090 us (632.01 MiB/s)</td>
<td>118.13 us (807.33 MiB/s)</td>
<td>1.1947 ms (798.24 MiB/s)</td>
<td>11.986 ms (795.68 MiB/s)</td>
<td>119.39 ms (798.81 MiB/s)</td>
<td>1.1974 s (796.43 MiB/s)</td>
</tr>
<tr>
<td><a class="link" href="https://crates.io/crates/ring" target="_blank" rel="noopener"
>ring’s ChaCha20-Poly1305</a> v0.16.20</td>
<td>193.82 ns (492.04 MiB/s)</td>
<td>730.23 ns (1.2754 GiB/s)</td>
<td>48.293 us (1.9285 GiB/s)</td>
<td>490.64 us (1.8982 GiB/s)</td>
<td>5.0475 ms (1.8451 GiB/s)</td>
<td>51.438 ms (1.8106 GiB/s)</td>
<td>514.99 ms (1.8084 GiB/s)</td>
</tr>
<tr>
<td><a class="link" href="https://crates.io/crates/ring" target="_blank" rel="noopener"
>ring’s AES-256-GCM</a> v0.16.20</td>
<td>235.57 ns (404.83 MiB/s)</td>
<td>556.64 ns (1.6731 GiB/s)</td>
<td>34.609 us (2.6910 GiB/s)</td>
<td>343.41 us (2.7120 GiB/s)</td>
<td>3.5471 ms (2.6256 GiB/s)</td>
<td>34.873 ms (2.6706 GiB/s)</td>
<td>348.51 ms (2.6723 GiB/s)</td>
</tr>
<tr>
<td><a class="link" href="https://crates.io/crates/evercrypt" target="_blank" rel="noopener"
>HACL*’s ChaCha20-Poly1305</a> v0.0.10</td>
<td>862.79 ns (110.53 MiB/s)</td>
<td>1.2804 us (744.81 MiB/s)</td>
<td>55.550 us (1.6765 GiB/s)</td>
<td>549.11 us (1.6961 GiB/s)</td>
<td>5.8844 ms (1.5827 GiB/s)</td>
<td>88.801 ms (1.0488 GiB/s)</td>
<td>847.39 ms (1.0990 GiB/s)</td>
</tr>
<tr>
<td><a class="link" href="https://crates.io/crates/evercrypt" target="_blank" rel="noopener"
>HACL*’s AES-256-GCM</a> v0.0.10</td>
<td>238.12 ns (400.51 MiB/s)</td>
<td>598.56 ns (1.5560 GiB/s)</td>
<td>38.997 us (2.3882 GiB/s)</td>
<td>391.87 us (2.3766 GiB/s)</td>
<td>4.0217 ms (2.3157 GiB/s)</td>
<td>68.004 ms (1.3695 GiB/s)</td>
<td>642.12 ms (1.4504 GiB/s)</td>
</tr>
</tbody>
</table>
<p>It is interesting to see that the HACL* AES-256-GCM implementation is only slightly
slower than ring&rsquo;s (2.3GiB/s vs 2.7GiB/s) for 1MB and 10MB chunks.
But it significantly drops in performance for larger blobs while ring&rsquo;s performance
stays the same.
The picture for Chacha20Poly1305 is similar, which points to general issues of
handling large data sizes within HACL*.</p>
<p><a class="link" href="./aead-intel-benchmarks.txt" >Raw number</a></p>
<h2 id="m1">M1</h2>
<p>My main machine right now is a MacBook with M1 chip.
This is a very different machine.
Here are the numbers.</p>
<p>Note that HACL* doesn&rsquo;t support AES on ARM chips yet unfortunately.</p>
<table>
<thead>
<tr>
<th></th>
<th>100B</th>
<th>1kB</th>
<th>100kB</th>
<th>1MB</th>
<th>10MB</th>
<th>100MB</th>
<th>1GB</th>
</tr>
</thead>
<tbody>
<tr>
<td>[RustCrypto’s XChaCha20-Poly1305] v0.8.2</td>
<td>558.20 ns (170.85 MiB/s)</td>
<td>3.0136 us (316.46 MiB/s)</td>
<td>274.25 us (347.74 MiB/s)</td>