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/verification.h>
13 #include <crypto/hash.h>
14 #include <crypto/utils.h>
15 #include <keys/user-type.h>
16 #include <keys/asymmetric-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
;
33 return crypto_shash_tfm_digest(c
->hash_tfm
, node
, le32_to_cpu(ch
->len
),
38 * ubifs_hash_calc_hmac - calculate a HMAC from a hash
39 * @c: UBIFS file-system description object
40 * @hash: the node to calculate a HMAC for
41 * @hmac: the returned HMAC
43 * Returns 0 for success or a negative error code otherwise.
45 static int ubifs_hash_calc_hmac(const struct ubifs_info
*c
, const u8
*hash
,
48 return crypto_shash_tfm_digest(c
->hmac_tfm
, hash
, c
->hash_len
, hmac
);
52 * ubifs_prepare_auth_node - Prepare an authentication node
53 * @c: UBIFS file-system description object
54 * @node: the node to calculate a hash for
55 * @inhash: input hash of previous nodes
57 * This function prepares an authentication node for writing onto flash.
58 * It creates a HMAC from the given input hash and writes it to the node.
60 * Returns 0 for success or a negative error code otherwise.
62 int ubifs_prepare_auth_node(struct ubifs_info
*c
, void *node
,
63 struct shash_desc
*inhash
)
65 struct ubifs_auth_node
*auth
= node
;
66 u8 hash
[UBIFS_HASH_ARR_SZ
];
70 SHASH_DESC_ON_STACK(hash_desc
, c
->hash_tfm
);
72 hash_desc
->tfm
= c
->hash_tfm
;
73 ubifs_shash_copy_state(c
, inhash
, hash_desc
);
75 err
= crypto_shash_final(hash_desc
, hash
);
80 err
= ubifs_hash_calc_hmac(c
, hash
, auth
->hmac
);
84 auth
->ch
.node_type
= UBIFS_AUTH_NODE
;
85 ubifs_prepare_node(c
, auth
, ubifs_auth_node_sz(c
), 0);
89 static struct shash_desc
*ubifs_get_desc(const struct ubifs_info
*c
,
90 struct crypto_shash
*tfm
)
92 struct shash_desc
*desc
;
95 if (!ubifs_authenticated(c
))
98 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(tfm
), GFP_KERNEL
);
100 return ERR_PTR(-ENOMEM
);
104 err
= crypto_shash_init(desc
);
114 * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
115 * @c: UBIFS file-system description object
117 * This function returns a descriptor suitable for hashing a node. Free after use
120 struct shash_desc
*__ubifs_hash_get_desc(const struct ubifs_info
*c
)
122 return ubifs_get_desc(c
, c
->hash_tfm
);
126 * ubifs_bad_hash - Report hash mismatches
127 * @c: UBIFS file-system description object
129 * @hash: the expected hash
130 * @lnum: the LEB @node was read from
131 * @offs: offset in LEB @node was read from
133 * This function reports a hash mismatch when a node has a different hash than
136 void ubifs_bad_hash(const struct ubifs_info
*c
, const void *node
, const u8
*hash
,
139 int len
= min(c
->hash_len
, 20);
140 int cropped
= len
!= c
->hash_len
;
141 const char *cont
= cropped
? "..." : "";
143 u8 calc
[UBIFS_HASH_ARR_SZ
];
145 __ubifs_node_calc_hash(c
, node
, calc
);
147 ubifs_err(c
, "hash mismatch on node at LEB %d:%d", lnum
, offs
);
148 ubifs_err(c
, "hash expected: %*ph%s", len
, hash
, cont
);
149 ubifs_err(c
, "hash calculated: %*ph%s", len
, calc
, cont
);
153 * __ubifs_node_check_hash - check the hash of a node against given hash
154 * @c: UBIFS file-system description object
156 * @expected: the expected hash
158 * This function calculates a hash over a node and compares it to the given hash.
159 * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
160 * negative error code is returned.
162 int __ubifs_node_check_hash(const struct ubifs_info
*c
, const void *node
,
165 u8 calc
[UBIFS_HASH_ARR_SZ
];
168 err
= __ubifs_node_calc_hash(c
, node
, calc
);
172 if (ubifs_check_hash(c
, expected
, calc
))
179 * ubifs_sb_verify_signature - verify the signature of a superblock
180 * @c: UBIFS file-system description object
181 * @sup: The superblock node
183 * To support offline signed images the superblock can be signed with a
184 * PKCS#7 signature. The signature is placed directly behind the superblock
185 * node in an ubifs_sig_node.
187 * Returns 0 when the signature can be successfully verified or a negative
190 int ubifs_sb_verify_signature(struct ubifs_info
*c
,
191 const struct ubifs_sb_node
*sup
)
194 struct ubifs_scan_leb
*sleb
;
195 struct ubifs_scan_node
*snod
;
196 const struct ubifs_sig_node
*signode
;
198 sleb
= ubifs_scan(c
, UBIFS_SB_LNUM
, UBIFS_SB_NODE_SZ
, c
->sbuf
, 0);
204 if (sleb
->nodes_cnt
== 0) {
205 ubifs_err(c
, "Unable to find signature node");
210 snod
= list_first_entry(&sleb
->nodes
, struct ubifs_scan_node
, list
);
212 if (snod
->type
!= UBIFS_SIG_NODE
) {
213 ubifs_err(c
, "Signature node is of wrong type");
218 signode
= snod
->node
;
220 if (le32_to_cpu(signode
->len
) > snod
->len
+ sizeof(struct ubifs_sig_node
)) {
221 ubifs_err(c
, "invalid signature len %d", le32_to_cpu(signode
->len
));
226 if (le32_to_cpu(signode
->type
) != UBIFS_SIGNATURE_TYPE_PKCS7
) {
227 ubifs_err(c
, "Signature type %d is not supported\n",
228 le32_to_cpu(signode
->type
));
233 err
= verify_pkcs7_signature(sup
, sizeof(struct ubifs_sb_node
),
234 signode
->sig
, le32_to_cpu(signode
->len
),
235 NULL
, VERIFYING_UNSPECIFIED_SIGNATURE
,
239 ubifs_err(c
, "Failed to verify signature");
241 ubifs_msg(c
, "Successfully verified super block signature");
244 ubifs_scan_destroy(sleb
);
250 * ubifs_init_authentication - initialize UBIFS authentication support
251 * @c: UBIFS file-system description object
253 * This function returns 0 for success or a negative error code otherwise.
255 int ubifs_init_authentication(struct ubifs_info
*c
)
257 struct key
*keyring_key
;
258 const struct user_key_payload
*ukp
;
260 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
262 if (!c
->auth_hash_name
) {
263 ubifs_err(c
, "authentication hash name needed with authentication");
267 c
->auth_hash_algo
= match_string(hash_algo_name
, HASH_ALGO__LAST
,
269 if ((int)c
->auth_hash_algo
< 0) {
270 ubifs_err(c
, "Unknown hash algo %s specified",
275 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
278 keyring_key
= request_key(&key_type_logon
, c
->auth_key_name
, NULL
);
280 if (IS_ERR(keyring_key
)) {
281 ubifs_err(c
, "Failed to request key: %ld",
282 PTR_ERR(keyring_key
));
283 return PTR_ERR(keyring_key
);
286 down_read(&keyring_key
->sem
);
288 if (keyring_key
->type
!= &key_type_logon
) {
289 ubifs_err(c
, "key type must be logon");
294 ukp
= user_key_payload_locked(keyring_key
);
296 /* key was revoked before we acquired its semaphore */
301 c
->hash_tfm
= crypto_alloc_shash(c
->auth_hash_name
, 0, 0);
302 if (IS_ERR(c
->hash_tfm
)) {
303 err
= PTR_ERR(c
->hash_tfm
);
304 ubifs_err(c
, "Can not allocate %s: %d",
305 c
->auth_hash_name
, err
);
309 c
->hash_len
= crypto_shash_digestsize(c
->hash_tfm
);
310 if (c
->hash_len
> UBIFS_HASH_ARR_SZ
) {
311 ubifs_err(c
, "hash %s is bigger than maximum allowed hash size (%d > %d)",
312 c
->auth_hash_name
, c
->hash_len
, UBIFS_HASH_ARR_SZ
);
317 c
->hmac_tfm
= crypto_alloc_shash(hmac_name
, 0, 0);
318 if (IS_ERR(c
->hmac_tfm
)) {
319 err
= PTR_ERR(c
->hmac_tfm
);
320 ubifs_err(c
, "Can not allocate %s: %d", hmac_name
, err
);
324 c
->hmac_desc_len
= crypto_shash_digestsize(c
->hmac_tfm
);
325 if (c
->hmac_desc_len
> UBIFS_HMAC_ARR_SZ
) {
326 ubifs_err(c
, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
327 hmac_name
, c
->hmac_desc_len
, UBIFS_HMAC_ARR_SZ
);
332 err
= crypto_shash_setkey(c
->hmac_tfm
, ukp
->data
, ukp
->datalen
);
336 c
->authenticated
= true;
338 c
->log_hash
= ubifs_hash_get_desc(c
);
339 if (IS_ERR(c
->log_hash
)) {
340 err
= PTR_ERR(c
->log_hash
);
348 crypto_free_shash(c
->hmac_tfm
);
351 crypto_free_shash(c
->hash_tfm
);
353 up_read(&keyring_key
->sem
);
354 key_put(keyring_key
);
360 * __ubifs_exit_authentication - release resource
361 * @c: UBIFS file-system description object
363 * This function releases the authentication related resources.
365 void __ubifs_exit_authentication(struct ubifs_info
*c
)
367 if (!ubifs_authenticated(c
))
370 crypto_free_shash(c
->hmac_tfm
);
371 crypto_free_shash(c
->hash_tfm
);
376 * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
377 * @c: UBIFS file-system description object
378 * @node: the node to insert a HMAC into.
379 * @len: the length of the node
380 * @ofs_hmac: the offset in the node where the HMAC is inserted
381 * @hmac: returned HMAC
383 * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
384 * embedded into the node, so this area is not covered by the HMAC. Also not
385 * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
387 static int ubifs_node_calc_hmac(const struct ubifs_info
*c
, const void *node
,
388 int len
, int ofs_hmac
, void *hmac
)
390 SHASH_DESC_ON_STACK(shash
, c
->hmac_tfm
);
391 int hmac_len
= c
->hmac_desc_len
;
394 ubifs_assert(c
, ofs_hmac
> 8);
395 ubifs_assert(c
, ofs_hmac
+ hmac_len
< len
);
397 shash
->tfm
= c
->hmac_tfm
;
399 err
= crypto_shash_init(shash
);
403 /* behind common node header CRC up to HMAC begin */
404 err
= crypto_shash_update(shash
, node
+ 8, ofs_hmac
- 8);
408 /* behind HMAC, if any */
409 if (len
- ofs_hmac
- hmac_len
> 0) {
410 err
= crypto_shash_update(shash
, node
+ ofs_hmac
+ hmac_len
,
411 len
- ofs_hmac
- hmac_len
);
416 return crypto_shash_final(shash
, hmac
);
420 * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
421 * @c: UBIFS file-system description object
422 * @node: the node to insert a HMAC into.
423 * @len: the length of the node
424 * @ofs_hmac: the offset in the node where the HMAC is inserted
426 * This function inserts a HMAC at offset @ofs_hmac into the node given in
429 * This function returns 0 for success or a negative error code otherwise.
431 int __ubifs_node_insert_hmac(const struct ubifs_info
*c
, void *node
, int len
,
434 return ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, node
+ ofs_hmac
);
438 * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
439 * @c: UBIFS file-system description object
440 * @node: the node to insert a HMAC into.
441 * @len: the length of the node
442 * @ofs_hmac: the offset in the node where the HMAC is inserted
444 * This function verifies the HMAC at offset @ofs_hmac of the node given in
445 * @node. Returns 0 if successful or a negative error code otherwise.
447 int __ubifs_node_verify_hmac(const struct ubifs_info
*c
, const void *node
,
448 int len
, int ofs_hmac
)
450 int hmac_len
= c
->hmac_desc_len
;
454 hmac
= kmalloc(hmac_len
, GFP_NOFS
);
458 err
= ubifs_node_calc_hmac(c
, node
, len
, ofs_hmac
, hmac
);
464 err
= crypto_memneq(hmac
, node
+ ofs_hmac
, hmac_len
);
474 int __ubifs_shash_copy_state(const struct ubifs_info
*c
, struct shash_desc
*src
,
475 struct shash_desc
*target
)
480 state
= kmalloc(crypto_shash_descsize(src
->tfm
), GFP_NOFS
);
484 err
= crypto_shash_export(src
, state
);
488 err
= crypto_shash_import(target
, state
);
497 * ubifs_hmac_wkm - Create a HMAC of the well known message
498 * @c: UBIFS file-system description object
499 * @hmac: The HMAC of the well known message
501 * This function creates a HMAC of a well known message. This is used
502 * to check if the provided key is suitable to authenticate a UBIFS
503 * image. This is only a convenience to the user to provide a better
504 * error message when the wrong key is provided.
506 * This function returns 0 for success or a negative error code otherwise.
508 int ubifs_hmac_wkm(struct ubifs_info
*c
, u8
*hmac
)
510 const char well_known_message
[] = "UBIFS";
512 if (!ubifs_authenticated(c
))
515 return crypto_shash_tfm_digest(c
->hmac_tfm
, well_known_message
,
516 sizeof(well_known_message
) - 1, hmac
);
520 * ubifs_hmac_zero - test if a HMAC is zero
521 * @c: UBIFS file-system description object
522 * @hmac: the HMAC to test
524 * This function tests if a HMAC is zero and returns true if it is
525 * and false otherwise.
527 bool ubifs_hmac_zero(struct ubifs_info
*c
, const u8
*hmac
)
529 return !memchr_inv(hmac
, 0, c
->hmac_desc_len
);