]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/ubifs/auth.c
1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of UBIFS.
5 * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
9 * This file implements various helper functions for UBIFS authentication support
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>
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
26 * Returns 0 for success or a negative error code otherwise.
28 int __ubifs_node_calc_hash(const struct ubifs_info
*c
, const void *node
,
31 const struct ubifs_ch
*ch
= node
;
32 SHASH_DESC_ON_STACK(shash
, c
->hash_tfm
);
35 shash
->tfm
= c
->hash_tfm
;
37 err
= crypto_shash_digest(shash
, node
, le32_to_cpu(ch
->len
), hash
);
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
49 * Returns 0 for success or a negative error code otherwise.
51 static int ubifs_hash_calc_hmac(const struct ubifs_info
*c
, const u8
*hash
,
54 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
57 shash
->tfm
= c
->hmac_tfm
;
59 err
= crypto_shash_digest(shash
, hash
, c
->hash_len
, hmac
);
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
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.
74 * Returns 0 for success or a negative error code otherwise.
76 int ubifs_prepare_auth_node(struct ubifs_info
*c
, void *node
,
77 struct shash_desc
*inhash
)
79 SHASH_DESC_ON_STACK(hash_desc
, c
->hash_tfm
);
80 struct ubifs_auth_node
*auth
= node
;
84 hash
= kmalloc(crypto_shash_descsize(c
->hash_tfm
), GFP_NOFS
);
88 hash_desc
->tfm
= c
->hash_tfm
;
89 ubifs_shash_copy_state(c
, inhash
, hash_desc
);
91 err
= crypto_shash_final(hash_desc
, hash
);
95 err
= ubifs_hash_calc_hmac(c
, hash
, auth
->hmac
);
99 auth
->ch
.node_type
= UBIFS_AUTH_NODE
;
100 ubifs_prepare_node(c
, auth
, ubifs_auth_node_sz(c
), 0);
109 static struct shash_desc
*ubifs_get_desc(const struct ubifs_info
*c
,
110 struct crypto_shash
*tfm
)
112 struct shash_desc
*desc
;
115 if (!ubifs_authenticated(c
))
118 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(tfm
), GFP_KERNEL
);
120 return ERR_PTR(-ENOMEM
);
124 err
= crypto_shash_init(desc
);
134 * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
135 * @c: UBIFS file-system description object
137 * This function returns a descriptor suitable for hashing a node. Free after use
140 struct shash_desc
*__ubifs_hash_get_desc(const struct ubifs_info
*c
)
142 return ubifs_get_desc(c
, c
->hash_tfm
);
146 * __ubifs_shash_final - finalize shash
147 * @c: UBIFS file-system description object
148 * @desc: the descriptor
149 * @out: the output hash
151 * Simple wrapper around crypto_shash_final(), safe to be called with
152 * disabled authentication.
154 int __ubifs_shash_final(const struct ubifs_info
*c
, struct shash_desc
*desc
,
157 if (ubifs_authenticated(c
))
158 return crypto_shash_final(desc
, out
);
164 * ubifs_bad_hash - Report hash mismatches
165 * @c: UBIFS file-system description object
167 * @hash: the expected hash
168 * @lnum: the LEB @node was read from
169 * @offs: offset in LEB @node was read from
171 * This function reports a hash mismatch when a node has a different hash than
174 void ubifs_bad_hash(const struct ubifs_info
*c
, const void *node
, const u8
*hash
,
177 int len
= min(c
->hash_len
, 20);
178 int cropped
= len
!= c
->hash_len
;
179 const char *cont
= cropped
? "..." : "";
181 u8 calc
[UBIFS_HASH_ARR_SZ
];
183 __ubifs_node_calc_hash(c
, node
, calc
);
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
);
191 * __ubifs_node_check_hash - check the hash of a node against given hash
192 * @c: UBIFS file-system description object
194 * @expected: the expected hash
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.
200 int __ubifs_node_check_hash(const struct ubifs_info
*c
, const void *node
,
203 u8 calc
[UBIFS_HASH_ARR_SZ
];
206 err
= __ubifs_node_calc_hash(c
, node
, calc
);
210 if (ubifs_check_hash(c
, expected
, calc
))
217 * ubifs_init_authentication - initialize UBIFS authentication support
218 * @c: UBIFS file-system description object
220 * This function returns 0 for success or a negative error code otherwise.
222 int ubifs_init_authentication(struct ubifs_info
*c
)
224 struct key
*keyring_key
;
225 const struct user_key_payload
*ukp
;
227 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
229 if (!c
->auth_hash_name
) {
230 ubifs_err(c
, "authentication hash name needed with authentication");
234 c
->auth_hash_algo
= match_string(hash_algo_name
, HASH_ALGO__LAST
,
236 if ((int)c
->auth_hash_algo
< 0) {
237 ubifs_err(c
, "Unknown hash algo %s specified",
242 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
245 keyring_key
= request_key(&key_type_logon
, c
->auth_key_name
, NULL
);
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
);
253 down_read(&keyring_key
->sem
);
255 if (keyring_key
->type
!= &key_type_logon
) {
256 ubifs_err(c
, "key type must be logon");
261 ukp
= user_key_payload_locked(keyring_key
);
263 /* key was revoked before we acquired its semaphore */
268 c
->hash_tfm
= crypto_alloc_shash(c
->auth_hash_name
, 0, 0);
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
);
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
);
284 c
->hmac_tfm
= crypto_alloc_shash(hmac_name
, 0, 0);
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
);
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
);
299 err
= crypto_shash_setkey(c
->hmac_tfm
, ukp
->data
, ukp
->datalen
);
303 c
->authenticated
= true;
305 c
->log_hash
= ubifs_hash_get_desc(c
);
306 if (IS_ERR(c
->log_hash
))
313 crypto_free_shash(c
->hmac_tfm
);
316 crypto_free_shash(c
->hash_tfm
);
318 up_read(&keyring_key
->sem
);
319 key_put(keyring_key
);
325 * __ubifs_exit_authentication - release resource
326 * @c: UBIFS file-system description object
328 * This function releases the authentication related resources.
330 void __ubifs_exit_authentication(struct ubifs_info
*c
)
332 if (!ubifs_authenticated(c
))
335 crypto_free_shash(c
->hmac_tfm
);
336 crypto_free_shash(c
->hash_tfm
);
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
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.
352 static int ubifs_node_calc_hmac(const struct ubifs_info
*c
, const void *node
,
353 int len
, int ofs_hmac
, void *hmac
)
355 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
356 int hmac_len
= c
->hmac_desc_len
;
359 ubifs_assert(c
, ofs_hmac
> 8);
360 ubifs_assert(c
, ofs_hmac
+ hmac_len
< len
);
362 shash
->tfm
= c
->hmac_tfm
;
364 err
= crypto_shash_init(shash
);
368 /* behind common node header CRC up to HMAC begin */
369 err
= crypto_shash_update(shash
, node
+ 8, ofs_hmac
- 8);
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
);
381 return crypto_shash_final(shash
, hmac
);
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
391 * This function inserts a HMAC at offset @ofs_hmac into the node given in
394 * This function returns 0 for success or a negative error code otherwise.
396 int __ubifs_node_insert_hmac(const struct ubifs_info
*c
, void *node
, int len
,
399 return ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, node
+ ofs_hmac
);
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
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.
412 int __ubifs_node_verify_hmac(const struct ubifs_info
*c
, const void *node
,
413 int len
, int ofs_hmac
)
415 int hmac_len
= c
->hmac_desc_len
;
419 hmac
= kmalloc(hmac_len
, GFP_NOFS
);
423 err
= ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, hmac
);
427 err
= crypto_memneq(hmac
, node
+ ofs_hmac
, hmac_len
);
437 int __ubifs_shash_copy_state(const struct ubifs_info
*c
, struct shash_desc
*src
,
438 struct shash_desc
*target
)
443 state
= kmalloc(crypto_shash_descsize(src
->tfm
), GFP_NOFS
);
447 err
= crypto_shash_export(src
, state
);
451 err
= crypto_shash_import(target
, state
);
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
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.
469 * This function returns 0 for success or a negative error code otherwise.
471 int ubifs_hmac_wkm(struct ubifs_info
*c
, u8
*hmac
)
473 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
475 const char well_known_message
[] = "UBIFS";
477 if (!ubifs_authenticated(c
))
480 shash
->tfm
= c
->hmac_tfm
;
482 err
= crypto_shash_init(shash
);
486 err
= crypto_shash_update(shash
, well_known_message
,
487 sizeof(well_known_message
) - 1);
491 err
= crypto_shash_final(shash
, hmac
);