xref: /rk3399_rockchip-uboot/include/hash.h (revision 46fe2c04443f3d777791910da21649bb3ddf878f)
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 
201de7bb4fSMichael van der Westhuizen #ifndef USE_HOSTCC
21d20a40deSSimon Glass #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
22460408efSSimon Glass #define CONFIG_HASH_VERIFY
23460408efSSimon Glass #endif
24460408efSSimon Glass 
25460408efSSimon Glass struct hash_algo {
26460408efSSimon Glass 	const char *name;			/* Name of algorithm */
27460408efSSimon Glass 	int digest_size;			/* Length of digest */
28460408efSSimon Glass 	/**
29460408efSSimon Glass 	 * hash_func_ws: Generic hashing function
30460408efSSimon Glass 	 *
31460408efSSimon Glass 	 * This is the generic prototype for a hashing function. We only
32460408efSSimon Glass 	 * have the watchdog version at present.
33460408efSSimon Glass 	 *
34460408efSSimon Glass 	 * @input:	Input buffer
35460408efSSimon Glass 	 * @ilen:	Input buffer length
36460408efSSimon Glass 	 * @output:	Checksum result (length depends on algorithm)
37460408efSSimon Glass 	 * @chunk_sz:	Trigger watchdog after processing this many bytes
38460408efSSimon Glass 	 */
39460408efSSimon Glass 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
40460408efSSimon Glass 		unsigned char *output, unsigned int chunk_sz);
41460408efSSimon Glass 	int chunk_size;				/* Watchdog chunk size */
42bf007ebbSHung-ying Tyan 	/*
43bf007ebbSHung-ying Tyan 	 * hash_init: Create the context for progressive hashing
44bf007ebbSHung-ying Tyan 	 *
45bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
46bf007ebbSHung-ying Tyan 	 * @ctxp: Pointer to the pointer of the context for hashing
47bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
48bf007ebbSHung-ying Tyan 	 */
49bf007ebbSHung-ying Tyan 	int (*hash_init)(struct hash_algo *algo, void **ctxp);
50bf007ebbSHung-ying Tyan 	/*
51bf007ebbSHung-ying Tyan 	 * hash_update: Perform hashing on the given buffer
52bf007ebbSHung-ying Tyan 	 *
53bf007ebbSHung-ying Tyan 	 * The context is freed by this function if an error occurs.
54bf007ebbSHung-ying Tyan 	 *
55bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
56bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
57bf007ebbSHung-ying Tyan 	 * @buf: Pointer to the buffer being hashed
58bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer being hashed
59bf007ebbSHung-ying Tyan 	 * @is_last: 1 if this is the last update; 0 otherwise
60bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
61bf007ebbSHung-ying Tyan 	 */
62bf007ebbSHung-ying Tyan 	int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf,
63bf007ebbSHung-ying Tyan 			   unsigned int size, int is_last);
64bf007ebbSHung-ying Tyan 	/*
65bf007ebbSHung-ying Tyan 	 * hash_finish: Write the hash result to the given buffer
66bf007ebbSHung-ying Tyan 	 *
67bf007ebbSHung-ying Tyan 	 * The context is freed by this function.
68bf007ebbSHung-ying Tyan 	 *
69bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
70bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
71bf007ebbSHung-ying Tyan 	 * @dest_buf: Pointer to the buffer for the result
72bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer for the result
73bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -ENOSPC if size of the result buffer is too small
74bf007ebbSHung-ying Tyan 	 *   or -1 on other errors
75bf007ebbSHung-ying Tyan 	 */
76bf007ebbSHung-ying Tyan 	int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf,
77bf007ebbSHung-ying Tyan 			   int size);
78460408efSSimon Glass };
79460408efSSimon Glass 
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 /**
118bf007ebbSHung-ying Tyan  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
119bf007ebbSHung-ying Tyan  *
120bf007ebbSHung-ying Tyan  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
121bf007ebbSHung-ying Tyan  * algorithm is not available.
122bf007ebbSHung-ying Tyan  *
123bf007ebbSHung-ying Tyan  * @algo_name: Hash algorithm to look up
124bf007ebbSHung-ying Tyan  * @algop: Pointer to the hash_algo struct if found
125bf007ebbSHung-ying Tyan  *
126bf007ebbSHung-ying Tyan  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
127bf007ebbSHung-ying Tyan  */
128bf007ebbSHung-ying Tyan int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
12931890ae2SSimon Glass 
13031890ae2SSimon Glass /**
131*46fe2c04SRuchika Gupta  * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
132*46fe2c04SRuchika Gupta  *
133*46fe2c04SRuchika Gupta  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
134*46fe2c04SRuchika Gupta  * algorithm is not available with progressive hash support.
135*46fe2c04SRuchika Gupta  *
136*46fe2c04SRuchika Gupta  * @algo_name: Hash algorithm to look up
137*46fe2c04SRuchika Gupta  * @algop: Pointer to the hash_algo struct if found
138*46fe2c04SRuchika Gupta  *
139*46fe2c04SRuchika Gupta  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
140*46fe2c04SRuchika Gupta  */
141*46fe2c04SRuchika Gupta int hash_progressive_lookup_algo(const char *algo_name,
142*46fe2c04SRuchika Gupta 				 struct hash_algo **algop);
143*46fe2c04SRuchika Gupta 
144*46fe2c04SRuchika Gupta /**
14531890ae2SSimon Glass  * hash_show() - Print out a hash algorithm and value
14631890ae2SSimon Glass  *
14731890ae2SSimon Glass  * You will get a message like this (without a newline at the end):
14831890ae2SSimon Glass  *
14931890ae2SSimon Glass  * "sha1 for 9eb3337c ... 9eb3338f ==> 7942ef1df479fd3130f716eb9613d107dab7e257"
15031890ae2SSimon Glass  *
15131890ae2SSimon Glass  * @algo:		Algorithm used for hash
15231890ae2SSimon Glass  * @addr:		Address of data that was hashed
15331890ae2SSimon Glass  * @len:		Length of data that was hashed
15431890ae2SSimon Glass  * @output:		Hash value to display
15531890ae2SSimon Glass  */
15604819a4fSSimon Glass void hash_show(struct hash_algo *algo, ulong addr, ulong len,
15704819a4fSSimon Glass 	       uint8_t *output);
1581de7bb4fSMichael van der Westhuizen #endif /* !USE_HOSTCC */
159460408efSSimon Glass #endif
160