xref: /OK3568_Linux_fs/u-boot/include/hash.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2012 The Chromium OS Authors.
3*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #ifndef _HASH_H
7*4882a593Smuzhiyun #define _HASH_H
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Maximum digest size for all algorithms we support. Having this value
11*4882a593Smuzhiyun  * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun #define HASH_MAX_DIGEST_SIZE	32
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun enum {
16*4882a593Smuzhiyun 	HASH_FLAG_VERIFY	= 1 << 0,	/* Enable verify mode */
17*4882a593Smuzhiyun 	HASH_FLAG_ENV		= 1 << 1,	/* Allow env vars */
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct hash_algo {
21*4882a593Smuzhiyun 	const char *name;			/* Name of algorithm */
22*4882a593Smuzhiyun 	int digest_size;			/* Length of digest */
23*4882a593Smuzhiyun 	/**
24*4882a593Smuzhiyun 	 * hash_func_ws: Generic hashing function
25*4882a593Smuzhiyun 	 *
26*4882a593Smuzhiyun 	 * This is the generic prototype for a hashing function. We only
27*4882a593Smuzhiyun 	 * have the watchdog version at present.
28*4882a593Smuzhiyun 	 *
29*4882a593Smuzhiyun 	 * @input:	Input buffer
30*4882a593Smuzhiyun 	 * @ilen:	Input buffer length
31*4882a593Smuzhiyun 	 * @output:	Checksum result (length depends on algorithm)
32*4882a593Smuzhiyun 	 * @chunk_sz:	Trigger watchdog after processing this many bytes
33*4882a593Smuzhiyun 	 */
34*4882a593Smuzhiyun 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
35*4882a593Smuzhiyun 		unsigned char *output, unsigned int chunk_sz);
36*4882a593Smuzhiyun 	int chunk_size;				/* Watchdog chunk size */
37*4882a593Smuzhiyun 	/*
38*4882a593Smuzhiyun 	 * hash_init: Create the context for progressive hashing
39*4882a593Smuzhiyun 	 *
40*4882a593Smuzhiyun 	 * @algo: Pointer to the hash_algo struct
41*4882a593Smuzhiyun 	 * @ctxp: Pointer to the pointer of the context for hashing
42*4882a593Smuzhiyun 	 * @return 0 if ok, -1 on error
43*4882a593Smuzhiyun 	 */
44*4882a593Smuzhiyun 	int (*hash_init)(struct hash_algo *algo, void **ctxp);
45*4882a593Smuzhiyun 	/*
46*4882a593Smuzhiyun 	 * hash_update: Perform hashing on the given buffer
47*4882a593Smuzhiyun 	 *
48*4882a593Smuzhiyun 	 * The context is freed by this function if an error occurs.
49*4882a593Smuzhiyun 	 *
50*4882a593Smuzhiyun 	 * @algo: Pointer to the hash_algo struct
51*4882a593Smuzhiyun 	 * @ctx: Pointer to the context for hashing
52*4882a593Smuzhiyun 	 * @buf: Pointer to the buffer being hashed
53*4882a593Smuzhiyun 	 * @size: Size of the buffer being hashed
54*4882a593Smuzhiyun 	 * @is_last: 1 if this is the last update; 0 otherwise
55*4882a593Smuzhiyun 	 * @return 0 if ok, -1 on error
56*4882a593Smuzhiyun 	 */
57*4882a593Smuzhiyun 	int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf,
58*4882a593Smuzhiyun 			   unsigned int size, int is_last);
59*4882a593Smuzhiyun 	/*
60*4882a593Smuzhiyun 	 * hash_finish: Write the hash result to the given buffer
61*4882a593Smuzhiyun 	 *
62*4882a593Smuzhiyun 	 * The context is freed by this function.
63*4882a593Smuzhiyun 	 *
64*4882a593Smuzhiyun 	 * @algo: Pointer to the hash_algo struct
65*4882a593Smuzhiyun 	 * @ctx: Pointer to the context for hashing
66*4882a593Smuzhiyun 	 * @dest_buf: Pointer to the buffer for the result
67*4882a593Smuzhiyun 	 * @size: Size of the buffer for the result
68*4882a593Smuzhiyun 	 * @return 0 if ok, -ENOSPC if size of the result buffer is too small
69*4882a593Smuzhiyun 	 *   or -1 on other errors
70*4882a593Smuzhiyun 	 */
71*4882a593Smuzhiyun 	int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf,
72*4882a593Smuzhiyun 			   int size);
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #ifndef USE_HOSTCC
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun  * hash_command: Process a hash command for a particular algorithm
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * This common function is used to implement specific hash commands.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * @algo_name:		Hash algorithm being used (lower case!)
82*4882a593Smuzhiyun  * @flags:		Flags value (HASH_FLAG_...)
83*4882a593Smuzhiyun  * @cmdtp:		Pointer to command table entry
84*4882a593Smuzhiyun  * @flag:		Some flags normally 0 (see CMD_FLAG_.. above)
85*4882a593Smuzhiyun  * @argc:		Number of arguments (arg 0 must be the command text)
86*4882a593Smuzhiyun  * @argv:		Arguments
87*4882a593Smuzhiyun  */
88*4882a593Smuzhiyun int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
89*4882a593Smuzhiyun 		 int argc, char * const argv[]);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * hash_block() - Hash a block according to the requested algorithm
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * The caller probably knows the hash length for the chosen algorithm, but
95*4882a593Smuzhiyun  * in order to provide a general interface, and output_size parameter is
96*4882a593Smuzhiyun  * provided.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * @algo_name:		Hash algorithm to use
99*4882a593Smuzhiyun  * @data:		Data to hash
100*4882a593Smuzhiyun  * @len:		Lengh of data to hash in bytes
101*4882a593Smuzhiyun  * @output:		Place to put hash value
102*4882a593Smuzhiyun  * @output_size:	On entry, pointer to the number of bytes available in
103*4882a593Smuzhiyun  *			output. On exit, pointer to the number of bytes used.
104*4882a593Smuzhiyun  *			If NULL, then it is assumed that the caller has
105*4882a593Smuzhiyun  *			allocated enough space for the hash. This is possible
106*4882a593Smuzhiyun  *			since the caller is selecting the algorithm.
107*4882a593Smuzhiyun  * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
108*4882a593Smuzhiyun  * -ENOSPC if the output buffer is not large enough.
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun int hash_block(const char *algo_name, const void *data, unsigned int len,
111*4882a593Smuzhiyun 	       uint8_t *output, int *output_size);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun #endif /* !USE_HOSTCC */
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
119*4882a593Smuzhiyun  * algorithm is not available.
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * @algo_name: Hash algorithm to look up
122*4882a593Smuzhiyun  * @algop: Pointer to the hash_algo struct if found
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun  * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
132*4882a593Smuzhiyun  * algorithm is not available with progressive hash support.
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * @algo_name: Hash algorithm to look up
135*4882a593Smuzhiyun  * @algop: Pointer to the hash_algo struct if found
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
138*4882a593Smuzhiyun  */
139*4882a593Smuzhiyun int hash_progressive_lookup_algo(const char *algo_name,
140*4882a593Smuzhiyun 				 struct hash_algo **algop);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun  * hash_parse_string() - Parse hash string into a binary array
144*4882a593Smuzhiyun  *
145*4882a593Smuzhiyun  * The function parses a hash string into a binary array that
146*4882a593Smuzhiyun  * can for example easily be used to compare to hash values.
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * @algo_name: Hash algorithm to look up
149*4882a593Smuzhiyun  * @str: Hash string to get parsed
150*4882a593Smuzhiyun  * @result: Binary array of the parsed hash string
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
153*4882a593Smuzhiyun  */
154*4882a593Smuzhiyun int hash_parse_string(const char *algo_name, const char *str, uint8_t *result);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun #endif
157