xref: /rk3399_rockchip-uboot/include/hash.h (revision bf007ebb6f4b01af675782d23bacbddd17e1a627)
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 
9d20a40deSSimon Glass #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
10460408efSSimon Glass #define CONFIG_HASH_VERIFY
11460408efSSimon Glass #endif
12460408efSSimon Glass 
13460408efSSimon Glass struct hash_algo {
14460408efSSimon Glass 	const char *name;			/* Name of algorithm */
15460408efSSimon Glass 	int digest_size;			/* Length of digest */
16460408efSSimon Glass 	/**
17460408efSSimon Glass 	 * hash_func_ws: Generic hashing function
18460408efSSimon Glass 	 *
19460408efSSimon Glass 	 * This is the generic prototype for a hashing function. We only
20460408efSSimon Glass 	 * have the watchdog version at present.
21460408efSSimon Glass 	 *
22460408efSSimon Glass 	 * @input:	Input buffer
23460408efSSimon Glass 	 * @ilen:	Input buffer length
24460408efSSimon Glass 	 * @output:	Checksum result (length depends on algorithm)
25460408efSSimon Glass 	 * @chunk_sz:	Trigger watchdog after processing this many bytes
26460408efSSimon Glass 	 */
27460408efSSimon Glass 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
28460408efSSimon Glass 		unsigned char *output, unsigned int chunk_sz);
29460408efSSimon Glass 	int chunk_size;				/* Watchdog chunk size */
30*bf007ebbSHung-ying Tyan 	/*
31*bf007ebbSHung-ying Tyan 	 * hash_init: Create the context for progressive hashing
32*bf007ebbSHung-ying Tyan 	 *
33*bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
34*bf007ebbSHung-ying Tyan 	 * @ctxp: Pointer to the pointer of the context for hashing
35*bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
36*bf007ebbSHung-ying Tyan 	 */
37*bf007ebbSHung-ying Tyan 	int (*hash_init)(struct hash_algo *algo, void **ctxp);
38*bf007ebbSHung-ying Tyan 	/*
39*bf007ebbSHung-ying Tyan 	 * hash_update: Perform hashing on the given buffer
40*bf007ebbSHung-ying Tyan 	 *
41*bf007ebbSHung-ying Tyan 	 * The context is freed by this function if an error occurs.
42*bf007ebbSHung-ying Tyan 	 *
43*bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
44*bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
45*bf007ebbSHung-ying Tyan 	 * @buf: Pointer to the buffer being hashed
46*bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer being hashed
47*bf007ebbSHung-ying Tyan 	 * @is_last: 1 if this is the last update; 0 otherwise
48*bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -1 on error
49*bf007ebbSHung-ying Tyan 	 */
50*bf007ebbSHung-ying Tyan 	int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf,
51*bf007ebbSHung-ying Tyan 			   unsigned int size, int is_last);
52*bf007ebbSHung-ying Tyan 	/*
53*bf007ebbSHung-ying Tyan 	 * hash_finish: Write the hash result to the given buffer
54*bf007ebbSHung-ying Tyan 	 *
55*bf007ebbSHung-ying Tyan 	 * The context is freed by this function.
56*bf007ebbSHung-ying Tyan 	 *
57*bf007ebbSHung-ying Tyan 	 * @algo: Pointer to the hash_algo struct
58*bf007ebbSHung-ying Tyan 	 * @ctx: Pointer to the context for hashing
59*bf007ebbSHung-ying Tyan 	 * @dest_buf: Pointer to the buffer for the result
60*bf007ebbSHung-ying Tyan 	 * @size: Size of the buffer for the result
61*bf007ebbSHung-ying Tyan 	 * @return 0 if ok, -ENOSPC if size of the result buffer is too small
62*bf007ebbSHung-ying Tyan 	 *   or -1 on other errors
63*bf007ebbSHung-ying Tyan 	 */
64*bf007ebbSHung-ying Tyan 	int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf,
65*bf007ebbSHung-ying Tyan 			   int size);
66460408efSSimon Glass };
67460408efSSimon Glass 
68460408efSSimon Glass /*
69460408efSSimon Glass  * Maximum digest size for all algorithms we support. Having this value
70460408efSSimon Glass  * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
71460408efSSimon Glass  */
72460408efSSimon Glass #define HASH_MAX_DIGEST_SIZE	32
73460408efSSimon Glass 
74d5b76673SSimon Glass enum {
75d5b76673SSimon Glass 	HASH_FLAG_VERIFY	= 1 << 0,	/* Enable verify mode */
76d5b76673SSimon Glass 	HASH_FLAG_ENV		= 1 << 1,	/* Allow env vars */
77d5b76673SSimon Glass };
78d5b76673SSimon Glass 
79460408efSSimon Glass /**
80460408efSSimon Glass  * hash_command: Process a hash command for a particular algorithm
81460408efSSimon Glass  *
82460408efSSimon Glass  * This common function is used to implement specific hash commands.
83460408efSSimon Glass  *
84218da0f3SSimon Glass  * @algo_name:		Hash algorithm being used (lower case!)
85d5b76673SSimon Glass  * @flags:		Flags value (HASH_FLAG_...)
86460408efSSimon Glass  * @cmdtp:		Pointer to command table entry
87460408efSSimon Glass  * @flag:		Some flags normally 0 (see CMD_FLAG_.. above)
88460408efSSimon Glass  * @argc:		Number of arguments (arg 0 must be the command text)
89460408efSSimon Glass  * @argv:		Arguments
90460408efSSimon Glass  */
91d5b76673SSimon Glass int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
92460408efSSimon Glass 		 int argc, char * const argv[]);
93460408efSSimon Glass 
946f907b42SSimon Glass /**
956f907b42SSimon Glass  * hash_block() - Hash a block according to the requested algorithm
966f907b42SSimon Glass  *
976f907b42SSimon Glass  * The caller probably knows the hash length for the chosen algorithm, but
986f907b42SSimon Glass  * in order to provide a general interface, and output_size parameter is
996f907b42SSimon Glass  * provided.
1006f907b42SSimon Glass  *
1016f907b42SSimon Glass  * @algo_name:		Hash algorithm to use
1026f907b42SSimon Glass  * @data:		Data to hash
1036f907b42SSimon Glass  * @len:		Lengh of data to hash in bytes
1046f907b42SSimon Glass  * @output:		Place to put hash value
1056f907b42SSimon Glass  * @output_size:	On entry, pointer to the number of bytes available in
1066f907b42SSimon Glass  *			output. On exit, pointer to the number of bytes used.
1076f907b42SSimon Glass  *			If NULL, then it is assumed that the caller has
1086f907b42SSimon Glass  *			allocated enough space for the hash. This is possible
1096f907b42SSimon Glass  *			since the caller is selecting the algorithm.
1106f907b42SSimon Glass  * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
1116f907b42SSimon Glass  * -ENOSPC if the output buffer is not large enough.
1126f907b42SSimon Glass  */
1136f907b42SSimon Glass int hash_block(const char *algo_name, const void *data, unsigned int len,
1146f907b42SSimon Glass 	       uint8_t *output, int *output_size);
1156f907b42SSimon Glass 
116*bf007ebbSHung-ying Tyan /**
117*bf007ebbSHung-ying Tyan  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
118*bf007ebbSHung-ying Tyan  *
119*bf007ebbSHung-ying Tyan  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
120*bf007ebbSHung-ying Tyan  * algorithm is not available.
121*bf007ebbSHung-ying Tyan  *
122*bf007ebbSHung-ying Tyan  * @algo_name: Hash algorithm to look up
123*bf007ebbSHung-ying Tyan  * @algop: Pointer to the hash_algo struct if found
124*bf007ebbSHung-ying Tyan  *
125*bf007ebbSHung-ying Tyan  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
126*bf007ebbSHung-ying Tyan  */
127*bf007ebbSHung-ying Tyan int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
128460408efSSimon Glass #endif
129