Lines Matching +full:a +full:- +full:c

1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
16 #include <keys/user-type.h>
17 #include <keys/asymmetric-type.h>
22 * ubifs_node_calc_hash - calculate the hash of a UBIFS node
23 * @c: UBIFS file-system description object
24 * @node: the node to calculate a hash for
27 * Returns 0 for success or a negative error code otherwise.
29 int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node, in __ubifs_node_calc_hash() argument
34 return crypto_shash_tfm_digest(c->hash_tfm, node, le32_to_cpu(ch->len), in __ubifs_node_calc_hash()
39 * ubifs_hash_calc_hmac - calculate a HMAC from a hash
40 * @c: UBIFS file-system description object
41 * @hash: the node to calculate a HMAC for
44 * Returns 0 for success or a negative error code otherwise.
46 static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash, in ubifs_hash_calc_hmac() argument
49 return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac); in ubifs_hash_calc_hmac()
53 * ubifs_prepare_auth_node - Prepare an authentication node
54 * @c: UBIFS file-system description object
55 * @node: the node to calculate a hash for
59 * It creates a HMAC from the given input hash and writes it to the node.
61 * Returns 0 for success or a negative error code otherwise.
63 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node, in ubifs_prepare_auth_node() argument
71 SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm); in ubifs_prepare_auth_node()
73 hash_desc->tfm = c->hash_tfm; in ubifs_prepare_auth_node()
74 ubifs_shash_copy_state(c, inhash, hash_desc); in ubifs_prepare_auth_node()
81 err = ubifs_hash_calc_hmac(c, hash, auth->hmac); in ubifs_prepare_auth_node()
85 auth->ch.node_type = UBIFS_AUTH_NODE; in ubifs_prepare_auth_node()
86 ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0); in ubifs_prepare_auth_node()
90 static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c, in ubifs_get_desc() argument
96 if (!ubifs_authenticated(c)) in ubifs_get_desc()
101 return ERR_PTR(-ENOMEM); in ubifs_get_desc()
103 desc->tfm = tfm; in ubifs_get_desc()
115 * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
116 * @c: UBIFS file-system description object
118 * This function returns a descriptor suitable for hashing a node. Free after use
121 struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c) in __ubifs_hash_get_desc() argument
123 return ubifs_get_desc(c, c->hash_tfm); in __ubifs_hash_get_desc()
127 * ubifs_bad_hash - Report hash mismatches
128 * @c: UBIFS file-system description object
134 * This function reports a hash mismatch when a node has a different hash than
137 void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash, in ubifs_bad_hash() argument
140 int len = min(c->hash_len, 20); in ubifs_bad_hash()
141 int cropped = len != c->hash_len; in ubifs_bad_hash()
146 __ubifs_node_calc_hash(c, node, calc); in ubifs_bad_hash()
148 ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs); in ubifs_bad_hash()
149 ubifs_err(c, "hash expected: %*ph%s", len, hash, cont); in ubifs_bad_hash()
150 ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont); in ubifs_bad_hash()
154 * __ubifs_node_check_hash - check the hash of a node against given hash
155 * @c: UBIFS file-system description object
159 * This function calculates a hash over a node and compares it to the given hash.
160 * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
163 int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node, in __ubifs_node_check_hash() argument
169 err = __ubifs_node_calc_hash(c, node, calc); in __ubifs_node_check_hash()
173 if (ubifs_check_hash(c, expected, calc)) in __ubifs_node_check_hash()
174 return -EPERM; in __ubifs_node_check_hash()
180 * ubifs_sb_verify_signature - verify the signature of a superblock
181 * @c: UBIFS file-system description object
184 * To support offline signed images the superblock can be signed with a
188 * Returns 0 when the signature can be successfully verified or a negative
191 int ubifs_sb_verify_signature(struct ubifs_info *c, in ubifs_sb_verify_signature() argument
199 sleb = ubifs_scan(c, UBIFS_SB_LNUM, UBIFS_SB_NODE_SZ, c->sbuf, 0); in ubifs_sb_verify_signature()
205 if (sleb->nodes_cnt == 0) { in ubifs_sb_verify_signature()
206 ubifs_err(c, "Unable to find signature node"); in ubifs_sb_verify_signature()
207 err = -EINVAL; in ubifs_sb_verify_signature()
211 snod = list_first_entry(&sleb->nodes, struct ubifs_scan_node, list); in ubifs_sb_verify_signature()
213 if (snod->type != UBIFS_SIG_NODE) { in ubifs_sb_verify_signature()
214 ubifs_err(c, "Signature node is of wrong type"); in ubifs_sb_verify_signature()
215 err = -EINVAL; in ubifs_sb_verify_signature()
219 signode = snod->node; in ubifs_sb_verify_signature()
221 if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) { in ubifs_sb_verify_signature()
222 ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len)); in ubifs_sb_verify_signature()
223 err = -EINVAL; in ubifs_sb_verify_signature()
227 if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) { in ubifs_sb_verify_signature()
228 ubifs_err(c, "Signature type %d is not supported\n", in ubifs_sb_verify_signature()
229 le32_to_cpu(signode->type)); in ubifs_sb_verify_signature()
230 err = -EINVAL; in ubifs_sb_verify_signature()
235 signode->sig, le32_to_cpu(signode->len), in ubifs_sb_verify_signature()
240 ubifs_err(c, "Failed to verify signature"); in ubifs_sb_verify_signature()
242 ubifs_msg(c, "Successfully verified super block signature"); in ubifs_sb_verify_signature()
251 * ubifs_init_authentication - initialize UBIFS authentication support
252 * @c: UBIFS file-system description object
254 * This function returns 0 for success or a negative error code otherwise.
256 int ubifs_init_authentication(struct ubifs_info *c) in ubifs_init_authentication() argument
263 if (!c->auth_hash_name) { in ubifs_init_authentication()
264 ubifs_err(c, "authentication hash name needed with authentication"); in ubifs_init_authentication()
265 return -EINVAL; in ubifs_init_authentication()
268 c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST, in ubifs_init_authentication()
269 c->auth_hash_name); in ubifs_init_authentication()
270 if ((int)c->auth_hash_algo < 0) { in ubifs_init_authentication()
271 ubifs_err(c, "Unknown hash algo %s specified", in ubifs_init_authentication()
272 c->auth_hash_name); in ubifs_init_authentication()
273 return -EINVAL; in ubifs_init_authentication()
277 c->auth_hash_name); in ubifs_init_authentication()
279 keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL); in ubifs_init_authentication()
282 ubifs_err(c, "Failed to request key: %ld", in ubifs_init_authentication()
287 down_read(&keyring_key->sem); in ubifs_init_authentication()
289 if (keyring_key->type != &key_type_logon) { in ubifs_init_authentication()
290 ubifs_err(c, "key type must be logon"); in ubifs_init_authentication()
291 err = -ENOKEY; in ubifs_init_authentication()
298 err = -EKEYREVOKED; in ubifs_init_authentication()
302 c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0); in ubifs_init_authentication()
303 if (IS_ERR(c->hash_tfm)) { in ubifs_init_authentication()
304 err = PTR_ERR(c->hash_tfm); in ubifs_init_authentication()
305 ubifs_err(c, "Can not allocate %s: %d", in ubifs_init_authentication()
306 c->auth_hash_name, err); in ubifs_init_authentication()
310 c->hash_len = crypto_shash_digestsize(c->hash_tfm); in ubifs_init_authentication()
311 if (c->hash_len > UBIFS_HASH_ARR_SZ) { in ubifs_init_authentication()
312 ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)", in ubifs_init_authentication()
313 c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ); in ubifs_init_authentication()
314 err = -EINVAL; in ubifs_init_authentication()
318 c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); in ubifs_init_authentication()
319 if (IS_ERR(c->hmac_tfm)) { in ubifs_init_authentication()
320 err = PTR_ERR(c->hmac_tfm); in ubifs_init_authentication()
321 ubifs_err(c, "Can not allocate %s: %d", hmac_name, err); in ubifs_init_authentication()
325 c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm); in ubifs_init_authentication()
326 if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) { in ubifs_init_authentication()
327 ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)", in ubifs_init_authentication()
328 hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ); in ubifs_init_authentication()
329 err = -EINVAL; in ubifs_init_authentication()
333 err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen); in ubifs_init_authentication()
337 c->authenticated = true; in ubifs_init_authentication()
339 c->log_hash = ubifs_hash_get_desc(c); in ubifs_init_authentication()
340 if (IS_ERR(c->log_hash)) { in ubifs_init_authentication()
341 err = PTR_ERR(c->log_hash); in ubifs_init_authentication()
349 crypto_free_shash(c->hmac_tfm); in ubifs_init_authentication()
352 crypto_free_shash(c->hash_tfm); in ubifs_init_authentication()
354 up_read(&keyring_key->sem); in ubifs_init_authentication()
361 * __ubifs_exit_authentication - release resource
362 * @c: UBIFS file-system description object
366 void __ubifs_exit_authentication(struct ubifs_info *c) in __ubifs_exit_authentication() argument
368 if (!ubifs_authenticated(c)) in __ubifs_exit_authentication()
371 crypto_free_shash(c->hmac_tfm); in __ubifs_exit_authentication()
372 crypto_free_shash(c->hash_tfm); in __ubifs_exit_authentication()
373 kfree(c->log_hash); in __ubifs_exit_authentication()
377 * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
378 * @c: UBIFS file-system description object
379 * @node: the node to insert a HMAC into.
384 * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
388 static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node, in ubifs_node_calc_hmac() argument
391 SHASH_DESC_ON_STACK(shash, c->hmac_tfm); in ubifs_node_calc_hmac()
392 int hmac_len = c->hmac_desc_len; in ubifs_node_calc_hmac()
395 ubifs_assert(c, ofs_hmac > 8); in ubifs_node_calc_hmac()
396 ubifs_assert(c, ofs_hmac + hmac_len < len); in ubifs_node_calc_hmac()
398 shash->tfm = c->hmac_tfm; in ubifs_node_calc_hmac()
405 err = crypto_shash_update(shash, node + 8, ofs_hmac - 8); in ubifs_node_calc_hmac()
410 if (len - ofs_hmac - hmac_len > 0) { in ubifs_node_calc_hmac()
412 len - ofs_hmac - hmac_len); in ubifs_node_calc_hmac()
421 * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
422 * @c: UBIFS file-system description object
423 * @node: the node to insert a HMAC into.
427 * This function inserts a HMAC at offset @ofs_hmac into the node given in
430 * This function returns 0 for success or a negative error code otherwise.
432 int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len, in __ubifs_node_insert_hmac() argument
435 return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac); in __ubifs_node_insert_hmac()
439 * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
440 * @c: UBIFS file-system description object
441 * @node: the node to insert a HMAC into.
446 * @node. Returns 0 if successful or a negative error code otherwise.
448 int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node, in __ubifs_node_verify_hmac() argument
451 int hmac_len = c->hmac_desc_len; in __ubifs_node_verify_hmac()
457 return -ENOMEM; in __ubifs_node_verify_hmac()
459 err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac); in __ubifs_node_verify_hmac()
472 return -EPERM; in __ubifs_node_verify_hmac()
475 int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src, in __ubifs_shash_copy_state() argument
481 state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS); in __ubifs_shash_copy_state()
483 return -ENOMEM; in __ubifs_shash_copy_state()
498 * ubifs_hmac_wkm - Create a HMAC of the well known message
499 * @c: UBIFS file-system description object
502 * This function creates a HMAC of a well known message. This is used
503 * to check if the provided key is suitable to authenticate a UBIFS
504 * image. This is only a convenience to the user to provide a better
507 * This function returns 0 for success or a negative error code otherwise.
509 int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac) in ubifs_hmac_wkm() argument
511 SHASH_DESC_ON_STACK(shash, c->hmac_tfm); in ubifs_hmac_wkm()
515 if (!ubifs_authenticated(c)) in ubifs_hmac_wkm()
518 shash->tfm = c->hmac_tfm; in ubifs_hmac_wkm()
525 sizeof(well_known_message) - 1); in ubifs_hmac_wkm()
536 * ubifs_hmac_zero - test if a HMAC is zero
537 * @c: UBIFS file-system description object
540 * This function tests if a HMAC is zero and returns true if it is
543 bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac) in ubifs_hmac_zero() argument
545 return !memchr_inv(hmac, 0, c->hmac_desc_len); in ubifs_hmac_zero()