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