]>
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 struct ubifs_auth_node
*auth
= node
;
83 hash
= kmalloc(crypto_shash_descsize(c
->hash_tfm
), GFP_NOFS
);
88 SHASH_DESC_ON_STACK(hash_desc
, c
->hash_tfm
);
90 hash_desc
->tfm
= c
->hash_tfm
;
91 ubifs_shash_copy_state(c
, inhash
, hash_desc
);
93 err
= crypto_shash_final(hash_desc
, hash
);
98 err
= ubifs_hash_calc_hmac(c
, hash
, auth
->hmac
);
102 auth
->ch
.node_type
= UBIFS_AUTH_NODE
;
103 ubifs_prepare_node(c
, auth
, ubifs_auth_node_sz(c
), 0);
112 static struct shash_desc
*ubifs_get_desc(const struct ubifs_info
*c
,
113 struct crypto_shash
*tfm
)
115 struct shash_desc
*desc
;
118 if (!ubifs_authenticated(c
))
121 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(tfm
), GFP_KERNEL
);
123 return ERR_PTR(-ENOMEM
);
127 err
= crypto_shash_init(desc
);
137 * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
138 * @c: UBIFS file-system description object
140 * This function returns a descriptor suitable for hashing a node. Free after use
143 struct shash_desc
*__ubifs_hash_get_desc(const struct ubifs_info
*c
)
145 return ubifs_get_desc(c
, c
->hash_tfm
);
149 * ubifs_bad_hash - Report hash mismatches
150 * @c: UBIFS file-system description object
152 * @hash: the expected hash
153 * @lnum: the LEB @node was read from
154 * @offs: offset in LEB @node was read from
156 * This function reports a hash mismatch when a node has a different hash than
159 void ubifs_bad_hash(const struct ubifs_info
*c
, const void *node
, const u8
*hash
,
162 int len
= min(c
->hash_len
, 20);
163 int cropped
= len
!= c
->hash_len
;
164 const char *cont
= cropped
? "..." : "";
166 u8 calc
[UBIFS_HASH_ARR_SZ
];
168 __ubifs_node_calc_hash(c
, node
, calc
);
170 ubifs_err(c
, "hash mismatch on node at LEB %d:%d", lnum
, offs
);
171 ubifs_err(c
, "hash expected: %*ph%s", len
, hash
, cont
);
172 ubifs_err(c
, "hash calculated: %*ph%s", len
, calc
, cont
);
176 * __ubifs_node_check_hash - check the hash of a node against given hash
177 * @c: UBIFS file-system description object
179 * @expected: the expected hash
181 * This function calculates a hash over a node and compares it to the given hash.
182 * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
183 * negative error code is returned.
185 int __ubifs_node_check_hash(const struct ubifs_info
*c
, const void *node
,
188 u8 calc
[UBIFS_HASH_ARR_SZ
];
191 err
= __ubifs_node_calc_hash(c
, node
, calc
);
195 if (ubifs_check_hash(c
, expected
, calc
))
202 * ubifs_init_authentication - initialize UBIFS authentication support
203 * @c: UBIFS file-system description object
205 * This function returns 0 for success or a negative error code otherwise.
207 int ubifs_init_authentication(struct ubifs_info
*c
)
209 struct key
*keyring_key
;
210 const struct user_key_payload
*ukp
;
212 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
214 if (!c
->auth_hash_name
) {
215 ubifs_err(c
, "authentication hash name needed with authentication");
219 c
->auth_hash_algo
= match_string(hash_algo_name
, HASH_ALGO__LAST
,
221 if ((int)c
->auth_hash_algo
< 0) {
222 ubifs_err(c
, "Unknown hash algo %s specified",
227 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
230 keyring_key
= request_key(&key_type_logon
, c
->auth_key_name
, NULL
);
232 if (IS_ERR(keyring_key
)) {
233 ubifs_err(c
, "Failed to request key: %ld",
234 PTR_ERR(keyring_key
));
235 return PTR_ERR(keyring_key
);
238 down_read(&keyring_key
->sem
);
240 if (keyring_key
->type
!= &key_type_logon
) {
241 ubifs_err(c
, "key type must be logon");
246 ukp
= user_key_payload_locked(keyring_key
);
248 /* key was revoked before we acquired its semaphore */
253 c
->hash_tfm
= crypto_alloc_shash(c
->auth_hash_name
, 0, 0);
254 if (IS_ERR(c
->hash_tfm
)) {
255 err
= PTR_ERR(c
->hash_tfm
);
256 ubifs_err(c
, "Can not allocate %s: %d",
257 c
->auth_hash_name
, err
);
261 c
->hash_len
= crypto_shash_digestsize(c
->hash_tfm
);
262 if (c
->hash_len
> UBIFS_HASH_ARR_SZ
) {
263 ubifs_err(c
, "hash %s is bigger than maximum allowed hash size (%d > %d)",
264 c
->auth_hash_name
, c
->hash_len
, UBIFS_HASH_ARR_SZ
);
269 c
->hmac_tfm
= crypto_alloc_shash(hmac_name
, 0, 0);
270 if (IS_ERR(c
->hmac_tfm
)) {
271 err
= PTR_ERR(c
->hmac_tfm
);
272 ubifs_err(c
, "Can not allocate %s: %d", hmac_name
, err
);
276 c
->hmac_desc_len
= crypto_shash_digestsize(c
->hmac_tfm
);
277 if (c
->hmac_desc_len
> UBIFS_HMAC_ARR_SZ
) {
278 ubifs_err(c
, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
279 hmac_name
, c
->hmac_desc_len
, UBIFS_HMAC_ARR_SZ
);
284 err
= crypto_shash_setkey(c
->hmac_tfm
, ukp
->data
, ukp
->datalen
);
288 c
->authenticated
= true;
290 c
->log_hash
= ubifs_hash_get_desc(c
);
291 if (IS_ERR(c
->log_hash
))
298 crypto_free_shash(c
->hmac_tfm
);
301 crypto_free_shash(c
->hash_tfm
);
303 up_read(&keyring_key
->sem
);
304 key_put(keyring_key
);
310 * __ubifs_exit_authentication - release resource
311 * @c: UBIFS file-system description object
313 * This function releases the authentication related resources.
315 void __ubifs_exit_authentication(struct ubifs_info
*c
)
317 if (!ubifs_authenticated(c
))
320 crypto_free_shash(c
->hmac_tfm
);
321 crypto_free_shash(c
->hash_tfm
);
326 * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
327 * @c: UBIFS file-system description object
328 * @node: the node to insert a HMAC into.
329 * @len: the length of the node
330 * @ofs_hmac: the offset in the node where the HMAC is inserted
331 * @hmac: returned HMAC
333 * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
334 * embedded into the node, so this area is not covered by the HMAC. Also not
335 * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
337 static int ubifs_node_calc_hmac(const struct ubifs_info
*c
, const void *node
,
338 int len
, int ofs_hmac
, void *hmac
)
340 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
341 int hmac_len
= c
->hmac_desc_len
;
344 ubifs_assert(c
, ofs_hmac
> 8);
345 ubifs_assert(c
, ofs_hmac
+ hmac_len
< len
);
347 shash
->tfm
= c
->hmac_tfm
;
349 err
= crypto_shash_init(shash
);
353 /* behind common node header CRC up to HMAC begin */
354 err
= crypto_shash_update(shash
, node
+ 8, ofs_hmac
- 8);
358 /* behind HMAC, if any */
359 if (len
- ofs_hmac
- hmac_len
> 0) {
360 err
= crypto_shash_update(shash
, node
+ ofs_hmac
+ hmac_len
,
361 len
- ofs_hmac
- hmac_len
);
366 return crypto_shash_final(shash
, hmac
);
370 * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
371 * @c: UBIFS file-system description object
372 * @node: the node to insert a HMAC into.
373 * @len: the length of the node
374 * @ofs_hmac: the offset in the node where the HMAC is inserted
376 * This function inserts a HMAC at offset @ofs_hmac into the node given in
379 * This function returns 0 for success or a negative error code otherwise.
381 int __ubifs_node_insert_hmac(const struct ubifs_info
*c
, void *node
, int len
,
384 return ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, node
+ ofs_hmac
);
388 * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
389 * @c: UBIFS file-system description object
390 * @node: the node to insert a HMAC into.
391 * @len: the length of the node
392 * @ofs_hmac: the offset in the node where the HMAC is inserted
394 * This function verifies the HMAC at offset @ofs_hmac of the node given in
395 * @node. Returns 0 if successful or a negative error code otherwise.
397 int __ubifs_node_verify_hmac(const struct ubifs_info
*c
, const void *node
,
398 int len
, int ofs_hmac
)
400 int hmac_len
= c
->hmac_desc_len
;
404 hmac
= kmalloc(hmac_len
, GFP_NOFS
);
408 err
= ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, hmac
);
412 err
= crypto_memneq(hmac
, node
+ ofs_hmac
, hmac_len
);
422 int __ubifs_shash_copy_state(const struct ubifs_info
*c
, struct shash_desc
*src
,
423 struct shash_desc
*target
)
428 state
= kmalloc(crypto_shash_descsize(src
->tfm
), GFP_NOFS
);
432 err
= crypto_shash_export(src
, state
);
436 err
= crypto_shash_import(target
, state
);
445 * ubifs_hmac_wkm - Create a HMAC of the well known message
446 * @c: UBIFS file-system description object
447 * @hmac: The HMAC of the well known message
449 * This function creates a HMAC of a well known message. This is used
450 * to check if the provided key is suitable to authenticate a UBIFS
451 * image. This is only a convenience to the user to provide a better
452 * error message when the wrong key is provided.
454 * This function returns 0 for success or a negative error code otherwise.
456 int ubifs_hmac_wkm(struct ubifs_info
*c
, u8
*hmac
)
458 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
460 const char well_known_message
[] = "UBIFS";
462 if (!ubifs_authenticated(c
))
465 shash
->tfm
= c
->hmac_tfm
;
467 err
= crypto_shash_init(shash
);
471 err
= crypto_shash_update(shash
, well_known_message
,
472 sizeof(well_known_message
) - 1);
476 err
= crypto_shash_final(shash
, hmac
);