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