^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This file implements various helper functions for UBIFS authentication support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/verification.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <keys/asymmetric-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * ubifs_node_calc_hash - calculate the hash of a UBIFS node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @node: the node to calculate a hash for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @hash: the returned hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u8 *hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const struct ubifs_ch *ch = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return crypto_shash_tfm_digest(c->hash_tfm, node, le32_to_cpu(ch->len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * ubifs_hash_calc_hmac - calculate a HMAC from a hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @hash: the node to calculate a HMAC for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @hmac: the returned HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u8 *hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * ubifs_prepare_auth_node - Prepare an authentication node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @node: the node to calculate a hash for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @inhash: input hash of previous nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * This function prepares an authentication node for writing onto flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * It creates a HMAC from the given input hash and writes it to the node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct shash_desc *inhash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct ubifs_auth_node *auth = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 hash[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) hash_desc->tfm = c->hash_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ubifs_shash_copy_state(c, inhash, hash_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) err = crypto_shash_final(hash_desc, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) auth->ch.node_type = UBIFS_AUTH_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct crypto_shash *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct shash_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!ubifs_authenticated(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) desc->tfm = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * This function returns a descriptor suitable for hashing a node. Free after use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * with kfree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return ubifs_get_desc(c, c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * ubifs_bad_hash - Report hash mismatches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @node: the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @hash: the expected hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @lnum: the LEB @node was read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @offs: offset in LEB @node was read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * This function reports a hash mismatch when a node has a different hash than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int lnum, int offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int len = min(c->hash_len, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int cropped = len != c->hash_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const char *cont = cropped ? "..." : "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u8 calc[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __ubifs_node_calc_hash(c, node, calc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ubifs_err(c, "hash expected: %*ph%s", len, hash, cont);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * __ubifs_node_check_hash - check the hash of a node against given hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @node: the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @expected: the expected hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * This function calculates a hash over a node and compares it to the given hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * negative error code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) const u8 *expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u8 calc[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = __ubifs_node_calc_hash(c, node, calc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (ubifs_check_hash(c, expected, calc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * ubifs_sb_verify_signature - verify the signature of a superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @sup: The superblock node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * To support offline signed images the superblock can be signed with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * PKCS#7 signature. The signature is placed directly behind the superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * node in an ubifs_sig_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Returns 0 when the signature can be successfully verified or a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * error code if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ubifs_sb_verify_signature(struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const struct ubifs_sb_node *sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) const struct ubifs_sig_node *signode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) sleb = ubifs_scan(c, UBIFS_SB_LNUM, UBIFS_SB_NODE_SZ, c->sbuf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (IS_ERR(sleb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) err = PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (sleb->nodes_cnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ubifs_err(c, "Unable to find signature node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) snod = list_first_entry(&sleb->nodes, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (snod->type != UBIFS_SIG_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ubifs_err(c, "Signature node is of wrong type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) signode = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ubifs_err(c, "Signature type %d is not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) le32_to_cpu(signode->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) err = verify_pkcs7_signature(sup, sizeof(struct ubifs_sb_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) signode->sig, le32_to_cpu(signode->len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) NULL, VERIFYING_UNSPECIFIED_SIGNATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ubifs_err(c, "Failed to verify signature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ubifs_msg(c, "Successfully verified super block signature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) out_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * ubifs_init_authentication - initialize UBIFS authentication support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * This function returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int ubifs_init_authentication(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct key *keyring_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) const struct user_key_payload *ukp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) char hmac_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!c->auth_hash_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ubifs_err(c, "authentication hash name needed with authentication");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) c->auth_hash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if ((int)c->auth_hash_algo < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) ubifs_err(c, "Unknown hash algo %s specified",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) c->auth_hash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) c->auth_hash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (IS_ERR(keyring_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ubifs_err(c, "Failed to request key: %ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) PTR_ERR(keyring_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return PTR_ERR(keyring_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) down_read(&keyring_key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (keyring_key->type != &key_type_logon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ubifs_err(c, "key type must be logon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) err = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ukp = user_key_payload_locked(keyring_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!ukp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* key was revoked before we acquired its semaphore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) err = -EKEYREVOKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (IS_ERR(c->hash_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = PTR_ERR(c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ubifs_err(c, "Can not allocate %s: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) c->auth_hash_name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) c->hash_len = crypto_shash_digestsize(c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (c->hash_len > UBIFS_HASH_ARR_SZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto out_free_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (IS_ERR(c->hmac_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err = PTR_ERR(c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ubifs_err(c, "Can not allocate %s: %d", hmac_name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto out_free_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) goto out_free_hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) goto out_free_hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) c->authenticated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) c->log_hash = ubifs_hash_get_desc(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (IS_ERR(c->log_hash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = PTR_ERR(c->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto out_free_hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) out_free_hmac:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) crypto_free_shash(c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) out_free_hash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) crypto_free_shash(c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) up_read(&keyring_key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) key_put(keyring_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * __ubifs_exit_authentication - release resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * This function releases the authentication related resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void __ubifs_exit_authentication(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!ubifs_authenticated(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) crypto_free_shash(c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) crypto_free_shash(c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) kfree(c->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * @node: the node to insert a HMAC into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * @len: the length of the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * @ofs_hmac: the offset in the node where the HMAC is inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * @hmac: returned HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * embedded into the node, so this area is not covered by the HMAC. Also not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int len, int ofs_hmac, void *hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) int hmac_len = c->hmac_desc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ubifs_assert(c, ofs_hmac > 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) ubifs_assert(c, ofs_hmac + hmac_len < len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) shash->tfm = c->hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) err = crypto_shash_init(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* behind common node header CRC up to HMAC begin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) err = crypto_shash_update(shash, node + 8, ofs_hmac - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* behind HMAC, if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (len - ofs_hmac - hmac_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) err = crypto_shash_update(shash, node + ofs_hmac + hmac_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) len - ofs_hmac - hmac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return crypto_shash_final(shash, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @node: the node to insert a HMAC into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @len: the length of the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @ofs_hmac: the offset in the node where the HMAC is inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * This function inserts a HMAC at offset @ofs_hmac into the node given in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * @node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * This function returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int ofs_hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * @node: the node to insert a HMAC into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * @len: the length of the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * @ofs_hmac: the offset in the node where the HMAC is inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * This function verifies the HMAC at offset @ofs_hmac of the node given in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * @node. Returns 0 if successful or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) int len, int ofs_hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) int hmac_len = c->hmac_desc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) u8 *hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) hmac = kmalloc(hmac_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (!hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) kfree(hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) err = crypto_memneq(hmac, node + ofs_hmac, hmac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) kfree(hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct shash_desc *target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) u8 *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) err = crypto_shash_export(src, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) err = crypto_shash_import(target, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) kfree(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * ubifs_hmac_wkm - Create a HMAC of the well known message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * @hmac: The HMAC of the well known message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * This function creates a HMAC of a well known message. This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * to check if the provided key is suitable to authenticate a UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * image. This is only a convenience to the user to provide a better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * error message when the wrong key is provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * This function returns 0 for success or a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) const char well_known_message[] = "UBIFS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!ubifs_authenticated(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) shash->tfm = c->hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = crypto_shash_init(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) err = crypto_shash_update(shash, well_known_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) sizeof(well_known_message) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) err = crypto_shash_final(shash, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * ubifs_hmac_zero - test if a HMAC is zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * @hmac: the HMAC to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * This function tests if a HMAC is zero and returns true if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * and false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return !memchr_inv(hmac, 0, c->hmac_desc_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }