xref: /openbmc/u-boot/include/hash.h (revision e9221d03)
183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2460408efSSimon Glass /*
3460408efSSimon Glass  * Copyright (c) 2012 The Chromium OS Authors.
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  */
13*e9221d03SReuben Dowle #if defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
14*e9221d03SReuben Dowle #define HASH_MAX_DIGEST_SIZE	64
15*e9221d03SReuben Dowle #else
161de7bb4fSMichael van der Westhuizen #define HASH_MAX_DIGEST_SIZE	32
17*e9221d03SReuben Dowle #endif
181de7bb4fSMichael van der Westhuizen 
191de7bb4fSMichael van der Westhuizen enum {
201de7bb4fSMichael van der Westhuizen 	HASH_FLAG_VERIFY	= 1 << 0,	/* Enable verify mode */
211de7bb4fSMichael van der Westhuizen 	HASH_FLAG_ENV		= 1 << 1,	/* Allow env vars */
221de7bb4fSMichael van der Westhuizen };
231de7bb4fSMichael van der Westhuizen 
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 
1172dd90027SRuchika Gupta #endif /* !USE_HOSTCC */
1182dd90027SRuchika Gupta 
1192dd90027SRuchika Gupta /**
120bf007ebbSHung-ying Tyan  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
121bf007ebbSHung-ying Tyan  *
122bf007ebbSHung-ying Tyan  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
123bf007ebbSHung-ying Tyan  * algorithm is not available.
124bf007ebbSHung-ying Tyan  *
125bf007ebbSHung-ying Tyan  * @algo_name: Hash algorithm to look up
126bf007ebbSHung-ying Tyan  * @algop: Pointer to the hash_algo struct if found
127bf007ebbSHung-ying Tyan  *
128bf007ebbSHung-ying Tyan  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
129bf007ebbSHung-ying Tyan  */
130bf007ebbSHung-ying Tyan int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
13131890ae2SSimon Glass 
13231890ae2SSimon Glass /**
13346fe2c04SRuchika Gupta  * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
13446fe2c04SRuchika Gupta  *
13546fe2c04SRuchika Gupta  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
13646fe2c04SRuchika Gupta  * algorithm is not available with progressive hash support.
13746fe2c04SRuchika Gupta  *
13846fe2c04SRuchika Gupta  * @algo_name: Hash algorithm to look up
13946fe2c04SRuchika Gupta  * @algop: Pointer to the hash_algo struct if found
14046fe2c04SRuchika Gupta  *
14146fe2c04SRuchika Gupta  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
14246fe2c04SRuchika Gupta  */
14346fe2c04SRuchika Gupta int hash_progressive_lookup_algo(const char *algo_name,
14446fe2c04SRuchika Gupta 				 struct hash_algo **algop);
14546fe2c04SRuchika Gupta 
1468f0b1e24SStefan Roese /**
1478f0b1e24SStefan Roese  * hash_parse_string() - Parse hash string into a binary array
1488f0b1e24SStefan Roese  *
1498f0b1e24SStefan Roese  * The function parses a hash string into a binary array that
1508f0b1e24SStefan Roese  * can for example easily be used to compare to hash values.
1518f0b1e24SStefan Roese  *
1528f0b1e24SStefan Roese  * @algo_name: Hash algorithm to look up
1538f0b1e24SStefan Roese  * @str: Hash string to get parsed
1548f0b1e24SStefan Roese  * @result: Binary array of the parsed hash string
1558f0b1e24SStefan Roese  *
1568f0b1e24SStefan Roese  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
1578f0b1e24SStefan Roese  */
1588f0b1e24SStefan Roese int hash_parse_string(const char *algo_name, const char *str, uint8_t *result);
1598f0b1e24SStefan Roese 
160460408efSSimon Glass #endif
161