xref: /rk3399_rockchip-uboot/include/hash.h (revision 8f0b1e24e2887713bdcbf35b08902e9555ee7b92)
1460408efSSimon Glass /*
2460408efSSimon Glass  * Copyright (c) 2012 The Chromium OS Authors.
31a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
4460408efSSimon Glass  */
5460408efSSimon Glass 
6460408efSSimon Glass #ifndef _HASH_H
7460408efSSimon Glass #define _HASH_H
8460408efSSimon Glass 
91de7bb4fSMichael van der Westhuizen /*
101de7bb4fSMichael van der Westhuizen  * Maximum digest size for all algorithms we support. Having this value
111de7bb4fSMichael van der Westhuizen  * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
121de7bb4fSMichael van der Westhuizen  */
131de7bb4fSMichael van der Westhuizen #define HASH_MAX_DIGEST_SIZE	32
141de7bb4fSMichael van der Westhuizen 
151de7bb4fSMichael van der Westhuizen enum {
161de7bb4fSMichael van der Westhuizen 	HASH_FLAG_VERIFY	= 1 << 0,	/* Enable verify mode */
171de7bb4fSMichael van der Westhuizen 	HASH_FLAG_ENV		= 1 << 1,	/* Allow env vars */
181de7bb4fSMichael van der Westhuizen };
191de7bb4fSMichael van der Westhuizen 
20d20a40deSSimon Glass #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
21460408efSSimon Glass #define CONFIG_HASH_VERIFY
22460408efSSimon Glass #endif
23460408efSSimon Glass 
24460408efSSimon Glass struct hash_algo {
25460408efSSimon Glass 	const char *name;			/* Name of algorithm */
26460408efSSimon Glass 	int digest_size;			/* Length of digest */
27460408efSSimon Glass 	/**
28460408efSSimon Glass 	 * hash_func_ws: Generic hashing function
29460408efSSimon Glass 	 *
30460408efSSimon Glass 	 * This is the generic prototype for a hashing function. We only
31460408efSSimon Glass 	 * have the watchdog version at present.
32460408efSSimon Glass 	 *
33460408efSSimon Glass 	 * @input:	Input buffer
34460408efSSimon Glass 	 * @ilen:	Input buffer length
35460408efSSimon Glass 	 * @output:	Checksum result (length depends on algorithm)
36460408efSSimon Glass 	 * @chunk_sz:	Trigger watchdog after processing this many bytes
37460408efSSimon Glass 	 */
38460408efSSimon Glass 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
39460408efSSimon Glass 		unsigned char *output, unsigned int chunk_sz);
40460408efSSimon Glass 	int chunk_size;				/* Watchdog chunk size */
41bf007ebbSHung-ying Tyan 	/*
42bf007ebbSHung-ying Tyan 	 * hash_init: Create the context for progressive hashing
43bf007ebbSHung-ying Tyan 	 *
44bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
45bf007ebbSHung-ying Tyan 	 * @ctxp: Pointer to the pointer of the context for hashing
46bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
47bf007ebbSHung-ying Tyan 	 */
48bf007ebbSHung-ying Tyan 	int (*hash_init)(struct hash_algo *algo, void **ctxp);
49bf007ebbSHung-ying Tyan 	/*
50bf007ebbSHung-ying Tyan 	 * hash_update: Perform hashing on the given buffer
51bf007ebbSHung-ying Tyan 	 *
52bf007ebbSHung-ying Tyan 	 * The context is freed by this function if an error occurs.
53bf007ebbSHung-ying Tyan 	 *
54bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
55bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
56bf007ebbSHung-ying Tyan 	 * @buf: Pointer to the buffer being hashed
57bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer being hashed
58bf007ebbSHung-ying Tyan 	 * @is_last: 1 if this is the last update; 0 otherwise
59bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
60bf007ebbSHung-ying Tyan 	 */
61bf007ebbSHung-ying Tyan 	int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf,
62bf007ebbSHung-ying Tyan 			   unsigned int size, int is_last);
63bf007ebbSHung-ying Tyan 	/*
64bf007ebbSHung-ying Tyan 	 * hash_finish: Write the hash result to the given buffer
65bf007ebbSHung-ying Tyan 	 *
66bf007ebbSHung-ying Tyan 	 * The context is freed by this function.
67bf007ebbSHung-ying Tyan 	 *
68bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
69bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
70bf007ebbSHung-ying Tyan 	 * @dest_buf: Pointer to the buffer for the result
71bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer for the result
72bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -ENOSPC if size of the result buffer is too small
73bf007ebbSHung-ying Tyan 	 *   or -1 on other errors
74bf007ebbSHung-ying Tyan 	 */
75bf007ebbSHung-ying Tyan 	int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf,
76bf007ebbSHung-ying Tyan 			   int size);
77460408efSSimon Glass };
78460408efSSimon Glass 
792dd90027SRuchika Gupta #ifndef USE_HOSTCC
80460408efSSimon Glass /**
81460408efSSimon Glass  * hash_command: Process a hash command for a particular algorithm
82460408efSSimon Glass  *
83460408efSSimon Glass  * This common function is used to implement specific hash commands.
84460408efSSimon Glass  *
85218da0f3SSimon Glass  * @algo_name:		Hash algorithm being used (lower case!)
86d5b76673SSimon Glass  * @flags:		Flags value (HASH_FLAG_...)
87460408efSSimon Glass  * @cmdtp:		Pointer to command table entry
88460408efSSimon Glass  * @flag:		Some flags normally 0 (see CMD_FLAG_.. above)
89460408efSSimon Glass  * @argc:		Number of arguments (arg 0 must be the command text)
90460408efSSimon Glass  * @argv:		Arguments
91460408efSSimon Glass  */
92d5b76673SSimon Glass int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
93460408efSSimon Glass 		 int argc, char * const argv[]);
94460408efSSimon Glass 
956f907b42SSimon Glass /**
966f907b42SSimon Glass  * hash_block() - Hash a block according to the requested algorithm
976f907b42SSimon Glass  *
986f907b42SSimon Glass  * The caller probably knows the hash length for the chosen algorithm, but
996f907b42SSimon Glass  * in order to provide a general interface, and output_size parameter is
1006f907b42SSimon Glass  * provided.
1016f907b42SSimon Glass  *
1026f907b42SSimon Glass  * @algo_name:		Hash algorithm to use
1036f907b42SSimon Glass  * @data:		Data to hash
1046f907b42SSimon Glass  * @len:		Lengh of data to hash in bytes
1056f907b42SSimon Glass  * @output:		Place to put hash value
1066f907b42SSimon Glass  * @output_size:	On entry, pointer to the number of bytes available in
1076f907b42SSimon Glass  *			output. On exit, pointer to the number of bytes used.
1086f907b42SSimon Glass  *			If NULL, then it is assumed that the caller has
1096f907b42SSimon Glass  *			allocated enough space for the hash. This is possible
1106f907b42SSimon Glass  *			since the caller is selecting the algorithm.
1116f907b42SSimon Glass  * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
1126f907b42SSimon Glass  * -ENOSPC if the output buffer is not large enough.
1136f907b42SSimon Glass  */
1146f907b42SSimon Glass int hash_block(const char *algo_name, const void *data, unsigned int len,
1156f907b42SSimon Glass 	       uint8_t *output, int *output_size);
1166f907b42SSimon Glass 
117bf007ebbSHung-ying Tyan /**
1182dd90027SRuchika Gupta  * hash_show() - Print out a hash algorithm and value
1192dd90027SRuchika Gupta  *
1202dd90027SRuchika Gupta  * You will get a message like this (without a newline at the end):
1212dd90027SRuchika Gupta  *
1222dd90027SRuchika Gupta  * "sha1 for 9eb3337c ... 9eb3338f ==> 7942ef1df479fd3130f716eb9613d107dab7e257"
1232dd90027SRuchika Gupta  *
1242dd90027SRuchika Gupta  * @algo:		Algorithm used for hash
1252dd90027SRuchika Gupta  * @addr:		Address of data that was hashed
1262dd90027SRuchika Gupta  * @len:		Length of data that was hashed
1272dd90027SRuchika Gupta  * @output:		Hash value to display
1282dd90027SRuchika Gupta  */
1292dd90027SRuchika Gupta void hash_show(struct hash_algo *algo, ulong addr, ulong len,
1302dd90027SRuchika Gupta 	       uint8_t *output);
1312dd90027SRuchika Gupta 
1322dd90027SRuchika Gupta #endif /* !USE_HOSTCC */
1332dd90027SRuchika Gupta 
1342dd90027SRuchika Gupta /**
135bf007ebbSHung-ying Tyan  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
136bf007ebbSHung-ying Tyan  *
137bf007ebbSHung-ying Tyan  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
138bf007ebbSHung-ying Tyan  * algorithm is not available.
139bf007ebbSHung-ying Tyan  *
140bf007ebbSHung-ying Tyan  * @algo_name: Hash algorithm to look up
141bf007ebbSHung-ying Tyan  * @algop: Pointer to the hash_algo struct if found
142bf007ebbSHung-ying Tyan  *
143bf007ebbSHung-ying Tyan  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
144bf007ebbSHung-ying Tyan  */
145bf007ebbSHung-ying Tyan int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
14631890ae2SSimon Glass 
14731890ae2SSimon Glass /**
14846fe2c04SRuchika Gupta  * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
14946fe2c04SRuchika Gupta  *
15046fe2c04SRuchika Gupta  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
15146fe2c04SRuchika Gupta  * algorithm is not available with progressive hash support.
15246fe2c04SRuchika Gupta  *
15346fe2c04SRuchika Gupta  * @algo_name: Hash algorithm to look up
15446fe2c04SRuchika Gupta  * @algop: Pointer to the hash_algo struct if found
15546fe2c04SRuchika Gupta  *
15646fe2c04SRuchika Gupta  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
15746fe2c04SRuchika Gupta  */
15846fe2c04SRuchika Gupta int hash_progressive_lookup_algo(const char *algo_name,
15946fe2c04SRuchika Gupta 				 struct hash_algo **algop);
16046fe2c04SRuchika Gupta 
161*8f0b1e24SStefan Roese /**
162*8f0b1e24SStefan Roese  * hash_parse_string() - Parse hash string into a binary array
163*8f0b1e24SStefan Roese  *
164*8f0b1e24SStefan Roese  * The function parses a hash string into a binary array that
165*8f0b1e24SStefan Roese  * can for example easily be used to compare to hash values.
166*8f0b1e24SStefan Roese  *
167*8f0b1e24SStefan Roese  * @algo_name: Hash algorithm to look up
168*8f0b1e24SStefan Roese  * @str: Hash string to get parsed
169*8f0b1e24SStefan Roese  * @result: Binary array of the parsed hash string
170*8f0b1e24SStefan Roese  *
171*8f0b1e24SStefan Roese  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
172*8f0b1e24SStefan Roese  */
173*8f0b1e24SStefan Roese int hash_parse_string(const char *algo_name, const char *str, uint8_t *result);
174*8f0b1e24SStefan Roese 
175460408efSSimon Glass #endif
176