Commit ff1cdb6
authored
Some terminology:
- `EK`: encapsulated key, i.e. ciphertext, not to be confused with
"encapsulation key", the public key
- `SS`: shared secret, output of the decapsulator when given a
ciphertext a.k.a. encapsulated key
`EK` and `SK` were previously generic parameters on the `Encapsulate`
and `Decapsulate` traits, however KEMs don't benefit from overlapping
impls and have relatively fixed notions of what these types should be.
This commit replaces them with new type aliases `Ciphertext` and
`SharedSecret`:
- `Ciphertext<K>`: type alias for `Array<u8, K::CiphertextSize>`, a.k.a.
"encapsulated key", where `Array` is from `hybrid-array`.
- `SharedSecret<K>`: type alias for `Array<u8, K::SharedSecretSize>`
This means consumers of the traits always use bytestrings, which should
hopefully make it dramatically simpler to implement things generically
across KEMs.
The `K` generic parameter above is for types which impl a new
`KemParams` trait which defines two associated `ArraySize`s:
- `KemParams::CiphertextSize`: size of the ciphertext
- `KemParams::SharedSecretSize`: size of the shared secret
This was split out into its own trait so the `Ciphertext<K>` and
`SharedSecret<K>` type aliases work with either encapsulators or
decapsulators.
Next, `Decapsulate` was split into three(!) traits to handle fallible
decapsulation:
- `Decapsulate`: what we had before with the `Decapsulate::Encapsulator`
associated type extracted into a supertrait `Decapsulator`, which it now
bounds on. It has a provided `decapsulate_slice` method which returns
`core::array::TryFromSliceError` in the event the provided slice does
not match `CiphertextSize`
- `TryDecapsulate`: fallible equivalent of `Decapsulate`, kind of like
what we had prior to #2216, with an associated `Error` type and with a
`try_decapsulate` method that returns a result. It also bounds on
`Decapsulator` as its supertrait.
- `Decapsulator`: common supertrait of `Decapsulate` and
`TryDecapsulate` which defines the associated `Encapsulator` and
provides the `Decapsulator::encapsulator` method for retrieving it.
A blanket impl of `TryDecapsulate` is provided for types which impl
`Decapsulate` which uses `Infallible` as the error type, so any type
which impls `Decapsulate` can be used as a `TryDecapsulate`-bounded
argument. Likewise `Decapsulate` carries a `TryDecapsulate<Error =
Infallible>` bound which is satisfied by the blanket impl but also
enforces this property.
The reason we need to reintroduce fallible decapsulation is `dhkem`:
when I was scanning over our KEMs repo looking at the error types,
`dhkem` has `Error = Infallible`, but this hides that it was using
`elliptic_curve::PublicKey<C>` as its "encapsulated key" / ciphertext
type, which is a well-typed wrapper for a valid curve point.
With this type now being a raw byte slice, `dhkem` needs to handle
decoding the curve point in the `TryDecapsulate` impl, and if the point
fails to decode return an error (there are ways we could pseudorandomly
select a different point in constant time to compute a rejection symbol,
but having it return an error in this case seems like a straightforward
way to start).
Closes #2219
1 parent 5537635 commit ff1cdb6
1 file changed
Lines changed: 101 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | 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 | + | |
20 | 46 | | |
21 | 47 | | |
22 | 48 | | |
23 | 49 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
29 | 57 | | |
30 | 58 | | |
31 | 59 | | |
32 | | - | |
| 60 | + | |
33 | 61 | | |
34 | 62 | | |
35 | 63 | | |
36 | 64 | | |
37 | 65 | | |
38 | 66 | | |
39 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
40 | 89 | | |
41 | 90 | | |
42 | 91 | | |
| 92 | + | |
43 | 93 | | |
44 | 94 | | |
45 | 95 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
49 | 99 | | |
50 | | - | |
51 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
52 | 108 | | |
53 | | - | |
54 | | - | |
| 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 | + | |
55 | 142 | | |
0 commit comments