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
//! The contract private key, and the envelope it travels in.
//!
//! `CertificateInstallationRes` and `CertificateUpdateRes` deliver a contract
//! certificate *and its private key* — which is the one place in ISO 15118
//! where a secret crosses the wire. \[V2G2-814\] permits it only encrypted, and
//! the rest of §7.9.2.4.3 pins every choice:
//!
//! ```text
//! ContractSignatureEncryptedPrivateKey
//! = IV ‖ AES-128-CBC( K, private key ) [V2G2-815], [V2G2-817]
//! IV 16 bytes, random, never reused
//! K the ECDH session key, below
//! key 32 bytes, big-endian, no padding [V2G2-816]
//!
//! K = leftmost 128 bits of [V2G2-818]
//! SHA-256( 00 00 00 01 ‖ Z ‖ 01 55 56 )
//! Z the ECDH shared secret's x-coordinate
//! 01 AlgorithmID
//! 55 IDU, the sender ("U")
//! 56 IDV, the receiver ("V")
//!
//! DHpublickey = the sender's ephemeral public key, uncompressed [V2G2-819]
//! ```
//!
//! The ECDH is the ephemeral-static one-pass scheme of NIST SP 800-56A
//! §6.2.2.2: the sender is party U with an ephemeral key pair, and the receiver
//! is party V, whose *static* key is the one already in a certificate — the OEM
//! Provisioning Certificate for an installation \[V2G2-820\], the existing
//! Contract Certificate for an update \[V2G2-821\]. The key derivation is the
//! concatenation KDF with SHA-256, and 128 bits fits in one round, which is why
//! the counter above is `1` and appears once.
//!
//! # The check that is easy to leave out
//!
//! \[V2G2-823\] is explicit, and it is the only thing standing between a
//! vehicle and a contract key that does not belong to the certificate it
//! arrived with: the value "must be strictly smaller than the order of the base
//! point, and multiplication of the base point with this value must generate a
//! key matching the public key of the contract certificate".
//!
//! So [`open`] takes the certificate's public key and does that comparison, and
//! there is no call that skips it. A vehicle that installs an unchecked key has
//! a contract it can never use and cannot diagnose — every later signature it
//! makes is valid, over the wrong key, and every station refuses it.
//!
//! # What is still the caller's
//!
//! The randomness, as everywhere in this crate. Sealing needs an ephemeral key
//! pair and an IV that is "randomly generated ... and never reused"
//! \[V2G2-815\]; both are parameters, and a repeated IV under one session key
//! leaks the difference between two private keys.
//!
//! The elliptic-curve arithmetic and the block cipher, as traits —
//! [`KeyAgreement`] and [`Aes128Cbc`] — for the same reason [`Sign`](super::Sign)
//! is one: a contract key that is meant to live in a secure element must never
//! materialise in this crate's address space, and on that hardware the ECDH
//! happens inside the element too.
use Vec;
use ;
/// Length of the initialisation vector, in bytes. \[V2G2-815\]
pub const IV_LEN: usize = 16;
/// Length of a contract private key, in bytes.
///
/// ISO 15118-2 uses secp256r1 throughout \[V2G2-007\], so the scalar is 256
/// bits — and NOTE 7 of \[V2G2-815\] points out that this is already a multiple
/// of AES's block size, which is why the ciphertext carries no padding.
pub const PRIVATE_KEY_LEN: usize = 32;
/// Length of `DHpublickey` — an uncompressed SEC1 point on secp256r1.
///
/// \[V2G2-819\] and its NOTE 9: `DHpublickey` "is 65 bytes long. The first
/// byte has the fixed value 0x04 indicating the uncompressed form." The
/// compressed form is excluded outright.
pub const DH_PUBLIC_KEY_LEN: usize = 65;
/// Total length of `ContractSignatureEncryptedPrivateKey`.
pub const ENVELOPE_LEN: usize = IV_LEN + PRIVATE_KEY_LEN;
/// The `AlgorithmID`, `IDU` and `IDV` of \[V2G2-818\], in the order the KDF
/// concatenates them.
const OTHER_INFO: = ;
/// The elliptic-curve operations the envelope needs, beyond signing.
///
/// Separate from [`Sign`](super::Sign) because they are a different capability
/// on the same key: a secure element that will sign with a contract key may or
/// may not agree with it, and ISO 15118 says so — \[V2G2-822\] requires the
/// `keyAgreement` usage flag on exactly the certificates whose keys do this,
/// and on no others.
/// AES-128 in CBC mode, over whole blocks and with no padding.
///
/// ISO 15118 needs exactly this and nothing more: two blocks in each direction,
/// a private key that is already a multiple of the block size, and therefore no
/// padding scheme to disagree about \[V2G2-815\] NOTE 7.
/// Derives the session key from an ECDH shared secret. \[V2G2-818\]
///
/// The concatenation KDF of NIST SP 800-56A with SHA-256, over the fixed
/// `OtherInfo` ISO 15118 pins. 128 bits fit in one round of a 256-bit hash, so
/// the counter is `1` and appears once — which is why the whole derivation is
/// one digest and needs nothing from the caller but the [`Hash`] it already has.
///
/// Exposed because a caller doing the SA's half, or checking a capture by hand,
/// needs the same sixteen bytes and should not re-derive the layout from prose.
/// Opens a `ContractSignatureEncryptedPrivateKey`, returning the contract
/// private key.
///
/// * `envelope` — the field as it arrived: [`IV_LEN`] bytes of IV followed by
/// the ciphertext \[V2G2-815\].
/// * `dh_public_key` — the sender's `DHpublickey` \[V2G2-819\].
/// * `certificate_public_key` — the public key of the contract certificate this
/// key is supposed to belong to, uncompressed, as
/// [`pki::Certificate::public_key`](super::pki::Certificate::public_key)
/// hands it over.
///
/// The last one is not optional, and that is the point: \[V2G2-823\] makes the
/// check part of receiving a contract certificate, and a signature made with a
/// key that does not match its certificate verifies perfectly and is refused by
/// every station.
///
/// # Errors
///
/// [`PncError::BadEnvelope`] for a field that is not [`ENVELOPE_LEN`] bytes or a
/// `DHpublickey` that is not an uncompressed point;
/// [`PncError::KeyMismatch`] when the delivered key does not generate the
/// certificate's public key; and whatever the backend returns for a shared
/// secret it could not compute.
/// Builds a `ContractSignatureEncryptedPrivateKey`. \[V2G2-815\]
///
/// The secondary actor's half, and the one a test needs to have any confidence
/// in the other. `iv` must be randomly generated and **never reused** under one
/// session key: two private keys encrypted under the same key and IV differ by
/// exactly the XOR of their first blocks. This crate has no RNG, so the caller
/// supplies it, and the requirement is stated here rather than assumed.
///
/// `agreement` is the *sender's* ephemeral key, against the receiver's static
/// public key — the reverse of [`open`]'s pairing, which is what makes the two
/// arrive at the same secret.
///
/// # Errors
///
/// [`PncError::BadEnvelope`] for a private key that is not
/// [`PRIVATE_KEY_LEN`] bytes, and whatever the backend returns otherwise.