blob: c60eddc6fbf4a4a2b363226c9f95e908dc1314b7 [file] [log] [blame] [view]
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +02001This document lists current limitations of the PSA Crypto API (as of version
21.1) that may impact our ability to (1) use it for all crypto operations in
3TLS and X.509 and (2) support isolation of all long-term secrets in TLS (that
Manuel Pégourié-Gonnard8e559da2022-02-01 10:26:07 +01004is, goals G1 and G2 in [strategy.md](strategy.md) in the same directory).
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +02005
6This is supposed to be a complete list, based on a exhaustive review of crypto
7operations done in TLS and X.509 code, but of course it's still possible that
8subtle-but-important issues have been missed. The only way to be really sure
9is, of course, to actually do the migration work.
10
11Limitations relevant for G1 (performing crypto operations)
12==========================================================
13
14Restartable ECC operations
15--------------------------
16
17There is currently no support for that in PSA at all. API design, as well as
18implementation, would be non-trivial.
19
20Currently, `MBEDTLS_USE_PSA_CRYPTO` is simply incompatible with
21`MBEDTLS_ECP_RESTARTABLE`.
22
Manuel Pégourié-Gonnard8e559da2022-02-01 10:26:07 +010023Things that are in the API but not implemented yet
24--------------------------------------------------
25
26PSA Crypto has an API for FFDH, but it's not implemented in Mbed TLS yet.
27(Regarding FFDH, see the next section as well.) See issue [3261][ffdh] on
28github.
29
30[ffdh]: https://github.com/ARMmbed/mbedtls/issues/3261
31
32PSA Crypto has an experimental API for EC J-PAKE, but it's not implemented in
33Mbed TLS yet. See the [EC J-PAKE follow-up EPIC][ecjp] on github.
34
35[ecjp]: https://github.com/orgs/ARMmbed/projects/18#column-15836385
36
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020037Arbitrary parameters for FFDH
38-----------------------------
39
Manuel Pégourié-Gonnard8e559da2022-02-01 10:26:07 +010040(See also the first paragraph in the previous section.)
41
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020042Currently, the PSA Crypto API can only perform FFDH with a limited set of
43well-know parameters (some of them defined in the spec, but implementations
44are free to extend that set).
45
46TLS 1.2 (and earlier) on the other hand have the server send explicit
47parameters (P and G) in is ServerKeyExchange message. This has been found to
48be suboptimal for security, as it is prohibitively hard for the client to
49verify the strength of these parameters. This led to the development of RFC
507919 which allows use of named groups in TLS 1.2 - however as this is only an
51extension, servers can still send custom parameters if they don't support the
52extension.
53
54In TLS 1.3 the situation will be simpler: named groups are the only
55option, so the current PSA Crypto API is a good match for that. (Not
56coincidentally, the groups used by RFC 7919 and TLS 1.3 are part those defined
57in the specification.)
58
59There are several options here:
60
611. Implement support for custom FFDH parameters in PSA Crypto: this would pose
62 non-trivial API design problem, but most importantly seems backwards, as
63the crypto community is moving away from custom FFDH parameters.
642. Drop the DHE-RSA and DHE-PSK key exchanges in TLS 1.2 when moving to PSA.
653. Implement RFC 7919, support DHE-RSA and DHE-PSK only in conjunction with it
66 when moving to PSA. We can modify our server so that it only selects a DHE
67 ciphersuite if the client offered name FFDH groups; unfortunately
68client-side the only option is to offer named groups and break the handshake
69if the server didn't take on our offer. This is not fully satisfying, but is
70perhaps the least unsatisfying option in terms of result; it's also probably
71the one that requires the most work, but it would deliver value beyond PSA
72migration by implementing RFC 7919.
73
74RSA-PSS parameters
75------------------
76
77RSA-PSS signatures are defined by PKCS#1 v2, re-published as RFC 8017
78(previously RFC 3447).
79
80As standardized, the signature scheme takes several parameters, in addition to
81the hash algorithm potentially used to hash the message being signed:
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020082- a hash algorithm used for the encoding function
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020083- a mask generation function
84 - most commonly MGF1, which in turn is parametrized by a hash algorithm
85- a salt length
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020086- a trailer field - this is universally 0xBC as far as I've seen
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020087
88Both the existing `mbedtls_` API and the PSA API support only MGF1 as the
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020089generation function (and only 0xBC as the trailer field), but there are
90discrepancies in handling the salt length and which of the various hash
91algorithms can differ from each other.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020092
93### API comparison
94
95- RSA:
96 - signature: `mbedtls_rsa_rsassa_pss_sign()`
97 - message hashed externally
98 - encoding hash = MGF1 hash (from context, or argument = message hash)
99 - salt length: always using the maximum legal value
100 - signature: `mbedtls_rsa_rsassa_pss_sign_ext()`
101 - message hashed externally
102 - encoding hash = MGF1 hash (from context, or argument = message hash)
103 - salt length: specified explicitly
104 - verification: `mbedtls_rsassa_pss_verify()`
105 - message hashed externally
106 - encoding hash = MGF1 hash (from context, or argument = message hash)
107 - salt length: any valid length accepted
108 - verification: `mbedtls_rsassa_pss_verify_ext()`
109 - message hashed externally
110 - encoding hash = MGF1 hash from dedicated argument
111 - expected salt length: specified explicitly, can specify "ANY"
112- PK:
113 - signature: not supported
114 - verification: `mbedtls_pk_verify_ext()`
115 - message hashed externally
116 - encoding hash = MGF1 hash, specified explicitly
117 - expected salt length: specified explicitly, can specify "ANY"
118- PSA:
119 - algorithm specification:
120 - hash alg used for message hashing, encoding and MGF1
Manuel Pégourié-Gonnard539b9a52022-02-07 10:19:08 +0100121 - salt length can be either "standard" (<= hashlen, see note) or "any"
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200122 - signature generation:
Manuel Pégourié-Gonnard539b9a52022-02-07 10:19:08 +0100123 - salt length: always <= hashlen (see note) and random salt
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200124 - verification:
Manuel Pégourié-Gonnard539b9a52022-02-07 10:19:08 +0100125 - salt length: either <= hashlen (see note), or any depending on algorithm
126
127Note: above, "<= hashlen" means that hashlen is used if possible, but if it
Manuel Pégourié-Gonnard80759c42022-02-08 10:33:11 +0100128doesn't fit because the key is too short, then the maximum length that fits is
Manuel Pégourié-Gonnard539b9a52022-02-07 10:19:08 +0100129used.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200130
131The RSA/PK API is in principle more flexible than the PSA Crypto API. The
132following sub-sections study whether and how this matters in practice.
133
134### Use in X.509
135
136RFC 4055 Section 3.1 defines the encoding of RSA-PSS that's used in X.509.
137It allows independently specifying the message hash (also used for encoding
138hash), the MGF (and its hash if MGF1 is used), and the salt length (plus an
139extra parameter "trailer field" that doesn't vary in practice"). These can be
140encoded as part of the key, and of the signature. If both encoding are
141presents, all values must match except possibly for the salt length, where the
142value from the signature parameters is used.
143
144In Mbed TLS, RSA-PSS parameters can be parsed and displayed for various
145objects (certificates, CRLs, CSRs). During parsing, the following properties
146are enforced:
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200147- the extra "trailer field" parameter must have its default value
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200148- the mask generation function is MGF1
149- encoding hash = message hashing algorithm (may differ from MGF1 hash)
150
151When it comes to cryptographic operations, only two things are supported:
152- verifying the signature on a certificate from its parent;
153- verifying the signature on a CRL from the issuing CA.
154
155The verification is done using `mbedtls_pk_verify_ext()`.
156
157Note: since X.509 parsing ensures that message hash = encoding hash, and
158`mbedtls_pk_verify_ext()` use encoding hash = mgf1 hash, it looks like all
159three hash algorithms must be equal, which would be good news as it would
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200160match a limitation of the PSA API.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200161
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200162It is unclear what parameters people use in practice. It looks like by default
163OpenSSL picks saltlen = keylen - hashlen - 2 (tested with openssl 1.1.1f).
164The `certool` command provided by GnuTLS seems to be picking saltlen = hashlen
Manuel Pégourié-Gonnard839bb8a2022-02-08 10:33:41 +0100165by default (tested with GnuTLS 3.6.13). FIPS 186-4 requires 0 <= saltlen <=
Manuel Pégourié-Gonnard8e559da2022-02-01 10:26:07 +0100166hashlen.
Manuel Pégourié-Gonnardf5ee4b32021-10-21 13:04:01 +0200167
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200168### Use in TLS
169
170In TLS 1.2 (or lower), RSA-PSS signatures are never used, except via X.509.
171
172In TLS 1.3, RSA-PSS signatures can be used directly in the protocol (in
173addition to indirect use via X.509). It has two sets of three signature
174algorithm identifiers (for SHA-256, SHA-384 and SHA-512), depending of what
175the OID of the public key is (rsaEncryption or RSASSA-PSS).
176
177In both cases, it specifies that:
178- the mask generation function is MGF1
179- all three hashes are equal
180- the length of the salt MUST be equal to the length of the digest algorithm
181
182When signing, the salt length picked by PSA is the one required by TLS 1.3
183(unless the key is unreasonably small).
184
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200185When verifying signatures, PSA will by default enforce the salt len is the one
186required by TLS 1.3.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200187
188### Current testing - X509
189
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200190All test files use the default trailer field of 0xBC, as enforced by our
191parser. (There's a negative test for that using the
192`x509_parse_rsassa_pss_params` test function and hex data.)
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200193
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200194Files with "bad" in the name are expected to be invalid and rejected in tests.
Manuel Pégourié-Gonnardf5ee4b32021-10-21 13:04:01 +0200195
196**Test certificates:**
197
198server9-bad-mgfhash.crt (announcing mgf1(sha224), signed with another mgf)
199 Hash Algorithm: sha256
200 Mask Algorithm: mgf1 with sha224
201 Salt Length: 0xDE
202server9-bad-saltlen.crt (announcing saltlen = 0xDE, signed with another len)
203 Hash Algorithm: sha256
204 Mask Algorithm: mgf1 with sha256
205 Salt Length: 0xDE
206server9-badsign.crt (one bit flipped in the signature)
207 Hash Algorithm: sha1 (default)
208 Mask Algorithm: mgf1 with sha1 (default)
209 Salt Length: 0xEA
210server9-defaults.crt
211 Hash Algorithm: sha1 (default)
212 Mask Algorithm: mgf1 with sha1 (default)
213 Salt Length: 0x14 (default)
214server9-sha224.crt
215 Hash Algorithm: sha224
216 Mask Algorithm: mgf1 with sha224
217 Salt Length: 0xE2
218server9-sha256.crt
219 Hash Algorithm: sha256
220 Mask Algorithm: mgf1 with sha256
221 Salt Length: 0xDE
222server9-sha384.crt
223 Hash Algorithm: sha384
224 Mask Algorithm: mgf1 with sha384
225 Salt Length: 0xCE
226server9-sha512.crt
227 Hash Algorithm: sha512
228 Mask Algorithm: mgf1 with sha512
229 Salt Length: 0xBE
230server9-with-ca.crt
231 Hash Algorithm: sha1 (default)
232 Mask Algorithm: mgf1 with sha1 (default)
233 Salt Length: 0xEA
234server9.crt
235 Hash Algorithm: sha1 (default)
236 Mask Algorithm: mgf1 with sha1 (default)
237 Salt Length: 0xEA
238
239These certificates are signed with a 2048-bit key. It appears that they are
240all using saltlen = keylen - hashlen - 2, except for server9-defaults which is
241using saltlen = hashlen.
242
243**Test CRLs:**
244
245crl-rsa-pss-sha1-badsign.pem
246 Hash Algorithm: sha1 (default)
247 Mask Algorithm: mgf1 with sha1 (default)
248 Salt Length: 0xEA
249crl-rsa-pss-sha1.pem
250 Hash Algorithm: sha1 (default)
251 Mask Algorithm: mgf1 with sha1 (default)
252 Salt Length: 0xEA
253crl-rsa-pss-sha224.pem
254 Hash Algorithm: sha224
255 Mask Algorithm: mgf1 with sha224
256 Salt Length: 0xE2
257crl-rsa-pss-sha256.pem
258 Hash Algorithm: sha256
259 Mask Algorithm: mgf1 with sha256
260 Salt Length: 0xDE
261crl-rsa-pss-sha384.pem
262 Hash Algorithm: sha384
263 Mask Algorithm: mgf1 with sha384
264 Salt Length: 0xCE
265crl-rsa-pss-sha512.pem
266 Hash Algorithm: sha512
267 Mask Algorithm: mgf1 with sha512
268 Salt Length: 0xBE
269
270These CRLs are signed with a 2048-bit key. It appears that they are
271all using saltlen = keylen - hashlen - 2.
272
273**Test CSRs:**
274
275server9.req.sha1
276 Hash Algorithm: sha1 (default)
277 Mask Algorithm: mgf1 with sha1 (default)
278 Salt Length: 0x6A
279server9.req.sha224
280 Hash Algorithm: sha224
281 Mask Algorithm: mgf1 with sha224
282 Salt Length: 0x62
283server9.req.sha256
284 Hash Algorithm: sha256
285 Mask Algorithm: mgf1 with sha256
286 Salt Length: 0x5E
287server9.req.sha384
288 Hash Algorithm: sha384
289 Mask Algorithm: mgf1 with sha384
290 Salt Length: 0x4E
291server9.req.sha512
292 Hash Algorithm: sha512
293 Mask Algorithm: mgf1 with sha512
294 Salt Length: 0x3E
295
296These CSRss are signed with a 2048-bit key. It appears that they are
297all using saltlen = keylen - hashlen - 2.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200298
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200299### Possible courses of action
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200300
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200301There's no question about what to do with TLS (any version); the only question
302is about X.509 signature verification. Options include:
303
3041. Doing all verifications with `PSA_ALG_RSA_PSS_ANY_SALT` - while this
305 wouldn't cause a concrete security issue, this would be non-compliant.
3062. Doing verifications with `PSA_ALG_RSA_PSS` when we're lucky and the encoded
307 saltlen happens to match hashlen, and falling back to `ANY_SALT` otherwise.
308Same issue as with the previous point, except more contained.
3093. Reject all certificates with saltlen != hashlen. This includes all
310 certificates generate with OpenSSL using the default parameters, so it's
311probably not acceptable.
3124. Request an extension to the PSA Crypto API and use one of the above options
313 in the meantime. Such an extension seems inconvenient and not motivated by
314strong security arguments, so it's unclear whether it would be accepted.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200315
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100316HKDF: Expand not exposed on its own (TLS 1.3)
317---------------------------------------------
318
319The HKDF function uses and Extract-then-Expand approch, that is:
320
321 HKDF(x, ...) = HKDF-Expand(HKDF-Extract(x, ...), ...)
322
323Only the full HKDF function is safe in general, however there are cases when
324one case safely use the individual Extract and Expand; the TLS 1.3 key
325schedule does so. Specifically, looking at the [hierarchy of secrets][13hs]
326is seems that Expand and Extract are always chained, so that this hierarchy
327can be implemented using only the full HKDF. However, looking at the
328derivation of traffic keys (7.3) and the update mechanism (7.2) it appears
329that calls to HKDF-Expand are iterated without any intermediated call to
330HKDF-Extract : that is, the traffic keys are computed as
331
332 HKDF-Expand(HKDF-Expand(HKDF-Extract(...)))
333
334(with possibly more than two Expands in a row with update).
335
336[13hs]: https://datatracker.ietf.org/doc/html/rfc8446#page-93
337
338In the short term (early 2022), we'll work around that by re-implementing HKDF
339in `ssl_tls13_keys.c` based on the `psa_mac_` APIs (for HMAC).
340
Manuel Pégourié-Gonnard8e559da2022-02-01 10:26:07 +0100341In the long term, it is desirable to extend the PSA API. See
342https://github.com/ARM-software/psa-crypto-api/issues/539
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100343
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200344Limitations relevant for G2 (isolation of long-term secrets)
345============================================================
346
347Custom key derivations for mixed-PSK handshake
348----------------------------------------------
349
350Currently, `MBEDTLS_USE_PSA_CRYPTO` enables the new configuration function
351`mbedtls_ssl_conf_psk_opaque()` which allows a PSA-held key to be used for the
352(pure) `PSK` key exchange in TLS 1.2. This requires that the derivation of the
353Master Secret (MS) be done on the PSA side. To support this, an algorithm
354family `PSA_ALG_TLS12_PSK_TO_MS(hash_alg)` was added to PSA Crypto.
355
356If we want to support key isolation for the "mixed PSK" key exchanges:
357DHE-PSK, RSA-PSK, ECDHE-PSK, where the PSK is concatenated with the result of
358a DH key agreement (resp. RSA decryption) to form the pre-master secret (PMS)
359from which the MS is derived. If the value of the PSK is to remain hidden, we
360need the derivation PSK + secondary secret -> MS to be implemented as an
361ad-hoc PSA key derivation algorithm.
362
363Adding this new, TLS-specific, key derivation algorithm to PSA Crypto should
364be no harder than it was to add `PSA_ALG_TLS12_PSK_TO_MS()` but still requires
365an extension to PSA Crypto.
366
367Note: looking at RFCs 4279 and 5489, it appears that the structure of the PMS
368is always the same: 2-byte length of the secondary secret, secondary secret,
3692-byte length of the PSK, PSK. So, a single key derivation algorithm should be
370able to cover the 3 key exchanges DHE-PSK, RSA-PSK and ECDHE-PSK. (That's a
371minor gain: adding 3 algorithms would not be a blocker anyway.)
372
373Note: if later we want to also isolate short-term secret (G3), the "secondary
374secret" (output of DHE/ECDHE key agreement or RSA decryption) could be a
375candidate. This wouldn't be a problem as the PSA key derivation API always
376allows inputs from key slots. (Tangent: the hard part in isolating the result
377of RSA decryption would be still checking that is has the correct format:
37848 bytes, the first two matching the TLS version - note that this is timing
379sensitive.)
380
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100381HKDF: Expand not exposed on its own (TLS 1.3)
382---------------------------------------------
383
Manuel Pégourié-Gonnard8ebed212022-02-07 10:23:49 +0100384See the section with the same name in the G1 part above for background.
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100385
386The work-around mentioned there works well enough just for acceleration, but
387is not sufficient for key isolation or generally proper key management (it
388requires marking keys are usable for HMAC while they should only be used for
389key derivation).
390
391The obvious long-term solution is to make HKDF-Expand available as a new KDF
392(in addition to the full HKDF) in PSA (with appropriate warnings in the
393documentation).