]>
Commit | Line | Data |
---|---|---|
49525e5e SH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * This file is part of UBIFS. | |
4 | * | |
5 | * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> | |
6 | */ | |
7 | ||
8 | /* | |
9 | * This file implements various helper functions for UBIFS authentication support | |
10 | */ | |
11 | ||
12 | #include <linux/crypto.h> | |
13 | #include <crypto/hash.h> | |
14 | #include <crypto/sha.h> | |
15 | #include <crypto/algapi.h> | |
16 | #include <keys/user-type.h> | |
17 | ||
18 | #include "ubifs.h" | |
19 | ||
20 | /** | |
21 | * ubifs_node_calc_hash - calculate the hash of a UBIFS node | |
22 | * @c: UBIFS file-system description object | |
23 | * @node: the node to calculate a hash for | |
24 | * @hash: the returned hash | |
25 | * | |
26 | * Returns 0 for success or a negative error code otherwise. | |
27 | */ | |
28 | int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node, | |
29 | u8 *hash) | |
30 | { | |
31 | const struct ubifs_ch *ch = node; | |
32 | SHASH_DESC_ON_STACK(shash, c->hash_tfm); | |
33 | int err; | |
34 | ||
35 | shash->tfm = c->hash_tfm; | |
49525e5e SH |
36 | |
37 | err = crypto_shash_digest(shash, node, le32_to_cpu(ch->len), hash); | |
38 | if (err < 0) | |
39 | return err; | |
40 | return 0; | |
41 | } | |
42 | ||
43 | /** | |
44 | * ubifs_hash_calc_hmac - calculate a HMAC from a hash | |
45 | * @c: UBIFS file-system description object | |
46 | * @hash: the node to calculate a HMAC for | |
47 | * @hmac: the returned HMAC | |
48 | * | |
49 | * Returns 0 for success or a negative error code otherwise. | |
50 | */ | |
51 | static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash, | |
52 | u8 *hmac) | |
53 | { | |
54 | SHASH_DESC_ON_STACK(shash, c->hmac_tfm); | |
55 | int err; | |
56 | ||
57 | shash->tfm = c->hmac_tfm; | |
49525e5e SH |
58 | |
59 | err = crypto_shash_digest(shash, hash, c->hash_len, hmac); | |
60 | if (err < 0) | |
61 | return err; | |
62 | return 0; | |
63 | } | |
64 | ||
65 | /** | |
66 | * ubifs_prepare_auth_node - Prepare an authentication node | |
67 | * @c: UBIFS file-system description object | |
68 | * @node: the node to calculate a hash for | |
69 | * @hash: input hash of previous nodes | |
70 | * | |
71 | * This function prepares an authentication node for writing onto flash. | |
72 | * It creates a HMAC from the given input hash and writes it to the node. | |
73 | * | |
74 | * Returns 0 for success or a negative error code otherwise. | |
75 | */ | |
76 | int ubifs_prepare_auth_node(struct ubifs_info *c, void *node, | |
77 | struct shash_desc *inhash) | |
78 | { | |
79 | SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm); | |
80 | struct ubifs_auth_node *auth = node; | |
81 | u8 *hash; | |
82 | int err; | |
83 | ||
84 | hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS); | |
85 | if (!hash) | |
86 | return -ENOMEM; | |
87 | ||
88 | hash_desc->tfm = c->hash_tfm; | |
49525e5e SH |
89 | ubifs_shash_copy_state(c, inhash, hash_desc); |
90 | ||
91 | err = crypto_shash_final(hash_desc, hash); | |
92 | if (err) | |
93 | goto out; | |
94 | ||
95 | err = ubifs_hash_calc_hmac(c, hash, auth->hmac); | |
96 | if (err) | |
97 | goto out; | |
98 | ||
99 | auth->ch.node_type = UBIFS_AUTH_NODE; | |
100 | ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0); | |
101 | ||
102 | err = 0; | |
103 | out: | |
104 | kfree(hash); | |
105 | ||
106 | return err; | |
107 | } | |
108 | ||
109 | static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c, | |
110 | struct crypto_shash *tfm) | |
111 | { | |
112 | struct shash_desc *desc; | |
113 | int err; | |
114 | ||
115 | if (!ubifs_authenticated(c)) | |
116 | return NULL; | |
117 | ||
118 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL); | |
119 | if (!desc) | |
120 | return ERR_PTR(-ENOMEM); | |
121 | ||
122 | desc->tfm = tfm; | |
49525e5e SH |
123 | |
124 | err = crypto_shash_init(desc); | |
125 | if (err) { | |
126 | kfree(desc); | |
127 | return ERR_PTR(err); | |
128 | } | |
129 | ||
130 | return desc; | |
131 | } | |
132 | ||
133 | /** | |
134 | * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node | |
135 | * @c: UBIFS file-system description object | |
136 | * | |
137 | * This function returns a descriptor suitable for hashing a node. Free after use | |
138 | * with kfree. | |
139 | */ | |
140 | struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c) | |
141 | { | |
142 | return ubifs_get_desc(c, c->hash_tfm); | |
143 | } | |
144 | ||
145 | /** | |
146 | * __ubifs_shash_final - finalize shash | |
147 | * @c: UBIFS file-system description object | |
148 | * @desc: the descriptor | |
149 | * @out: the output hash | |
150 | * | |
151 | * Simple wrapper around crypto_shash_final(), safe to be called with | |
152 | * disabled authentication. | |
153 | */ | |
154 | int __ubifs_shash_final(const struct ubifs_info *c, struct shash_desc *desc, | |
155 | u8 *out) | |
156 | { | |
157 | if (ubifs_authenticated(c)) | |
158 | return crypto_shash_final(desc, out); | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | /** | |
164 | * ubifs_bad_hash - Report hash mismatches | |
165 | * @c: UBIFS file-system description object | |
166 | * @node: the node | |
167 | * @hash: the expected hash | |
168 | * @lnum: the LEB @node was read from | |
169 | * @offs: offset in LEB @node was read from | |
170 | * | |
171 | * This function reports a hash mismatch when a node has a different hash than | |
172 | * expected. | |
173 | */ | |
174 | void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash, | |
175 | int lnum, int offs) | |
176 | { | |
177 | int len = min(c->hash_len, 20); | |
178 | int cropped = len != c->hash_len; | |
179 | const char *cont = cropped ? "..." : ""; | |
180 | ||
181 | u8 calc[UBIFS_HASH_ARR_SZ]; | |
182 | ||
183 | __ubifs_node_calc_hash(c, node, calc); | |
184 | ||
185 | ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs); | |
186 | ubifs_err(c, "hash expected: %*ph%s", len, hash, cont); | |
187 | ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont); | |
188 | } | |
189 | ||
190 | /** | |
191 | * __ubifs_node_check_hash - check the hash of a node against given hash | |
192 | * @c: UBIFS file-system description object | |
193 | * @node: the node | |
194 | * @expected: the expected hash | |
195 | * | |
196 | * This function calculates a hash over a node and compares it to the given hash. | |
197 | * Returns 0 if both hashes are equal or authentication is disabled, otherwise a | |
198 | * negative error code is returned. | |
199 | */ | |
200 | int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node, | |
201 | const u8 *expected) | |
202 | { | |
203 | u8 calc[UBIFS_HASH_ARR_SZ]; | |
204 | int err; | |
205 | ||
206 | err = __ubifs_node_calc_hash(c, node, calc); | |
207 | if (err) | |
208 | return err; | |
209 | ||
210 | if (ubifs_check_hash(c, expected, calc)) | |
211 | return -EPERM; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | /** | |
217 | * ubifs_init_authentication - initialize UBIFS authentication support | |
218 | * @c: UBIFS file-system description object | |
219 | * | |
220 | * This function returns 0 for success or a negative error code otherwise. | |
221 | */ | |
222 | int ubifs_init_authentication(struct ubifs_info *c) | |
223 | { | |
224 | struct key *keyring_key; | |
225 | const struct user_key_payload *ukp; | |
226 | int err; | |
227 | char hmac_name[CRYPTO_MAX_ALG_NAME]; | |
228 | ||
229 | if (!c->auth_hash_name) { | |
230 | ubifs_err(c, "authentication hash name needed with authentication"); | |
231 | return -EINVAL; | |
232 | } | |
233 | ||
234 | c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST, | |
235 | c->auth_hash_name); | |
236 | if ((int)c->auth_hash_algo < 0) { | |
237 | ubifs_err(c, "Unknown hash algo %s specified", | |
238 | c->auth_hash_name); | |
239 | return -EINVAL; | |
240 | } | |
241 | ||
242 | snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", | |
243 | c->auth_hash_name); | |
244 | ||
245 | keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL); | |
246 | ||
247 | if (IS_ERR(keyring_key)) { | |
248 | ubifs_err(c, "Failed to request key: %ld", | |
249 | PTR_ERR(keyring_key)); | |
250 | return PTR_ERR(keyring_key); | |
251 | } | |
252 | ||
253 | down_read(&keyring_key->sem); | |
254 | ||
255 | if (keyring_key->type != &key_type_logon) { | |
256 | ubifs_err(c, "key type must be logon"); | |
257 | err = -ENOKEY; | |
258 | goto out; | |
259 | } | |
260 | ||
261 | ukp = user_key_payload_locked(keyring_key); | |
262 | if (!ukp) { | |
263 | /* key was revoked before we acquired its semaphore */ | |
264 | err = -EKEYREVOKED; | |
265 | goto out; | |
266 | } | |
267 | ||
3d234b33 | 268 | c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0); |
49525e5e SH |
269 | if (IS_ERR(c->hash_tfm)) { |
270 | err = PTR_ERR(c->hash_tfm); | |
271 | ubifs_err(c, "Can not allocate %s: %d", | |
272 | c->auth_hash_name, err); | |
273 | goto out; | |
274 | } | |
275 | ||
276 | c->hash_len = crypto_shash_digestsize(c->hash_tfm); | |
277 | if (c->hash_len > UBIFS_HASH_ARR_SZ) { | |
278 | ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)", | |
279 | c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ); | |
280 | err = -EINVAL; | |
281 | goto out_free_hash; | |
282 | } | |
283 | ||
3d234b33 | 284 | c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); |
49525e5e SH |
285 | if (IS_ERR(c->hmac_tfm)) { |
286 | err = PTR_ERR(c->hmac_tfm); | |
287 | ubifs_err(c, "Can not allocate %s: %d", hmac_name, err); | |
288 | goto out_free_hash; | |
289 | } | |
290 | ||
291 | c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm); | |
292 | if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) { | |
293 | ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)", | |
294 | hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ); | |
295 | err = -EINVAL; | |
296 | goto out_free_hash; | |
297 | } | |
298 | ||
299 | err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen); | |
300 | if (err) | |
301 | goto out_free_hmac; | |
302 | ||
303 | c->authenticated = true; | |
304 | ||
305 | c->log_hash = ubifs_hash_get_desc(c); | |
306 | if (IS_ERR(c->log_hash)) | |
307 | goto out_free_hmac; | |
308 | ||
309 | err = 0; | |
310 | ||
311 | out_free_hmac: | |
312 | if (err) | |
313 | crypto_free_shash(c->hmac_tfm); | |
314 | out_free_hash: | |
315 | if (err) | |
316 | crypto_free_shash(c->hash_tfm); | |
317 | out: | |
318 | up_read(&keyring_key->sem); | |
319 | key_put(keyring_key); | |
320 | ||
321 | return err; | |
322 | } | |
323 | ||
324 | /** | |
325 | * __ubifs_exit_authentication - release resource | |
326 | * @c: UBIFS file-system description object | |
327 | * | |
328 | * This function releases the authentication related resources. | |
329 | */ | |
330 | void __ubifs_exit_authentication(struct ubifs_info *c) | |
331 | { | |
332 | if (!ubifs_authenticated(c)) | |
333 | return; | |
334 | ||
335 | crypto_free_shash(c->hmac_tfm); | |
336 | crypto_free_shash(c->hash_tfm); | |
337 | kfree(c->log_hash); | |
338 | } | |
339 | ||
340 | /** | |
341 | * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node | |
342 | * @c: UBIFS file-system description object | |
343 | * @node: the node to insert a HMAC into. | |
344 | * @len: the length of the node | |
345 | * @ofs_hmac: the offset in the node where the HMAC is inserted | |
346 | * @hmac: returned HMAC | |
347 | * | |
348 | * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be | |
349 | * embedded into the node, so this area is not covered by the HMAC. Also not | |
350 | * covered is the UBIFS_NODE_MAGIC and the CRC of the node. | |
351 | */ | |
352 | static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node, | |
353 | int len, int ofs_hmac, void *hmac) | |
354 | { | |
355 | SHASH_DESC_ON_STACK(shash, c->hmac_tfm); | |
356 | int hmac_len = c->hmac_desc_len; | |
357 | int err; | |
358 | ||
359 | ubifs_assert(c, ofs_hmac > 8); | |
360 | ubifs_assert(c, ofs_hmac + hmac_len < len); | |
361 | ||
362 | shash->tfm = c->hmac_tfm; | |
49525e5e SH |
363 | |
364 | err = crypto_shash_init(shash); | |
365 | if (err) | |
366 | return err; | |
367 | ||
368 | /* behind common node header CRC up to HMAC begin */ | |
369 | err = crypto_shash_update(shash, node + 8, ofs_hmac - 8); | |
370 | if (err < 0) | |
371 | return err; | |
372 | ||
373 | /* behind HMAC, if any */ | |
374 | if (len - ofs_hmac - hmac_len > 0) { | |
375 | err = crypto_shash_update(shash, node + ofs_hmac + hmac_len, | |
376 | len - ofs_hmac - hmac_len); | |
377 | if (err < 0) | |
378 | return err; | |
379 | } | |
380 | ||
381 | return crypto_shash_final(shash, hmac); | |
382 | } | |
383 | ||
384 | /** | |
385 | * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node | |
386 | * @c: UBIFS file-system description object | |
387 | * @node: the node to insert a HMAC into. | |
388 | * @len: the length of the node | |
389 | * @ofs_hmac: the offset in the node where the HMAC is inserted | |
390 | * | |
391 | * This function inserts a HMAC at offset @ofs_hmac into the node given in | |
392 | * @node. | |
393 | * | |
394 | * This function returns 0 for success or a negative error code otherwise. | |
395 | */ | |
396 | int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len, | |
397 | int ofs_hmac) | |
398 | { | |
399 | return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac); | |
400 | } | |
401 | ||
402 | /** | |
403 | * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node | |
404 | * @c: UBIFS file-system description object | |
405 | * @node: the node to insert a HMAC into. | |
406 | * @len: the length of the node | |
407 | * @ofs_hmac: the offset in the node where the HMAC is inserted | |
408 | * | |
409 | * This function verifies the HMAC at offset @ofs_hmac of the node given in | |
410 | * @node. Returns 0 if successful or a negative error code otherwise. | |
411 | */ | |
412 | int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node, | |
413 | int len, int ofs_hmac) | |
414 | { | |
415 | int hmac_len = c->hmac_desc_len; | |
416 | u8 *hmac; | |
417 | int err; | |
418 | ||
419 | hmac = kmalloc(hmac_len, GFP_NOFS); | |
420 | if (!hmac) | |
421 | return -ENOMEM; | |
422 | ||
423 | err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac); | |
424 | if (err) | |
425 | return err; | |
426 | ||
427 | err = crypto_memneq(hmac, node + ofs_hmac, hmac_len); | |
428 | ||
429 | kfree(hmac); | |
430 | ||
431 | if (!err) | |
432 | return 0; | |
433 | ||
434 | return -EPERM; | |
435 | } | |
436 | ||
437 | int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src, | |
438 | struct shash_desc *target) | |
439 | { | |
440 | u8 *state; | |
441 | int err; | |
442 | ||
443 | state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS); | |
444 | if (!state) | |
445 | return -ENOMEM; | |
446 | ||
447 | err = crypto_shash_export(src, state); | |
448 | if (err) | |
449 | goto out; | |
450 | ||
451 | err = crypto_shash_import(target, state); | |
452 | ||
453 | out: | |
454 | kfree(state); | |
455 | ||
456 | return err; | |
457 | } | |
458 | ||
459 | /** | |
460 | * ubifs_hmac_wkm - Create a HMAC of the well known message | |
461 | * @c: UBIFS file-system description object | |
462 | * @hmac: The HMAC of the well known message | |
463 | * | |
464 | * This function creates a HMAC of a well known message. This is used | |
465 | * to check if the provided key is suitable to authenticate a UBIFS | |
466 | * image. This is only a convenience to the user to provide a better | |
467 | * error message when the wrong key is provided. | |
468 | * | |
469 | * This function returns 0 for success or a negative error code otherwise. | |
470 | */ | |
471 | int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac) | |
472 | { | |
473 | SHASH_DESC_ON_STACK(shash, c->hmac_tfm); | |
474 | int err; | |
475 | const char well_known_message[] = "UBIFS"; | |
476 | ||
477 | if (!ubifs_authenticated(c)) | |
478 | return 0; | |
479 | ||
480 | shash->tfm = c->hmac_tfm; | |
49525e5e SH |
481 | |
482 | err = crypto_shash_init(shash); | |
483 | if (err) | |
484 | return err; | |
485 | ||
486 | err = crypto_shash_update(shash, well_known_message, | |
487 | sizeof(well_known_message) - 1); | |
488 | if (err < 0) | |
489 | return err; | |
490 | ||
491 | err = crypto_shash_final(shash, hmac); | |
492 | if (err) | |
493 | return err; | |
494 | return 0; | |
495 | } |