-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathecdh_kem.rs
More file actions
170 lines (153 loc) · 5.82 KB
/
Copy pathecdh_kem.rs
File metadata and controls
170 lines (153 loc) · 5.82 KB
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
//! Generic Elliptic Curve Diffie-Hellman KEM adapter.
use crate::{DecapsulationKey, DhKem, EncapsulationKey};
use core::marker::PhantomData;
use elliptic_curve::{
AffinePoint, CurveArithmetic, Error, FieldBytesSize, PublicKey,
ecdh::EphemeralSecret,
sec1::{
FromEncodedPoint, ModulusSize, ToEncodedPoint, UncompressedPoint, UncompressedPointSize,
},
};
use kem::{
Ciphertext, Encapsulate, Generate, InvalidKey, KemParams, KeyExport, KeySizeUser, SharedSecret,
TryDecapsulate, TryKeyInit,
};
use rand_core::{CryptoRng, TryCryptoRng};
/// Elliptic Curve Diffie-Hellman Decapsulation Key (i.e. secret decryption key)
///
/// Generic around an elliptic curve `C`.
pub type EcdhDecapsulationKey<C> = DecapsulationKey<EphemeralSecret<C>, PublicKey<C>>;
/// Elliptic Curve Diffie-Hellman Encapsulation Key (i.e. public encryption key)
///
/// Generic around an elliptic curve `C`.
pub type EcdhEncapsulationKey<C> = EncapsulationKey<PublicKey<C>>;
/// Generic Elliptic Curve Diffie-Hellman KEM adapter compatible with curves implemented using
/// traits from the `elliptic-curve` crate.
///
/// Implements a KEM interface that internally uses ECDH.
pub struct EcdhKem<C: CurveArithmetic>(PhantomData<C>);
impl<C> KemParams for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
{
type CiphertextSize = UncompressedPointSize<C>;
type SharedSecretSize = FieldBytesSize<C>;
}
/// From [RFC9810 §7.1.1]: `SerializePublicKey` and `DeserializePublicKey`:
///
/// > For P-256, P-384, and P-521, the SerializePublicKey() function of the
/// > KEM performs the uncompressed Elliptic-Curve-Point-to-Octet-String
/// > conversion according to [SECG].
///
/// [RFC9810 §7.1.1]: https://datatracker.ietf.org/doc/html/rfc9180#name-serializepublickey-and-dese
/// [SECG]: https://www.secg.org/sec1-v2.pdf
impl<C> KeySizeUser for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
{
type KeySize = UncompressedPointSize<C>;
}
/// From [RFC9810 §7.1.1]: `SerializePublicKey` and `DeserializePublicKey`:
///
/// > DeserializePublicKey() performs the uncompressed
/// > Octet-String-to-Elliptic-Curve-Point conversion.
///
/// [RFC9810 §7.1.1]: https://datatracker.ietf.org/doc/html/rfc9180#name-serializepublickey-and-dese
impl<C> TryKeyInit for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
fn new(encapsulation_key: &UncompressedPoint<C>) -> Result<Self, InvalidKey> {
PublicKey::<C>::from_sec1_bytes(encapsulation_key)
.map(Into::into)
.map_err(|_| InvalidKey)
}
}
/// From [RFC9810 §7.1.1]: `SerializePublicKey` and `DeserializePublicKey`:
///
/// > For P-256, P-384, and P-521, the SerializePublicKey() function of the
/// > KEM performs the uncompressed Elliptic-Curve-Point-to-Octet-String
/// > conversion according to [SECG].
///
/// [RFC9810 §7.1.1]: https://datatracker.ietf.org/doc/html/rfc9180#name-serializepublickey-and-dese
/// [SECG]: https://www.secg.org/sec1-v2.pdf
impl<C> KeyExport for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
fn to_bytes(&self) -> UncompressedPoint<C> {
// TODO(tarcieri): self.0.to_uncompressed_point()
let mut ret = UncompressedPoint::<C>::default();
ret.copy_from_slice(self.to_encoded_point(false).as_bytes());
ret
}
}
impl<C> Encapsulate for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
fn encapsulate_with_rng<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
) -> Result<(Ciphertext<Self>, SharedSecret<Self>), R::Error> {
// ECDH encapsulation involves creating a new ephemeral key pair and then doing DH
let sk = EphemeralSecret::try_generate_from_rng(rng)?;
let ss = sk.diffie_hellman(&self.0);
// TODO(tarcieri): sk.public_key().to_uncompressed_point()
let mut pk = UncompressedPoint::<C>::default();
pk.copy_from_slice(sk.public_key().to_encoded_point(false).as_bytes());
Ok((pk, ss.raw_secret_bytes().clone()))
}
}
impl<C> Generate for EcdhDecapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
{
fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Ok(EphemeralSecret::try_generate_from_rng(rng)?.into())
}
}
impl<C> TryDecapsulate for EcdhDecapsulationKey<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
type Error = Error;
fn try_decapsulate(
&self,
encapsulated_key: &Ciphertext<Self>,
) -> Result<SharedSecret<Self>, Error> {
let encapsulated_key = PublicKey::<C>::from_sec1_bytes(encapsulated_key)?;
let shared_secret = self.dk.diffie_hellman(&encapsulated_key);
Ok(shared_secret.raw_secret_bytes().clone())
}
}
impl<C> DhKem for EcdhKem<C>
where
C: CurveArithmetic,
FieldBytesSize<C>: ModulusSize,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
type DecapsulatingKey = EcdhDecapsulationKey<C>;
type EncapsulatingKey = EcdhEncapsulationKey<C>;
type EncapsulatedKey = Ciphertext<EcdhDecapsulationKey<C>>;
type SharedSecret = SharedSecret<EcdhDecapsulationKey<C>>;
fn random_keypair<R: CryptoRng + ?Sized>(
rng: &mut R,
) -> (Self::DecapsulatingKey, Self::EncapsulatingKey) {
// TODO(tarcieri): propagate RNG errors
let sk = EphemeralSecret::try_generate_from_rng(rng).expect("RNG failure");
let pk = PublicKey::from(&sk);
(DecapsulationKey::from(sk), EncapsulationKey(pk))
}
}