/** * __ubifs_node_calc_hash - calculate the hash of a UBIFS node * @c: UBIFS file-system description object * @node: the node to calculate a hash for * @hash: the returned hash * * Returns 0 for success or a negative error code otherwise.
*/ int __ubifs_node_calc_hash(conststruct ubifs_info *c, constvoid *node,
u8 *hash)
{ conststruct ubifs_ch *ch = node;
/** * ubifs_hash_calc_hmac - calculate a HMAC from a hash * @c: UBIFS file-system description object * @hash: the node to calculate a HMAC for * @hmac: the returned HMAC * * Returns 0 for success or a negative error code otherwise.
*/ staticint ubifs_hash_calc_hmac(conststruct ubifs_info *c, const u8 *hash,
u8 *hmac)
{ return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac);
}
/** * ubifs_prepare_auth_node - Prepare an authentication node * @c: UBIFS file-system description object * @node: the node to calculate a hash for * @inhash: input hash of previous nodes * * This function prepares an authentication node for writing onto flash. * It creates a HMAC from the given input hash and writes it to the node. * * Returns 0 for success or a negative error code otherwise.
*/ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node, struct shash_desc *inhash)
{ struct ubifs_auth_node *auth = node;
u8 hash[UBIFS_HASH_ARR_SZ]; int err;
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL); if (!desc) return ERR_PTR(-ENOMEM);
desc->tfm = tfm;
err = crypto_shash_init(desc); if (err) {
kfree(desc); return ERR_PTR(err);
}
return desc;
}
/** * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node * @c: UBIFS file-system description object * * This function returns a descriptor suitable for hashing a node. Free after use * with kfree.
*/ struct shash_desc *__ubifs_hash_get_desc(conststruct ubifs_info *c)
{ return ubifs_get_desc(c, c->hash_tfm);
}
/** * ubifs_bad_hash - Report hash mismatches * @c: UBIFS file-system description object * @node: the node * @hash: the expected hash * @lnum: the LEB @node was read from * @offs: offset in LEB @node was read from * * This function reports a hash mismatch when a node has a different hash than * expected.
*/ void ubifs_bad_hash(conststruct ubifs_info *c, constvoid *node, const u8 *hash, int lnum, int offs)
{ int len = min(c->hash_len, 20); int cropped = len != c->hash_len; constchar *cont = cropped ? "..." : "";
/** * __ubifs_node_check_hash - check the hash of a node against given hash * @c: UBIFS file-system description object * @node: the node * @expected: the expected hash * * This function calculates a hash over a node and compares it to the given hash. * Returns 0 if both hashes are equal or authentication is disabled, otherwise a * negative error code is returned.
*/ int __ubifs_node_check_hash(conststruct ubifs_info *c, constvoid *node, const u8 *expected)
{
u8 calc[UBIFS_HASH_ARR_SZ]; int err;
err = __ubifs_node_calc_hash(c, node, calc); if (err) return err;
if (ubifs_check_hash(c, expected, calc)) return -EPERM;
return 0;
}
/** * ubifs_sb_verify_signature - verify the signature of a superblock * @c: UBIFS file-system description object * @sup: The superblock node * * To support offline signed images the superblock can be signed with a * PKCS#7 signature. The signature is placed directly behind the superblock * node in an ubifs_sig_node. * * Returns 0 when the signature can be successfully verified or a negative * error code if not.
*/ int ubifs_sb_verify_signature(struct ubifs_info *c, conststruct ubifs_sb_node *sup)
{ int err; struct ubifs_scan_leb *sleb; struct ubifs_scan_node *snod; conststruct ubifs_sig_node *signode;
if (snod->type != UBIFS_SIG_NODE) {
ubifs_err(c, "Signature node is of wrong type");
err = -EINVAL; goto out_destroy;
}
signode = snod->node;
if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) {
ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len));
err = -EINVAL; goto out_destroy;
}
if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) {
ubifs_err(c, "Signature type %d is not supported\n",
le32_to_cpu(signode->type));
err = -EINVAL; goto out_destroy;
}
/** * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node * @c: UBIFS file-system description object * @node: the node to insert a HMAC into. * @len: the length of the node * @ofs_hmac: the offset in the node where the HMAC is inserted * @hmac: returned HMAC * * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be * embedded into the node, so this area is not covered by the HMAC. Also not * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
*/ staticint ubifs_node_calc_hmac(conststruct ubifs_info *c, constvoid *node, int len, int ofs_hmac, void *hmac)
{
SHASH_DESC_ON_STACK(shash, c->hmac_tfm); int hmac_len = c->hmac_desc_len; int err;
err = crypto_shash_init(shash); if (err) return err;
/* behind common node header CRC up to HMAC begin */
err = crypto_shash_update(shash, node + 8, ofs_hmac - 8); if (err < 0) return err;
/* behind HMAC, if any */ if (len - ofs_hmac - hmac_len > 0) {
err = crypto_shash_update(shash, node + ofs_hmac + hmac_len,
len - ofs_hmac - hmac_len); if (err < 0) return err;
}
return crypto_shash_final(shash, hmac);
}
/** * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node * @c: UBIFS file-system description object * @node: the node to insert a HMAC into. * @len: the length of the node * @ofs_hmac: the offset in the node where the HMAC is inserted * * This function inserts a HMAC at offset @ofs_hmac into the node given in * @node. * * This function returns 0 for success or a negative error code otherwise.
*/ int __ubifs_node_insert_hmac(conststruct ubifs_info *c, void *node, int len, int ofs_hmac)
{ return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac);
}
/** * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node * @c: UBIFS file-system description object * @node: the node to insert a HMAC into. * @len: the length of the node * @ofs_hmac: the offset in the node where the HMAC is inserted * * This function verifies the HMAC at offset @ofs_hmac of the node given in * @node. Returns 0 if successful or a negative error code otherwise.
*/ int __ubifs_node_verify_hmac(conststruct ubifs_info *c, constvoid *node, int len, int ofs_hmac)
{ int hmac_len = c->hmac_desc_len;
u8 *hmac; int err;
hmac = kmalloc(hmac_len, GFP_NOFS); if (!hmac) return -ENOMEM;
int __ubifs_shash_copy_state(conststruct ubifs_info *c, struct shash_desc *src, struct shash_desc *target)
{
u8 *state; int err;
state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS); if (!state) return -ENOMEM;
err = crypto_shash_export(src, state); if (err) goto out;
err = crypto_shash_import(target, state);
out:
kfree(state);
return err;
}
/** * ubifs_hmac_wkm - Create a HMAC of the well known message * @c: UBIFS file-system description object * @hmac: The HMAC of the well known message * * This function creates a HMAC of a well known message. This is used * to check if the provided key is suitable to authenticate a UBIFS * image. This is only a convenience to the user to provide a better * error message when the wrong key is provided. * * This function returns 0 for success or a negative error code otherwise.
*/ int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac)
{ constchar well_known_message[] = "UBIFS";
/* * ubifs_hmac_zero - test if a HMAC is zero * @c: UBIFS file-system description object * @hmac: the HMAC to test * * This function tests if a HMAC is zero and returns true if it is * and false otherwise.
*/ bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac)
{ return !memchr_inv(hmac, 0, c->hmac_desc_len);
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.