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