xref: /openbmc/u-boot/include/hash.h (revision ea818dbb)
1 /*
2  * Copyright (c) 2012 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21 
22 #ifndef _HASH_H
23 #define _HASH_H
24 
25 #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
26 #define CONFIG_HASH_VERIFY
27 #endif
28 
29 struct hash_algo {
30 	const char *name;			/* Name of algorithm */
31 	int digest_size;			/* Length of digest */
32 	/**
33 	 * hash_func_ws: Generic hashing function
34 	 *
35 	 * This is the generic prototype for a hashing function. We only
36 	 * have the watchdog version at present.
37 	 *
38 	 * @input:	Input buffer
39 	 * @ilen:	Input buffer length
40 	 * @output:	Checksum result (length depends on algorithm)
41 	 * @chunk_sz:	Trigger watchdog after processing this many bytes
42 	 */
43 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
44 		unsigned char *output, unsigned int chunk_sz);
45 	int chunk_size;				/* Watchdog chunk size */
46 };
47 
48 /*
49  * Maximum digest size for all algorithms we support. Having this value
50  * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
51  */
52 #define HASH_MAX_DIGEST_SIZE	32
53 
54 enum {
55 	HASH_FLAG_VERIFY	= 1 << 0,	/* Enable verify mode */
56 	HASH_FLAG_ENV		= 1 << 1,	/* Allow env vars */
57 };
58 
59 /**
60  * hash_command: Process a hash command for a particular algorithm
61  *
62  * This common function is used to implement specific hash commands.
63  *
64  * @algo_name:		Hash algorithm being used (lower case!)
65  * @flags:		Flags value (HASH_FLAG_...)
66  * @cmdtp:		Pointer to command table entry
67  * @flag:		Some flags normally 0 (see CMD_FLAG_.. above)
68  * @argc:		Number of arguments (arg 0 must be the command text)
69  * @argv:		Arguments
70  */
71 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
72 		 int argc, char * const argv[]);
73 
74 /**
75  * hash_block() - Hash a block according to the requested algorithm
76  *
77  * The caller probably knows the hash length for the chosen algorithm, but
78  * in order to provide a general interface, and output_size parameter is
79  * provided.
80  *
81  * @algo_name:		Hash algorithm to use
82  * @data:		Data to hash
83  * @len:		Lengh of data to hash in bytes
84  * @output:		Place to put hash value
85  * @output_size:	On entry, pointer to the number of bytes available in
86  *			output. On exit, pointer to the number of bytes used.
87  *			If NULL, then it is assumed that the caller has
88  *			allocated enough space for the hash. This is possible
89  *			since the caller is selecting the algorithm.
90  * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
91  * -ENOSPC if the output buffer is not large enough.
92  */
93 int hash_block(const char *algo_name, const void *data, unsigned int len,
94 	       uint8_t *output, int *output_size);
95 
96 #endif
97